private async Task UploadResizedAsync(IFormFile file, string id, CancellationToken ct) { await using var assetResized = TempAssetFile.Create(file.ToAssetFile()); var resizeOptions = new ResizeOptions { TargetWidth = 128, TargetHeight = 128 }; try { await using (var originalStream = file.OpenReadStream()) { await using (var resizeStream = assetResized.OpenWrite()) { await assetThumbnailGenerator.CreateThumbnailAsync(originalStream, file.ContentType, resizeStream, resizeOptions, ct); } } } catch { throw new ValidationException(T.Get("validation.notAnImage")); } await using (var resizeStream = assetResized.OpenWrite()) { await userPictureStore.UploadAsync(id, resizeStream, ct); } }
public async Task EnhanceAsync(MetadataRequest request) { var file = request.File; if (request.Type != MediaType.Unknown) { return; } var mimeType = file.MimeType; ImageInfo?imageInfo = null; await using (var uploadStream = file.OpenRead()) { imageInfo = await assetThumbnailGenerator.GetImageInfoAsync(uploadStream, mimeType); } if (imageInfo != null) { var isSwapped = imageInfo.Orientation > ImageOrientation.TopLeft; if (isSwapped) { var tempFile = TempAssetFile.Create(file); await using (var uploadStream = file.OpenRead()) { await using (var tempStream = tempFile.OpenWrite()) { await assetThumbnailGenerator.FixOrientationAsync(uploadStream, mimeType, tempStream); } } await using (var tempStream = tempFile.OpenRead()) { imageInfo = await assetThumbnailGenerator.GetImageInfoAsync(tempStream, mimeType) ?? imageInfo; } await file.DisposeAsync(); request.File = tempFile; } } if (imageInfo != null) { request.Type = MediaType.Image; request.Metadata.SetPixelWidth(imageInfo.PixelWidth); request.Metadata.SetPixelHeight(imageInfo.PixelHeight); } }
public async Task EnhanceAsync(UploadAssetCommand command) { if (command.Type == AssetType.Unknown || command.Type == AssetType.Image) { var mimeType = command.File.MimeType; ImageInfo?imageInfo = null; await using (var uploadStream = command.File.OpenRead()) { imageInfo = await assetThumbnailGenerator.GetImageInfoAsync(uploadStream, mimeType); } if (imageInfo != null) { var isSwapped = imageInfo.Orientation > ImageOrientation.TopLeft; if (command.File != null && isSwapped) { var tempFile = TempAssetFile.Create(command.File); await using (var uploadStream = command.File.OpenRead()) { await using (var tempStream = tempFile.OpenWrite()) { await assetThumbnailGenerator.FixOrientationAsync(uploadStream, mimeType, tempStream); } } await using (var tempStream = tempFile.OpenRead()) { imageInfo = await assetThumbnailGenerator.GetImageInfoAsync(tempStream, mimeType) ?? imageInfo; } await command.File.DisposeAsync(); command.File = tempFile; } if (command.Type == AssetType.Unknown || isSwapped) { command.Type = AssetType.Image; command.Metadata.SetPixelWidth(imageInfo.PixelWidth); command.Metadata.SetPixelHeight(imageInfo.PixelHeight); } } } if (command.Tags == null) { return; } if (command.Type == AssetType.Image) { command.Tags.Add("image"); var wh = command.Metadata.GetPixelWidth() + command.Metadata.GetPixelWidth(); if (wh > 2000) { command.Tags.Add("image/large"); } else if (wh > 1000) { command.Tags.Add("image/medium"); } else { command.Tags.Add("image/small"); } } }