Skip to content

Commit

Permalink
增加快捷键:手动上传(忽略内容控制)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeric-X committed Oct 3, 2024
1 parent 679f28c commit 9a84621
Show file tree
Hide file tree
Showing 13 changed files with 174 additions and 60 deletions.
3 changes: 2 additions & 1 deletion src/SyncClipboard.Core/AppCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ public static void ConfigurateUserService(IServiceCollection services)
services.AddSingleton<IService, ServerService>();
services.AddSingleton<UploadService>();
services.AddSingleton<IService, UploadService>(sp => sp.GetRequiredService<UploadService>());
services.AddSingleton<IService, DownloadService>();
services.AddSingleton<DownloadService>();
services.AddSingleton<IService, DownloadService>(sp => sp.GetRequiredService<DownloadService>());
}

private async void PrepareRemoteWorkingFolder()
Expand Down
16 changes: 10 additions & 6 deletions src/SyncClipboard.Core/Clipboard/ClipboardFactoryBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ public abstract class ClipboardFactoryBase : IClipboardFactory, IProfileDtoHelpe
protected abstract IWebDav WebDav { get; set; }

public abstract Task<ClipboardMetaInfomation> GetMetaInfomation(CancellationToken ctk);
public Task<Profile> CreateProfileFromMeta(ClipboardMetaInfomation metaInfomation, CancellationToken ctk)
{
return CreateProfileFromMeta(metaInfomation, true, ctk);
}

public async Task<Profile> CreateProfileFromMeta(ClipboardMetaInfomation metaInfomation, CancellationToken ctk)
public async Task<Profile> CreateProfileFromMeta(ClipboardMetaInfomation metaInfomation, bool contentControl, CancellationToken ctk)
{
if (metaInfomation.Files != null && metaInfomation.Files.Length >= 1)
{
Expand All @@ -27,24 +31,24 @@ public async Task<Profile> CreateProfileFromMeta(ClipboardMetaInfomation metaInf
{
if (ImageHelper.FileIsImage(filename))
{
return await ImageProfile.Create(filename, ctk);
return await ImageProfile.Create(filename, contentControl, ctk);
}
return await FileProfile.Create(filename, ctk);
return await FileProfile.Create(filename, contentControl, ctk);
}
else
{
return await GroupProfile.Create(metaInfomation.Files, ctk);
return await GroupProfile.Create(metaInfomation.Files, contentControl, ctk);
}
}

if (metaInfomation.Text != null)
{
return new TextProfile(metaInfomation.Text);
return new TextProfile(metaInfomation.Text, contentControl);
}

if (metaInfomation.Image != null)
{
return await ImageProfile.Create(metaInfomation.Image, ctk);
return await ImageProfile.Create(metaInfomation.Image, contentControl, ctk);
}

return new UnknownProfile();
Expand Down
1 change: 1 addition & 0 deletions src/SyncClipboard.Core/Clipboard/IClipboardFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public interface IClipboardFactory
{
Task<ClipboardMetaInfomation> GetMetaInfomation(CancellationToken ctk);
Task<Profile> CreateProfileFromMeta(ClipboardMetaInfomation metaInfomation, CancellationToken ctk);
Task<Profile> CreateProfileFromMeta(ClipboardMetaInfomation metaInfomation, bool contentControl, CancellationToken ctk);
Task<Profile> CreateProfileFromRemote(CancellationToken cancelToken);
Task<Profile> CreateProfileFromLocal(CancellationToken ctk);
}
35 changes: 24 additions & 11 deletions src/SyncClipboard.Core/Clipboard/Profile/FileProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using SyncClipboard.Core.Models;
using SyncClipboard.Core.Models.UserConfigs;
using SyncClipboard.Core.Utilities;
using SyncClipboard.Core.Utilities.Image;
using System.Security.Cryptography;
using System.Text;

Expand All @@ -27,25 +28,37 @@ protected override IClipboardSetter<Profile> ClipboardSetter

private static readonly string RemoteFileFolder = Env.RemoteFileFolder;

protected FileProfile(string fullPath, string hash) : this(fullPath, Path.GetFileName(fullPath), hash)
protected FileProfile(string fullPath, string hash, bool contentControl = true)
: this(fullPath, Path.GetFileName(fullPath), hash, contentControl)
{
}

public FileProfile(ClipboardProfileDTO profileDTO) : this(null, profileDTO.File, profileDTO.Clipboard)
{
}

private FileProfile(string? fullPath, string fileName, string hash)
private FileProfile(string? fullPath, string fileName, string hash, bool contentControl = true)
{
Hash = hash;
FullPath = fullPath;
FileName = fileName;
ContentControl = contentControl;
}

public static async Task<FileProfile> Create(string fullPath, CancellationToken token)
public static async Task<FileProfile> Create(string fullPath, bool contentControl, CancellationToken token)
{
var hash = await GetMD5HashFromFile(fullPath, token);
return new FileProfile(fullPath, hash);
var hash = await GetMD5HashFromFile(fullPath, contentControl, token);
if (ImageHelper.FileIsImage(fullPath))
{
return await ImageProfile.Create(fullPath, contentControl, token);
}

return new FileProfile(fullPath, hash, contentControl);
}

public static Task<FileProfile> Create(string fullPath, CancellationToken token)
{
return Create(fullPath, true, token);
}

protected string GetTempLocalFilePath()
Expand Down Expand Up @@ -92,16 +105,16 @@ public override async Task BeforeSetLocal(CancellationToken cancelToken,
string localPath = GetTempLocalFilePath();

await WebDav.GetFile(remotePath, localPath, progress, cancelToken);
await CheckHash(localPath, cancelToken);
await CheckHash(localPath, false, cancelToken);

Logger.Write("[PULL] download OK " + localPath);
FullPath = localPath;
_statusTip = FileName;
}

protected virtual async Task CheckHash(string localPath, CancellationToken cancelToken)
protected virtual async Task CheckHash(string localPath, bool checkSize, CancellationToken cancelToken)
{
var downloadedMd5 = await GetMD5HashFromFile(localPath, cancelToken);
var downloadedMd5 = await GetMD5HashFromFile(localPath, checkSize, cancelToken);
var existedMd5 = Hash;
if (string.IsNullOrEmpty(existedMd5))
{
Expand Down Expand Up @@ -148,10 +161,10 @@ protected override bool Same(Profile rhs)
}
}

protected async static Task<string> GetMD5HashFromFile(string fileName, CancellationToken? cancelToken)
protected async static Task<string> GetMD5HashFromFile(string fileName, bool checkSize, CancellationToken? cancelToken)
{
var fileInfo = new FileInfo(fileName);
if (fileInfo.Length > Config.GetConfig<SyncConfig>().MaxFileByte)
if (checkSize && fileInfo.Length > Config.GetConfig<SyncConfig>().MaxFileByte)
{
return MD5_FOR_OVERSIZED_FILE;
}
Expand Down Expand Up @@ -200,7 +213,7 @@ protected bool Oversized()

public override bool IsAvailableFromRemote() => !Oversized();

public override bool IsAvailableFromLocal() => IsFileAvailableAfterFilter(FullPath!)
public override bool IsAvailableAfterFilter() => IsFileAvailableAfterFilter(FullPath!)
&& !Oversized() && Config.GetConfig<SyncConfig>().EnableUploadSingleFile;

protected static bool IsFileAvailableAfterFilter(string fileName)
Expand Down
43 changes: 27 additions & 16 deletions src/SyncClipboard.Core/Clipboard/Profile/GroupProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,34 @@ public class GroupProfile : FileProfile
protected override IClipboardSetter<Profile> ClipboardSetter
=> ServiceProvider.GetRequiredService<IClipboardSetter<GroupProfile>>();

private GroupProfile(IEnumerable<string> files, string hash)
private GroupProfile(IEnumerable<string> files, string hash, bool contentControl)
: base(Path.Combine(LocalTemplateFolder, $"{Path.GetRandomFileName()}.zip"), hash)
{
_files = files.ToArray();
ContentControl = contentControl;
}

public GroupProfile(ClipboardProfileDTO profileDTO) : base(profileDTO)
{
}

public static async Task<Profile> Create(string[] files, CancellationToken token)
public static async Task<Profile> Create(string[] files, bool contentControl, CancellationToken token)
{
var filterdFiles = files.Where(file => IsFileAvailableAfterFilter(file));
if (filterdFiles.Count() == 1 && File.Exists(filterdFiles.First()))
return await Create(filterdFiles.First(), token);
if (contentControl)
{
var filterdFiles = files.Where(file => IsFileAvailableAfterFilter(file));
if (filterdFiles.Count() == 1 && File.Exists(filterdFiles.First()))
return await Create(filterdFiles.First(), contentControl, token);

var hash = await Task.Run(() => CaclHash(filterdFiles, contentControl, token)).WaitAsync(token);
return new GroupProfile(filterdFiles, hash, contentControl);
}
else
{
var hash = await Task.Run(() => CaclHash(files, contentControl, token)).WaitAsync(token);
return new GroupProfile(files, hash, contentControl);
}

var hash = await Task.Run(() => CaclHash(filterdFiles, token)).WaitAsync(token);
return new GroupProfile(files, hash);
}

private static int FileCompare(FileInfo file1, FileInfo file2)
Expand All @@ -56,7 +66,7 @@ private static int FileNameCompare(string file1, string file2)
);
}

private static string CaclHash(IEnumerable<string> filesEnum, CancellationToken token)
private static string CaclHash(IEnumerable<string> filesEnum, bool contentControl, CancellationToken token)
{
var files = filesEnum.ToArray();
var maxSize = Config.GetConfig<SyncConfig>().MaxFileByte;
Expand All @@ -75,19 +85,19 @@ private static string CaclHash(IEnumerable<string> filesEnum, CancellationToken
foreach (var subFile in subFiles)
{
sumSize += subFile.Length;
if (sumSize > maxSize)
if (contentControl && sumSize > maxSize)
return MD5_FOR_OVERSIZED_FILE;
hash = (hash * -1521134295) + (subFile.Name + subFile.Length.ToString()).ListHashCode();
}
}
else if (File.Exists(file) && IsFileAvailableAfterFilter(file))
else if (File.Exists(file) && (!contentControl || IsFileAvailableAfterFilter(file)))
{
var fileInfo = new FileInfo(file);
sumSize += fileInfo.Length;
hash = (hash * -1521134295) + (fileInfo.Name + fileInfo.Length.ToString()).ListHashCode();
}

if (sumSize > maxSize)
if (contentControl && sumSize > maxSize)
{
return MD5_FOR_OVERSIZED_FILE;
}
Expand Down Expand Up @@ -125,11 +135,12 @@ public Task PrepareTransferFile(CancellationToken token)
zip.AddItem(path, "");
}
});
foreach (var item in zip.Entries)

if (ContentControl)
{
if (!item.IsDirectory)
foreach (var item in zip.Entries)
{
if (!IsFileAvailableAfterFilter(item.FileName))
if (!item.IsDirectory && !IsFileAvailableAfterFilter(item.FileName))
{
zip.RemoveEntry(item.FileName);
}
Expand Down Expand Up @@ -170,7 +181,7 @@ protected override ClipboardMetaInfomation CreateMetaInformation()
return new ClipboardMetaInfomation() { Files = _files };
}

protected override Task CheckHash(string _, CancellationToken _1) => Task.CompletedTask;
protected override Task CheckHash(string _, bool _1, CancellationToken _2) => Task.CompletedTask;

protected override void SetNotification(INotification notification)
{
Expand Down Expand Up @@ -198,7 +209,7 @@ public override string ShowcaseText()
return string.Join("\n", _files.Select(file => Path.GetFileName(file)));
}

public override bool IsAvailableFromLocal()
public override bool IsAvailableAfterFilter()
{
bool hasItem = null != _files?.FirstOrDefault(name => Directory.Exists(name) || IsFileAvailableAfterFilter(name));
return hasItem && !Oversized() && Config.GetConfig<SyncConfig>().EnableUploadMultiFile;
Expand Down
18 changes: 12 additions & 6 deletions src/SyncClipboard.Core/Clipboard/Profile/ImageProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,23 @@ protected override IClipboardSetter<Profile> ClipboardSetter

private static string ImageTemplateFolder => Path.Combine(LocalTemplateFolder, "temp images");

private ImageProfile(string fullPath, string hash) : base(fullPath, hash)
private ImageProfile(string fullPath, string hash, bool contentControl = true)
: base(fullPath, hash, contentControl)
{
}

public ImageProfile(ClipboardProfileDTO profileDTO) : base(profileDTO)
{
}

public static async Task<ImageProfile> Create(IClipboardImage image, CancellationToken token)
public static async Task<ImageProfile> Create(IClipboardImage image, bool contentControl, CancellationToken token)
{
for (int i = 0; ; i++)
{
try
{
var fullPath = await Task.Run(() => SaveImageToFile(image)).WaitAsync(token);
return await Create(fullPath, token);
return await Create(fullPath, contentControl, token);
}
catch when (!token.IsCancellationRequested)
{
Expand All @@ -40,10 +41,15 @@ public static async Task<ImageProfile> Create(IClipboardImage image, Cancellatio
}
}

public static new async Task<ImageProfile> Create(string fullPath, CancellationToken token)
public static new async Task<ImageProfile> Create(string fullPath, bool contentControl, CancellationToken token)
{
var hash = await GetMD5HashFromFile(fullPath, token);
return new ImageProfile(fullPath, hash);
var hash = await GetMD5HashFromFile(fullPath, contentControl, token);
return new ImageProfile(fullPath, hash, contentControl);
}

public static new Task<ImageProfile> Create(string fullPath, CancellationToken token)
{
return Create(fullPath, true, token);
}

private static string SaveImageToFile(IClipboardImage image)
Expand Down
4 changes: 3 additions & 1 deletion src/SyncClipboard.Core/Clipboard/Profile/Profile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ public virtual Task BeforeSetLocal(CancellationToken cancelToken,
return Task.CompletedTask;
}

public bool ContentControl { get; set; } = true;
public virtual bool IsAvailableFromRemote() => true;
public virtual bool IsAvailableFromLocal() => true;
public bool IsAvailableFromLocal() => !ContentControl || IsAvailableAfterFilter();
public virtual bool IsAvailableAfterFilter() => true;

public virtual Task EnsureAvailable(CancellationToken token) => Task.CompletedTask;

Expand Down
5 changes: 3 additions & 2 deletions src/SyncClipboard.Core/Clipboard/Profile/TextProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ public class TextProfile : Profile
protected override IClipboardSetter<Profile> ClipboardSetter
=> ServiceProvider.GetRequiredService<IClipboardSetter<TextProfile>>();

public TextProfile(string text)
public TextProfile(string text, bool contentControl = true)
{
Text = text;
ContentControl = contentControl;
}

public override string ToolTip()
Expand Down Expand Up @@ -88,5 +89,5 @@ private static bool HasUrl(string str, out string? url)
return false;
}

public override bool IsAvailableFromLocal() => Config.GetConfig<SyncConfig>().EnableUploadText;
public override bool IsAvailableAfterFilter() => Config.GetConfig<SyncConfig>().EnableUploadText;
}
18 changes: 18 additions & 0 deletions src/SyncClipboard.Core/I18n/Strings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/SyncClipboard.Core/I18n/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -564,4 +564,10 @@
<data name="SyncContentControl" xml:space="preserve">
<value>Sync Content Control</value>
</data>
<data name="UploadWithoutFilter" xml:space="preserve">
<value>Upload without Content Control</value>
</data>
<data name="CopyAndUploadWithoutFilter" xml:space="preserve">
<value>Copy And Upload without Content Control</value>
</data>
</root>
6 changes: 6 additions & 0 deletions src/SyncClipboard.Core/I18n/Strings.zh-CN.resx
Original file line number Diff line number Diff line change
Expand Up @@ -564,4 +564,10 @@
<data name="SyncContentControl" xml:space="preserve">
<value>同步內容控制</value>
</data>
<data name="UploadWithoutFilter" xml:space="preserve">
<value>上传本地到远程(忽略内容控制)</value>
</data>
<data name="CopyAndUploadWithoutFilter" xml:space="preserve">
<value>复制并上传本地到远程(忽略内容控制)</value>
</data>
</root>
Loading

0 comments on commit 9a84621

Please sign in to comment.