Skip to content

Commit

Permalink
文件夹支持:上传部分
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeric-X committed May 5, 2024
1 parent 593f80c commit b40bc24
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="DotNetZip" Version="1.16.0" />
<PackageVersion Include="Magick.NET-Q16-x64" Version="13.5.0" />
<PackageVersion Include="Magick.NET-Q16-x86" Version="13.5.0" />
<PackageVersion Include="Magick.NET-Q16-arm64" Version="13.5.0" />
Expand Down
2 changes: 1 addition & 1 deletion src/SyncClipboard.Core/AppCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void Run()
contextMenu.AddMenuItemGroup(configManager.Menu);

PrepareRemoteWorkingFolder();
PrepareWorkingFolder(configManager);
DelegateExtention.InvokeNoExcept(() => PrepareWorkingFolder(configManager));
ServiceManager = Services.GetRequiredService<ServiceManager>();
ServiceManager.StartUpAllService();

Expand Down
8 changes: 6 additions & 2 deletions src/SyncClipboard.Core/Clipboard/ClipboardFactoryBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,21 @@ public abstract class ClipboardFactoryBase : IClipboardFactory, IProfileDtoHelpe

public Profile CreateProfileFromMeta(ClipboardMetaInfomation metaInfomation)
{
if (metaInfomation.Files != null)
if (metaInfomation.Files != null && metaInfomation.Files.Length >= 1)
{
var filename = metaInfomation.Files[0];
if (File.Exists(filename))
if (metaInfomation.Files.Length == 1 && File.Exists(filename))
{
if (ImageHelper.FileIsImage(filename))
{
return new ImageProfile(filename);
}
return new FileProfile(filename);
}
else
{
return new GroupProfile(metaInfomation.Files);
}
}

if (metaInfomation.Text != null)
Expand Down
49 changes: 49 additions & 0 deletions src/SyncClipboard.Core/Clipboard/Profile/GroupProfile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using Ionic.Zip;
using Microsoft.Extensions.DependencyInjection;
using SyncClipboard.Core.Interfaces;
using SyncClipboard.Core.Utilities;
using System.Text;

namespace SyncClipboard.Core.Clipboard;

public class GroupProfile : FileProfile
{
private readonly string[] _files;

protected override IClipboardSetter<Profile> ClipboardSetter
=> ServiceProvider.GetRequiredService<IClipboardSetter<GroupProfile>>();

public GroupProfile(string[] files) : base()
{
_files = files;
}

public override async Task UploadProfile(IWebDav webdav, CancellationToken token)
{
await PrepareTransferFile(token);
await base.UploadProfile(webdav, token);
}

protected async Task PrepareTransferFile(CancellationToken token)
{
var filePath = Path.Combine(LocalTemplateFolder, $"{Path.GetRandomFileName()}.zip");

using ZipFile zip = new ZipFile();
zip.AlternateEncoding = Encoding.UTF8;
zip.AlternateEncodingUsage = ZipOption.Always;
_files.ForEach(path =>
{
if (Directory.Exists(path))
{
zip.AddDirectory(path, Path.GetFileName(path));
}
else if (File.Exists(path))
{
zip.AddItem(path, "");
}
});

await Task.Run(() => zip.Save(filePath), token).WaitAsync(token);
FullPath = filePath;
}
}
1 change: 1 addition & 0 deletions src/SyncClipboard.Core/SyncClipboard.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<PackageReference Include="CommunityToolkit.Mvvm" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" />
<PackageReference Include="SharpHook" />
<PackageReference Include="DotNetZip" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected override async Task HandleClipboard(ClipboardMetaInfomation metaInfo,
{
var file = metaInfo.Files![0];
var newPath = await CompatibilityCast(_serviceProvider, file, cancellationToken);
new ImageProfile(newPath, _serviceProvider).SetLocalClipboard(false, cancellationToken);
new ImageProfile(newPath).SetLocalClipboard(false, cancellationToken);
}
catch (Exception ex)
{
Expand Down
12 changes: 12 additions & 0 deletions src/SyncClipboard.Core/Utilities/DelegateExtention.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,16 @@ public static Action<T> NoExcept<T>(this Action<T> action, string? logTag = null
}
};
}

public static void InvokeNoExcept(this Action action, string? logTag = null)
{
try
{
action.Invoke();
}
catch (Exception ex)
{
AppCore.Current?.Logger.Write(logTag, $"Invoke Unhandled Exception {ex.Message}\n{ex.StackTrace}");
}
}
}

0 comments on commit b40bc24

Please sign in to comment.