public async Task <Either <ActionResult, DataFileInfo> > Upload(Guid releaseId, IFormFile dataFormFile, IFormFile metaFormFile, string userName, Guid?replacingFileId = null, string subjectName = null) { return(await _persistenceHelper .CheckEntityExists <Release>(releaseId) .OnSuccess(_userService.CheckCanUpdateRelease) .OnSuccess(async release => { return await _persistenceHelper.CheckOptionalEntityExists <File>(replacingFileId) .OnSuccess(async replacingFile => { return await ValidateSubjectName(releaseId, subjectName, replacingFile) .OnSuccess(validSubjectName => _fileUploadsValidatorService .ValidateDataFilesForUpload(releaseId, dataFormFile, metaFormFile) // First, create with status uploading to prevent other users uploading the same datafile .OnSuccess(async() => await _importService.CreateImportTableRow(releaseId, dataFormFile.FileName.ToLower())) .OnSuccess(async() => { var dataFile = await _fileRepository.Create( releaseId: releaseId, filename: dataFormFile.FileName.ToLower(), type: FileType.Data, replacingFile: replacingFile); var metaFile = await _fileRepository.Create( releaseId: releaseId, filename: metaFormFile.FileName.ToLower(), Metadata); var dataInfo = GetDataFileMetaValues( name: validSubjectName, metaFileName: metaFile.Filename, userName: userName, numberOfRows: CalculateNumberOfRows(dataFormFile.OpenReadStream()) ); var metaDataInfo = GetMetaDataFileMetaValues( dataFileName: dataFile.Filename ); await UploadFileToStorage(dataFile, dataFormFile, dataInfo); await UploadFileToStorage(metaFile, metaFormFile, metaDataInfo); await _importService.Import( releaseId: releaseId, dataFileName: dataFile.Filename, metaFileName: metaFile.Filename, dataFile: dataFormFile, isZip: false); var blob = await _blobStorageService.GetBlob( PrivateFilesContainerName, dataFile.Path() ); return new DataFileInfo { Id = dataFile.Id, FileName = dataFile.Filename, Name = validSubjectName, Path = blob.Path, Size = blob.Size, MetaFileId = metaFile.Id, MetaFileName = blob.GetMetaFileName(), Rows = blob.GetNumberOfRows(), UserName = blob.GetUserName(), Status = IStatus.QUEUED, Created = blob.Created, Permissions = await _userService.GetDataFilePermissions(releaseId, blob.FileName) }; })); }); })); }
public async Task <Either <ActionResult, DataFileInfo> > Upload(Guid releaseId, IFormFile dataFormFile, IFormFile metaFormFile, string userName, Guid?replacingFileId = null, string subjectName = null) { return(await _persistenceHelper .CheckEntityExists <Release>(releaseId) .OnSuccess(_userService.CheckCanUpdateRelease) .OnSuccess(async release => { return await _persistenceHelper .CheckOptionalEntityExists <File>(replacingFileId) .OnSuccessDo(replacingFile => _fileUploadsValidatorService.ValidateDataFilesForUpload(releaseId, dataFormFile, metaFormFile, replacingFile)) // First, create with status uploading to prevent other users uploading the same datafile .OnSuccessCombineWith(replacingFile => ValidateSubjectName(releaseId, subjectName, replacingFile)) .OnSuccess(async replacingFileAndSubjectName => { var(replacingFile, validSubjectName) = replacingFileAndSubjectName; var subjectId = await _releaseRepository .CreateStatisticsDbReleaseAndSubjectHierarchy(releaseId); var dataFile = await _releaseDataFileRepository.Create( releaseId: releaseId, subjectId: subjectId, filename: dataFormFile.FileName.ToLower(), type: FileType.Data, createdById: _userService.GetUserId(), name: validSubjectName, replacingFile: replacingFile); var metaFile = await _releaseDataFileRepository.Create( releaseId: releaseId, subjectId: subjectId, filename: metaFormFile.FileName.ToLower(), type: Metadata, createdById: _userService.GetUserId()); var dataInfo = GetDataFileMetaValues( metaFileName: metaFile.Filename, numberOfRows: CalculateNumberOfRows(dataFormFile.OpenReadStream()) ); await UploadFileToStorage(dataFile, dataFormFile, dataInfo); await UploadFileToStorage(metaFile, metaFormFile); await _dataImportService.Import( subjectId: subjectId, dataFile: dataFile, metaFile: metaFile, formFile: dataFormFile); var blob = await _blobStorageService.GetBlob( PrivateReleaseFiles, dataFile.Path() ); await _contentDbContext.Entry(dataFile) .Reference(f => f.CreatedBy) .LoadAsync(); return new DataFileInfo { Id = dataFile.Id, FileName = dataFile.Filename, Name = validSubjectName, Size = blob.Size, MetaFileId = metaFile.Id, MetaFileName = metaFile.Filename, Rows = blob.GetNumberOfRows(), UserName = dataFile.CreatedBy.Email, Status = DataImportStatus.QUEUED, Created = dataFile.Created, Permissions = await _userService.GetDataFilePermissions(dataFile) }; }); })); }