Beispiel #1
0
        public bool ValidateImagesForMember(int userId, IEnumerable <ReportImage> reportImages,
                                            IEnumerable <IFormFile> images)
        {
            int  imagesCount          = 0;
            long userReportImagesSize = 0;

            reportImages.Where(ri => ri.UserId == userId).ToList()
            .ForEach(ri => (imagesCount, userReportImagesSize) = (imagesCount + 1,
                                                                  userReportImagesSize + ri.Size));

            foreach (var image in images)
            {
                if (userReportImagesSize + image.Length <= Constants.MaximumReportImagesSizePerUser)
                {
                    imagesCount += (int)Math.Ceiling(image.Length / (decimal)Constants.MaximumReportImageSize);
                }
                else
                {
                    return(false);
                }

                if (imagesCount > Constants.MaximumReportImagesCountPerUser)
                {
                    throw new UploadFileException(
                              ValidatorMessages.MaxFilesCountValidatorMessage(Constants.MaximumReportImagesCountPerUser));
                }
            }

            return(true);
        }
Beispiel #2
0
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            var files = value as List <IFormFile>;

            if (files?.Count > maxFilesCount)
            {
                return(new ValidationResult(ValidatorMessages.MaxFilesCountValidatorMessage(maxFilesCount)));
            }

            return(ValidationResult.Success);
        }
Beispiel #3
0
 public static IRuleBuilderOptions <T, TElement> MaxFilesCountIs <T, TElement>(this IRuleBuilderOptions <T, TElement> ruleBuilder, int maxFilesCount)
 => ruleBuilder.Must(files => (files as List <IFormFile>) == null ? true : (files as List <IFormFile>).Count <= maxFilesCount).WithMessage(ValidatorMessages.MaxFilesCountValidatorMessage(maxFilesCount));