Skip to content

Commit

Permalink
feat(Ui): 添加文件名非法字符替换功能并优化下载逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
K12f committed Dec 10, 2024
1 parent a21e2d6 commit 0552c6a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 19 deletions.
25 changes: 25 additions & 0 deletions src/BlueCatKoKo.Ui/Extensions/FileNameExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.IO;
using System.Text;

namespace BlueCatKoKo.Ui.Extensions;

public static class FileNameExtensions
{
/// <summary>
/// 替换非法字符
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
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();
}
}
28 changes: 9 additions & 19 deletions src/BlueCatKoKo.Ui/ViewModels/Pages/HomeViewModel.cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 + "下载成功~";
Expand Down

0 comments on commit 0552c6a

Please sign in to comment.