Skip to content

Commit

Permalink
feat: 添加文件已存在自动重命名的开关 Fixes: #27 Fixes: #21
Browse files Browse the repository at this point in the history
  • Loading branch information
yaobiao131 committed Feb 4, 2024
1 parent 6fce331 commit d2db8d7
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 5 deletions.
1 change: 1 addition & 0 deletions DownKyi.Core/Settings/Models/BasicSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ public class BasicSettings
public AllowStatus IsAutoDownloadAll { get; set; } = AllowStatus.NONE;
public DownloadFinishedSort DownloadFinishedSort { get; set; } = DownloadFinishedSort.NOT_SET;
public RepeatDownloadStrategy RepeatDownloadStrategy { get; set; } = RepeatDownloadStrategy.Ask;
public bool RepeatFileAutoAddNumberSuffix { get; set; } = false;
}
36 changes: 34 additions & 2 deletions DownKyi.Core/Settings/SettingsManager.Basic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ public partial class SettingsManager

// 下载完成列表排序
private readonly DownloadFinishedSort _finishedSort = DownloadFinishedSort.DOWNLOAD;

// 重复下载策略
private readonly RepeatDownloadStrategy _repeatDownloadStrategy = RepeatDownloadStrategy.Ask;

// 重复文件自动添加数字后缀
private readonly bool _repeatFileAutoAddNumberSuffix = false;

/// <summary>
/// 获取下载完成后的操作
/// </summary>
Expand Down Expand Up @@ -190,7 +193,7 @@ public bool SetDownloadFinishedSort(DownloadFinishedSort finishedSort)
appSettings.Basic.DownloadFinishedSort = finishedSort;
return SetSettings();
}

/// <summary>
/// 获取重复下载策略
/// </summary>
Expand Down Expand Up @@ -218,4 +221,33 @@ public bool SetRepeatDownloadStrategy(RepeatDownloadStrategy repeatDownloadStrat
appSettings.Basic.RepeatDownloadStrategy = repeatDownloadStrategy;
return SetSettings();
}

/// <summary>
/// 重复文件自动添加数字后缀
/// </summary>
/// <returns></returns>
public bool IsRepeatFileAutoAddNumberSuffix()
{
appSettings = GetSettings();
if (appSettings.Basic.RepeatFileAutoAddNumberSuffix == false)
{
// 第一次获取,先设置默认值
IsRepeatFileAutoAddNumberSuffix(_repeatFileAutoAddNumberSuffix);
return _repeatFileAutoAddNumberSuffix;
}

return appSettings.Basic.RepeatFileAutoAddNumberSuffix;
}

/// <summary>
/// 设置重复文件自动添加数字后缀
/// </summary>
/// <param name="repeatFileAutoAddNumberSuffix"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool IsRepeatFileAutoAddNumberSuffix(bool repeatFileAutoAddNumberSuffix)
{
appSettings.Basic.RepeatFileAutoAddNumberSuffix = repeatFileAutoAddNumberSuffix;
return SetSettings();
}
}
2 changes: 1 addition & 1 deletion DownKyi.Core/Utils/Format.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,6 @@ public static string FormatFileName(string originName)
destName = destName.Trim('.');

// 如果只有空白字符、dot符
return destName is "" or "." ? "[empty title]" : destName;
return destName is " " or "." ? "[empty title]" : destName;
}
}
1 change: 1 addition & 0 deletions DownKyi/Languanges/Default.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@
<system:String x:Key="RepeatDownloadAsk">每次询问</system:String>
<system:String x:Key="RepeatDownloadReDownload">重新下载</system:String>
<system:String x:Key="RepeatDownloadReJumpOver">跳过重复项</system:String>
<system:String x:Key="RepeatFileAutoAddNumberSuffix">重复文件自动添加数字序号后缀</system:String>
<system:String x:Key="AutoDownloadAll">解析后自动下载已解析视频</system:String>
<system:String x:Key="Network">网络</system:String>
<system:String x:Key="UseSSL">启用https(若下载器提示SSL错误,则关闭此项)</system:String>
Expand Down
21 changes: 21 additions & 0 deletions DownKyi/Services/Download/AddToDownloadService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,27 @@ await dialogService.ShowDialogAsync(ViewAlreadyDownloadedDialogViewModel.Tag, pa
// 合成绝对路径
var filePath = Path.Combine(directory, fileName.RelativePath());

if (SettingsManager.GetInstance().IsRepeatFileAutoAddNumberSuffix())
{
// 如果存在同名文件,自动重命名
// todo 如果重新下载呢。还没想好
var directoryName = Path.GetDirectoryName(filePath);
var files = Directory.GetFiles(directoryName).Select(Path.GetFileNameWithoutExtension).Distinct().ToList();

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

filePath = newFilePath;
}
}

// 视频类别
PlayStreamType playStreamType;
switch (_videoInfoView.TypeId)
Expand Down
22 changes: 20 additions & 2 deletions DownKyi/ViewModels/Settings/ViewBasicViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ public bool AutoDownloadAll
get => _autoDownloadAll;
set => SetProperty(ref _autoDownloadAll, value);
}

private bool _repeatFileAutoAddNumberSuffix;

public bool RepeatFileAutoAddNumberSuffix
{
get => _repeatFileAutoAddNumberSuffix;
set => SetProperty(ref _repeatFileAutoAddNumberSuffix, value);
}

private List<RepeatDownloadStrategyDisplay> _repeatDownloadStrategy;

Expand Down Expand Up @@ -260,13 +268,23 @@ private void ExecuteAutoDownloadAllCommand()
PublishTip(isSucceed);
}

// 解析范围事件
private DelegateCommand? _repeatFileAutoAddNumberSuffixCommand;

public DelegateCommand RepeatFileAutoAddNumberSuffixCommand => _repeatFileAutoAddNumberSuffixCommand ??= new DelegateCommand(ExecuteRepeatFileAutoAddNumberSuffixCommand);

private void ExecuteRepeatFileAutoAddNumberSuffixCommand()
{
var isSucceed = SettingsManager.GetInstance().IsRepeatFileAutoAddNumberSuffix(RepeatFileAutoAddNumberSuffix);
PublishTip(isSucceed);
}

// 重复下载策略事件
private DelegateCommand<object>? _repeatDownloadStrategyCommand;

public DelegateCommand<object> RepeatDownloadStrategyCommand => _repeatDownloadStrategyCommand ??= new DelegateCommand<object>(ExecuteRepeatDownloadStrategyCommand);

/// <summary>
/// 解析范围事件
/// 重复下载策略事件
/// </summary>
/// <param name="parameter"></param>
private void ExecuteRepeatDownloadStrategyCommand(object parameter)
Expand Down
9 changes: 9 additions & 0 deletions DownKyi/Views/Settings/ViewBasic.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,15 @@

</ComboBox>
</StackPanel>
<CheckBox
Margin="0,20,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Command="{Binding RepeatFileAutoAddNumberSuffixCommand}"
Content="{DynamicResource RepeatFileAutoAddNumberSuffix}"
Foreground="{DynamicResource BrushTextDark}"
IsChecked="{Binding RepeatFileAutoAddNumberSuffix, Mode=TwoWay}"
Theme="{StaticResource CheckBoxStyle}" />
<StackPanel Margin="10" />
</StackPanel>
</ScrollViewer>
Expand Down

0 comments on commit d2db8d7

Please sign in to comment.