team-ljyzipelo

공개
73줄4.4 KB
1using System.IO;
2using System.Security;
3using Zipelo.Core;
4using Zipelo.Runtime;
5using Zipelo.Ui;
6
7namespace Zipelo.Desktop;
8
9internal 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 (ZipExtractionWorkflow.ValidationEnabled ? " " + ZipExtractionWorkflow.ScopeMessage : " 일반 압축 풀기는 비활성입니다.") +
15 (ZipCreationWorkflow.ValidationEnabled ? " " + ZipCreationWorkflow.ScopeMessage : " 생성 연결은 개발 검증 빌드에서만 사용할 수 있습니다.");
16
17 public bool CanExecute(WorkspaceAction action, string format) => IsAvailable &&
18 (action == WorkspaceAction.Create
19 ? ZipCreationWorkflow.ValidationEnabled && NewZipPublisher.IsSupportedPlatform &&
20 format.Equals("zip", StringComparison.OrdinalIgnoreCase)
21 : action == WorkspaceAction.Extract
22 ? ZipExtractionWorkflow.ValidationEnabled && NewFolderPublisher.IsSupportedPlatform &&
23 client.SupportsMemoryExtraction && format.Equals("zip", StringComparison.OrdinalIgnoreCase)
24 : listing.CanExecute(action, format));
25
26 public async Task<WorkspaceResult> ExecuteAsync(WorkspaceRequest request, IProgress<WorkspaceProgress> progress,
27 Func<PasswordChallenge, CancellationToken, Task<SecureString?>> requestPassword, CancellationToken token)
28 {
29 if (!CanExecute(request.Action, request.Format)) { throw new ArchiveException(ArchiveError.UnsupportedOperation); }
30 if (request.Action == WorkspaceAction.Extract)
31 {
32 if (request.Sources.Count != 1 || request.Destination is null ||
33 ArchiveOpenArguments.ValidatePath(request.Sources[0]).Error is not null)
34 {
35 throw new ArchiveException(ArchiveError.UnsupportedOperation);
36 }
37 var extraction = await ZipExtractionWorkflow.RunAsync(client, request.JobId, cancellation =>
38 {
39 cancellation.ThrowIfCancellationRequested();
40 return Task.FromResult<Stream>(new FileStream(request.Sources[0], FileMode.Open,
41 FileAccess.Read, FileShare.Read, 65536, FileOptions.Asynchronous | FileOptions.SequentialScan));
42 }, request.Destination, token).ConfigureAwait(false);
43 var extracted = CoreWorkspace.MapResult(extraction.Job);
44 var extractionError = extraction.Publication?.Error ?? extraction.Job.Error;
45 var extractionOutcome = ZipExtractionWorkflow.Published(extraction) ? WorkspaceOutcome.Completed
46 : extractionError == ArchiveError.Cancelled ? WorkspaceOutcome.Cancelled : WorkspaceOutcome.Failed;
47 return extracted with { Outcome = extractionOutcome, Error = extractionError, Extraction = extraction };
48 }
49 if (request.Action != WorkspaceAction.Create)
50 {
51 return await listing.ExecuteAsync(request, progress, requestPassword, token).ConfigureAwait(false);
52 }
53 if (request.Destination is null) { throw new ArchiveException(ArchiveError.OutputUnavailable); }
54 var sources = request.Sources.Select(path => new CreationSource(Path.GetFileName(path), cancellation =>
55 {
56 cancellation.ThrowIfCancellationRequested();
57 // 디렉터리 재귀 수집 없이 사용자가 고른 파일만 읽음.
58 if ((File.GetAttributes(path) & FileAttributes.Directory) != 0)
59 {
60 throw new ArchiveException(ArchiveError.UnsupportedEntry);
61 }
62 return Task.FromResult<Stream>(new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read,
63 65536, FileOptions.Asynchronous | FileOptions.SequentialScan));
64 })).ToArray();
65 var result = await ZipCreationWorkflow.RunAsync(client, request.JobId, sources, request.Destination, token)
66 .ConfigureAwait(false);
67 var mapped = CoreWorkspace.MapResult(result.Job);
68 var error = result.Publication?.Error ?? result.Job.Error;
69 var outcome = result.Publication?.Published == true ? WorkspaceOutcome.Completed
70 : error == ArchiveError.Cancelled ? WorkspaceOutcome.Cancelled : WorkspaceOutcome.Failed;
71 return mapped with { Outcome = outcome, Error = error, Creation = result };
72 }
73}