コード例 #1
0
        public async Task <Either <ActionResult, DataFileInfo> > UploadAsZip(Guid releaseId,
                                                                             IFormFile zipFormFile,
                                                                             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 =>
                               _dataArchiveValidationService.ValidateDataArchiveFile(releaseId, zipFormFile)
                               .OnSuccess(async archiveFile =>
                    {
                        var dataInfo = GetDataFileMetaValues(
                            metaFileName: archiveFile.MetaFileName,
                            numberOfRows: 0
                            );

                        return await _fileUploadsValidatorService
                        .ValidateDataArchiveEntriesForUpload(releaseId, archiveFile)
                        .OnSuccess(async() =>
                        {
                            var subjectId = await _releaseRepository
                                            .CreateStatisticsDbReleaseAndSubjectHierarchy(releaseId);

                            var zipFile = await _releaseDataFileRepository.CreateZip(
                                filename: zipFormFile.FileName.ToLower(),
                                releaseId: releaseId,
                                createdById: _userService.GetUserId());

                            var dataFile = await _releaseDataFileRepository.Create(
                                releaseId: releaseId,
                                subjectId: subjectId,
                                filename: archiveFile.DataFileName,
                                type: FileType.Data,
                                createdById: _userService.GetUserId(),
                                name: validSubjectName,
                                replacingFile: replacingFile,
                                source: zipFile);

                            var metaFile = await _releaseDataFileRepository.Create(
                                releaseId: releaseId,
                                subjectId: subjectId,
                                filename: archiveFile.MetaFileName,
                                type: Metadata,
                                createdById: _userService.GetUserId(),
                                source: zipFile);

                            await UploadFileToStorage(zipFile, zipFormFile, dataInfo);

                            await _dataImportService.ImportZip(
                                subjectId: subjectId,
                                dataFile: dataFile,
                                metaFile: metaFile,
                                zipFile: zipFile);

                            var blob = await _blobStorageService.GetBlob(
                                PrivateReleaseFiles,
                                zipFile.Path()
                                );

                            await _contentDbContext.Entry(dataFile)
                            .Reference(f => f.CreatedBy)
                            .LoadAsync();

                            return new DataFileInfo
                            {
                                // TODO size and rows are for zip file but they need to be for
                                // the datafile which isn't extracted yet
                                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)
                            };
                        });
                    }));
                });
            }));
        }