public async Task <ActionResult <PropertyImageDto> > Create(CancellationToken cancellationToken) { if (Request.Form is null) { throw new AppException("form is null"); } if (Request.Form.Files.Count != 1) { throw new AppException("files are not set OR more than one"); } var image = await GetImageFromFormData(cancellationToken); var file = _entityService.AsQueryable(r => r.Id == image.Id).FirstOrDefault(); var imagePath = _pathProvider.GetImagePhysicalPath <PropertyImage>(nameof(PropertyImageDto.ImageContentFull), file.Id.ToString()); var tumbPath = _pathProvider.GetImagePhysicalPath <PropertyImage>(nameof(PropertyImageDto.ImageContentTumblr), file.Id.ToString()); _uploadHelperService.ImageService.SaveImage(image.ImageContentFull, imagePath); _uploadHelperService.ImageService.SaveSmallerImage(image.ImageContentTumblr, tumbPath); file.ImagePath = _pathProvider.GetImageVirtualPath <PropertyImage>(nameof(PropertyImageDto.ImageContentFull), file.Id.ToString()); file.TumbPath = _pathProvider.GetImageVirtualPath <PropertyImage>(nameof(PropertyImageDto.ImageContentTumblr), file.Id.ToString()); _entityService.DbContext.Entry(file).State = EntityState.Modified; await _entityService.DbContext.SaveChangesAsync(); var property = await _propertyService.GetAsync(file.PropertyId, cancellationToken); if (property != null && property.IsPublished) { property.IsPublished = false; await _propertyService.UpdateAsync(property, cancellationToken); } return(new PropertyImageDto { Id = file.Id, PropertyId = file.PropertyId, UploadDate = file.UploadDate, ImageCaption = file.ImageCaption, ImageSize = file.ImageSize, ImageExtension = file.ImageExtension, ImagePath = file.ImagePath, TumbPath = file.TumbPath, Is360View = file.Is360View, Priority = file.Priority }); }
public async Task <ActionResult> UpdatePicture(CancellationToken cancellationToken) { if (Request.Form is null) { throw new AppException("form is null"); } if (Request.Form.Files.Count != 1) { throw new AppException("files are not set OR more than one"); } var userId = int.Parse(Request.Form["userId"]); var user = await ModelService.GetAsync(userId, cancellationToken); if (user == null) { throw new AppException("Not found UserId"); } var file = Request.Form.Files[0]; var fileInfo = _uploadHelperService.FileService.GetFileInfo(file.FileName, file.Length); if (!_uploadHelperService.ImageService.IsValidImageExtension(fileInfo.FileExtension)) { throw new InvalidOperationException( $"The extension {fileInfo.FileExtension} is not valid for image"); } var imageFull = await _uploadHelperService .ImageService.GetImageBytes(file.OpenReadStream(), cancellationToken); var imageTumb = await _uploadHelperService .ImageService.GetSmallerImageBytes(file.OpenReadStream(), cancellationToken); var imagePath = _pathProvider.GetImagePhysicalPath <UserAccount>("User", user.Id.ToString()); var tumbPath = _pathProvider.GetImagePhysicalPath <UserAccount>("UserTumb", user.Id.ToString()); _uploadHelperService.ImageService.SaveImage(imageFull, imagePath); _uploadHelperService.ImageService.SaveSmallerImage(imageTumb, tumbPath); user.UserPicture = _pathProvider.GetImageVirtualPath <UserAccount>("User", user.Id.ToString()); user.UserPictureTumblr = _pathProvider.GetImageVirtualPath <UserAccount>("UserTumb", user.Id.ToString()); ModelService.DbContext.Entry(user).State = EntityState.Modified; await ModelService.DbContext.SaveChangesAsync(); return(Ok()); }
public async Task <ActionResult <PropertyFloorPlanDto> > SetPropertyFloorPlanImage(CancellationToken cancellationToken) { var file = Request.Form.Files[0]; if (file == null || file.Length <= 0) { throw new AppException("file is not set"); } var propertyFloorPlanId = int.Parse(Request.Form["propertyFloorPlanId"]); var floorPlan = await ModelService.GetAsync(propertyFloorPlanId, cancellationToken); if (floorPlan == null) { throw new AppException("not found floor plan"); } var fileInfo = _uploadHelperService.FileService.GetFileInfo(file.FileName, file.Length); var imageFull = await _uploadHelperService .ImageService.GetImageBytes(file.OpenReadStream(), cancellationToken); var imageTumb = await _uploadHelperService .ImageService.GetSmallerImageBytes(file.OpenReadStream(), cancellationToken); var imagePath = _pathProvider.GetImagePhysicalPath <PropertyFloorPlan>(nameof(PropertyFloorPlanDto.ImageFull), floorPlan.Id.ToString()); var tumbPath = _pathProvider.GetImagePhysicalPath <PropertyFloorPlan>(nameof(PropertyFloorPlanDto.ImageTumb), floorPlan.Id.ToString()); _uploadHelperService.ImageService.SaveImage(imageFull, imagePath); _uploadHelperService.ImageService.SaveSmallerImage(imageTumb, tumbPath); floorPlan.ImagePath = _pathProvider.GetImageVirtualPath <PropertyFloorPlan>(nameof(PropertyFloorPlanDto.ImageFull), floorPlan.Id.ToString()); floorPlan.TumbPath = _pathProvider.GetImageVirtualPath <PropertyFloorPlan>(nameof(PropertyFloorPlanDto.ImageTumb), floorPlan.Id.ToString()); floorPlan.UploadDate = DateTime.UtcNow; floorPlan.ImageExtension = fileInfo.FileExtension; floorPlan.ImageSize = fileInfo.FileSize; await ModelService.UpdateAsync(floorPlan, cancellationToken); return(new PropertyFloorPlanDto { Id = floorPlan.Id, PropertyId = floorPlan.PropertyId, FloorName = floorPlan.FloorName, FloorPrice = floorPlan.FloorPrice, PricePostfix = floorPlan.PricePostfix, FloorSize = floorPlan.FloorSize, SizePostfix = floorPlan.SizePostfix, Bedrooms = floorPlan.Bedrooms, Bathrooms = floorPlan.Bathrooms, PlanDescription = floorPlan.PlanDescription, ImageCaption = floorPlan.ImageCaption, UploadDate = floorPlan.UploadDate, ImageExtension = fileInfo.FileExtension, ImageSize = fileInfo.FileSize, ImagePath = floorPlan.ImagePath, TumbPath = floorPlan.TumbPath, Is360View = floorPlan.Is360View, }); }