public async Task <string> Handle(GetResizedFileByIdQuery request, CancellationToken cancellationToken) { var criteria = new ImageResizeCriteria(request.ImageId, request.Query); var criteriaValidator = new ImageResizeCriteriaValidator(_configuration); Guard.AgainstInvalidArgumentWithMessage("Invalid criteria", criteriaValidator.ValidCriteria(criteria)); (int?width, int?height)widthHeightPair = criteriaValidator.GetCriteria(criteria); var fileDtos = await _fileReadRepository.GetAllImageOfIdAsync(request.ImageId, widthHeightPair.width, widthHeightPair.height); Guard.AgainstNullOrNotAny(Constants.File, fileDtos); BaseFileDto fileFound; fileFound = fileDtos.FirstOrDefault(file => (widthHeightPair.width == null || file.Width == widthHeightPair.width) && (widthHeightPair.height == null || file.Height == widthHeightPair.height) && !criteria.Empty); if (fileFound != null) { return(fileFound.FilePath); } BaseFileDto originalFile = fileDtos.FirstOrDefault(file => file.Id == criteria.ImageId); Guard.AgainstNull(Constants.OriginalFile, originalFile); bool shoudResize = _imageService.ShouldResize(((int)originalFile.Width, (int)originalFile.Height), widthHeightPair); if (criteria.Empty || !shoudResize) { fileFound = originalFile; } return(fileFound?.FilePath); }
public async Task <bool> Handle(ResizeImageCommand request, CancellationToken cancellationToken) { var criteria = new ImageResizeCriteria(request.ImageId, request.Query); var criteriaValidator = new ImageResizeCriteriaValidator(_configuration); Guard.AgainstInvalidArgumentWithMessage("Invalid criteria", criteriaValidator.ValidCriteria(criteria)); (int?width, int?height)widthHeightPair = criteriaValidator.GetCriteria(criteria); var files = _fileRepository.GetFiles(criteria.ImageId, widthHeightPair.width, widthHeightPair.height); Guard.AgainstNullOrNotAny("Files", files); var originalImage = files.FirstOrDefault(file => file.Id == request.ImageId); Guard.AgainstNull("OriginalFile", originalImage); //if request has no param -> ask for original image -> return directly to original image if (criteria.Empty) { return(true); } //resized image is always the first one in the list var fileResized = files.OrderByDescending(file => file.OriginalId) .FirstOrDefault(); bool canResize = _imageService.CanResize(widthHeightPair, (fileResized.Width, fileResized.Height)); Guard.AgainstInvalidOperationWithMessage("Cannot Resize this picture with given resolution", canResize); bool shouldResize = _imageService.ShouldResize(((int)originalImage.Width, (int)originalImage.Height), widthHeightPair); if (!shouldResize) { return(true); } //file not exist if (fileResized.OriginalId == null) { if ((widthHeightPair.width == null || fileResized.Width == widthHeightPair.width) && (widthHeightPair.height == null || fileResized.Height == widthHeightPair.height)) { return(true); } using (var imageData = _imageService.ReadImage(fileResized.FilePath)) { var oldFileName = Path.GetFileName(fileResized.FilePath); string fileExtension = Path.GetExtension(fileResized.FilePath); var imagePath = Path.GetDirectoryName(fileResized.FilePath); var newFileName = string.Concat(Path.GetFileNameWithoutExtension(oldFileName), widthHeightPair.width == null ? "" : $"_w{widthHeightPair.width}", widthHeightPair.height == null ? "" : $"_h{widthHeightPair.height}", fileExtension); string cloudPath = PathHelper.CombineUrl(imagePath.Replace(_configuration.RootDataFolder, _configuration.CloudDataUrl), string.Empty); using (FileStream outputStream = new FileStream(Path.Combine(imagePath, newFileName), FileMode.CreateNew)) { (int?width, int?height)sizeAfterResized = await _imageService.ResizeImage(imageData, (widthHeightPair.width, widthHeightPair.height), fileExtension, outputStream); var filebuilder = new FileBuilder(); var newFile = filebuilder.BuildCloudUrl(cloudPath, newFileName) .BuildFilePath(imagePath, newFileName) .BuildFileInfo(new BlueprintFile { CreatedDate = DateTime.UtcNow, CreatedBy = nameof(System), FileType = fileResized.FileType, Height = sizeAfterResized.height, Width = sizeAfterResized.width, OriginalFileName = newFileName, Source = fileResized.Source, OriginalId = fileResized.Id, Extension = fileResized.Extension, CompanyId = fileResized.CompanyId }) .Build(); await _fileRepository.AddAsync(newFile); await _fileRepository.CommitAsync(); return(true); } } } return(true); }