网站首页 网站源码
website
站点相关全部源代码,隐藏了一些关于服务器的信息
using System.Net.Http.Json;
using System.Text.Json.Nodes;
using Dpz.Core.Infrastructure;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace Dpz.Core.Service.ObjectStorage.VideoHandle;

public class VideoPretreatment(
    HttpClient httpClient,
    ILogger<VideoPretreatment> logger,
    IConfiguration configuration,
    UpyunOperator upyunOperator
)
{
    public async Task SliceAsync(string source, string name)
    {
        var argument = new PretreatmentArgument
        {
            Service = upyunOperator.Bucket ?? throw new InvalidConfigurationException(),
            NotifyUrl = configuration["NotifyUrl"] ?? throw new InvalidConfigurationException(),
            Source = source,
            Tasks =
            [
                new PretreatmentTask
                {
                    Type = "video",
                    AvOpts = [new 切片(5), new 视频分辨率()],
                    SaveAs = $"/Video/{name}/1080p.m3u8"
                }
            ]
        };

        var request = new HttpRequestMessage(HttpMethod.Post, "pretreatment");
        request.Content = new FormUrlEncodedContent(
            [
                new KeyValuePair<string, string>("service", argument.Service),
                new KeyValuePair<string, string>("notify_url", argument.NotifyUrl),
                new KeyValuePair<string, string>("source", argument.Source),
                new KeyValuePair<string, string>("tasks", argument.TasksBase64String()),
                new KeyValuePair<string, string>("Accept", argument.Accept),
            ]
        );

        await request.SignatureAsync(upyunOperator);
        var response = await httpClient.SendAsync(request);

        if (!response.IsSuccessStatusCode)
        {
            logger.LogError("pretreatment fail,status code:{StatusCode}", response.StatusCode);
            throw new BusinessException(
                $"pretreatment fail,response status code:{response.StatusCode}"
            );
        }

        var taskIds = await response.Content.ReadFromJsonAsync<List<string>>();
        if (taskIds == null || taskIds.Count == 0)
        {
            logger.LogWarning("pretreatment fail,task ids is empty");
            return;
        }

        await QueryTaskResultAsync(taskIds[0]);
    }

    public async Task QueryTaskResultAsync(string taskId)
    {
        var request = new HttpRequestMessage(
            HttpMethod.Get,
            $"status?service={upyunOperator.Bucket}&task_ids={taskId}"
        );
        await request.SignatureAsync(upyunOperator);
        var response = await httpClient.SendAsync(request);
        if (!response.IsSuccessStatusCode)
        {
            logger.LogError("query task result fail,status code:{StatusCode}", response.StatusCode);
            throw new BusinessException(
                $"query task result fail,response status code:{response.StatusCode}"
            );
        }

        var json = await response.Content.ReadAsStringAsync();

        var root = JsonNode.Parse(json);
        var status = root?["tasks"]?[taskId]?.GetValue<int?>();

        if (status == null)
        {
            logger.LogWarning("query task result fail,task status is null,data :{Data}", json);
        }
        if (status is null or < 100)
        {
            logger.LogInformation("task is processing : {Process}%", status ?? 0);
            await Task.Delay(TimeSpan.FromSeconds(3));
            await QueryTaskResultAsync(taskId);
            return;
        }

        logger.LogInformation("task is processed");
    }
}
loading