private async Task <bool> TryStreamCachedAllFilesZip( Release release, Stream outputStream, CancellationToken?cancellationToken = null) { var path = release.AllFilesZipPath(); var allFilesZip = await _blobStorageService.FindBlob(PublicReleaseFiles, path); // Ideally, we would have some way to do this caching via annotations, // but this a chunk of work to get working properly as piping // the cached file to target stream isn't super trivial. // For now, we'll just do this manually as it's way easier. if (allFilesZip?.Updated is not null && allFilesZip.Updated.Value.AddSeconds(AllFilesZipTtl) >= DateTime.UtcNow) { await _blobStorageService.DownloadToStream( containerName : PublicReleaseFiles, path : path, stream : outputStream, cancellationToken : cancellationToken ); await outputStream.DisposeAsync(); return(true); } return(false); }
private async Task ZipAllFilesToStream( Release release, Stream outputStream, CancellationToken?cancellationToken = null) { var releaseFiles = (await QueryReleaseFiles(release.Id).ToListAsync()) .OrderBy(rf => rf.File.ZipFileEntryName()) .ToList(); var path = Path.GetTempPath() + release.AllFilesZipFileName(); var fileStream = File.Open(path, FileMode.OpenOrCreate, FileAccess.ReadWrite); await using var multiWriteStream = new MultiWriteStream(outputStream, fileStream); await DoZipFilesToStream(releaseFiles, release, multiWriteStream, cancellationToken); await multiWriteStream.FlushAsync(); // Now cache the All files zip into blob storage // so that we can quickly fetch it again. fileStream.Position = 0; await _blobStorageService.UploadStream( containerName : PublicReleaseFiles, path : release.AllFilesZipPath(), stream : fileStream, contentType : MediaTypeNames.Application.Zip ); await fileStream.DisposeAsync(); File.Delete(path); }
public void AllFilesZipPath() { const string releaseSlug = "release-slug"; const string publicationSlug = "publication-slug"; var release = new Release { Id = Guid.NewGuid(), Slug = releaseSlug, Publication = new Publication { Slug = publicationSlug } }; Assert.Equal($"{release.Id}/zip/{publicationSlug}_{releaseSlug}.zip", release.AllFilesZipPath()); }