public void Remove(StorageFileId storageFileId) { if (_localStore.ContainsKey(GetKey(storageFileId))) { _localStore.Remove(GetKey(storageFileId)); } }
public StorageData Get(StorageFileId storageFileId) { var pdfBytes = new MemoryStream(); _storageClient.DownloadObject(_settings.GoogleBucketName, GetObjectName(storageFileId), pdfBytes, null, null); return(new StorageData(storageFileId, pdfBytes.ToArray())); }
public void Remove(StorageFileId storageFileId) { var deleteObjectRequest = new DeleteObjectRequest { BucketName = _bucketName, Key = GetKey(storageFileId) }; _s3Client.DeleteObjectAsync(deleteObjectRequest).Wait(); }
public ActionResult <MergeResponse> MergePdfs(string groupId, [Required][FromBody] PdfMergeRequest request) { if (request.PdfIds.Length < 1) { return(BadRequest("Atleast one pdf must be defined, current length 0")); } var underlayingPdfFiles = _context.PdfFiles .Where(x => x.GroupId == groupId && !x.Removed) .Where(x => request.PdfIds.Any(id => x.FileId == id)) .ToList(); var missingPdfFiles = request.PdfIds.Where(x => !underlayingPdfFiles.Any(file => x == file.FileId)); if (missingPdfFiles.Any()) { var message = $"Pdf files not found, missing files from group '{groupId}' are '{missingPdfFiles.Aggregate("", (a, b) => $"{a}, {b}").Trim(',')}'"; _logger.LogWarning($"Requested merge but it failed: {message}"); return(BadRequest(message)); } var mergeEntity = _context.PdfFiles.Add(new PdfEntity(groupId, PdfType.Merge)).Entity; var filePath = $"{_settings.BaseUrl}/v1/pdf/{groupId}/{mergeEntity.FileId}.pdf"; request.PdfIds.ToList().ForEach(id => { _mqMessages.PdfOpened(groupId, id); underlayingPdfFiles.Single(x => x.FileId == id).Usage.Add(new PdfOpenedEntity()); }); var entitiesToPriritize = underlayingPdfFiles .Where(x => !x.Processed) .Where(x => x.IsValidForHighPriority()) .ToList(); entitiesToPriritize.ForEach(pdfEntity => { pdfEntity.MarkAsHighPriority( _backgroundJob.EnqueueWithHighPriority <IPdfQueue>(que => que.CreatePdf(pdfEntity.Id), originalJobId: pdfEntity.HangfireJobId)); }); _context.SaveChanges(); var storageFile = new StorageFileId(mergeEntity, "pdf"); mergeEntity.HangfireJobId = _backgroundJob.EnqueueWithHighPriority <IPdfMerger>(merger => merger.MergePdf(storageFile, request.PdfIds)); _context.SaveChanges(); return(Accepted(new MergeResponse(mergeEntity.FileId, filePath))); }
public StorageData(StorageFileId storageFileId, byte[] data) { Data = data; StorageFileId = storageFileId; ContentType = storageFileId.Extension switch { "html" => "text/html", "pdf" => "application/pdf", _ => "text/plain", }; }
public StorageData Get(StorageFileId storageFileId) { var request = new GetObjectRequest { BucketName = _bucketName, Key = GetKey(storageFileId) }; using var response = _s3Client.GetObjectAsync(request).Result; using var responseStream = response.ResponseStream; using var memstream = new MemoryStream(); response.ResponseStream.CopyTo(memstream); return(new StorageData(storageFileId, memstream.ToArray())); }
public void MergePdf(StorageFileId storageIdForMergedPdf, string[] pdfIds) { Debug.Assert(storageIdForMergedPdf.Extension == "pdf"); var temp = ResolveTemporaryDirectory(); _logger.LogInformation($"Using temporary folder: {temp}"); try { var mergedFile = _context.PdfFiles.Single(x => x.GroupId == storageIdForMergedPdf.Group && x.FileId == storageIdForMergedPdf.Id); var pdfEntities = _context.PdfFiles .Where(x => x.GroupId == storageIdForMergedPdf.Group) .Where(x => pdfIds.Any(id => x.FileId == id)) .ToList(); if(pdfEntities.Any(x => !x.Processed)) throw new InvalidOperationException( $"Tried to merge files that are not ready, non ready pdf list is '{pdfEntities.Where(x => !x.Processed).Select(x => x.FileId).Aggregate("", (a, b) => $"{a}, {b}").Trim(',')}'"); var pdfs = pdfIds .Select(id => _pdfStorage.Get(new StorageFileId(storageIdForMergedPdf.Group, id, "pdf"))) .Select(pdf => (tempFile: Path.Combine($@"{temp}", $"{pdf.StorageFileId.Id}.pdf"), data: pdf.Data)).ToList(); pdfs.ForEach(x => File.WriteAllBytes(x.tempFile, x.data)); var mergedPdf = MergeFiles(temp, pdfs.Select(x => x.tempFile)); _pdfStorage.AddOrReplace(new StorageData(storageIdForMergedPdf, mergedPdf)); mergedFile.Processed = true; _context.SaveChanges(); } finally { _logger.LogInformation($"Removing temporary folder: {temp}"); Directory.Delete(temp, true); } }
private static string GetObjectName(StorageFileId storageFileId) { return($"{storageFileId.Group}_{storageFileId.Id}.{storageFileId.Extension}"); }
public void Remove(StorageFileId storageFileId) { _storageClient.DeleteObject(_settings.GoogleBucketName, GetObjectName(storageFileId)); }
public StorageData Get(StorageFileId storageFileId) { return(_localStore[GetKey(storageFileId)]); }
private string GetKey(StorageFileId storageFileId) { return($"{storageFileId.Group}_{storageFileId.Id}_{storageFileId.Extension}"); }