合并252 pull request

croire 3 years ago
parent 9fda62b962
commit 6f21977c8f

@ -1,4 +1,4 @@
using DownKyi.Core.BiliApi.VideoStream;
using DownKyi.Core.BiliApi.VideoStream;
using System;
using System.Collections.Generic;
@ -11,6 +11,7 @@ namespace DownKyi.Models
{
// 初始化下载的文件列表
DownloadFiles = new Dictionary<string, string>();
DownloadedFiles = new List<string>();
}
// Aria相关
@ -19,6 +20,9 @@ namespace DownKyi.Models
// 下载的文件
public Dictionary<string, string> DownloadFiles { get; set; }
// 已下载的文件
public List<string> DownloadedFiles { get; set; }
// 视频类别
public PlayStreamType PlayStreamType { get; set; }

@ -1,4 +1,4 @@
using DownKyi.Core.Aria2cNet;
using DownKyi.Core.Aria2cNet;
using DownKyi.Core.Aria2cNet.Client;
using DownKyi.Core.Aria2cNet.Client.Entity;
using DownKyi.Core.Aria2cNet.Server;
@ -141,6 +141,11 @@ namespace DownKyi.Services.Download
string fileName = Guid.NewGuid().ToString("N");
string key = $"{downloadVideo.Id}_{downloadVideo.Codecs}";
// 老版本数据库没有这一项会变成null
if (downloading.Downloading.DownloadedFiles == null)
downloading.Downloading.DownloadedFiles = new List<string>();
if (downloading.Downloading.DownloadedFiles.Contains(key))
return Path.Combine(path, fileName);
if (downloading.Downloading.DownloadFiles.ContainsKey(key))
{
// 如果存在,表示下载过,
@ -155,6 +160,9 @@ namespace DownKyi.Services.Download
downloading.Downloading.DownloadFiles.Add(key, fileName);
}
catch (ArgumentException) { }
// Gid最好能是每个文件单独存储现在复用有可能会混
// 不过好消息是下载是按固定顺序的,而且下载了两个音频会混流不过
downloading.Downloading.Gid = null;
}
// 开始下载
@ -162,6 +170,7 @@ namespace DownKyi.Services.Download
switch (downloadStatus)
{
case DownloadResult.SUCCESS:
downloading.Downloading.DownloadedFiles.Add(key);
return Path.Combine(path, fileName);
case DownloadResult.FAILED:
case DownloadResult.ABORT:
@ -544,6 +553,8 @@ namespace DownKyi.Services.Download
Directory.CreateDirectory(path);
}
try
{
await Task.Run(new Action(() =>
{
// 初始化
@ -554,10 +565,6 @@ namespace DownKyi.Services.Download
// 解析并依次下载音频、视频、弹幕、字幕、封面等内容
Parse(downloading);
// 设置下载状态
// 必须在解析之后设置为DOWNLOADING
downloading.Downloading.DownloadStatus = DownloadStatus.DOWNLOADING;
// 暂停
Pause(downloading);
// 是否存在
@ -795,6 +802,10 @@ namespace DownKyi.Services.Download
}));
}));
}
catch (OperationCanceledException)
{
}
}
/// <summary>
/// 下载失败后的处理
@ -836,33 +847,11 @@ namespace DownKyi.Services.Download
/// <param name="downloading"></param>
private void Pause(DownloadingItem downloading)
{
string oldStatus = downloading.DownloadStatusTitle;
downloading.DownloadStatusTitle = DictionaryResource.GetString("Pausing");
while (downloading.Downloading.DownloadStatus == DownloadStatus.PAUSE)
if (downloading.Downloading.DownloadStatus == DownloadStatus.PAUSE)
{
// 降低CPU占用
Thread.Sleep(100);
throw new OperationCanceledException("Stop thread by pause");
}
downloading.DownloadStatusTitle = DictionaryResource.GetString("Waiting");
int maxDownloading = SettingsManager.GetInstance().GetAriaMaxConcurrentDownloads();
int downloadingCount;
do
{
downloadingCount = 0;
foreach (DownloadingItem item in downloadingList)
{
if (item.Downloading.DownloadStatus == DownloadStatus.DOWNLOADING)
{
downloadingCount++;
}
}
// 降低CPU占用
Thread.Sleep(100);
} while (downloadingCount > maxDownloading);
downloading.DownloadStatusTitle = oldStatus;
}
/// <summary>
@ -958,6 +947,18 @@ namespace DownKyi.Services.Download
// path已斜杠结尾去掉斜杠
path = path.TrimEnd('/').TrimEnd('\\');
//检查gid对应任务如果已创建那么直接使用
//但是代理设置会出现不能随时更新的问题
if (downloading.Downloading.Gid != null)
{
Task<AriaTellStatus> status = AriaClient.TellStatus(downloading.Downloading.Gid);
if (status == null || status.Result == null)
downloading.Downloading.Gid = null;
}
if (downloading.Downloading.Gid == null)
{
AriaSendOption option = new AriaSendOption
{
//HttpProxy = $"http://{Settings.GetAriaHttpProxy()}:{Settings.GetAriaHttpProxyListenPort()}",
@ -984,12 +985,17 @@ namespace DownKyi.Services.Download
// 保存gid
string gid = ariaAddUri.Result.Result;
downloading.Downloading.Gid = gid;
}
else
{
Task<AriaPause> ariaUnpause = AriaClient.UnpauseAsync(downloading.Downloading.Gid);
}
// 管理下载
AriaManager ariaManager = new AriaManager();
ariaManager.TellStatus += AriaTellStatus;
ariaManager.DownloadFinish += AriaDownloadFinish;
return ariaManager.GetDownloadStatus(gid, new Action(() =>
return ariaManager.GetDownloadStatus(downloading.Downloading.Gid, new Action(() =>
{
switch (downloading.Downloading.DownloadStatus)
{
@ -999,7 +1005,6 @@ namespace DownKyi.Services.Download
Pause(downloading);
break;
case DownloadStatus.DOWNLOADING:
Task<AriaPause> ariaUnpause = AriaClient.UnpauseAsync(downloading.Downloading.Gid);
break;
}
}));

Loading…
Cancel
Save