Exemple #1
0
        protected virtual async Task <FileNameValidationResultModel> ValidateFileName(
            ICollection collection,
            string rawFileName,
            long?fileLength,
            CancellationToken cancellationToken)
        {
            if (collection == null)
            {
                throw new ArgumentNullException(nameof(collection));
            }

            var fileName = Path.GetFileName(rawFileName);
            var fileNameValidationService = _fileNameValidationServiceProvider.GetFileNameValidationService(collection.CollectionName);
            var validationResult          = await fileNameValidationService.ValidateFileNameAsync(collection.CollectionName, fileName, collection.FileNameFormat, fileLength, cancellationToken);

            if (validationResult.ValidationResult != FileNameValidationResult.Valid)
            {
                ModelState.AddModelError(ErrorMessageKeys.Submission_FileFieldKey, validationResult.FieldError);
                ModelState.AddModelError(ErrorMessageKeys.ErrorSummaryKey, validationResult.SummaryError);

                _logger.LogWarning($"User uploaded invalid file with name :{fileName}");
            }

            return(validationResult);
        }
        public async Task <IActionResult> BulkUpload(IFormFile file, CancellationToken cancellationToken)
        {
            if (file == null)
            {
                return(View("AddNewOption"));
            }

            var collection = _collections.SingleOrDefault(s => string.Equals(ProvidersUploadCollectionName, s.CollectionName, StringComparison.CurrentCultureIgnoreCase));

            var fileName     = Path.GetFileName(file.FileName);
            var collectionDb = await _collectionService.GetCollectionAsync(ProvidersUploadCollectionName);

            if (collectionDb == null || !collectionDb.IsOpen)
            {
                _logger.LogWarning($"collection {ProvidersUploadCollectionName} is not open/available, but file is being uploaded");
                ModelState.AddModelError(ErrorMessageKeys.ErrorSummaryKey, $"collection {ProvidersUploadCollectionName} is not open/available.");
                return(View("AddNewOption"));
            }

            var fileNameValidationService = _fileNameValidationServiceProvider.GetFileNameValidationService(collection.CollectionName);
            var validationResult          = await fileNameValidationService.ValidateFileNameAsync(collection.CollectionName, fileName.ToUpper(CultureInfo.CurrentUICulture), collection.FileNameFormat, file.Length, cancellationToken);

            if (validationResult.ValidationResult != FileNameValidationResult.Valid)
            {
                ModelState.AddModelError(ErrorMessageKeys.Submission_FileFieldKey, validationResult.FieldError);
                ModelState.AddModelError(ErrorMessageKeys.ErrorSummaryKey, validationResult.SummaryError);

                _logger.LogWarning($"User uploaded invalid file with name :{fileName}");
                return(View("AddNewOption"));
            }

            await(await _storageService.GetAzureStorageReferenceService(_opsDataLoadServiceConfigSettings.ConnectionString, collectionDb.StorageReference)).SaveAsync(fileName, file?.OpenReadStream());

            var jobId = await _jobService.SubmitJob(
                new JobSubmission
            {
                CollectionName   = ProvidersUploadCollectionName,
                FileName         = fileName,
                FileSizeBytes    = file.Length,
                SubmittedBy      = User.Name(),
                NotifyEmail      = User.Email(),
                StorageReference = collectionDb.StorageReference
            }, cancellationToken);

            return(RedirectToAction("InProgress", new { jobId }));
        }