using Downloader; using Microsoft.Extensions.DependencyInjection; using SqlSugar; using SqlSugar.IOC; using System; using System.ComponentModel; using System.Diagnostics; using System.IO; using System.Net; using System.Threading.Tasks; using VideoAnalysisCore.Job; using VideoAnalysisCore.Model; using VideoAnalysisCore.Model.Enum; using AlibabaCloud.SDK.Vod20170321; using UserCenter.Model.Enum; namespace VideoAnalysisCore.Common { public static class DownloadFileExpand { /// /// 初始化下载器 /// /// 下载速度mb/s public static void AddDownloadFileExpand(this IServiceCollection services) { DownloadFile.DownloadSpeed = AppCommon.Config.TaskSetting.DownloadSpeed; services.AddTransient(); DownloadFile.Opt = new DownloadConfiguration() { // 通常,主机支持的最大值为8000字节,默认值是8000 BufferBlockSize = 10240, // 要下载的文件部分数量,默认值是1 ChunkCount = 8, // 下载速度限制为2MB/秒,默认值为零(即无限制) MaximumBytesPerSecond = 1024 * 1024 * DownloadFile.DownloadSpeed, // 故障转移时的最大重试次数 MaxTryAgainOnFailover = 5, // 每50MB后释放内存缓冲区 MaximumMemoryBufferBytes = 1024 * 1024 * 50, // 是否并行下载文件的各个部分。默认值为false ParallelDownload = true, // 并行下载的数量。默认值与分块数量相同 ParallelCount = 4, // 每个流块读取器的超时时间(毫秒),默认值是1000 Timeout = 1000, // 如果只想下载大型文件的特定字节范围,则设置为true RangeDownload = false, // 大型文件下载范围的起始偏移量 RangeLow = 0, // 大型文件下载范围的结束偏移量 RangeHigh = 0, // 下载失败完成时清除包块数据,默认值为false ClearPackageOnCompletionWithFailure = true, // 将文件分多个部分下载时的最小分块大小,默认值是512 MinimumSizeOfChunking = 1024, // 在开始下载之前,按照文件大小预留文件的存储空间,默认值为false ReserveStorageSpaceBeforeStartingDownload = true, // 在下载进度改变事件中,通过ReceivedBytes获取按需下载的数据 EnableLiveStreaming = false, // 配置和自定义请求头 RequestConfiguration = { Accept = "*/*", //CookieContainer = cookies, Headers = new WebHeaderCollection(), // { 你的自定义头部信息 } KeepAlive = true, // 默认值为false ProtocolVersion = HttpVersion.Version11, // 默认值是HTTP 1.1 UseDefaultCredentials = false, // 你的自定义用户代理或你的应用名称/应用版本。 UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64)", //Proxy = new WebProxy()//使用代理 //{ // Address = new Uri("http://Learn.VideoAnalysis"), // UseDefaultCredentials = false, // Credentials = System.Net.CredentialCache.DefaultNetworkCredentials, // BypassProxyOnLocal = true //} } }; } } /// /// /// public class DownloadFile { public static DownloadConfiguration Opt { get; set; } = default!; public static int DownloadSpeed { get; set; } = default!; private readonly Repository videoTaskDB; private readonly Client vodClient; public DownloadFile(Repository videoTaskDB, Client vodClient) { this.videoTaskDB = videoTaskDB; this.vodClient = vodClient; } // 根据 Content-Type 映射文件后缀 static string GetExtensionFromContentType(HttpResponseMessage res) { var contentType = res.Content.Headers.ContentType?.MediaType; return contentType switch { "application/pdf" => ".pdf", "image/jpeg" => ".jpg", "image/png" => ".png", "application/zip" => ".zip", "text/html" => ".html", "audio/wav" => ".wav", "audio/mp4" => ".mp4", "audio/mp3" => ".mp3", // 根据需要添加其他 Content-Type 映射 _ => ".mp4", // 默认二进制文件 }; } /// /// 使用 HttpClient 下载任务的资源文件到本地 /// /// /// public async Task RunTask(string task) { var taskId = long.Parse(task); //获取资源文件 地址 var taskInfo = await videoTaskDB.AsQueryable() .Where(s => s.Id == taskId).FirstAsync(); if (taskInfo is null ) throw new Exception($"任务为null/是教研视频/没有视频课程名称"); var fileUrl = taskInfo.MediaUrl; if (string.IsNullOrEmpty(fileUrl)) { var videoInfo = await vodClient.GetPlayInfoAsync(new AlibabaCloud.SDK.Vod20170321.Models.GetPlayInfoRequest() { VideoId = taskInfo.TagId, Formats = "mp4", OutputType = "cdn", AuthTimeout = 3600 * 24 * 12, }); if (videoInfo is null || videoInfo.StatusCode != 200 && !videoInfo.Body.PlayInfoList.PlayInfo.Any()) throw new Exception($"{DateTime.Now} 视频订阅=>获取阿里云视频信息失败 VideoCode {taskInfo.TagId} StatusCode {videoInfo?.StatusCode}"); fileUrl = videoInfo.Body.PlayInfoList.PlayInfo.First().PlayURL; } if (string.IsNullOrEmpty(fileUrl)) throw new Exception($"任务id[{task}] 资源地址无效 {fileUrl}"); // 尝试从 URL 中获取文件后缀 string fileExtension = Path.GetExtension(new Uri(fileUrl).AbsolutePath); //否则 获取 从Content-Type 获取文件后缀 if (string.IsNullOrEmpty(fileExtension)) throw new Exception($"未能从资源路径中获取文件后缀"); //创建下载文件缓存路径 if (!Directory.Exists(AppCommon.TaskCachedFile)) Directory.CreateDirectory(AppCommon.TaskCachedFile); var localPath = task.LocalPath(); var outputPath = Path.Combine(localPath, task + fileExtension); if (!Directory.Exists(localPath)) Directory.CreateDirectory(localPath); await videoTaskDB .AsUpdateable() .SetColumns(it => it.LocalMediaPath == outputPath) .Where(it => it.Id == long.Parse(task)) .ExecuteCommandAsync(); //下载PPT视频 if (!string.IsNullOrEmpty(taskInfo.PPTVideoCode)) { try { var url = string.Empty; if (taskInfo.PPTVideoCode.Contains("http")) url = taskInfo.PPTVideoCode; else { var videoInfo = await vodClient.GetPlayInfoAsync(new AlibabaCloud.SDK.Vod20170321.Models.GetPlayInfoRequest() { VideoId = taskInfo.PPTVideoCode, Formats = "mp4", OutputType = "cdn", AuthTimeout = 3600 * 24 * 12, }); if (videoInfo is null || videoInfo.StatusCode != 200 && !videoInfo.Body.PlayInfoList.PlayInfo.Any()) throw new Exception($"{DateTime.Now} 视频订阅=>获取阿里云视频信息失败 VideoCode {taskInfo.TagId} StatusCode {videoInfo?.StatusCode}"); url = videoInfo.Body.PlayInfoList.PlayInfo.First().PlayURL; } await Download(url, localPath, "ppt.mp4", (s, e) => RedisExpand.SetTaskProgress(task, "PPT->" + Math.Round(e.ProgressPercentage, 1) )); } catch { throw; } } try {//下载原视频 await Download(fileUrl, localPath, task + fileExtension, (s, e) => RedisExpand.SetTaskProgress(task, Math.Round(e.ProgressPercentage,1) )); } catch { throw; } } /// /// 下载文件 /// /// /// /// /// public async Task Download(string fileUrl, string localPath, string fileName,Action change) { var res = new TaskCompletionSource(); using IDownload download = DownloadBuilder.New() .WithUrl(fileUrl) .WithDirectory(localPath) .WithFileName(fileName) .WithConfiguration(Opt) .Build(); var pI = 0; download.DownloadProgressChanged += (object? sender, Downloader.DownloadProgressChangedEventArgs e) => { pI++; if (pI % 20 == 0) change(sender, e); }; download.DownloadFileCompleted += async (object? sender, AsyncCompletedEventArgs e) => { if (download.Status == DownloadStatus.Failed && e.Error != null) { res.SetException(e.Error); } else if (download.Status == DownloadStatus.Completed) { res.SetResult(); } }; await download.StartAsync(); // 等待回调函数完成 await res.Task; } } }