feat: 添加统一的跨平台处理函数

This commit is contained in:
姚彪
2023-12-14 22:04:15 +08:00
parent 3f4f565038
commit 2a09cd499a
5 changed files with 104 additions and 70 deletions

View File

@@ -36,7 +36,7 @@ public abstract class DownloadService
protected Task workTask;
protected CancellationTokenSource tokenSource;
protected CancellationToken cancellationToken;
protected List<Task> downloadingTasks = new List<Task>();
protected List<Task> downloadingTasks = new();
protected readonly int retry = 5;
protected readonly string nullMark = "<null>";
@@ -453,7 +453,7 @@ public abstract class DownloadService
}
await Task.WhenAny(Task.WhenAll(downloadingTasks), Task.Delay(30000));
foreach (Task tsk in downloadingTasks.FindAll((m) => !m.IsCompleted))
foreach (var tsk in downloadingTasks.FindAll((m) => !m.IsCompleted))
{
Console.PrintLine($"{Tag}.DoWork() 任务结束超时");
LogManager.Debug($"{Tag}.DoWork()", "任务结束超时");
@@ -485,8 +485,8 @@ public abstract class DownloadService
Console.PrintLine(Tag, e.ToString());
LogManager.Debug(Tag, e.Message);
AlertService alertService = new AlertService(dialogService);
ButtonResult result =
var alertService = new AlertService(dialogService);
var result =
await alertService.ShowError($"{path}{DictionaryResource.GetString("DirectoryError")}");
return;
@@ -781,7 +781,7 @@ public abstract class DownloadService
break;
case AfterDownloadOperation.CLOSE_SYSTEM:
// 关机
Process.Start("shutdown.exe", "-s");
// Process.Start("shutdown.exe", "-s");
break;
default:
break;
@@ -801,7 +801,7 @@ public abstract class DownloadService
//先简单等待一下
// 下载数据存储服务
// DownloadStorageService downloadStorageService = new DownloadStorageService();
var downloadStorageService = new DownloadStorageService();
// 保存数据
foreach (DownloadingItem item in downloadingList)
{
@@ -829,12 +829,12 @@ public abstract class DownloadService
item.Progress = 0;
// downloadStorageService.UpdateDownloading(item);
downloadStorageService.UpdateDownloading(item);
}
foreach (DownloadedItem item in downloadedList)
foreach (var item in downloadedList)
{
// downloadStorageService.UpdateDownloaded(item);
downloadStorageService.UpdateDownloaded(item);
}
}

View File

@@ -0,0 +1,41 @@
using System;
using System.Diagnostics;
namespace DownKyi.Utils;
public static class PlatformHelper
{
/// <summary>
/// 打开文件夹
/// </summary>
/// <param name="folder">路径</param>
public static void OpenFolder(string folder)
{
if (OperatingSystem.IsWindows())
{
Process.Start("explorer.exe", $"/select,{folder}");
}
if (OperatingSystem.IsMacOS())
{
Process.Start("open", $"\"{folder}\"");
}
}
/// <summary>
/// 打开各种 (文件、url)
/// </summary>
/// <param name="filename">文件名</param>
public static void Open(string filename)
{
if (OperatingSystem.IsWindows())
{
Process.Start(filename);
}
if (OperatingSystem.IsMacOS())
{
Process.Start("open", $"\"{filename}\"");
}
}
}

View File

@@ -1,6 +1,4 @@
using System;
using System.Diagnostics;
using System.IO;
using System.IO;
using System.Linq;
using DownKyi.Images;
using DownKyi.Models;
@@ -57,9 +55,10 @@ namespace DownKyi.ViewModels.DownloadManager
}
}
#region
#region
private VectorImage openFolder;
public VectorImage OpenFolder
{
get => openFolder;
@@ -67,6 +66,7 @@ namespace DownKyi.ViewModels.DownloadManager
}
private VectorImage openVideo;
public VectorImage OpenVideo
{
get => openVideo;
@@ -74,6 +74,7 @@ namespace DownKyi.ViewModels.DownloadManager
}
private VectorImage removeVideo;
public VectorImage RemoveVideo
{
get => removeVideo;
@@ -86,19 +87,25 @@ namespace DownKyi.ViewModels.DownloadManager
// 打开文件夹事件
private DelegateCommand openFolderCommand;
public DelegateCommand OpenFolderCommand => openFolderCommand ?? (openFolderCommand = new DelegateCommand(ExecuteOpenFolderCommand));
public DelegateCommand OpenFolderCommand =>
openFolderCommand ?? (openFolderCommand = new DelegateCommand(ExecuteOpenFolderCommand));
/// <summary>
/// 打开文件夹事件
/// </summary>
private void ExecuteOpenFolderCommand()
{
if (DownloadBase == null) { return; }
if (DownloadBase == null)
{
return;
}
//TODO:这里不光有mp4视频文件也可能存在音频文件、字幕或者其他文件类型
//fix bug:Issues #709
//这里根据需要下载的类型判断,具体对应的文件后缀名
var downLoadContents = DownloadBase.NeedDownloadContent.Where(e => e.Value == true).Select(e => e.Key);
string fileSuffix = string.Empty;
var fileSuffix = string.Empty;
if (downLoadContents.Contains("downloadVideo"))
{
fileSuffix = ".mp4";
@@ -111,14 +118,12 @@ namespace DownKyi.ViewModels.DownloadManager
{
fileSuffix = ".jpg";
}
string videoPath = $"{DownloadBase.FilePath}{fileSuffix}";
FileInfo fileInfo = new FileInfo(videoPath);
var videoPath = $"{DownloadBase.FilePath}{fileSuffix}";
var fileInfo = new FileInfo(videoPath);
if (File.Exists(fileInfo.FullName))
{
if (OperatingSystem.IsWindows())
{
Process.Start("Explorer", "/select," + fileInfo.FullName);
}
PlatformHelper.OpenFolder(fileInfo.FullName);
}
else
{
@@ -135,16 +140,16 @@ namespace DownKyi.ViewModels.DownloadManager
/// </summary>
private void ExecuteOpenVideoCommand()
{
if (DownloadBase == null) { return; }
if (DownloadBase == null)
{
return;
}
string videoPath = $"{DownloadBase.FilePath}.mp4";
var videoPath = $"{DownloadBase.FilePath}.mp4";
var fileInfo = new FileInfo(videoPath);
if (File.Exists(fileInfo.FullName))
{
if (OperatingSystem.IsWindows())
{
Process.Start(fileInfo.FullName);
}
PlatformHelper.Open(fileInfo.FullName);
}
else
{
@@ -155,7 +160,10 @@ namespace DownKyi.ViewModels.DownloadManager
// 删除事件
private DelegateCommand removeVideoCommand;
public DelegateCommand RemoveVideoCommand => removeVideoCommand ?? (removeVideoCommand = new DelegateCommand(ExecuteRemoveVideoCommand));
public DelegateCommand RemoveVideoCommand => removeVideoCommand ??
(removeVideoCommand =
new DelegateCommand(ExecuteRemoveVideoCommand));
/// <summary>
/// 删除事件
@@ -168,11 +176,10 @@ namespace DownKyi.ViewModels.DownloadManager
{
return;
}
App.DownloadedList.Remove(this);
}
#endregion
}
}
}

View File

@@ -1,5 +1,4 @@
using System.Diagnostics;
using DownKyi.Core.Settings;
using DownKyi.Core.Settings;
using DownKyi.Events;
using DownKyi.Models;
using DownKyi.Utils;
@@ -97,7 +96,7 @@ public class ViewAboutViewModel : ViewModelBase
/// </summary>
private void ExecuteAppNameCommand()
{
Process.Start("https://github.com/leiurayer/downkyi");
PlatformHelper.Open("https://github.com/leiurayer/downkyi");
}
// 检查更新事件
@@ -124,7 +123,7 @@ public class ViewAboutViewModel : ViewModelBase
/// </summary>
private void ExecuteFeedbackCommand()
{
Process.Start("https://github.com/leiurayer/downkyi/issues");
PlatformHelper.Open("https://github.com/leiurayer/downkyi/issues");
}
// 是否接收测试版更新事件
@@ -173,7 +172,7 @@ public class ViewAboutViewModel : ViewModelBase
/// </summary>
private void ExecuteBrotliLicenseCommand()
{
Process.Start("https://licenses.nuget.org/MIT");
PlatformHelper.Open("https://licenses.nuget.org/MIT");
}
// Google.Protobuf许可证查看事件
@@ -188,7 +187,7 @@ public class ViewAboutViewModel : ViewModelBase
/// </summary>
private void ExecuteProtobufLicenseCommand()
{
Process.Start("https://github.com/protocolbuffers/protobuf/blob/master/LICENSE");
PlatformHelper.Open("https://github.com/protocolbuffers/protobuf/blob/master/LICENSE");
}
// Newtonsoft.Json许可证查看事件
@@ -203,7 +202,7 @@ public class ViewAboutViewModel : ViewModelBase
/// </summary>
private void ExecuteNewtonsoftLicenseCommand()
{
Process.Start("https://licenses.nuget.org/MIT");
PlatformHelper.Open("https://licenses.nuget.org/MIT");
}
// Prism.DryIoc许可证查看事件
@@ -218,7 +217,7 @@ public class ViewAboutViewModel : ViewModelBase
/// </summary>
private void ExecutePrismLicenseCommand()
{
Process.Start("https://www.nuget.org/packages/Prism.DryIoc/8.1.97/license");
PlatformHelper.Open("https://www.nuget.org/packages/Prism.DryIoc/8.1.97/license");
}
// QRCoder许可证查看事件
@@ -233,7 +232,7 @@ public class ViewAboutViewModel : ViewModelBase
/// </summary>
private void ExecuteQRCoderLicenseCommand()
{
Process.Start("https://licenses.nuget.org/MIT");
PlatformHelper.Open("https://licenses.nuget.org/MIT");
}
// System.Data.SQLite.Core许可证查看事件
@@ -248,7 +247,7 @@ public class ViewAboutViewModel : ViewModelBase
/// </summary>
private void ExecuteSQLiteLicenseCommand()
{
Process.Start("https://www.sqlite.org/copyright.html");
PlatformHelper.Open("https://www.sqlite.org/copyright.html");
}
// Aria2c许可证查看事件
@@ -262,7 +261,7 @@ public class ViewAboutViewModel : ViewModelBase
/// </summary>
private void ExecuteAriaLicenseCommand()
{
Process.Start("aria2_COPYING.txt");
PlatformHelper.Open("aria2_COPYING.txt");
}
// FFmpeg许可证查看事件
@@ -277,7 +276,7 @@ public class ViewAboutViewModel : ViewModelBase
/// </summary>
private void ExecuteFFmpegLicenseCommand()
{
Process.Start("FFmpeg_LICENSE.txt");
PlatformHelper.Open("FFmpeg_LICENSE.txt");
}
#endregion

View File

@@ -1,9 +1,8 @@
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using DownKyi.Core.BiliApi.BiliUtils;
using DownKyi.Core.Logging;
using DownKyi.Utils;
using Prism.Commands;
using Prism.Events;
using Console = DownKyi.Core.Utils.Debugging.Console;
@@ -20,32 +19,32 @@ public class ViewBiliHelperViewModel : ViewModelBase
public string Avid
{
get { return avid; }
set { SetProperty(ref avid, value); }
get => avid;
set => SetProperty(ref avid, value);
}
private string bvid;
public string Bvid
{
get { return bvid; }
set { SetProperty(ref bvid, value); }
get => bvid;
set => SetProperty(ref bvid, value);
}
private string danmakuUserID;
public string DanmakuUserID
{
get { return danmakuUserID; }
set { SetProperty(ref danmakuUserID, value); }
get => danmakuUserID;
set => SetProperty(ref danmakuUserID, value);
}
private string userMid;
public string UserMid
{
get { return userMid; }
set { SetProperty(ref userMid, value); }
get => userMid;
set => SetProperty(ref userMid, value);
}
#endregion
@@ -129,20 +128,8 @@ public class ViewBiliHelperViewModel : ViewModelBase
/// </summary>
private void ExecuteGotoWebCommand()
{
var baseUrl = "https://www.bilibili.com/video/";
if (OperatingSystem.IsWindows())
{
Process.Start(baseUrl + Bvid);
}
if (OperatingSystem.IsMacOS())
{
Process.Start(new ProcessStartInfo
{
FileName = "open",
ArgumentList = { baseUrl + Bvid },
});
}
var url = $"https://www.bilibili.com/video/{Bvid}";
PlatformHelper.Open(url);
}
// 查询弹幕发送者事件
@@ -190,8 +177,8 @@ public class ViewBiliHelperViewModel : ViewModelBase
return;
}
string baseUrl = "https://space.bilibili.com/";
Process.Start(baseUrl + UserMid);
var userSpace = $"https://space.bilibili.com/{UserMid}";
PlatformHelper.Open(userSpace);
}
#endregion