コード例 #1
0
        public virtual async Task<IHttpActionResult> UploadFile()
        {
            if (!Request.Content.IsMimeMultipartContent())
                throw new Exception();
            var provider = new MultipartMemoryStreamProvider();
            await Request.Content.ReadAsMultipartAsync(provider);
            var fileInfo = new FileDataContract {UserName = User.Identity.Name};
            Stream buffer = null;
            foreach (var content in provider.Contents)
            {
                var name = content.Headers.ContentDisposition.Name.Trim('\"');
                if (string.Equals(name, "referenceNotes", StringComparison.InvariantCultureIgnoreCase))
                {
                    fileInfo.ReferenceNotes = await content.ReadAsStringAsync();
                    continue;
                }
                if (string.Equals(name, "fileHandler", StringComparison.InvariantCultureIgnoreCase))
                {
                    fileInfo.HandlerName = await content.ReadAsStringAsync();
                    continue;
                }
                if (string.Equals(name, "mediaType", StringComparison.InvariantCultureIgnoreCase))
                {
                    fileInfo.MediaType = await content.ReadAsStringAsync();
                    continue;
                }
                if (!string.Equals(name, "file", StringComparison.InvariantCultureIgnoreCase)) continue;
                fileInfo.OriginalFileName = content.Headers.ContentDisposition.FileName.Trim('\"');
                buffer = await content.ReadAsStreamAsync();
            }
            var result = await service.UpsertFileDataSource(fileInfo, buffer);

            return Ok(result);
        }
コード例 #2
0
 public virtual Task<SourceResultDataContract> UpsertFileDataSource(FileDataContract fileData, Stream stream)
 {
     return Task.Run(() =>
         {
             lock (UpsertFileDataSourceLock)
             {
                 var hash = GetHash(stream);
                 var fileSource = Context.GetFileDataSource(fileData.SourceId, hash);
                 if (fileSource == null)
                 {
                     fileSource = new FileDataSource
                         {
                             OriginalFileName = fileData.OriginalFileName,
                             DateEdit = DateTime.Now,
                             InputStatus = SourceStatus.PendingExtraction,
                             ReferenceNotes = fileData.ReferenceNotes,
                             UserName = fileData.UserName,
                             HandlerName = fileData.HandlerName,
                             FileHash = hash,
                             MediaType = fileData.MediaType
                         };
                     fileSource = Context.AddDataSource(fileSource);
                     var newFileName = streamManager
                         .StageStream(
                             fileSource.Id,
                             fileData.OriginalFileName,
                             stream);
                     fileSource.CurrentFileName = newFileName;
                 }
                 if (fileSource.InputStatus != SourceStatus.PendingExtraction)
                 {
                     return DataContractError(fileSource.Id,
                                              DataSourceServiceResources.FileSourceIsNotPendingExtraction);
                 }
                 fileSource.OriginalFileName = fileData.OriginalFileName ?? fileSource.OriginalFileName;
                 fileSource.HandlerName = fileData.HandlerName ?? fileSource.HandlerName;
                 fileSource.MediaType = fileData.MediaType ?? fileSource.MediaType;
                 fileSource.UserName = fileData.UserName;
                 fileSource.DateEdit = DateTime.Now;
                 fileSource.ReferenceNotes = fileData.ReferenceNotes ?? fileSource.ReferenceNotes;
                 Context.UpdateDataSource(fileSource);
                 return ValidateSource(fileSource.Id);
             }
         });
 }
コード例 #3
0
        public void UpsertFileDataMustNotUpdateReferenceNotesIfNull()
        {
            //Arrange
            var source = new FileDataSource
                {
                    Id = newSourceId, 
                    InputStatus = SourceStatus.PendingExtraction,
                    HandlerName = "oldHandlerName",
                    OriginalFileName = "oldFileName",
                    ReferenceNotes ="oldReferenceNotes"
                };
            mockContext
                .Setup(context => context.GetFileDataSource(It.IsAny<Guid>(), It.IsAny<string>()))
                .Returns(source)
                .Verifiable();
            CreateService();
            mockService
                .Setup(service => service.ValidateSource(It.IsAny<Guid>()))
                .Returns((SourceResultDataContract)null)
                .Verifiable();
            var contract = new FileDataContract
            {
                SourceId = newSourceId,
                HandlerName = "newHandlerName",
                OriginalFileName = "newFileName",
            };            

            //Act
            mockService.Object.UpsertFileDataSource(contract,new MemoryStream());

            //Assert
            mockService.Verify();
            mockContext.Verify();
            mockContext
                .Verify(context => context.UpdateDataSource(It.Is<FileDataSource>(
                    dataSource =>
                    (dataSource.Id == newSourceId) &&
                    (dataSource.UserName == "testUserName") &&
                    (dataSource.OriginalFileName == "newFileName") &&
                    (dataSource.HandlerName == "newHandlerName") &&
                    (dataSource.ReferenceNotes == "oldReferenceNotes"))));
        }
コード例 #4
0
        public void UpsertFileDataSourceMustCallValidateIfUpdating()
        {
            //Arrange
            var source = new FileDataSource { Id = newSourceId,InputStatus =  SourceStatus.PendingExtraction};
            mockContext
                .Setup(context => context.GetFileDataSource(It.IsAny<Guid>(), It.IsAny<string>()))
                .Returns(source)
                .Verifiable();

            var uploadContract = new FileDataContract
            {
                SourceId = newSourceId,
                OriginalFileName = "Original Name"
            };
            var fileStream = new MemoryStream(new byte[] { 100 });

            //Act
            CreateService().UpsertFileDataSource(uploadContract, fileStream);

            //Assert
            mockService
                .Verify(service => service.ValidateSource(It.Is<Guid>(guid => guid == newSourceId)));
        }
コード例 #5
0
        public void UpsertFileDataSourceMustSetTheFileHashIfInserting()
        {
            //Arrange
            var source = new FileDataSource { Id = newSourceId };
            mockContext
                .Setup(context => context.AddDataSource(It.IsAny<FileDataSource>()))
                .Returns(source)
                .Verifiable();

            var uploadContract = new FileDataContract
            {
                SourceId = newSourceId,
                OriginalFileName = "Original Name",
            };
            var fileStream = new MemoryStream(new byte[] {100});

            //Act
            CreateService().UpsertFileDataSource(uploadContract, fileStream);

            //Assert
             mockContext
                .Verify(context => context.AddDataSource(
                    It.Is<FileDataSource>(
                    dataSource => 
                    (dataSource.OriginalFileName == "Original Name") &&
                    (dataSource.FileHash) == "<686ÏNffi¢]¢€¡†\\-(t")));
        }
コード例 #6
0
        public void UpsertFileDataSourceMustCancelfInsertingAndSourceIdNotNull()
        {
            //Arrange
            var source = new FileDataSource { Id = newSourceId };
            mockContext
                .Setup(context => context.AddDataSource(It.IsAny<FileDataSource>()))
                .Returns(source)
                .Verifiable();

            var uploadContract = new FileDataContract
            {
                SourceId = newSourceId,
                OriginalFileName = "Original Name"
            };

            //Act
            CreateService().UpsertFileDataSource(uploadContract,new MemoryStream());

            //Assert
            mockContext.Verify();
            mockService
                .Verify(service => service.CancelFileSourceExtraction(It.Is<Guid>(guid => guid == newSourceId)));
        }
コード例 #7
0
        public void UpsertFilesourceMustUpdateCurrentFileNameWithTheNewName()
        {
            //Arrange
            var source = new FileDataSource { Id = newSourceId, InputStatus = SourceStatus.PendingExtraction };
            mockStreamManager
                .Setup(manager => manager.StageStream(
                    It.Is<Guid>(guid => guid == newSourceId),
                    It.Is<string>(s => s == "Original Name"),
                    It.IsAny<Stream>()))
                .Returns("NewFileName")
                .Verifiable();
            mockContext
                .Setup(context => context.AddDataSource(It.IsAny<FileDataSource>()))
                .Returns(source);

            var uploadContract = new FileDataContract
                {
                    SourceId = newSourceId,
                    OriginalFileName = "Original Name"
                };

            //Act
            CreateService().UpsertFileDataSource(uploadContract,new MemoryStream());

            //Assert
            mockService.Verify();
            mockStreamManager.Verify();
            mockContext.Verify(context => context.UpdateDataSource(It.Is<FileDataSource>(
                dataSource =>
                (dataSource.Id == newSourceId) &&
                (dataSource.InputStatus == SourceStatus.PendingExtraction) &&
                (dataSource.CurrentFileName == "NewFileName"))), Times.Once);
        }
コード例 #8
0
        public void UpsertFileDataSourceMustCallAddDatasourceWhenInserting()
        {
            //Arrange
            var contract = new FileDataContract
                {
                    ReferenceNotes = "reference notes",
                    OriginalFileName = "Original name",
                    HandlerName = "handler name"
                };
            mockContext
                .Setup(context => context.AddDataSource(It.Is<FileDataSource>(
                    source =>
                    (source.InputStatus == SourceStatus.PendingExtraction) &&
                    (source.ReferenceNotes == "reference notes") &&
                    (source.OriginalFileName == "Original name") &&
                    (source.HandlerName == "handler name") &&
                    (source.UserName == "TestUserName"))))
                .Returns(new FileDataSource {Id = newSourceId, InputStatus = SourceStatus.PendingExtraction})
                .Verifiable();

            //Act
            CreateService().UpsertFileDataSource(contract, new MemoryStream());

            //Assert
            mockContext.Verify();
        }