Example #1
0
        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);
        }
Example #2
0
 public async Task <IEnumerable <BaseFileDto> > GetAllImageOfIdAsync(long id, int?width, int?height)
 {
     return(await _fileReadRepository.GetAllImageOfIdAsync(id, width, height));
 }