| | | 1 | | using System.IO.Compression; |
| | | 2 | | using Elsa.Common; |
| | | 3 | | using Elsa.Http.Options; |
| | | 4 | | using FluentStorage.Blobs; |
| | | 5 | | using Microsoft.Extensions.Logging; |
| | | 6 | | using Microsoft.Extensions.Options; |
| | | 7 | | |
| | | 8 | | namespace Elsa.Http.Services; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Provides a helper service for zipping downloadable content. |
| | | 12 | | /// </summary> |
| | | 13 | | internal class ZipManager |
| | | 14 | | { |
| | | 15 | | private readonly ISystemClock _clock; |
| | | 16 | | private readonly IFileCacheStorageProvider _fileCacheStorageProvider; |
| | | 17 | | private readonly IOptions<HttpFileCacheOptions> _fileCacheOptions; |
| | | 18 | | private readonly ILogger<ZipManager> _logger; |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Initializes a new instance of the <see cref="ZipManager"/> class. |
| | | 22 | | /// </summary> |
| | 0 | 23 | | public ZipManager(ISystemClock clock, IFileCacheStorageProvider fileCacheStorageProvider, IOptions<HttpFileCacheOpti |
| | | 24 | | { |
| | 0 | 25 | | _clock = clock; |
| | 0 | 26 | | _fileCacheStorageProvider = fileCacheStorageProvider; |
| | 0 | 27 | | _fileCacheOptions = fileCacheOptions; |
| | 0 | 28 | | _logger = logger; |
| | 0 | 29 | | } |
| | | 30 | | |
| | | 31 | | public async Task<(Blob, Stream, Action)> CreateAsync( |
| | | 32 | | ICollection<Func<ValueTask<Downloadable>>> downloadables, |
| | | 33 | | bool cache, |
| | | 34 | | string? downloadCorrelationId, |
| | | 35 | | string? downloadAsFilename = default, |
| | | 36 | | string? contentType = default, |
| | | 37 | | CancellationToken cancellationToken = default) |
| | | 38 | | { |
| | | 39 | | // Create a temporary file. |
| | 0 | 40 | | var tempFilePath = GetTempFilePath(); |
| | | 41 | | |
| | | 42 | | // Create a zip archive from the downloadables. |
| | 0 | 43 | | await CreateZipArchiveAsync(tempFilePath, downloadables, cancellationToken); |
| | | 44 | | |
| | | 45 | | // Create a blob with metadata for resuming the download. |
| | 0 | 46 | | var zipBlob = CreateBlob(tempFilePath, downloadAsFilename, contentType); |
| | | 47 | | |
| | | 48 | | // If resumable downloads are enabled, cache the file. |
| | 0 | 49 | | if (cache && !string.IsNullOrWhiteSpace(downloadCorrelationId)) |
| | 0 | 50 | | await CreateCachedZipBlobAsync(tempFilePath, downloadCorrelationId, downloadAsFilename, contentType, cancell |
| | | 51 | | |
| | 0 | 52 | | var zipStream = File.OpenRead(tempFilePath); |
| | 0 | 53 | | return (zipBlob, zipStream, () => Cleanup(tempFilePath)); |
| | 0 | 54 | | } |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Loads a cached zip blob for the specified download correlation ID. |
| | | 58 | | /// </summary> |
| | | 59 | | /// <param name="downloadCorrelationId">The download correlation ID.</param> |
| | | 60 | | /// <param name="cancellationToken">An optional cancellation token.</param> |
| | | 61 | | /// <returns>A tuple containing the blob and the stream.</returns> |
| | | 62 | | public async Task<(Blob, Stream)?> LoadAsync(string downloadCorrelationId, CancellationToken cancellationToken = def |
| | | 63 | | { |
| | 0 | 64 | | var fileCacheStorage = _fileCacheStorageProvider.GetStorage(); |
| | 0 | 65 | | var fileCacheFilename = $"{downloadCorrelationId}.tmp"; |
| | 0 | 66 | | var blob = await fileCacheStorage.GetBlobAsync(fileCacheFilename, cancellationToken); |
| | | 67 | | |
| | 0 | 68 | | if (blob == null) |
| | 0 | 69 | | return null; |
| | | 70 | | |
| | | 71 | | // Check if the blob has expired. |
| | 0 | 72 | | var expiresAt = DateTimeOffset.Parse(blob.Metadata["ExpiresAt"]); |
| | | 73 | | |
| | 0 | 74 | | if (_clock.UtcNow > expiresAt) |
| | | 75 | | { |
| | | 76 | | // File expired. Try to delete it. |
| | | 77 | | try |
| | | 78 | | { |
| | 0 | 79 | | await fileCacheStorage.DeleteAsync(blob.FullPath, cancellationToken); |
| | 0 | 80 | | } |
| | 0 | 81 | | catch (Exception e) |
| | | 82 | | { |
| | 0 | 83 | | _logger.LogWarning(e, "Failed to delete expired file {FullPath}", blob.FullPath); |
| | 0 | 84 | | } |
| | | 85 | | |
| | 0 | 86 | | return null; |
| | | 87 | | } |
| | | 88 | | |
| | 0 | 89 | | var stream = await fileCacheStorage.OpenReadAsync(blob.FullPath, cancellationToken); |
| | 0 | 90 | | return (blob, stream); |
| | 0 | 91 | | } |
| | | 92 | | |
| | | 93 | | /// <summary> |
| | | 94 | | /// Creates a zip archive from the specified <see cref="Downloadable"/> instances. |
| | | 95 | | /// </summary> |
| | | 96 | | private async Task CreateZipArchiveAsync(string filePath, IEnumerable<Func<ValueTask<Downloadable>>> downloadables, |
| | | 97 | | { |
| | 0 | 98 | | var currentFileIndex = 0; |
| | | 99 | | |
| | | 100 | | // Write the zip archive to the temporary file. |
| | 0 | 101 | | await using var tempFileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.Read, buf |
| | | 102 | | |
| | 0 | 103 | | using var zipArchive = new ZipArchive(tempFileStream, ZipArchiveMode.Create, true); |
| | 0 | 104 | | foreach (var downloadableFunc in downloadables) |
| | | 105 | | { |
| | 0 | 106 | | var downloadable = await downloadableFunc(); |
| | 0 | 107 | | var entryName = !string.IsNullOrWhiteSpace(downloadable.Filename) ? downloadable.Filename : $"file-{currentF |
| | 0 | 108 | | var entry = zipArchive.CreateEntry(entryName); |
| | 0 | 109 | | var fileStream = downloadable.Stream; |
| | 0 | 110 | | await using var entryStream = entry.Open(); |
| | 0 | 111 | | await fileStream.CopyToAsync(entryStream, cancellationToken); |
| | 0 | 112 | | await entryStream.FlushAsync(cancellationToken); |
| | 0 | 113 | | entryStream.Close(); |
| | 0 | 114 | | currentFileIndex++; |
| | 0 | 115 | | } |
| | 0 | 116 | | } |
| | | 117 | | |
| | | 118 | | /// <summary> |
| | | 119 | | /// Creates a cached zip blob for the specified file. |
| | | 120 | | /// </summary> |
| | | 121 | | /// <param name="localPath">The full path of the file to upload.</param> |
| | | 122 | | /// <param name="downloadCorrelationId">The download correlation ID.</param> |
| | | 123 | | /// <param name="downloadAsFilename">The filename to use when downloading the file.</param> |
| | | 124 | | /// <param name="contentType">The content type of the file.</param> |
| | | 125 | | /// <param name="cancellationToken">An optional cancellation token.</param> |
| | | 126 | | private async Task CreateCachedZipBlobAsync(string localPath, string downloadCorrelationId, string? downloadAsFilena |
| | | 127 | | { |
| | 0 | 128 | | var fileCacheStorage = _fileCacheStorageProvider.GetStorage(); |
| | 0 | 129 | | var fileCacheFilename = $"{downloadCorrelationId}.tmp"; |
| | 0 | 130 | | var expiresAt = _clock.UtcNow.Add(_fileCacheOptions.Value.TimeToLive); |
| | 0 | 131 | | var cachedBlob = CreateBlob(fileCacheFilename, downloadAsFilename, contentType, expiresAt); |
| | 0 | 132 | | await fileCacheStorage.WriteFileAsync(fileCacheFilename, localPath, cancellationToken); |
| | 0 | 133 | | await fileCacheStorage.SetBlobAsync(cachedBlob, cancellationToken: cancellationToken); |
| | 0 | 134 | | } |
| | | 135 | | |
| | | 136 | | /// <summary> |
| | | 137 | | /// Creates a blob for the specified file. |
| | | 138 | | /// </summary> |
| | | 139 | | /// <param name="fullPath">The full path of the file.</param> |
| | | 140 | | /// <param name="downloadAsFilename">The filename to use when downloading the file.</param> |
| | | 141 | | /// <param name="contentType">The content type of the file.</param> |
| | | 142 | | /// <param name="expiresAt">The date and time at which the file expires.</param> |
| | | 143 | | /// <returns>The blob.</returns> |
| | | 144 | | private Blob CreateBlob(string fullPath, string? downloadAsFilename, string? contentType, DateTimeOffset? expiresAt |
| | | 145 | | { |
| | 0 | 146 | | (downloadAsFilename, contentType) = GetDownloadableMetadata(downloadAsFilename, contentType); |
| | | 147 | | |
| | 0 | 148 | | var now = _clock.UtcNow; |
| | | 149 | | |
| | 0 | 150 | | var blob = new Blob(fullPath) |
| | 0 | 151 | | { |
| | 0 | 152 | | Metadata = |
| | 0 | 153 | | { |
| | 0 | 154 | | ["ContentType"] = contentType, |
| | 0 | 155 | | ["Filename"] = downloadAsFilename |
| | 0 | 156 | | }, |
| | 0 | 157 | | CreatedTime = now, |
| | 0 | 158 | | LastModificationTime = now |
| | 0 | 159 | | }; |
| | | 160 | | |
| | 0 | 161 | | if(expiresAt.HasValue) |
| | 0 | 162 | | blob.Metadata["ExpiresAt"] = expiresAt.Value.ToString("O"); |
| | | 163 | | |
| | 0 | 164 | | return blob; |
| | | 165 | | } |
| | | 166 | | |
| | | 167 | | private (string downloadAsFilename, string contentType) GetDownloadableMetadata(string? contentType, string? downloa |
| | | 168 | | { |
| | 0 | 169 | | contentType = !string.IsNullOrWhiteSpace(contentType) ? contentType : System.Net.Mime.MediaTypeNames.Application |
| | 0 | 170 | | downloadAsFilename = !string.IsNullOrWhiteSpace(downloadAsFilename) ? downloadAsFilename : "download.zip"; |
| | | 171 | | |
| | 0 | 172 | | return (downloadAsFilename, contentType); |
| | | 173 | | } |
| | | 174 | | |
| | | 175 | | private string GetTempFilePath() |
| | | 176 | | { |
| | 0 | 177 | | var tempFileName = Path.GetRandomFileName(); |
| | 0 | 178 | | var tempFilePath = Path.Combine(_fileCacheOptions.Value.LocalCacheDirectory, tempFileName); |
| | 0 | 179 | | return tempFilePath; |
| | | 180 | | } |
| | | 181 | | |
| | | 182 | | private void Cleanup(string filePath) |
| | | 183 | | { |
| | | 184 | | try |
| | | 185 | | { |
| | 0 | 186 | | File.Delete(filePath); |
| | 0 | 187 | | } |
| | 0 | 188 | | catch (Exception e) |
| | | 189 | | { |
| | 0 | 190 | | _logger.LogWarning(e, "Failed to delete temporary file {TempFilePath}", filePath); |
| | 0 | 191 | | } |
| | 0 | 192 | | } |
| | | 193 | | } |