public async Task <ImageResponse> UploadImage(ImageUploadDto model) { try { if (!SecurityHandler.IsValidPassword(model?.password)) { throw new UnauthorizedAccessException(); } var filename = ContentDispositionHeaderValue .Parse(model.file.ContentDisposition) .FileName .ToString().Trim('"'); var fileStream = model.file.OpenReadStream(); var cmd = new UploadImageCommand(model, fileStream, filename); var imageId = await UploadImageCommandHandler.HandleAsync(cmd).ConfigureAwait(false); return(await Task.FromResult(GetImageById(imageId.ToString())).ConfigureAwait(false)); } catch (Exception ex) { throw ex; } }
public void PassingImageDtoAndFileInfoGetsCorrectlyConverted() { var dto = new ImageUploadDto(); dto.annotation = "a"; dto.date = "date"; dto.description = "desc"; dto.inverted = "i"; dto.tags = "one,two,three"; dto.title = "title"; var stream = new MemoryStream(Encoding.UTF8.GetBytes("asdf")); var filename = "testname"; var cmd = new UploadImageCommand(dto, stream, filename); Assert.NotNull(cmd.File); Assert.That(cmd.Image.Filename, Is.EqualTo(filename)); Assert.That(cmd.Image.Thumb.Filename, Is.EqualTo($"thumb_{filename}")); Assert.That(cmd.Image.Annotation, Is.EqualTo(dto.annotation)); Assert.That(cmd.Image.Date, Is.EqualTo(dto.date)); Assert.That(cmd.Image.Description, Is.EqualTo(dto.description)); Assert.That(cmd.Image.Inverted, Is.EqualTo(dto.inverted)); Assert.That(cmd.Image.Title, Is.EqualTo(dto.title)); Assert.That(cmd.Image.Tags.Length, Is.EqualTo(3)); }
public UploadImageCommand(ImageUploadDto model, Stream file, string fileName) { if (file?.Length > 0) { File = new MemoryStream(); file.CopyTo(File); File.Position = 0; file.Dispose(); Image = new Image { Filename = fileName, Description = model.description, Title = model.title, Tags = model.tags?.Split(',') ?? new [] { "default" }, Date = model.date, Thumb = new Thumbnail { Filename = $"thumb_{fileName}" }, Annotation = model.annotation, Inverted = model.inverted }; } }
public async Task <string> UploadCK([FromForm] ImageUploadDto input) { Seller.Seller seller = await GetCurrentSeller(); string url = await ImageManager.UploadWithResult(seller?.Id, input.Files[0]); return(url); }
public async Task <IActionResult> SavePath(ImageUploadDto ImageUploadDto) { var userToCreate = new UploadImage { Name = ImageUploadDto.Name }; var createdImage = await _repo.AddImage(userToCreate, ImageUploadDto.Name, ImageUploadDto.Address, ImageUploadDto.ImgPath); return(StatusCode(201)); }
public ImageDto CreateImage(ImageUploadDto image) { Image imageCreated = _mapper.Map <Image>(image); imageCreated.ImagePath = image.File.FileName; imageCreated = _imageRepo.Create(imageCreated); string filePath = Path.Combine(@$ "Images/", imageCreated.ImagePath); new FileInfo(filePath).Directory?.Create(); using (FileStream stream = new FileStream(filePath, FileMode.Create)) { image.File.CopyTo(stream); } return(_mapper.Map <ImageDto>(imageCreated)); }
public async Task <ActionResult> Post([FromBody] ImageUploadDto dto) { if (!_authService.CheckIfUserIsAuthorized(dto.OwnerId)) { return(Unauthorized("Cannot create image on behalf of someone else")); } var imageEntity = _mapper.Map <ImagehubImage>(dto); imageEntity.Owner = null; await _service.CreateAsync(imageEntity); return(Accepted(new { imageEntity.FileName, imageEntity.Id, imageEntity.OwnerId })); }
public async Task <IActionResult> UploadOne([FromBody] ImageUploadDto photoModel) { // var photoEntity = _mapper.Map<MissionPhoto>(photoModel); // Validations: if (photoModel.UploadedImage == null) { return(BadRequest("Null file")); } if (photoModel.UploadedImage.Length == 0) { return(BadRequest("Empty file")); } if (photoModel.UploadedImage.Length > MAX_BYTES) { return(BadRequest("Max file size exceeded")); // MAX_BYTES could be stored in a configuraton file } if (ACCEPTED_FILE_TYPES.Any(s => s == Path.GetExtension(photoModel.UploadedImage.FileName).ToLower())) { return(BadRequest("Invalid file type")); } if (ModelState.IsValid) { string uniqueFileName = UploadedFile(photoModel.UploadedImage); MissionPhoto photo = new MissionPhoto { UniquePhotoName = uniqueFileName, UploadDate = DateTime.Now, UploadedBy = photoModel.UploadedBy }; await _imageRepo.Add(photo); } return(Ok("Photo-Upload-Success")); }
public async Task <long[]> UploadSeller([FromForm] ImageUploadDto input) { Seller.Seller seller = await GetCurrentSeller(); return(await ImageManager.UploadImages(seller.Id, input.Files)); }
public async Task <long[]> Upload([FromForm] ImageUploadDto input) { return(await ImageManager.UploadImages(null, input.Files)); }
public IActionResult CreateImage([FromForm] ImageUploadDto image) { return(Ok(_service.CreateImage(image))); }