Skip to content

Commit

Permalink
GroupClipboardSetter(Windows)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeric-X committed May 5, 2024
1 parent 5e12850 commit e21b210
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/SyncClipboard.Core/Clipboard/ClipboardFactoryBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ private static Profile GetProfileBy(ClipboardProfileDTO profileDTO)
}
case ProfileType.Image:
return new ImageProfile(profileDTO);
case ProfileType.Group:
return new GroupProfile(profileDTO);
}

return new UnknownProfile();
Expand Down
50 changes: 47 additions & 3 deletions src/SyncClipboard.Core/Clipboard/Profile/GroupProfile.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
using Ionic.Zip;
using Microsoft.Extensions.DependencyInjection;
using SyncClipboard.Abstract;
using SyncClipboard.Core.Interfaces;
using SyncClipboard.Core.Models;
using SyncClipboard.Core.Utilities;
using System.Text;

namespace SyncClipboard.Core.Clipboard;

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

public override ProfileType Type => ProfileType.Group;
public override string FileName
{
get
{
if (string.IsNullOrEmpty(base.FileName))
{
FileName = $"{Path.GetRandomFileName()}.zip";
}
return base.FileName;
}
set => base.FileName = value;
}

protected override IClipboardSetter<Profile> ClipboardSetter
=> ServiceProvider.GetRequiredService<IClipboardSetter<GroupProfile>>();
Expand All @@ -18,6 +34,10 @@ public GroupProfile(string[] files) : base()
_files = files;
}

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

public override async Task UploadProfile(IWebDav webdav, CancellationToken token)
{
await PrepareTransferFile(token);
Expand All @@ -26,11 +46,13 @@ public override async Task UploadProfile(IWebDav webdav, CancellationToken token

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

using ZipFile zip = new ZipFile();
zip.AlternateEncoding = Encoding.UTF8;
zip.AlternateEncodingUsage = ZipOption.Always;
zip.AlternateEncodingUsage = ZipOption.AsNecessary;

ArgumentNullException.ThrowIfNull(_files);
_files.ForEach(path =>
{
if (Directory.Exists(path))
Expand All @@ -46,4 +68,26 @@ protected async Task PrepareTransferFile(CancellationToken token)
await Task.Run(() => zip.Save(filePath), token).WaitAsync(token);
FullPath = filePath;
}

public override async Task BeforeSetLocal(CancellationToken token, IProgress<HttpDownloadProgress>? progress)
{
await base.BeforeSetLocal(token, progress);

ArgumentNullException.ThrowIfNull(FullPath);
var extractPath = FullPath[..^4];
if (!Directory.Exists(extractPath))
Directory.CreateDirectory(extractPath);

var fileList = new List<string>();
using ZipFile zip = ZipFile.Read(FullPath);

await Task.Run(() => zip.ExtractAll(extractPath, ExtractExistingFileAction.DoNotOverwrite), token).WaitAsync(token);
_files = zip.EntryFileNames.Select(fileName => Path.Combine(extractPath, fileName.TrimEnd('\\', '/'))).ToArray();
}

protected override ClipboardMetaInfomation CreateMetaInformation()
{
ArgumentNullException.ThrowIfNull(_files);
return new ClipboardMetaInfomation() { Files = _files };
}
}
1 change: 1 addition & 0 deletions src/SyncClipboard.WinUI3/AppServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public static ServiceCollection ConfigureServices()
services.AddTransient<IClipboardSetter<TextProfile>, TextClipboardSetter>();
services.AddTransient<IClipboardSetter<FileProfile>, FileClipboardSetter>();
services.AddTransient<IClipboardSetter<ImageProfile>, ImageClipboardSetter>();
services.AddTransient<IClipboardSetter<GroupProfile>, GroupClipboardSetter>();

return services;
}
Expand Down
35 changes: 35 additions & 0 deletions src/SyncClipboard.WinUI3/ClipboardWinUI/GroupClipboardSetter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using SyncClipboard.Core.Clipboard;
using SyncClipboard.Core.Models;
using System;
using System.IO;
using System.Linq;
using Windows.ApplicationModel.DataTransfer;
using Windows.Storage;

namespace SyncClipboard.WinUI3.ClipboardWinUI;

internal class GroupClipboardSetter : ClipboardSetterBase<GroupProfile>
{
protected override DataPackage CreatePackage(ClipboardMetaInfomation metaInfomation)
{
if (metaInfomation.Files is null || metaInfomation.Files.Length == 0)
{
throw new ArgumentException("Not Contain File.");
}

var items = metaInfomation.Files
.Where(file => Directory.Exists(file) || File.Exists(file))
.Select<string, IStorageItem>(file =>
{
if (Directory.Exists(file))
{
return StorageFolder.GetFolderFromPathAsync(file).AsTask().Result;
}
return StorageFile.GetFileFromPathAsync(file).AsTask().Result;
});

var dataObject = new DataPackage();
dataObject.SetStorageItems(items.ToArray());
return dataObject;
}
}

0 comments on commit e21b210

Please sign in to comment.