| 1 | using System.IO; |
| 2 | using System.Security; |
| 3 | using Zipelo.Core; |
| 4 | using Zipelo.Runtime; |
| 5 | using Zipelo.Ui; |
| 6 | |
| 7 | namespace Zipelo.Desktop; |
| 8 | |
| 9 | internal sealed class CreationWorkspace(CoreWorkspace listing, ZipWorkerClient client, Func<bool> filesReady) |
| 10 | : IArchiveWorkspace, IWorkspaceCapabilities |
| 11 | { |
| 12 | public bool IsAvailable => listing.IsAvailable && filesReady(); |
| 13 | public string AvailabilityMessage => listing.AvailabilityMessage + |
| 14 | (ZipCreationWorkflow.ValidationEnabled ? " " + ZipCreationWorkflow.ScopeMessage : " 생성 연결은 개발 검증 빌드에서만 사용할 수 있습니다."); |
| 15 | |
| 16 | public bool CanExecute(WorkspaceAction action, string format) => IsAvailable && |
| 17 | (action == WorkspaceAction.Create |
| 18 | ? ZipCreationWorkflow.ValidationEnabled && NewZipPublisher.IsSupportedPlatform && |
| 19 | format.Equals("zip", StringComparison.OrdinalIgnoreCase) |
| 20 | : listing.CanExecute(action, format)); |
| 21 | |
| 22 | public async Task<WorkspaceResult> ExecuteAsync(WorkspaceRequest request, IProgress<WorkspaceProgress> progress, |
| 23 | Func<PasswordChallenge, CancellationToken, Task<SecureString?>> requestPassword, CancellationToken token) |
| 24 | { |
| 25 | if (!CanExecute(request.Action, request.Format)) { throw new ArchiveException(ArchiveError.UnsupportedOperation); } |
| 26 | if (request.Action != WorkspaceAction.Create) |
| 27 | { |
| 28 | return await listing.ExecuteAsync(request, progress, requestPassword, token).ConfigureAwait(false); |
| 29 | } |
| 30 | if (request.Destination is null) { throw new ArchiveException(ArchiveError.OutputUnavailable); } |
| 31 | var sources = request.Sources.Select(path => new CreationSource(Path.GetFileName(path), cancellation => |
| 32 | { |
| 33 | cancellation.ThrowIfCancellationRequested(); |
| 34 | // 디렉터리 재귀 수집 없이 사용자가 고른 파일만 읽음. |
| 35 | if ((File.GetAttributes(path) & FileAttributes.Directory) != 0) |
| 36 | { |
| 37 | throw new ArchiveException(ArchiveError.UnsupportedEntry); |
| 38 | } |
| 39 | return Task.FromResult<Stream>(new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, |
| 40 | 65536, FileOptions.Asynchronous | FileOptions.SequentialScan)); |
| 41 | })).ToArray(); |
| 42 | var result = await ZipCreationWorkflow.RunAsync(client, request.JobId, sources, request.Destination, token) |
| 43 | .ConfigureAwait(false); |
| 44 | var mapped = CoreWorkspace.MapResult(result.Job); |
| 45 | var error = result.Publication?.Error ?? result.Job.Error; |
| 46 | var outcome = result.Publication?.Published == true ? WorkspaceOutcome.Completed |
| 47 | : error == ArchiveError.Cancelled ? WorkspaceOutcome.Cancelled : WorkspaceOutcome.Failed; |
| 48 | return mapped with { Outcome = outcome, Error = error, Creation = result }; |
| 49 | } |
| 50 | } |