public async Task UploadFilesAsync(UploadFilesCommand command) { // Check file metadata against upload session information Guid sessionId = new Guid(command.UploadSessionId); UploadSession session = await this.UploadSessionRepository.GetByIdAsync(sessionId); if (session == null) { //TODO: Throw exception: the specified upload session does not exist. throw new UploadSessionNotStartedException("No upload session has been started. Please, make sure to first start the upload session before uploading files."); } if (!session.IsStarted) { throw new UploadSessionNotStartedException("The specified upload session is not in the started state so no files can be uploaded. Please, start a new session for uploading."); } // Check that id's specified in the command are valid if (!session.AreFilesRegistered(command.Files.Select(f => new Guid(f.FileId)).AsEnumerable())) { throw new UploadSessionFileNotRegisteredException("Some of the files specified were not previously registered for upload. Please, make sure that all files are first registered for upload at session start."); } // Upload files (first store file content, second update session information) foreach (var file in command.Files) { Guid fileId = new Guid(file.FileId); FileUploadDescription uploadDescription = session.GetFileUploadDescription(fileId); // Upload file ResourceUri sourceStoreFileUri = await this.UploadFileWithoutCommitingDomainChangesAsync(fileId, session.OwnerId, uploadDescription.FileName, uploadDescription.ContentType, uploadDescription.ContentLength, file.Content, session.UploadAsPublic); // Update session (mark file as uploaded and assign access uri) ResourceUri fileAccessUri = RoutingService.BuildUriForFile(uploadDescription, sourceStoreFileUri, session.UploadAsPublic); session.MarkFileAsUploaded(fileId); session.AssignUriToFileDescription(fileId, fileAccessUri); } // Save changes await this.unitOfWork.CommitChangesAsync(); }