示例#1
0
        public async Task ShouldRespond404ForInvalidGallery(string gallery, string display)
        {
            var imageId  = ConstantFactory.Id;
            var path     = $"/{imageId}.{gallery}.{display}.jpg";
            var image    = new byte[Size5MB];
            var formData = new ImageFormData().Upload(image, "image/jpeg");
            var response = await _client.PutAsync(path, formData);

            Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
        }
示例#2
0
        public async Task ShouldRespond404WhenTrySaveThumbnailDirectly(string gallery)
        {
            var imageId  = ConstantFactory.Id;
            var path     = $"/{imageId}.{gallery}.thumbnail.jpg";
            var image    = new byte[Size5MB];
            var formData = new ImageFormData().Upload(image, "image/jpeg");
            var response = await _client.PutAsync(path, formData);

            Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
        }
示例#3
0
        public async Task ShouldRespond204AfterSaveSuccessfully(string gallery, string display)
        {
            var imageId = ConstantFactory.Id;
            var path    = $"/{imageId}.{gallery}.{display}.jpg";
            var image   = await File.ReadAllBytesAsync($"{_server.Environment.ContentRootPath}/Fixtures/upload.jpg");

            var formData = new ImageFormData().Upload(image, "image/jpeg");
            var response = await _client.PutAsync(path, formData);

            Assert.Equal(HttpStatusCode.NoContent, response.StatusCode);
        }
示例#4
0
        public static HttpContent Upload(this ImageFormData image, byte[] bytes, string contentType)
        {
            var file = new ByteArrayContent(bytes);

            file.Headers.ContentType = new MediaTypeHeaderValue(contentType);

            var formData = new MultipartFormDataContent();

            formData.Add(file, nameof(ImageFormData.File), nameof(ImageFormData.File));

            return(formData);
        }
示例#5
0
        public async Task ShouldAlwaysSaveJpeg(string gallery, string display)
        {
            var imageId = ConstantFactory.Id;
            var path    = $"/{imageId}.{gallery}.{display}.jpg";
            var fixture = $"{_server.Environment.ContentRootPath}/Fixtures/upload.jpg";
            var image   = await File.ReadAllBytesAsync(fixture);

            var formData = new ImageFormData().Upload(image, "image/jpeg");
            var response = await _client.PutAsync(path, formData);

            Assert.All(_server.FileStorage.SavedFiles, file =>
                       Assert.Equal("image/jpeg", file.ContentType));
        }
示例#6
0
        public async Task ShouldSaveCover(string gallery, string contentType)
        {
            var imageId = ConstantFactory.Id;
            var path    = $"/{imageId}.{gallery}.cover.jpg";
            var image   = await File.ReadAllBytesAsync($"{_server.Environment.ContentRootPath}/Fixtures/upload.jpg");

            var formData = new ImageFormData().Upload(image, contentType);
            var response = await _client.PutAsync(path, formData);

            var filenameCover = $"{_token.TenantId}-{imageId}.{gallery}.cover.jpg";

            Assert.Contains(_server.FileStorage.SavedFiles, file => file.Name == filenameCover);
        }
示例#7
0
        public async Task ShouldRespond400ForSizeExceeded(string gallery, string display)
        {
            var imageId  = ConstantFactory.Id;
            var path     = $"/{imageId}.{gallery}.{display}.jpg";
            var image    = new byte[Size10MB];
            var formData = new ImageFormData().Upload(image, "image/jpeg");
            var response = await _client.PutAsync(path, formData);

            var jsonResponse = await _client.ReadAsJsonAsync <BadRequestError>(response);

            Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
            Assert.Contains(jsonResponse.Errors, error => error == "File must be an image with a maximum size of 5MB.");
        }
示例#8
0
        public async Task ShouldRespond400ForUnacceptableContentType(string gallery, string display)
        {
            var imageId  = ConstantFactory.Id;
            var path     = $"/{imageId}.{gallery}.{display}.jpg";
            var image    = new byte[Size5MB];
            var formData = new ImageFormData().Upload(image, "image/svg+xml");
            var response = await _client.PutAsync(path, formData);

            var jsonResponse = await _client.ReadAsJsonAsync <BadRequestError>(response);

            Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
            Assert.Contains(jsonResponse.Errors, error => error == "File must be an image in JPEG or PNG format.");
        }
示例#9
0
        public ActionResult <List <String> > Post([FromForm] ImageFormData data)
        {
            var filepath = new List <string>();

            foreach (IFormFile file in HttpContext.Request.Form.Files)
            {
                String filename = this._imageService.SaveImg(file);

                filepath.Add(filename);
            }

            return(filepath);
        }
        public async Task <IActionResult> Save([FromForm] ImageFormData formData,
                                               [FromRoute] string image, [FromRoute] string gallery, [FromRoute] string display)
        {
            var tenantId     = User.Claims.TenantId();
            var imageGallery = new ImageGallery(_fileStorage, _messageBroker);

            using (var stream = new MemoryStream())
            {
                await formData.File.CopyToAsync(stream);

                await imageGallery.Save(tenantId, image, gallery, display, stream);
            }

            return(NoContent());
        }
示例#11
0
        public async Task ShouldPublishEventAfterSavingCover(string gallery)
        {
            var imageId = ConstantFactory.Id;
            var path    = $"/{imageId}.{gallery}.cover.jpg";
            var fixture = $"{_server.Environment.ContentRootPath}/Fixtures/upload.jpg";
            var image   = await File.ReadAllBytesAsync(fixture);

            var formData = new ImageFormData().Upload(image, "image/jpeg");
            var response = await _client.PutAsync(path, formData);

            var filenameCover = $"{_token.TenantId}-{imageId}.{gallery}.cover.jpg";

            Assert.Contains(_server.EventBus.PublishedEvents, @event =>
                            @event.Name == "gallery.image.created" &&
                            (@event.Payload as ImageCreatedPayload).Filename == filenameCover);
        }
        public ActionResult Post([FromForm] ImageFormData img)
        {
            // Getting Image
            IFormFile imageDat = img.Data;

            if (img.Data.ContentType == "image/jpeg" || img.Data.ContentType == "image/png")
            {
                // load image
                try
                {
                    string fileName = _imageService.SaveImg(imageDat);
                    return(Accepted(new { fileName }));
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
            return(BadRequest());
        }