public async Task <IActionResult> UploadImage([FromForm] UploadImageRequest request) { if (request.Name == null) { return(ControllerResultWithToast.FailureWithResourceMesg("NameNotSet", this)); } if (request.Description == null) { return(ControllerResultWithToast.FailureWithResourceMesg("DescriptionNotSet", this)); } if (request.Source == null) { return(ControllerResultWithToast.FailureWithResourceMesg("SourceNotSet", this)); } if (request.File == null) { return(ControllerResultWithToast.FailureWithResourceMesg("FileNotSet", this)); } var userId = await UserServices.UserIdFromContextAsync(HttpContext, userManager); using var stream = request.File.OpenReadStream(); using var reader = new BinaryReader(stream); var fileContent = reader.ReadBytes((int)request.File.Length); var applicationRequest = new StoreImage.Request(userId, request.Name.Trim(), request.Description.Trim(), request.Source.Trim(), request.File.ContentType, fileContent); await new StoreImage(callContext).RunAsync(applicationRequest); return(ControllerResultWithToast.Success($"{Get("ImageSavedWithName")} '{applicationRequest.Name.Trim()}'", this)); }
public async Task <IActionResult> UploadImage([FromForm] UploadImageRequest request) { if (request.Name == null) { return(BadRequest(UploadImageResultViewModel.Failure(localizer["NameNotSet"].Value, false, localizer))); } if (request.Description == null) { return(BadRequest(UploadImageResultViewModel.Failure(localizer["DescriptionNotSet"].Value, false, localizer))); } if (request.Source == null) { return(BadRequest(UploadImageResultViewModel.Failure(localizer["SourceNotSet"].Value, false, localizer))); } if (request.File == null) { return(BadRequest(UploadImageResultViewModel.Failure(localizer["FileNotSet"].Value, false, localizer))); } try { var user = await userManager.GetUserAsync(HttpContext.User); if (user == null) { return(BadRequest(UploadImageResultViewModel.Failure(localizer["NeedLogin"].Value, false, localizer))); } using (var stream = request.File.OpenReadStream()) using (var reader = new BinaryReader(stream)) { var fileContent = reader.ReadBytes((int)request.File.Length); var applicationRequest = new StoreImage.Request(user, request.Name, request.Description, request.Source, request.File.ContentType, fileContent); var id = await new StoreImage(dbContext, localizer).RunAsync(applicationRequest); if (id == Guid.Empty) { throw new ApplicationException("Stored image with empty GUID as id"); } return(Ok(UploadImageResultViewModel.Success(localizer["ImageSavedWithName"].Value + $" '{applicationRequest.Name}'", localizer))); } } catch (Exception e) { return(ControllerError.BadRequest(e, this)); } }
private async Task InsertImageInDbAsync(Region region, MemCheckUser user) { if (region.GetImageDbId(dbContext) != Guid.Empty) { logger.LogDebug($"Image for region '{region.Name}' already in DB"); return; } logger.LogDebug($"Inserting image in DB for region '{region.Name}'"); using var stream = File.OpenRead(Path.Combine(sourceDir, region.ImageFileName)); using var reader = new BinaryReader(stream); var blob = reader.ReadBytes((int)stream.Length); var request = new StoreImage.Request(user.Id, region.ImageDbName, $"Région {region.Name} dans la carte de France", region.ImageSource, "image/svg+xml", blob); await new StoreImage(dbContext.AsCallContext()).RunAsync(request); }