예제 #1
0
        public async Task ShouldCreateFile(string name, string contentType, byte[] content)
        {
            // Arrange
            var fileMock = new Mock <IFile>();

            fileMock.SetupGet(x => x.Name).Returns(name);
            fileMock.SetupGet(x => x.ContentType).Returns(contentType);
            fileMock.Setup(x => x.OpenReadStream()).Returns(() => new MemoryStream(content));

            var request = new UploadFileCommand()
            {
                File = fileMock.Object
            };

            // Act
            await LoginAsDefaultUserAsync();

            Guid fileId = await SendAsync(request);

            File file = await FindAsync <File>(x => x.Id == fileId);

            // Assert
            file.Should().NotBeNull();
            file.Id.Should().Be(fileId);
            file.Name.Should().Be(name);
            file.ContentType.Should().Be(contentType);
            file.Content.Should().Equal(content);
        }
예제 #2
0
        private async Task <bool> HaveValidPictureExtension(Guid?id, CancellationToken cancellationToken)
        {
            Domian.Entities.File picture = await _db.Files.FirstOrDefaultAsync(x => x.Id == id, cancellationToken);

            if (picture == null)
            {
                return(false);
            }

            string extension = Path.GetExtension(picture.Name);

            return(PictureExtensions.Contains(extension));
        }
예제 #3
0
        public async Task <Guid> Handle(UploadFileCommand request, CancellationToken cancellationToken)
        {
            using var ms = new MemoryStream();

            using Stream fs = request.File.OpenReadStream();

            await fs.CopyToAsync(ms, cancellationToken);

            File file = new File()
            {
                Name        = request.File.Name,
                ContentType = request.File.ContentType,
                Content     = ms.ToArray()
            };

            await _db.Files.AddAsync(file, cancellationToken);

            return(file.Id);
        }