public async Task UploadFiles_GivenValidArgsAndSessionStarted_DoesNotThrowException()
        {
            // Arrange
            string             sessionId = "780352d9-ed37-47de-b03d-d47327421df9";
            string             file01Id  = "d9c2e134-9687-4c2f-a639-4080d4bf15d9";
            string             file02Id  = "8a4a155b-92b9-48f9-8d5c-d5f9c495ea53";
            UploadFilesCommand command   = new UploadFilesCommand(Guid.NewGuid().ToString(),
                                                                  Guid.NewGuid().ToString(),
                                                                  new CommandIssuer(Guid.NewGuid().ToString(), Guid.NewGuid().ToString()),
                                                                  sessionId, new List <FileData>()
            {
                new FileData(file01Id, new MemoryStream(Encoding.UTF8.GetBytes("testcontent01"))),
                new FileData(file02Id, new MemoryStream(Encoding.UTF8.GetBytes("testcontent02"))),
            });
            var applicationService      = this.Fixture.Kernel.Get <IFileStorageCommandApplicationService>();
            var uploadSessionRepository = this.Fixture.Kernel.Get <IUploadSessionRepository>();

            // Act
            await applicationService.UploadFilesAsync(command);

            // Assert
            UploadSession session = await uploadSessionRepository.GetByIdAsync(new Guid(sessionId));

            Assert.NotNull(session);
            Assert.True(session.IsFileUploaded(new Guid(file01Id)));
            Assert.True(session.IsFileUploaded(new Guid(file02Id)));
        }
Exemple #2
0
        public async Task <ActionResult> UploadFiles()
        {
            var command = new UploadFilesCommand();

            if (Request.HasFormContentType)
            {
                foreach (var file in Request.Form.Files)
                {
                    byte[] bytes;
                    var    fileName = file.FileName;

                    using (var reader = new BinaryReader(file.OpenReadStream()))
                        bytes = reader.ReadBytes((int)file.OpenReadStream().Length);

                    command.Files.Add(new UploadFileDto(
                                          fileName,
                                          file.ContentType,
                                          bytes
                                          ));
                }
            }

            await Mediator.Send(command);

            return(StatusCode((int)HttpStatusCode.Created));
        }
        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();
        }
Exemple #4
0
        public async override Task UploadFilesAsync(UploadFilesCommand command)
        {
            this.Logger.LogInformation("[Start] {0} received.", command.GetType().Name);

            if (command != null)
            {
                this.Logger.LogInformation("[Start] Processing {0} with id = '{1}' and correlationId = '{2}'. Uploading files.",
                                           command.GetType().Name, command.CommandId, command.CorrelationId);
            }

            await base.UploadFilesAsync(command);

            if (command != null)
            {
                this.Logger.LogInformation("[Finish] Processing {0} with id = '{1}' and correlationId = '{2}'. Uploading files.",
                                           command.GetType().Name, command.CommandId, command.CorrelationId);
            }

            this.Logger.LogInformation("[Finish] {0} received.", command.GetType().Name);
        }
Exemple #5
0
 public async virtual Task UploadFilesAsync(UploadFilesCommand command)
 {
     await this.ApplicationService.UploadFilesAsync(command);
 }
 public async override Task UploadFilesAsync(UploadFilesCommand command)
 {
     await base.UploadFilesAsync(command);
 }