private async Task UploadImage(ImageUploadJob job) { var handler = new MongoDbFileHandler(_mongoConnectionString); var image = await handler.GetMessageObject <DtoImage>(job.Id); using (var client = new HttpClient()) using (var content = new MultipartFormDataContent()) { content.Add(new StringContent(job.User), "User"); content.Add(new StringContent(job.Message), "Message"); content.Add(new StringContent("devspace2016"), "APIKEY"); content.Add(new ByteArrayContent(image.Data), "Image", image.FileName); await client.PostAsync(_externalAppUrl, content); } }
/// <summary> /// Returns a thumbnails processing params depending on the provided processing profile /// </summary> /// <param name="uploadJob"></param> /// <returns></returns> private async Task <List <ImageThumbnailParamsModel> > GetThumbnailParams(ImageUploadJob uploadJob) { if (!uploadJob.Params.ProfileId.HasValue) { return(uploadJob.Params.Thumbnails); } var profile = await this.processingProfileRepository.GetProfileById(uploadJob.Params.ProfileId.Value); if (profile != null) { return(profile.Parameters); } throw new AppFlowException(AppFlowExceptionType.InvalidParameters, uploadJob.Params.ProfileId.Value.ToString()); }
/// <summary> /// <inheritdoc /> /// </summary> public async Task <ImageUploadResultModel> ProcessThumbnails(ImageUploadJob uploadJob) { var storagePath = this.fileService.PrepareStoragePath(uploadJob.Params.OutputFormat); var result = new ImageUploadResultModel { Id = storagePath.Key }; this.logger.LogInformation($"A new upload with id {result.Id} was created"); // Save original file depending on the app settings if (this.appSettings.Value.Common.SaveOriginalFile) { await this.fileService.WriteFile(uploadJob.Stream, storagePath.FilePath); uploadJob.Stream.Reset(); result.OriginalFileUrl = this.fileService.NormalizeWebPath(storagePath.UrlPath); } // Load a new image and save it since loading it each time is extremely expensive // All image processing routines will clone it and use - it is about 10x times faster using var sourceImage = await this.imageReadingService.Read(uploadJob.Stream); // Process requested thumbnails var processedThumbnails = await this.GenerateThumbnails(uploadJob, sourceImage, storagePath); result.Thumbnails = processedThumbnails; // Send a notification about finished job to all listeners await this.mediator.Send(new ImageProcessingFinishedPayload { Result = result }); // Create a database record await this.imageUploadRepository.Create(storagePath, result.Thumbnails, uploadJob.IpAddress, uploadJob.Params.Lifetime); return(result); }
public async Task <IActionResult> Upload([Required, FromForm] ImageUploadParamsDto paramsDto) { var file = this.GetDefaultFile(); if (file == null) { return(BadRequest()); } var requestParamsModel = this.mapper.Map <ImageUploadParamsModel>(paramsDto); await using var imageStream = file.OpenReadStream(); var job = new ImageUploadJob { IpAddress = IpAddress, Params = requestParamsModel, Stream = imageStream }; var response = await this.imageThumbnailService.ProcessThumbnails(job); return(Ok(new Response <ImageUploadResultModel>(response))); }
/// <summary> /// Performs an image handling (resizing, effects, etc) and flushing to disk /// </summary> /// <param name="uploadJob"></param> /// <param name="sourceImage"></param> /// <param name="storagePath"></param> /// <returns></returns> private async Task <List <ImageThumbnailResultModel> > GenerateThumbnails( ImageUploadJob uploadJob, MagickImage sourceImage, StoragePathModel storagePath) { var resultsList = new List <ImageThumbnailResultModel>(); var thumbnailParams = await GetThumbnailParams(uploadJob); foreach (var thumbParam in thumbnailParams) { this.logger.LogInformation($"Begin a thumbnail processing with params ({thumbParam.Width}, {thumbParam.Height})"); // Prepare an image processing job var job = new ImageProcessingJob { Image = sourceImage, Settings = this.appSettings.Value.ImageProcessing, ThumbnailParams = thumbParam }; // Run an image processing job var processedImage = this.imageProcessingService.Process(job); // If watermark parameters were provided then apply a watermark if (thumbParam.Watermark != null) { using var watermarkImage = await this.imageReadingService.Read(thumbParam.Watermark.Url); processedImage = this.imageProcessingService.AddWatermark(processedImage, watermarkImage); } // Prepare disk paths to be used to save images var paths = this.fileService.PrepareThumbFilePath(storagePath, uploadJob.Params.OutputFormat, processedImage.Width, processedImage.Height); // Quality can be defined for each thumbnail individually // Otherwise a JPEG quality value will be loaded from the application settings or app default value var quality = thumbParam.Quality ?? this.appSettings.Value.ImageProcessing.JpegQuality ?? Constants.DefaultJpegQuality; // Save a processed image to the disk await this.imageWritingService.WriteToDisk(processedImage, new DiskImageSavingParamsModel { Format = uploadJob.Params.OutputFormat, Path = paths.DiskPath, Quality = quality }); this.logger.LogInformation($"Thumbnail processing finished. File saved as {paths.DiskPath}"); var thumbnailResult = new ImageThumbnailResultModel { Width = processedImage.Width, Height = processedImage.Height, Url = paths.Url, DiskPath = paths.DiskPath }; processedImage.Dispose(); resultsList.Add(thumbnailResult); } return(resultsList); }
public ImageModule(IFileHandler fileUploadHandler, MessageRepository messageRepo, ImageRepository imageRepo) { Get("/imagerequest", args => { var identity = this.Context.CurrentUser; if (identity == null) { return(View["Login"]); } else { return(View["ImageRequest"]); } }); Post("/imagerequest", async args => { var request = this.Bind <ImageRequest>(); var uploadResult = await fileUploadHandler.HandleUpload(request.File.Name, request.File.Value); var identity = this.Context.CurrentUser; var userCookie = Request.Cookies.ContainsKey("picflow_webapp_username") ? Request.Cookies["picflow_webapp_username"] : string.Empty; if (request.PostImage) { var procJob = new ImageProcessingJob { Id = uploadResult.Identifier, Overlay = request.EventOverlay, Resolution = 2 }; var uploadJob = new ImageUploadJob { User = userCookie, Message = request.Message }; procJob.Successors.Add(uploadJob); await messageRepo.SendImageProcessingJob(procJob); } foreach (var resolution in request.Resolutions) { var procJob = new ImageProcessingJob { Id = uploadResult.Identifier, Overlay = request.EventOverlay, Resolution = resolution }; var saveJob = new ImageSaveJob { UserId = Guid.Parse(identity.Identity.Name), Message = request.Message, SourceId = uploadResult.Identifier, Resolution = resolution }; procJob.Successors.Add(saveJob); await messageRepo.SendImageProcessingJob(procJob); } var model = new Home { Message = $"Auftrag mit Bild {request.File?.Name} wurde gestartet" }; model.Jobs = await imageRepo.GetProcessingJobs(Guid.Parse(identity.Identity.Name)); return(View["Home", model]); }); }
public Task SendUploadJob(ImageUploadJob job) { return(_bus.PublishAsync(job)); }