| 1 | using System.IO; |
| 2 | using Zipelo.Core; |
| 3 | using Zipelo.Runtime; |
| 4 | |
| 5 | namespace Zipelo.Ui; |
| 6 | |
| 7 | public sealed record ExtractionResult(JobResult Job, ZipPublicationResult? Publication); |
| 8 | |
| 9 | public static class ZipExtractionWorkflow |
| 10 | { |
| 11 | #if ZIPELO_EXTRACTION_VALIDATION |
| 12 | public static bool ValidationEnabled => true; |
| 13 | #else |
| 14 | public static bool ValidationEnabled => false; |
| 15 | #endif |
| 16 | public const string ScopeMessage = "개발 검증용: ZIP 입력/해제 합계 각각 16 MiB, 최대 1,024항목, 2분 한도입니다. 새 폴더만 게시하며 암호/분할/다른 형식은 지원하지 않습니다."; |
| 17 | |
| 18 | public static bool Published(ExtractionResult result) => |
| 19 | result.Job.State == JobState.Completed && result.Job.Error == ArchiveError.None && |
| 20 | result.Publication is { Published: true, Error: ArchiveError.None, CleanupFailed: false }; |
| 21 | |
| 22 | public static void ValidateFolderName(string name) |
| 23 | { |
| 24 | if (string.IsNullOrWhiteSpace(name) || name.Length > 128 || |
| 25 | OutputPathPolicy.Validate(name, OutputPathPolicy.CurrentPolicy).Count != 1) |
| 26 | { |
| 27 | throw new ArchiveException(ArchiveError.UnsafePath); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | public static async Task<ExtractionResult> RunAsync(ZipWorkerClient client, Guid jobId, |
| 32 | Func<CancellationToken, Task<Stream>> openRead, string destination, CancellationToken cancellationToken) |
| 33 | { |
| 34 | if (!ValidationEnabled || !NewFolderPublisher.IsSupportedPlatform || !client.SupportsMemoryExtraction) |
| 35 | { |
| 36 | throw new ArchiveException(ArchiveError.UnsupportedOperation); |
| 37 | } |
| 38 | if (!Path.IsPathFullyQualified(destination)) { throw new ArchiveException(ArchiveError.UnsafePath); } |
| 39 | ValidateFolderName(Path.GetFileName(destination)); |
| 40 | using var deadline = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); |
| 41 | deadline.CancelAfter(TimeSpan.FromMinutes(2)); |
| 42 | var token = deadline.Token; |
| 43 | try |
| 44 | { |
| 45 | token.ThrowIfCancellationRequested(); |
| 46 | using var staging = new MemoryStream(); |
| 47 | await using (var input = await openRead(token).ConfigureAwait(false)) |
| 48 | { |
| 49 | if (!input.CanRead || input.CanWrite) { throw new ArchiveException(ArchiveError.UnsupportedOperation); } |
| 50 | var buffer = new byte[65536]; |
| 51 | int count; |
| 52 | while ((count = await input.ReadAsync(buffer.AsMemory(), token).ConfigureAwait(false)) != 0) |
| 53 | { |
| 54 | if (staging.Length + count > 16L * 1024 * 1024) |
| 55 | { |
| 56 | throw new ArchiveException(ArchiveError.BudgetExceeded); |
| 57 | } |
| 58 | await staging.WriteAsync(buffer.AsMemory(0, count), token).ConfigureAwait(false); |
| 59 | } |
| 60 | } |
| 61 | // 동일한 제한 스냅샷을 읽기 전용으로 작업자에 전달함. |
| 62 | using var snapshot = new MemoryStream(staging.ToArray(), writable: false); |
| 63 | var limits = new JobLimits(1024, 16L * 1024 * 1024, 16L * 1024 * 1024, |
| 64 | TimeSpan.FromMinutes(2), 3); |
| 65 | var result = await client.ExtractAsync(jobId, snapshot, limits, token).ConfigureAwait(false); |
| 66 | if (result.Job.JobId != jobId) { throw new ArchiveException(ArchiveError.EngineFailure); } |
| 67 | if (result.Job.Error == ArchiveError.Cancelled && !cancellationToken.IsCancellationRequested && |
| 68 | deadline.IsCancellationRequested) |
| 69 | { |
| 70 | result = result with { Job = result.Job with { State = JobState.Failed, Error = ArchiveError.BudgetExceeded } }; |
| 71 | } |
| 72 | if (result.Job.State != JobState.Completed || result.Job.Error != ArchiveError.None) |
| 73 | { |
| 74 | return new ExtractionResult(result.Job, null); |
| 75 | } |
| 76 | var publication = await NewFolderPublisher.PublishAsync(result, destination, token).ConfigureAwait(false); |
| 77 | if (!publication.Published && publication.Error == ArchiveError.Cancelled && |
| 78 | !cancellationToken.IsCancellationRequested && deadline.IsCancellationRequested) |
| 79 | { |
| 80 | publication = publication with { Error = ArchiveError.BudgetExceeded }; |
| 81 | } |
| 82 | // 게시 이후에는 늦은 취소보다 실제 게시 결과를 우선함. |
| 83 | return new ExtractionResult(result.Job, publication); |
| 84 | } |
| 85 | catch (OperationCanceledException) when (!cancellationToken.IsCancellationRequested && deadline.IsCancellationRequested) |
| 86 | { |
| 87 | throw new ArchiveException(ArchiveError.BudgetExceeded); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | public static string Describe(ExtractionResult result) |
| 92 | { |
| 93 | if (result.Publication is { CleanupFailed: true }) |
| 94 | { |
| 95 | return "압축 풀기 또는 임시 결과 정리가 완료되지 않았습니다. 선택한 부모 폴더의 임시 항목을 확인하세요. 원본과 기존 대상은 덮어쓰지 않았습니다."; |
| 96 | } |
| 97 | if (Published(result)) { return "검증한 내용을 새 결과 폴더에 게시했습니다. 기존 파일/폴더는 덮어쓰지 않았습니다."; } |
| 98 | var error = result.Publication?.Error ?? result.Job.Error; |
| 99 | return error switch |
| 100 | { |
| 101 | ArchiveError.Cancelled => "압축 풀기가 중지되었습니다. 새 결과 폴더는 게시되지 않았습니다.", |
| 102 | ArchiveError.OutputConflict => "같은 이름의 대상이 있거나 ZIP 내부 이름이 충돌합니다. 기존 대상은 유지됩니다. 다른 새 폴더 이름/위치를 선택하거나 ZIP을 확인하세요.", |
| 103 | ArchiveError.OutputUnavailable => "이 위치에 안전하게 게시할 수 없습니다. 권한과 지원 파일시스템을 확인하고 다른 부모 폴더를 선택하세요.", |
| 104 | ArchiveError.UnsafePath => "안전하지 않은 경로나 링크 조건으로 게시하지 않았습니다. ZIP과 새 저장 위치를 확인하세요.", |
| 105 | ArchiveError.UnsupportedEntry => "링크 또는 지원하지 않는 항목이 있어 게시하지 않았습니다.", |
| 106 | ArchiveError.IntegrityFailure => "CRC/길이 등 무결성 검증에 실패해 게시하지 않았습니다. 정상 결과로 사용하지 마세요.", |
| 107 | ArchiveError.BudgetExceeded => "입력/해제 크기 또는 처리 시간 한도를 초과했습니다. 더 작은 ZIP으로 시도하세요.", |
| 108 | ArchiveError.UnsupportedOperation => "이 ZIP의 암호/압축 방식이나 현재 플랫폼은 개발 검증 해제에서 지원하지 않습니다.", |
| 109 | _ => "새 결과 폴더의 안전한 게시를 확인하지 못했습니다. 기존 목록과 선택을 유지했습니다." |
| 110 | }; |
| 111 | } |
| 112 | } |