| 1 | using System.IO; |
| 2 | using Zipelo.Core; |
| 3 | using Zipelo.Runtime; |
| 4 | |
| 5 | namespace Zipelo.Desktop; |
| 6 | |
| 7 | internal static class ZipWorkerWorkspace |
| 8 | { |
| 9 | public static IArchiveWorkspace? TryCreate(out string unavailableReason) |
| 10 | { |
| 11 | // 사용자 입력이나 압축파일에서 실행 경로를 얻지 않음. |
| 12 | unavailableReason = ""; |
| 13 | var workerDirectory = Path.Combine(AppContext.BaseDirectory, "worker"); |
| 14 | if (!HasRequiredFiles(workerDirectory)) |
| 15 | { |
| 16 | unavailableReason = "작업에 필요한 프로그램 파일이 누락되었습니다. Zipelo 폴더 전체를 복원한 뒤 다시 실행하세요."; |
| 17 | return null; |
| 18 | } |
| 19 | var workerPath = Path.Combine(workerDirectory, "Zipelo.Worker.exe"); |
| 20 | var client = new ZipWorkerClient(workerPath); |
| 21 | if (!client.IsAvailable) |
| 22 | { |
| 23 | unavailableReason = WorkspaceMessages.Error(ArchiveError.EngineUnavailable); |
| 24 | return null; |
| 25 | } |
| 26 | |
| 27 | var capabilities = client.Capabilities.Where(capability => |
| 28 | capability.Format.Equals("zip", StringComparison.OrdinalIgnoreCase) && |
| 29 | capability.Action == ArchiveAction.List).ToArray(); |
| 30 | var listing = new CoreWorkspace(async (request, progress, passwords, token) => |
| 31 | { |
| 32 | if (request.Action != WorkspaceAction.List || request.Sources.Count != 1 || |
| 33 | !request.Format.Equals("zip", StringComparison.OrdinalIgnoreCase) || |
| 34 | Path.GetExtension(request.Sources[0]).Equals(".jar", StringComparison.OrdinalIgnoreCase)) |
| 35 | { |
| 36 | throw new ArchiveException(ArchiveError.UnsupportedOperation); |
| 37 | } |
| 38 | |
| 39 | if (!HasRequiredFiles(workerDirectory)) |
| 40 | { |
| 41 | throw new ArchiveException(ArchiveError.EngineUnavailable); |
| 42 | } |
| 43 | |
| 44 | // 파싱은 작업자에서 수행함. Desktop은 읽기 전용 스트림만 전달함. |
| 45 | await using var input = new FileStream(request.Sources[0], FileMode.Open, FileAccess.Read, |
| 46 | FileShare.Read, 65536, FileOptions.Asynchronous | FileOptions.SequentialScan); |
| 47 | var limits = new JobLimits(1024, 16L * 1024 * 1024, 16L * 1024 * 1024, |
| 48 | TimeSpan.FromMinutes(2), 3); |
| 49 | var result = await client.ListAsync(request.JobId, input, limits, token).ConfigureAwait(false); |
| 50 | // 런타임은 현재 실시간 진행과 암호 요청을 제공하지 않음. 가짜 이벤트를 만들지 않음. |
| 51 | return result.Job; |
| 52 | }, capabilities, |
| 53 | "단일 ZIP 목록을 지원합니다. 일반 사용자용 생성과 압축 풀기는 검증 전입니다."); |
| 54 | return new CreationWorkspace(listing, client, () => HasRequiredFiles(workerDirectory)); |
| 55 | } |
| 56 | |
| 57 | private static bool HasRequiredFiles(string directory) |
| 58 | { |
| 59 | string[] required = |
| 60 | [ |
| 61 | "Zipelo.Worker.exe", |
| 62 | "Zipelo.Worker.dll", |
| 63 | "Zipelo.Worker.runtimeconfig.json", |
| 64 | "Zipelo.Worker.deps.json", |
| 65 | "Zipelo.Core.dll", |
| 66 | "Zipelo.Runtime.dll" |
| 67 | ]; |
| 68 | return required.All(name => File.Exists(Path.Combine(directory, name))); |
| 69 | } |
| 70 | |
| 71 | } |