|
| 1 | +using System.IO.Compression; |
| 2 | + |
| 3 | +namespace HotChocolate.Fusion.Packaging; |
| 4 | + |
| 5 | +internal sealed class ArchiveSession : IDisposable |
| 6 | +{ |
| 7 | + private readonly Dictionary<string, FileEntry> _files = []; |
| 8 | + private readonly ZipArchive _archive; |
| 9 | + private FusionArchiveMode _mode; |
| 10 | + private bool _disposed; |
| 11 | + |
| 12 | + public ArchiveSession(ZipArchive archive, FusionArchiveMode mode) |
| 13 | + { |
| 14 | + ArgumentNullException.ThrowIfNull(archive); |
| 15 | + |
| 16 | + _archive = archive; |
| 17 | + _mode = mode; |
| 18 | + } |
| 19 | + |
| 20 | + public bool HasUncommittedChanges |
| 21 | + => _files.Values.Any(file => file.State is not FileState.Read); |
| 22 | + |
| 23 | + public IEnumerable<string> GetFiles() |
| 24 | + { |
| 25 | + var tempFiles = _files.Where(file => file.Value.State is not FileState.Deleted).Select(file => file.Key); |
| 26 | + |
| 27 | + if (_mode is FusionArchiveMode.Create) |
| 28 | + { |
| 29 | + return tempFiles; |
| 30 | + } |
| 31 | + |
| 32 | + var files = new HashSet<string>(tempFiles); |
| 33 | + |
| 34 | + foreach (var entry in _archive.Entries) |
| 35 | + { |
| 36 | + files.Add(entry.FullName); |
| 37 | + } |
| 38 | + |
| 39 | + return files; |
| 40 | + } |
| 41 | + |
| 42 | + public async Task<bool> ExistsAsync(string path, CancellationToken cancellationToken) |
| 43 | + { |
| 44 | + if (_files.TryGetValue(path, out var file)) |
| 45 | + { |
| 46 | + return file.State is not FileState.Deleted; |
| 47 | + } |
| 48 | + |
| 49 | + if (_mode is not FusionArchiveMode.Create && _archive.GetEntry(path) is { } entry) |
| 50 | + { |
| 51 | + file = FileEntry.Read(path); |
| 52 | +#if NET10_0_OR_GREATER |
| 53 | + await entry.ExtractToFileAsync(file.TempPath, cancellationToken); |
| 54 | +#else |
| 55 | + entry.ExtractToFile(file.TempPath); |
| 56 | + await Task.CompletedTask; |
| 57 | +#endif |
| 58 | + _files.Add(path, file); |
| 59 | + return true; |
| 60 | + } |
| 61 | + |
| 62 | + return false; |
| 63 | + } |
| 64 | + |
| 65 | + public bool Exists(string path) |
| 66 | + { |
| 67 | + if (_files.TryGetValue(path, out var file)) |
| 68 | + { |
| 69 | + return file.State is not FileState.Deleted; |
| 70 | + } |
| 71 | + |
| 72 | + return _mode is not FusionArchiveMode.Create && _archive.GetEntry(path) is not null; |
| 73 | + } |
| 74 | + |
| 75 | + public async Task<Stream> OpenReadAsync(string path, CancellationToken cancellationToken) |
| 76 | + { |
| 77 | + if (_files.TryGetValue(path, out var file)) |
| 78 | + { |
| 79 | + if (file.State is FileState.Deleted) |
| 80 | + { |
| 81 | + throw new FileNotFoundException(path); |
| 82 | + } |
| 83 | + |
| 84 | + return File.OpenRead(file.TempPath); |
| 85 | + } |
| 86 | + |
| 87 | + if (_mode is not FusionArchiveMode.Create && _archive.GetEntry(path) is { } entry) |
| 88 | + { |
| 89 | + file = FileEntry.Read(path); |
| 90 | +#if NET10_0_OR_GREATER |
| 91 | + await entry.ExtractToFileAsync(file.TempPath, cancellationToken); |
| 92 | +#else |
| 93 | + entry.ExtractToFile(file.TempPath); |
| 94 | + await Task.CompletedTask; |
| 95 | +#endif |
| 96 | + var stream = File.OpenRead(file.TempPath); |
| 97 | + _files.Add(path, file); |
| 98 | + return stream; |
| 99 | + } |
| 100 | + |
| 101 | + throw new FileNotFoundException(path); |
| 102 | + } |
| 103 | + |
| 104 | + public Stream OpenWrite(string path) |
| 105 | + { |
| 106 | + if (_mode is FusionArchiveMode.Read) |
| 107 | + { |
| 108 | + throw new InvalidOperationException("Cannot write to a read-only archive."); |
| 109 | + } |
| 110 | + |
| 111 | + if (_files.TryGetValue(path, out var file)) |
| 112 | + { |
| 113 | + file.MarkMutated(); |
| 114 | + return File.Open(file.TempPath, FileMode.Create, FileAccess.Write); |
| 115 | + } |
| 116 | + |
| 117 | + if (_mode is not FusionArchiveMode.Create && _archive.GetEntry(path) is not null) |
| 118 | + { |
| 119 | + file = FileEntry.Read(path); |
| 120 | + file.MarkMutated(); |
| 121 | + } |
| 122 | + |
| 123 | + file ??= FileEntry.Created(path); |
| 124 | + var stream = File.Open(file.TempPath, FileMode.Create, FileAccess.Write); |
| 125 | + _files.Add(path, file); |
| 126 | + return stream; |
| 127 | + } |
| 128 | + |
| 129 | + public void SetMode(FusionArchiveMode mode) |
| 130 | + { |
| 131 | + _mode = mode; |
| 132 | + } |
| 133 | + |
| 134 | + public async Task CommitAsync(CancellationToken cancellationToken) |
| 135 | + { |
| 136 | + foreach (var file in _files.Values) |
| 137 | + { |
| 138 | +#if NET10_0_OR_GREATER |
| 139 | + switch (file.State) |
| 140 | + { |
| 141 | + case FileState.Created: |
| 142 | + await _archive.CreateEntryFromFileAsync( |
| 143 | + file.TempPath, |
| 144 | + file.Path, |
| 145 | + cancellationToken: cancellationToken); |
| 146 | + break; |
| 147 | + |
| 148 | + case FileState.Replaced: |
| 149 | + _archive.GetEntry(file.Path)?.Delete(); |
| 150 | + await _archive.CreateEntryFromFileAsync( |
| 151 | + file.TempPath, |
| 152 | + file.Path, |
| 153 | + cancellationToken); |
| 154 | + break; |
| 155 | + |
| 156 | + case FileState.Deleted: |
| 157 | + _archive.GetEntry(file.Path)?.Delete(); |
| 158 | + break; |
| 159 | + } |
| 160 | +#else |
| 161 | + switch (file.State) |
| 162 | + { |
| 163 | + case FileState.Created: |
| 164 | + _archive.CreateEntryFromFile(file.TempPath, file.Path); |
| 165 | + break; |
| 166 | + |
| 167 | + case FileState.Replaced: |
| 168 | + _archive.GetEntry(file.Path)?.Delete(); |
| 169 | + _archive.CreateEntryFromFile(file.TempPath, file.Path); |
| 170 | + break; |
| 171 | + |
| 172 | + case FileState.Deleted: |
| 173 | + _archive.GetEntry(file.Path)?.Delete(); |
| 174 | + break; |
| 175 | + } |
| 176 | + |
| 177 | + await Task.CompletedTask; |
| 178 | +#endif |
| 179 | + |
| 180 | + file.MarkRead(); |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + public void Dispose() |
| 185 | + { |
| 186 | + if (_disposed) |
| 187 | + { |
| 188 | + return; |
| 189 | + } |
| 190 | + |
| 191 | + _disposed = true; |
| 192 | + foreach (var file in _files.Values) |
| 193 | + { |
| 194 | + if (file.State is not FileState.Deleted) |
| 195 | + { |
| 196 | + File.Delete(file.TempPath); |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + private class FileEntry |
| 202 | + { |
| 203 | + private FileEntry(string path, string tempPath, FileState state) |
| 204 | + { |
| 205 | + Path = path; |
| 206 | + TempPath = tempPath; |
| 207 | + State = state; |
| 208 | + } |
| 209 | + |
| 210 | + public string Path { get; } |
| 211 | + |
| 212 | + public string TempPath { get; } |
| 213 | + |
| 214 | + public FileState State { get; private set; } |
| 215 | + |
| 216 | + public void MarkMutated() |
| 217 | + { |
| 218 | + if (State is FileState.Read or FileState.Deleted) |
| 219 | + { |
| 220 | + State = FileState.Replaced; |
| 221 | + } |
| 222 | + } |
| 223 | + |
| 224 | + public void MarkRead() |
| 225 | + { |
| 226 | + State = FileState.Read; |
| 227 | + } |
| 228 | + |
| 229 | + public static FileEntry Created(string path) |
| 230 | + => new(path, GetRandomTempFileName(), FileState.Created); |
| 231 | + |
| 232 | + public static FileEntry Read(string path) |
| 233 | + => new(path, GetRandomTempFileName(), FileState.Read); |
| 234 | + |
| 235 | + private static string GetRandomTempFileName() |
| 236 | + { |
| 237 | + var tempDir = System.IO.Path.GetTempPath(); |
| 238 | + var fileName = System.IO.Path.GetRandomFileName(); |
| 239 | + return System.IO.Path.Combine(tempDir, fileName); |
| 240 | + } |
| 241 | + } |
| 242 | + |
| 243 | + private enum FileState |
| 244 | + { |
| 245 | + Read, |
| 246 | + Created, |
| 247 | + Replaced, |
| 248 | + Deleted |
| 249 | + } |
| 250 | +} |
0 commit comments