From 0552c6a7cafe47e011ab9510c2c7672498415aca Mon Sep 17 00:00:00 2001 From: "k.12f" Date: Tue, 10 Dec 2024 15:29:15 +0800 Subject: [PATCH] =?UTF-8?q?feat(Ui):=20=E6=B7=BB=E5=8A=A0=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=90=8D=E9=9D=9E=E6=B3=95=E5=AD=97=E7=AC=A6=E6=9B=BF?= =?UTF-8?q?=E6=8D=A2=E5=8A=9F=E8=83=BD=E5=B9=B6=E4=BC=98=E5=8C=96=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Extensions/FileNameExtensions.cs | 25 +++++++++++++++++ .../ViewModels/Pages/HomeViewModel.cs | 28 ++++++------------- 2 files changed, 34 insertions(+), 19 deletions(-) create mode 100644 src/BlueCatKoKo.Ui/Extensions/FileNameExtensions.cs diff --git a/src/BlueCatKoKo.Ui/Extensions/FileNameExtensions.cs b/src/BlueCatKoKo.Ui/Extensions/FileNameExtensions.cs new file mode 100644 index 0000000..8f7f8fd --- /dev/null +++ b/src/BlueCatKoKo.Ui/Extensions/FileNameExtensions.cs @@ -0,0 +1,25 @@ +using System.IO; +using System.Text; + +namespace BlueCatKoKo.Ui.Extensions; + +public static class FileNameExtensions +{ + /// + /// 替换非法字符 + /// + /// + /// + public static string ReplaceInvalidCharacters(this string fileName) + { + var invalidChars = Path.GetInvalidFileNameChars(); + var replacedFileName = new StringBuilder(); + + foreach (var c in fileName) + { + replacedFileName.Append(!invalidChars.Contains(c) ? c : '#'); + } + + return replacedFileName.ToString(); + } +} \ No newline at end of file diff --git a/src/BlueCatKoKo.Ui/ViewModels/Pages/HomeViewModel.cs b/src/BlueCatKoKo.Ui/ViewModels/Pages/HomeViewModel.cs index 0cca062..c2b45c1 100644 --- a/src/BlueCatKoKo.Ui/ViewModels/Pages/HomeViewModel.cs +++ b/src/BlueCatKoKo.Ui/ViewModels/Pages/HomeViewModel.cs @@ -1,21 +1,16 @@ using System.ComponentModel.DataAnnotations; using System.IO; - using BlueCatKoKo.Ui.Constants; +using BlueCatKoKo.Ui.Extensions; using BlueCatKoKo.Ui.Models; using BlueCatKoKo.Ui.Services; - using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using CommunityToolkit.Mvvm.Messaging; using CommunityToolkit.Mvvm.Messaging.Messages; - using LibVLCSharp.Shared; - using Microsoft.Extensions.Options; - using Serilog; - using MediaPlayer = LibVLCSharp.Shared.MediaPlayer; namespace BlueCatKoKo.Ui.ViewModels.Pages @@ -181,29 +176,24 @@ private async Task DownloadAll() throw new InvalidDataException("无效的下载链接"); } - var filename = _appConfig.Value.DownloadPath + Data.Desc + ".mp4"; + var filepath = _appConfig.Value.DownloadPath; + var filename = Data.Desc + ".mp4"; + + var replaceFilename = filename.ReplaceInvalidCharacters(); switch (Data.Platform) { case ShortVideoPlatformEnum.DouYin: - await _douYinShortVideoService.DownloadAsync(Data.VideoUrl, _appConfig.Value.DownloadPath, - Data.Desc + ".mp4", - (sender, e) => - { - DownloadProcess = e.ProgressPercentage; - }, (sender, e) => + await _douYinShortVideoService.DownloadAsync(Data.VideoUrl, filepath, replaceFilename, + (sender, e) => { DownloadProcess = e.ProgressPercentage; }, (sender, e) => { DownloadProcess = 100; message = filename + "下载成功~"; }); break; case ShortVideoPlatformEnum.KuaiShou: - await _kuaiShortVideoService.DownloadAsync(Data.VideoUrl, _appConfig.Value.DownloadPath, - Data.Desc + ".mp4", - (sender, e) => - { - DownloadProcess = e.ProgressPercentage; - }, (sender, e) => + await _kuaiShortVideoService.DownloadAsync(Data.VideoUrl, filepath, replaceFilename, + (sender, e) => { DownloadProcess = e.ProgressPercentage; }, (sender, e) => { DownloadProcess = 100; message = filename + "下载成功~";