private static PatientFilesGroupViewModel GetViewModel(IBlobStorageManager storage, PatientFileGroup dbFileGroup, int dbUserId, Func <DateTime, DateTime> toLocal) { if (dbFileGroup == null) { return(new PatientFilesGroupViewModel()); } var result = new PatientFilesGroupViewModel { Id = dbFileGroup.Id, PatientId = dbFileGroup.PatientId, CreatedOn = dbFileGroup.CreatedOn, Title = dbFileGroup.GroupTitle, Notes = dbFileGroup.GroupNotes, FileGroupDate = toLocal(dbFileGroup.FileGroupDate), ReceiveDate = toLocal(dbFileGroup.ReceiveDate), }; result.Files.AddRange(dbFileGroup.PatientFiles.Select(dbFile => new PatientFileViewModel { Id = dbFile.Id, FileTitle = dbFile.Title, ContainerName = dbFile.FileMetadata.ContainerName, SourceFileName = dbFile.FileMetadata.SourceFileName, BlobName = dbFile.FileMetadata.BlobName, ExpirationDate = dbFile.FileMetadata.ExpirationDate, MetadataId = dbFile.FileMetadataId, })); FillFileLengths(storage, result, dbUserId); return(result); }
/// <summary> /// Creates an action result with a thumbnail image of a file in the storage. /// </summary> /// <param name="originalMetadata">Metadata of the original image file.</param> /// <param name="storage">The storage service used to store files.</param> /// <param name="dateTimeService">Date time service used to get current date and time.</param> /// <param name="maxWidth">Maximum width of the thumbnail image.</param> /// <param name="maxHeight">Maximum height of the thumbnail image.</param> /// <param name="useCache">Whether to use a cached thumbnail or not.</param> /// <returns>The ActionResult containing the thumbnail image.</returns> protected ActionResult GetOrCreateThumb( FileMetadata originalMetadata, IBlobStorageManager storage, IDateTimeService dateTimeService, int maxWidth, int maxHeight, bool useCache = true) { if (originalMetadata == null) { throw new ArgumentNullException("originalMetadata"); } if (originalMetadata.OwnerUserId == this.DbUser.Id) { var fileNamePrefix = Path.GetDirectoryName(originalMetadata.BlobName) + "\\"; var normalFileName = StringHelper.NormalizeFileName(originalMetadata.SourceFileName); var thumbName = string.Format( "{0}\\{1}file-{2}-thumb-{4}x{5}-{3}", originalMetadata.ContainerName, fileNamePrefix, originalMetadata.Id, normalFileName, maxWidth, maxHeight); var fileName = string.Format("{0}\\{1}", originalMetadata.ContainerName, originalMetadata.BlobName); int originalMetadataId = originalMetadata.Id; var metadataProvider = new DbFileMetadataProvider(this.db, dateTimeService, this.DbUser.PracticeId); var thumbResult = ImageHelper.TryGetOrCreateThumb( originalMetadataId, maxWidth, maxHeight, fileName, thumbName, useCache, storage, metadataProvider); switch (thumbResult.Status) { case CreateThumbStatus.Ok: return(this.File(thumbResult.Data, thumbResult.ContentType)); case CreateThumbStatus.SourceFileNotFound: return(new StatusCodeResult(HttpStatusCode.NotFound)); case CreateThumbStatus.SourceIsNotImage: return(this.Redirect(this.Url.Content("~/Content/Images/App/FileIcons/generic-outline.png"))); case CreateThumbStatus.SourceImageTooLarge: return(this.Redirect(this.Url.Content("~/Content/Images/App/FileIcons/generic-outline.png"))); default: throw new NotImplementedException(); } } return(new StatusCodeResult(HttpStatusCode.NotFound)); }
public static void DeleteFileByMetadata(FileMetadata metadata, CerebelloEntitiesAccessFilterWrapper db, IBlobStorageManager storage) { // deleting dependent files var relatedFiles = db.FileMetadatas.Where(f => f.RelatedFileMetadataId == metadata.Id).ToList(); foreach (var relatedFile in relatedFiles) { DeleteFileByMetadata(relatedFile, db, storage); } // deleting file metadata and storage entries storage.DeleteFileFromStorage(metadata.ContainerName, metadata.BlobName); db.FileMetadatas.DeleteObject(metadata); }
private static void FillFileLengths(IBlobStorageManager storage, PatientFilesGroupViewModel viewModel, int?dbUserId) { // reading file sizes from the storage // todo: use db to get file size (faster) foreach (var eachFile in viewModel.Files) { var fullStoragePath = string.Format("{0}\\{1}", eachFile.ContainerName, eachFile.BlobName); var mustStartWith = string.Format("patient-files-{0}\\", dbUserId); if (!fullStoragePath.StartsWith(mustStartWith)) { continue; } eachFile.FileLength = storage.GetFileLength(eachFile.ContainerName, eachFile.BlobName); } }
public override void HandleExecute(Common.workflow.ChainRequest request) { myRequest = (ButlerProcessRequest)request; IBlobStorageManager resource = BlobManagerFactory.CreateBlobManager(myRequest.ProcessConfigConn); foreach (string url in myRequest.ButlerRequest.MezzanineFiles) { resource.DeleteBlobFile(url); Trace.TraceInformation("{0} in process {1} processId {2} file {3} deleted", this.GetType().FullName, myRequest.ProcessTypeId, myRequest.ProcessInstanceId, url); } if (!string.IsNullOrEmpty(myRequest.ButlerRequest.ControlFileUri)) { resource.DeleteBlobFile(myRequest.ButlerRequest.ControlFileUri); Trace.TraceInformation("{0} in process {1} processId {2} file {3} deleted", this.GetType().FullName, myRequest.ProcessTypeId, myRequest.ProcessInstanceId, myRequest.ButlerRequest.ControlFileUri); } }
private void Setup() { IBlobStorageManager resource = BlobManagerFactory.CreateBlobManager(myRequest.ProcessConfigConn); string jsonControl = resource.ReadTextBlob(myRequest.ButlerRequest.ControlFileUri); myConfig = null; try { if (!string.IsNullOrEmpty(jsonControl)) { //instance process configuration myConfig = Newtonsoft.Json.JsonConvert.DeserializeObject <PremiunConfig>(jsonControl); } else { if (!string.IsNullOrEmpty(this.StepConfiguration)) { //general process configuration myConfig = Newtonsoft.Json.JsonConvert.DeserializeObject <PremiunConfig>(this.StepConfiguration); } } } catch (Exception) { Trace.TraceWarning("PremiumEncodingStep configuration Error, it will use default"); } if (myConfig == null) { //default configuration myConfig = new PremiunConfig() { AssetWorkflowID = null, EncodingJobName = "Media Bulter Premium Workflow encoding job", EncodigTaskName = "Media Bulter Premium Workflow encoding task" }; } }
/// <summary> /// Uploads a file to the Windows Azure Storage. /// </summary> /// <param name="stream">Stream containing the data to be saved into the file.</param> /// <param name="this">Blob storage manager to use.</param> /// <param name="location">Location of the blob to upload to.</param> public static void UploadFileToStorage(this IBlobStorageManager @this, [NotNull] Stream stream, BlobLocation location) { @this.UploadFileToStorage(stream, location.ContainerName, location.BlobName); }
/// <summary> /// Copies a file stored in Windows Azure Storage to another blob. /// </summary> /// <param name="this">Blob storage manager to use.</param> /// <param name="sourceLocation"> Source location of the file to copy data from. </param> /// <param name="destinationLocation"> Destination location to copy the file data to. </param> public static void CopyStoredFile(this IBlobStorageManager @this, BlobLocation sourceLocation, BlobLocation destinationLocation) { @this.CopyStoredFile(sourceLocation.ContainerName, sourceLocation.BlobName, destinationLocation.ContainerName, destinationLocation.BlobName); }
/// <summary> /// Deletes a file from the Window Azure Storage. /// </summary> /// <param name="this">Blob storage manager to use.</param> /// <param name="location">Location of the blob to delete.</param> public static void DeleteFileFromStorage(this IBlobStorageManager @this, BlobLocation location) { @this.DeleteFileFromStorage(location.ContainerName, location.BlobName); }
/// <summary> /// Downloads a file from the Windows Azure Storage. /// </summary> /// <param name="this">Blob storage manager to use.</param> /// <param name="location">Location of the blob to download.</param> /// <returns>Returns a valid stream that can be used to read file data, or null if the file does not exist.</returns> public static Stream DownloadFileFromStorage(this IBlobStorageManager @this, BlobLocation location) { return(@this.DownloadFileFromStorage(location.ContainerName, location.BlobName)); }
public AnalyzerController(IBlobStorageManager blobStorageManager, IImageAnalyzer imageAnalyzer) { _blobStorageManager = blobStorageManager ?? throw new ArgumentNullException(nameof(blobStorageManager)); _imageAnalyzer = imageAnalyzer ?? throw new ArgumentNullException(nameof(imageAnalyzer)); }
/// <summary> /// Gets the length of a file if it exists. /// Null if the file does not exist. /// </summary> /// <param name="this">Blob storage manager to use.</param> /// <param name="location">Location of the blob to get the length of.</param> /// <returns>Returns the length of the file if it exists, or null if it does not exist.</returns> public static long?GetFileLength(this IBlobStorageManager @this, BlobLocation location) { return(@this.GetFileLength(location.ContainerName, location.BlobName)); }
/// <summary> /// Creates a thumbnail image of a file in the storage. /// </summary> /// <param name="originalMetadataId">Metadata entry ID for the original image file.</param> /// <param name="maxWidth">Maximum width of the thumbnail image.</param> /// <param name="maxHeight">Maximum height of the thumbnail image.</param> /// <param name="sourceFullStorageFileName">Name of the source image file.</param> /// <param name="thumbFullStorageFileName">Name of the thumbnail image cache file.</param> /// <param name="loadFromCache">Whether to use a cached thumbnail or not.</param> /// <param name="storage">Storage service used to get file data.</param> /// <param name="fileMetadataProvider">File metadata provider used to create thumbnail image metadata.</param> /// <returns>Returns the result of the thumbnail creation process.</returns> public static CreateThumbResult TryGetOrCreateThumb( int originalMetadataId, int maxWidth, int maxHeight, string sourceFullStorageFileName, string thumbFullStorageFileName, bool loadFromCache, IBlobStorageManager storage, IFileMetadataProvider fileMetadataProvider) { var srcBlobLocation = new BlobLocation(sourceFullStorageFileName); var thumbBlobLocation = new BlobLocation(thumbFullStorageFileName); if (loadFromCache && !string.IsNullOrEmpty(thumbFullStorageFileName)) { var thumbStream = storage.DownloadFileFromStorage(thumbBlobLocation); if (thumbStream != null) { using (thumbStream) using (var stream = new MemoryStream((int)thumbStream.Length)) { thumbStream.CopyTo(stream); { return new CreateThumbResult( CreateThumbStatus.Ok, stream.ToArray(), MimeTypesHelper.GetContentType(Path.GetExtension(thumbFullStorageFileName))); } } } } if (!StringHelper.IsImageFileName(sourceFullStorageFileName)) return new CreateThumbResult(CreateThumbStatus.SourceIsNotImage, null, null); var srcStream = storage.DownloadFileFromStorage(srcBlobLocation); if (srcStream == null) return new CreateThumbResult(CreateThumbStatus.SourceFileNotFound, null, null); string contentType; byte[] array; using (srcStream) using (var srcImage = Image.FromStream(srcStream)) { var imageSizeMegabytes = srcImage.Width * srcImage.Height * 4 / 1024000.0; if (imageSizeMegabytes > 40.0) return new CreateThumbResult(CreateThumbStatus.SourceImageTooLarge, null, null); using (var newImage = ResizeImage(srcImage, maxWidth, maxHeight, keepAspect: true, canGrow: false)) using (var newStream = new MemoryStream()) { if (newImage == null) { srcStream.Position = 0; srcStream.CopyTo(newStream); contentType = MimeTypesHelper.GetContentType(Path.GetExtension(sourceFullStorageFileName)); } else { var imageFormat = (newImage.Width * newImage.Height > 10000) ? ImageFormat.Jpeg : ImageFormat.Png; contentType = (newImage.Width * newImage.Height > 10000) ? "image/jpeg" : "image/png"; newImage.Save(newStream, imageFormat); } array = newStream.ToArray(); if (loadFromCache && newImage != null && !string.IsNullOrEmpty(thumbFullStorageFileName)) { // saving thumbnail image file metadata var relationType = string.Format("thumb-{0}x{1}", maxWidth, maxHeight); var metadata = fileMetadataProvider.CreateRelated( originalMetadataId, relationType, thumbBlobLocation.ContainerName, thumbBlobLocation.FileName, thumbBlobLocation.BlobName, null); fileMetadataProvider.SaveChanges(); storage.UploadFileToStorage(new MemoryStream(array), thumbBlobLocation); } } } return new CreateThumbResult(CreateThumbStatus.Ok, array, contentType); }
/// <summary> /// Creates an action result with a thumbnail image of a file in the storage. /// </summary> /// <param name="originalMetadata">Metadata of the original image file.</param> /// <param name="storage">The storage service used to store files.</param> /// <param name="dateTimeService">Date time service used to get current date and time.</param> /// <param name="maxWidth">Maximum width of the thumbnail image.</param> /// <param name="maxHeight">Maximum height of the thumbnail image.</param> /// <param name="useCache">Whether to use a cached thumbnail or not.</param> /// <returns>The ActionResult containing the thumbnail image.</returns> protected ActionResult GetOrCreateThumb( FileMetadata originalMetadata, IBlobStorageManager storage, IDateTimeService dateTimeService, int maxWidth, int maxHeight, bool useCache = true) { if (originalMetadata == null) throw new ArgumentNullException("originalMetadata"); if (originalMetadata.OwnerUserId == this.DbUser.Id) { var fileNamePrefix = Path.GetDirectoryName(originalMetadata.BlobName) + "\\"; var normalFileName = StringHelper.NormalizeFileName(originalMetadata.SourceFileName); var thumbName = string.Format( "{0}\\{1}file-{2}-thumb-{4}x{5}-{3}", originalMetadata.ContainerName, fileNamePrefix, originalMetadata.Id, normalFileName, maxWidth, maxHeight); var fileName = string.Format("{0}\\{1}", originalMetadata.ContainerName, originalMetadata.BlobName); int originalMetadataId = originalMetadata.Id; var metadataProvider = new DbFileMetadataProvider(this.db, dateTimeService, this.DbUser.PracticeId); var thumbResult = ImageHelper.TryGetOrCreateThumb( originalMetadataId, maxWidth, maxHeight, fileName, thumbName, useCache, storage, metadataProvider); switch (thumbResult.Status) { case CreateThumbStatus.Ok: return this.File(thumbResult.Data, thumbResult.ContentType); case CreateThumbStatus.SourceFileNotFound: return new StatusCodeResult(HttpStatusCode.NotFound); case CreateThumbStatus.SourceIsNotImage: return this.Redirect(this.Url.Content("~/Content/Images/App/FileIcons/generic-outline.png")); case CreateThumbStatus.SourceImageTooLarge: return this.Redirect(this.Url.Content("~/Content/Images/App/FileIcons/generic-outline.png")); default: throw new NotImplementedException(); } } return new StatusCodeResult(HttpStatusCode.NotFound); }
private static PatientFilesGroupViewModel GetViewModel(IBlobStorageManager storage, PatientFileGroup dbFileGroup, int dbUserId, Func<DateTime, DateTime> toLocal) { if (dbFileGroup == null) return new PatientFilesGroupViewModel(); var result = new PatientFilesGroupViewModel { Id = dbFileGroup.Id, PatientId = dbFileGroup.PatientId, CreatedOn = dbFileGroup.CreatedOn, Title = dbFileGroup.GroupTitle, Notes = dbFileGroup.GroupNotes, FileGroupDate = toLocal(dbFileGroup.FileGroupDate), ReceiveDate = toLocal(dbFileGroup.ReceiveDate), }; result.Files.AddRange(dbFileGroup.PatientFiles.Select(dbFile => new PatientFileViewModel { Id = dbFile.Id, FileTitle = dbFile.Title, ContainerName = dbFile.FileMetadata.ContainerName, SourceFileName = dbFile.FileMetadata.SourceFileName, BlobName = dbFile.FileMetadata.BlobName, ExpirationDate = dbFile.FileMetadata.ExpirationDate, MetadataId = dbFile.FileMetadataId, })); FillFileLengths(storage, result, dbUserId); return result; }
public PatientFilesController(IBlobStorageManager storage, IDateTimeService datetimeService) { this.storage = storage; this.datetimeService = datetimeService; }
/// <summary> /// Creates a thumbnail image of a file in the storage. /// </summary> /// <param name="originalMetadataId">Metadata entry ID for the original image file.</param> /// <param name="maxWidth">Maximum width of the thumbnail image.</param> /// <param name="maxHeight">Maximum height of the thumbnail image.</param> /// <param name="sourceFullStorageFileName">Name of the source image file.</param> /// <param name="thumbFullStorageFileName">Name of the thumbnail image cache file.</param> /// <param name="loadFromCache">Whether to use a cached thumbnail or not.</param> /// <param name="storage">Storage service used to get file data.</param> /// <param name="fileMetadataProvider">File metadata provider used to create thumbnail image metadata.</param> /// <returns>Returns the result of the thumbnail creation process.</returns> public static CreateThumbResult TryGetOrCreateThumb( int originalMetadataId, int maxWidth, int maxHeight, string sourceFullStorageFileName, string thumbFullStorageFileName, bool loadFromCache, IBlobStorageManager storage, IFileMetadataProvider fileMetadataProvider) { var srcBlobLocation = new BlobLocation(sourceFullStorageFileName); var thumbBlobLocation = new BlobLocation(thumbFullStorageFileName); if (loadFromCache && !string.IsNullOrEmpty(thumbFullStorageFileName)) { var thumbStream = storage.DownloadFileFromStorage(thumbBlobLocation); if (thumbStream != null) { using (thumbStream) using (var stream = new MemoryStream((int)thumbStream.Length)) { thumbStream.CopyTo(stream); { return(new CreateThumbResult( CreateThumbStatus.Ok, stream.ToArray(), MimeTypesHelper.GetContentType(Path.GetExtension(thumbFullStorageFileName)))); } } } } if (!StringHelper.IsImageFileName(sourceFullStorageFileName)) { return(new CreateThumbResult(CreateThumbStatus.SourceIsNotImage, null, null)); } var srcStream = storage.DownloadFileFromStorage(srcBlobLocation); if (srcStream == null) { return(new CreateThumbResult(CreateThumbStatus.SourceFileNotFound, null, null)); } string contentType; byte[] array; using (srcStream) using (var srcImage = Image.FromStream(srcStream)) { var imageSizeMegabytes = srcImage.Width * srcImage.Height * 4 / 1024000.0; if (imageSizeMegabytes > 40.0) { return(new CreateThumbResult(CreateThumbStatus.SourceImageTooLarge, null, null)); } using (var newImage = ResizeImage(srcImage, maxWidth, maxHeight, keepAspect: true, canGrow: false)) using (var newStream = new MemoryStream()) { if (newImage == null) { srcStream.Position = 0; srcStream.CopyTo(newStream); contentType = MimeTypesHelper.GetContentType(Path.GetExtension(sourceFullStorageFileName)); } else { var imageFormat = (newImage.Width * newImage.Height > 10000) ? ImageFormat.Jpeg : ImageFormat.Png; contentType = (newImage.Width * newImage.Height > 10000) ? "image/jpeg" : "image/png"; newImage.Save(newStream, imageFormat); } array = newStream.ToArray(); if (loadFromCache && newImage != null && !string.IsNullOrEmpty(thumbFullStorageFileName)) { // saving thumbnail image file metadata var relationType = string.Format("thumb-{0}x{1}", maxWidth, maxHeight); var metadata = fileMetadataProvider.CreateRelated( originalMetadataId, relationType, thumbBlobLocation.ContainerName, thumbBlobLocation.FileName, thumbBlobLocation.BlobName, null); fileMetadataProvider.SaveChanges(); storage.UploadFileToStorage(new MemoryStream(array), thumbBlobLocation); } } } return(new CreateThumbResult(CreateThumbStatus.Ok, array, contentType)); }
public ImagesController(IBlobStorageManager blobStorageManager) { _blobStorageManager = blobStorageManager ?? throw new ArgumentNullException(nameof(blobStorageManager)); }
public ContentService(IBlobStorageManager blobStorageManager) { _blobStorageManager = blobStorageManager; }
public ImagesController(IBlobStorageManager blobStorageManager, IOptions <AzureStorageConfig> config) { _blobStorageManager = blobStorageManager ?? throw new ArgumentNullException(nameof(blobStorageManager)); storageConfig = config.Value; }
private static void FillFileLengths(IBlobStorageManager storage, PatientFilesGroupViewModel viewModel, int? dbUserId) { // reading file sizes from the storage // todo: use db to get file size (faster) foreach (var eachFile in viewModel.Files) { var fullStoragePath = string.Format("{0}\\{1}", eachFile.ContainerName, eachFile.BlobName); var mustStartWith = string.Format("patient-files-{0}\\", dbUserId); if (!fullStoragePath.StartsWith(mustStartWith)) continue; eachFile.FileLength = storage.GetFileLength(eachFile.ContainerName, eachFile.BlobName); } }
public TempFileController(IBlobStorageManager storage, IDateTimeService datetimeService) { this.storage = storage; this.datetimeService = datetimeService; }