Skip to content

Commit

Permalink
fix: 修复下载时开启自动添加后缀文件夹不存在时闪退问题 Fixes: #78 #74
Browse files Browse the repository at this point in the history
  • Loading branch information
yaobiao131 committed Apr 24, 2024
1 parent b91a7c9 commit 54895f4
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions DownKyi/Services/Download/AddToDownloadService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,8 @@ await dialogService.ShowDialogAsync(ViewDownloadSetterViewModel.Tag, null, resul
/// <param name="directory">下载路径</param>
/// <param name="isAll">是否下载所有,包括未选中项</param>
/// <returns>添加的数量</returns>
public async Task<int> AddToDownload(IEventAggregator eventAggregator, IDialogService dialogService, string? directory, bool isAll = false)
public async Task<int> AddToDownload(IEventAggregator eventAggregator, IDialogService dialogService,
string? directory, bool isAll = false)
{
if (string.IsNullOrEmpty(directory))
{
Expand Down Expand Up @@ -302,10 +303,12 @@ public async Task<int> AddToDownload(IEventAggregator eventAggregator, IDialogSe
continue;
}

if (item.DownloadBase.Cid == page.Cid && item.Resolution.Id == page.VideoQuality.Quality && item.AudioCodec.Name == page.AudioQualityFormat &&
if (item.DownloadBase.Cid == page.Cid && item.Resolution.Id == page.VideoQuality.Quality &&
item.AudioCodec.Name == page.AudioQualityFormat &&
item.VideoCodecName == page.VideoQuality.SelectedVideoCodec)
{
eventAggregator.GetEvent<MessageEvent>().Publish($"{page.Name}{DictionaryResource.GetString("TipAlreadyToAddDownloading")}");
eventAggregator.GetEvent<MessageEvent>()
.Publish($"{page.Name}{DictionaryResource.GetString("TipAlreadyToAddDownloading")}");
isDownloading = true;
break;
}
Expand All @@ -325,7 +328,8 @@ public async Task<int> AddToDownload(IEventAggregator eventAggregator, IDialogSe
continue;
}

if (item.DownloadBase.Cid == page.Cid && item.Resolution.Id == page.VideoQuality.Quality && item.AudioCodec.Name == page.AudioQualityFormat &&
if (item.DownloadBase.Cid == page.Cid && item.Resolution.Id == page.VideoQuality.Quality &&
item.AudioCodec.Name == page.AudioQualityFormat &&
item.VideoCodecName == page.VideoQuality.SelectedVideoCodec)
{
// eventAggregator.GetEvent<MessageEvent>().Publish($"{page.Name}{DictionaryResource.GetString("TipAlreadyToAddDownloaded")}");
Expand Down Expand Up @@ -447,19 +451,22 @@ await dialogService.ShowDialogAsync(ViewAlreadyDownloadedDialogViewModel.Tag, pa
// 如果存在同名文件,自动重命名
// todo 如果重新下载呢。还没想好
var directoryName = Path.GetDirectoryName(filePath);
var files = Directory.GetFiles(directoryName).Select(Path.GetFileNameWithoutExtension).Distinct().ToList();

if (files.Contains(Path.GetFileNameWithoutExtension(filePath)))
if (Directory.Exists(directoryName))
{
var count = 1;
var newFilePath = filePath;
while (files.Contains(Path.GetFileNameWithoutExtension(newFilePath)))
var files = Directory.GetFiles(directoryName).Select(Path.GetFileNameWithoutExtension).Distinct().ToList();

if (files.Contains(Path.GetFileNameWithoutExtension(filePath)))
{
newFilePath = Path.Combine(directory, $"{fileName.RelativePath()}({count})");
count++;
}
var count = 1;
var newFilePath = filePath;
while (files.Contains(Path.GetFileNameWithoutExtension(newFilePath)))
{
newFilePath = Path.Combine(directory, $"{fileName.RelativePath()}({count})");
count++;
}

filePath = newFilePath;
filePath = newFilePath;
}
}
}

Expand Down Expand Up @@ -518,7 +525,8 @@ await dialogService.ShowDialogAsync(ViewAlreadyDownloadedDialogViewModel.Tag, pa
Name = page.Name,
Duration = page.Duration,
VideoCodecName = page.VideoQuality.SelectedVideoCodec,
Resolution = new Quality { Name = page.VideoQuality.QualityFormat, Id = page.VideoQuality.Quality },
Resolution = new Quality
{ Name = page.VideoQuality.QualityFormat, Id = page.VideoQuality.Quality },
AudioCodec = Constant.GetAudioQualities().FirstOrDefault(t => { return t.Name == page.AudioQualityFormat; }),
Page = page.Page
};
Expand Down

2 comments on commit 54895f4

@NopantsNobra
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

问题依然存在,已关闭重复文件,已设置自建下载器。

@NopantsNobra
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

经测试,和同时下载数与线程数有关联,改小之后闪退情况大大改善。

Please sign in to comment.