예제 #1
0
        public ValidationResult ValidateFormImageFile(IFormFile formFile, List <FileExtensions> customFileExtensions = null, List <string> customMimeTypes = null, long customMaxFileSize = 0)
        {
            List <FileExtensions> allowedFileExtensions = customFileExtensions == null ? this.configurationConstants.ImageValidationRules.AllowedFormatExtensionsAsEnum : customFileExtensions;
            List <string>         allowedMimeTypes      = customMimeTypes == null ? this.configurationConstants.ImageValidationRules.AllowedMimeTypes : customMimeTypes;
            long maxAllowedFileSize = customMaxFileSize == 0 ? this.configurationConstants.ImageValidationRules.MaxAllowedFileSize : customMaxFileSize;

            var startupHandler       = new StartupHandler <IFormFile>();
            var fileExtensionHandler = new FileExtensionHandler(allowedFileExtensions);
            var fileMimeTypesHandler = new FileMimeTypesHandler(allowedMimeTypes);
            var fileSizeHandler      = new FileSizeHandler(maxAllowedFileSize);
            var imageBoxingHandler   = new ImageBoxingHandler();

            startupHandler
            .SetNext(fileExtensionHandler)
            .SetNext(fileMimeTypesHandler)
            .SetNext(fileSizeHandler)
            .SetNext(imageBoxingHandler);

            ValidationResult validationResult = new ValidationResult();
            string           resultMessage    = string.Empty;

            validationResult.Success = startupHandler.Handle(formFile, out resultMessage) != null;
            validationResult.Message = resultMessage;
            if (validationResult.Success)
            {
                validationResult.Message = "Image file is valid.";
            }

            return(validationResult);
        }
예제 #2
0
        /// <inheritdoc/>
        public ValidationResult ValidateFormFile(IFormFile formFile, List <FileExtensions> customFileExtensions = null, List <string> customMimeTypes = null, long customMaxFileSize = 0)
        {
            List <FileExtensions> allowedFileExtensions = customFileExtensions ?? this.allowedFileFormatExtensions;
            List <string>         allowedMimeTypes      = customMimeTypes ?? this.allowedFilesMimeTypes;
            long maxAllowedFileSize = customMaxFileSize == 0 ? this.options.MaxAllowedFileSize : customMaxFileSize;

            var startupHandler       = new StartupHandler <IFormFile>();
            var fileExtensionHandler = new FileExtensionHandler(allowedFileExtensions);
            var fileMimeTypesHandler = new FileMimeTypesHandler(allowedMimeTypes);
            var fileSizeHandler      = new FileSizeHandler(maxAllowedFileSize);

            startupHandler
            .SetNext(fileExtensionHandler)
            .SetNext(fileMimeTypesHandler)
            .SetNext(fileSizeHandler);

            ValidationResult validationResult = new ValidationResult();

            validationResult.Succeeded = startupHandler.Handle(formFile, out var resultMessage) != null;
            validationResult.Message   = resultMessage;
            if (validationResult.Succeeded)
            {
                validationResult.Message = "File is valid.";
            }

            return(validationResult);
        }
예제 #3
0
        public void HandleValidation_ValidWorkflow_SuccessPassing(string text, bool valid, string expectedErrorMessage)
        {
            var startupHandler = new StartupHandler <string>();

            startupHandler
            .SetNext(new LetterHandler())
            .SetNext(new DigitHandler());

            var result = startupHandler.Handle(text, out var errorMessage);

            Assert.Equal(valid, result == text);
            Assert.Equal(expectedErrorMessage, errorMessage);
        }