feat: 添加文件已存在自动重命名的开关 Fixes: #27 Fixes: #21

This commit is contained in:
姚彪
2024-02-04 21:49:24 +08:00
parent 6fce331de4
commit d2db8d7877
7 changed files with 87 additions and 5 deletions

View File

@@ -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;
}

View File

@@ -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>
@@ -190,7 +193,7 @@ public partial class SettingsManager
appSettings.Basic.DownloadFinishedSort = finishedSort;
return SetSettings();
}
/// <summary>
/// 获取重复下载策略
/// </summary>
@@ -218,4 +221,33 @@ public partial class SettingsManager
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();
}
}

View File

@@ -211,6 +211,6 @@ public static class Format
destName = destName.Trim('.');
// 如果只有空白字符、dot符
return destName is "" or "." ? "[empty title]" : destName;
return destName is " " or "." ? "[empty title]" : destName;
}
}

View File

@@ -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>

View File

@@ -445,6 +445,27 @@ public class AddToDownloadService
// 合成绝对路径
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)

View File

@@ -81,6 +81,14 @@ public class ViewBasicViewModel : ViewModelBase
get => _autoDownloadAll;
set => SetProperty(ref _autoDownloadAll, value);
}
private bool _repeatFileAutoAddNumberSuffix;
public bool RepeatFileAutoAddNumberSuffix
{
get => _repeatFileAutoAddNumberSuffix;
set => SetProperty(ref _repeatFileAutoAddNumberSuffix, value);
}
private List<RepeatDownloadStrategyDisplay> _repeatDownloadStrategy;
@@ -260,13 +268,23 @@ public class ViewBasicViewModel : ViewModelBase
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)

View File

@@ -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>