| 1 | using Zipelo.Core; |
| 2 | using Zipelo.Runtime; |
| 3 | using Zipelo.Ui; |
| 4 | |
| 5 | namespace Zipelo.Ubuntu; |
| 6 | |
| 7 | public interface IUbuntuArchiveWorkspace |
| 8 | { |
| 9 | bool CanListZip { get; } |
| 10 | bool CanCreateZip { get; } |
| 11 | bool CanExtractZip { get; } |
| 12 | Task<ExtractionResult> ExtractAsync(Guid jobId, Func<CancellationToken, Task<Stream>> openRead, |
| 13 | string destination, CancellationToken cancellationToken); |
| 14 | Task<CreationResult> CreateAsync(Guid jobId, IReadOnlyList<CreationSource> sources, |
| 15 | string destination, CancellationToken cancellationToken); |
| 16 | string AvailabilityMessage { get; } |
| 17 | Task<JobResult> ListAsync(Guid jobId, Stream input, CancellationToken cancellationToken); |
| 18 | } |
| 19 | |
| 20 | public sealed class UbuntuWorkspace : IUbuntuArchiveWorkspace |
| 21 | { |
| 22 | private readonly string directory = Path.Combine(AppContext.BaseDirectory, "worker"); |
| 23 | |
| 24 | // Linux 작업자는 같은 배포 폴더의 apphost만 사용함. PATH나 현재 폴더를 검색하지 않음. |
| 25 | private string WorkerPath => WorkerPlatform.GetWorkerPath(AppContext.BaseDirectory); |
| 26 | |
| 27 | public bool CanListZip => OperatingSystem.IsLinux() && HasRequiredFiles() && |
| 28 | new ZipWorkerClient(WorkerPath).Capabilities.Any(capability => |
| 29 | capability.Format.Equals("zip", StringComparison.OrdinalIgnoreCase) && capability.Action == ArchiveAction.List); |
| 30 | |
| 31 | public bool CanCreateZip => CanListZip && ZipCreationWorkflow.ValidationEnabled && NewZipPublisher.IsSupportedPlatform; |
| 32 | |
| 33 | public Task<CreationResult> CreateAsync(Guid jobId, IReadOnlyList<CreationSource> sources, |
| 34 | string destination, CancellationToken cancellationToken) |
| 35 | { |
| 36 | if (!CanCreateZip) { throw new ArchiveException(ArchiveError.UnsupportedOperation); } |
| 37 | return ZipCreationWorkflow.RunAsync(new ZipWorkerClient(WorkerPath), jobId, sources, destination, cancellationToken); |
| 38 | } |
| 39 | |
| 40 | public bool CanExtractZip => CanListZip && ZipExtractionWorkflow.ValidationEnabled && |
| 41 | NewFolderPublisher.IsSupportedPlatform && new ZipWorkerClient(WorkerPath).SupportsMemoryExtraction; |
| 42 | |
| 43 | public Task<ExtractionResult> ExtractAsync(Guid jobId, Func<CancellationToken, Task<Stream>> openRead, |
| 44 | string destination, CancellationToken cancellationToken) |
| 45 | { |
| 46 | if (!CanExtractZip) { throw new ArchiveException(ArchiveError.UnsupportedOperation); } |
| 47 | return ZipExtractionWorkflow.RunAsync(new ZipWorkerClient(WorkerPath), jobId, openRead, destination, cancellationToken); |
| 48 | } |
| 49 | |
| 50 | public string AvailabilityMessage => CanListZip |
| 51 | ? "Ubuntu UI/네이티브 게시는 검증 중입니다. ZIP 목록을 연결했습니다. " + |
| 52 | (ZipExtractionWorkflow.ValidationEnabled ? ZipExtractionWorkflow.ScopeMessage + " " : "일반 압축 풀기는 비활성입니다. ") + |
| 53 | (ZipCreationWorkflow.ValidationEnabled ? ZipCreationWorkflow.ScopeMessage : "생성 연결은 개발 검증 빌드에서만 사용할 수 있습니다.") |
| 54 | : "목록 읽기에 필요한 프로그램 파일이 준비되지 않았습니다. 생성과 압축 풀기도 준비 중입니다."; |
| 55 | |
| 56 | public async Task<JobResult> ListAsync(Guid jobId, Stream input, CancellationToken cancellationToken) |
| 57 | { |
| 58 | if (!CanListZip) |
| 59 | { |
| 60 | throw new ArchiveException(ArchiveError.EngineUnavailable); |
| 61 | } |
| 62 | var limits = new JobLimits(1024, 16L * 1024 * 1024, 16L * 1024 * 1024, |
| 63 | TimeSpan.FromMinutes(2), 3); |
| 64 | var result = await new ZipWorkerClient(WorkerPath).ListAsync(jobId, input, limits, cancellationToken) |
| 65 | .ConfigureAwait(false); |
| 66 | if (result.Job.JobId != jobId) |
| 67 | { |
| 68 | throw new ArchiveException(ArchiveError.EngineFailure); |
| 69 | } |
| 70 | return result.Job; |
| 71 | } |
| 72 | |
| 73 | private bool HasRequiredFiles() |
| 74 | { |
| 75 | string[] required = |
| 76 | [ |
| 77 | WorkerPlatform.ExecutableName, "Zipelo.Worker.dll", "Zipelo.Worker.runtimeconfig.json", |
| 78 | "Zipelo.Worker.deps.json", "Zipelo.Core.dll", "Zipelo.Runtime.dll" |
| 79 | ]; |
| 80 | return required.All(name => File.Exists(Path.Combine(directory, name))); |
| 81 | } |
| 82 | } |