public async Task <Download> DownloadAsync(Guid guid, ImageSize size = ImageSize.Origin, ImageProportion proportion = ImageProportion.Origin) { var fileReference = await ApplicationDbContext.FileReferences .AsNoTracking() .OrderByDescending(x => x.Date) .FirstOrDefaultAsync(x => x.Guid == guid); if (fileReference is null) { throw new FileReferenceNotFound(guid); } try { var fileName = ImageFormatterService.SpecifyImagePath(fileReference.Name, size, proportion); var path = PathToFile(fileReference.Entity, fileReference.Date, fileReference.Guid, fileName); if (File.Exists(path)) { var stream = new FileStream(path, FileMode.Open); new FileExtensionContentTypeProvider().TryGetContentType(fileName, out string contentType); return(new Download(stream, contentType)); } throw new FileNotFoundException("The specified file is not found!"); } catch (FileNotFoundException ex) { Logger.LogError(ex, "An error occured in {0} while trying to download", GetFullMemberName()); throw; } }
public async Task <IActionResult> Get(Guid guid, ImageSize imageSize = ImageSize.Origin, ImageProportion imageProportion = ImageProportion.Origin) { var download = await ImageService.DownloadAsync(guid, imageSize, imageProportion); return(new FileStreamResult(download.Stream, download.ContentType)); }
public bool CanBeProcessed(Image image, ImageSize size, ImageProportion proportion) { var formatter = ProportionServiceResolver(proportion); if (formatter is null) { throw new NotSupportedException($"Formatter of `{nameof(proportion)}` is not supported yet!"); } return(formatter.CanBeFormatted(image, size)); }
public async Task <Download> DownloadAsync(string entity, Guid guid, DateTime?date = null, ImageSize size = ImageSize.Origin, ImageProportion proportion = ImageProportion.Origin) { var objectId = await Validate(entity, guid); var fileReferences = ApplicationDbContext.FileReferences .AsNoTracking() .OrderByDescending(x => x.Date) .Where(x => x.ObjectId == objectId && x.Entity == entity && (!date.HasValue || date.Value.Date == x.Date.Date)); if (!await fileReferences.AnyAsync()) { throw new FileReferenceNotFound(objectId, entity); } try { foreach (var fileReference in await fileReferences.ToListAsync()) { var fileName = ImageFormatterService.SpecifyImagePath(fileReference.Name, size, proportion); var path = PathToFile(fileReference.Entity, fileReference.Date, fileReference.Guid, fileName); if (File.Exists(path)) { var stream = new FileStream(path, FileMode.Open); new FileExtensionContentTypeProvider().TryGetContentType(fileName, out string contentType); return(new Download(stream, contentType)); } } throw new FileNotFoundException("The specified file is not found!"); } catch (FileNotFoundException ex) { Logger.LogError(ex, "An error occured in {0} while trying to download", GetFullMemberName()); throw; } }
public Image ProcessImage(Image image, ImageSize size, ImageProportion proportion) { if (!Sizes.Contains(size)) { throw new ArgumentException($"Parameter `{nameof(size)}` is not supported at the moment!"); } if (!Proportions.Contains(proportion)) { throw new ArgumentException($"Parameter `{nameof(size)}` is not supported at the moment!"); } var formatter = ProportionServiceResolver(proportion); if (formatter is null) { throw new NotSupportedException($"Formatter of `{nameof(proportion)}` is not supported yet!"); } if (size == ImageSize.Origin && proportion == ImageProportion.Origin) { return(image); } try { return(formatter.PrepareImage(ResizeImage, image, size)); } catch (NotSupportedImageSizeException ex) { Logger.LogError(ex, "An exception ocurred in {0} while processing image for different sizes!", GetFullMemberName()); } return(null); }
public string SpecifyImagePath(string path, ImageSize size, ImageProportion proportion) => Path.Combine(Path.GetDirectoryName(path) ?? string.Empty, $"{size.GetDescription()}{size.GetFileNameSeparator()}" + $"{proportion.GetDescription()}{proportion.GetFileNameSeparator()}" + $"{Path.GetFileName(path)}");