From 7c6f686a9de36b7249e19cf5b0b603acbbb52103 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Mon, 27 Jan 2020 14:52:27 +0100 Subject: [PATCH] Proper dispose. --- .../Apps/AppCommandMiddleware.cs | 16 ++++--- .../Assets/AssetCommandMiddleware.cs | 9 ++-- .../Assets/ImageAssetMetadataSource.cs | 43 ++++++++++--------- .../ImageSharpAssetThumbnailGenerator.cs | 22 ++++++---- 4 files changed, 53 insertions(+), 37 deletions(-) diff --git a/backend/src/Squidex.Domain.Apps.Entities/Apps/AppCommandMiddleware.cs b/backend/src/Squidex.Domain.Apps.Entities/Apps/AppCommandMiddleware.cs index b9676d94c..7aa3ef993 100644 --- a/backend/src/Squidex.Domain.Apps.Entities/Apps/AppCommandMiddleware.cs +++ b/backend/src/Squidex.Domain.Apps.Entities/Apps/AppCommandMiddleware.cs @@ -58,14 +58,20 @@ namespace Squidex.Domain.Apps.Entities.Apps { var file = uploadImage.File; - var image = await assetThumbnailGenerator.GetImageInfoAsync(file.OpenRead()); - - if (image == null) + using (var uploadStream = file.OpenRead()) { - throw new ValidationException("File is not an image."); + var image = await assetThumbnailGenerator.GetImageInfoAsync(uploadStream); + + if (image == null) + { + throw new ValidationException("File is not an image."); + } } - await appImageStore.UploadAsync(uploadImage.AppId, file.OpenRead()); + using (var uploadStream = file.OpenRead()) + { + await appImageStore.UploadAsync(uploadImage.AppId, uploadStream); + } } } } diff --git a/backend/src/Squidex.Domain.Apps.Entities/Assets/AssetCommandMiddleware.cs b/backend/src/Squidex.Domain.Apps.Entities/Assets/AssetCommandMiddleware.cs index 4a268db7b..abfa36ac4 100644 --- a/backend/src/Squidex.Domain.Apps.Entities/Assets/AssetCommandMiddleware.cs +++ b/backend/src/Squidex.Domain.Apps.Entities/Assets/AssetCommandMiddleware.cs @@ -149,11 +149,14 @@ namespace Squidex.Domain.Apps.Entities.Assets private async Task EnrichWithHashAndUploadAsync(UploadAssetCommand command, string tempFile) { - using (var hashStream = new HasherStream(command.File.OpenRead(), HashAlgorithmName.SHA256)) + using (var uploadStream = command.File.OpenRead()) { - await assetFileStore.UploadAsync(tempFile, hashStream); + using (var hashStream = new HasherStream(uploadStream, HashAlgorithmName.SHA256)) + { + await assetFileStore.UploadAsync(tempFile, hashStream); - command.FileHash = $"{hashStream.GetHashStringAndReset()}{command.File.FileName}{command.File.FileSize}".Sha256Base64(); + command.FileHash = $"{hashStream.GetHashStringAndReset()}{command.File.FileName}{command.File.FileSize}".Sha256Base64(); + } } } diff --git a/backend/src/Squidex.Domain.Apps.Entities/Assets/ImageAssetMetadataSource.cs b/backend/src/Squidex.Domain.Apps.Entities/Assets/ImageAssetMetadataSource.cs index 52541e19a..36f8c924c 100644 --- a/backend/src/Squidex.Domain.Apps.Entities/Assets/ImageAssetMetadataSource.cs +++ b/backend/src/Squidex.Domain.Apps.Entities/Assets/ImageAssetMetadataSource.cs @@ -29,32 +29,35 @@ namespace Squidex.Domain.Apps.Entities.Assets { if (command.Type == AssetType.Unknown) { - var imageInfo = await assetThumbnailGenerator.GetImageInfoAsync(command.File.OpenRead()); - - if (imageInfo != null) + using (var uploadStream = command.File.OpenRead()) { - command.Type = AssetType.Image; - - command.Metadata.SetPixelWidth(imageInfo.PixelWidth); - command.Metadata.SetPixelHeight(imageInfo.PixelHeight); + var imageInfo = await assetThumbnailGenerator.GetImageInfoAsync(uploadStream); - if (tags != null) + if (imageInfo != null) { - tags.Add("image"); + command.Type = AssetType.Image; - var wh = imageInfo.PixelWidth + imageInfo.PixelHeight; + command.Metadata.SetPixelWidth(imageInfo.PixelWidth); + command.Metadata.SetPixelHeight(imageInfo.PixelHeight); - if (wh > 2000) - { - tags.Add("image/large"); - } - else if (wh > 1000) + if (tags != null) { - tags.Add("image/medium"); - } - else - { - tags.Add("image/small"); + tags.Add("image"); + + var wh = imageInfo.PixelWidth + imageInfo.PixelHeight; + + if (wh > 2000) + { + tags.Add("image/large"); + } + else if (wh > 1000) + { + tags.Add("image/medium"); + } + else + { + tags.Add("image/small"); + } } } } diff --git a/backend/src/Squidex.Infrastructure/Assets/ImageSharp/ImageSharpAssetThumbnailGenerator.cs b/backend/src/Squidex.Infrastructure/Assets/ImageSharp/ImageSharpAssetThumbnailGenerator.cs index 57063172e..fc8cc7330 100644 --- a/backend/src/Squidex.Infrastructure/Assets/ImageSharp/ImageSharpAssetThumbnailGenerator.cs +++ b/backend/src/Squidex.Infrastructure/Assets/ImageSharp/ImageSharpAssetThumbnailGenerator.cs @@ -77,19 +77,23 @@ namespace Squidex.Infrastructure.Assets.ImageSharp public Task GetImageInfoAsync(Stream source) { - return Task.Run(() => + ImageInfo? result = null; + + try { - try - { - var image = Image.Load(source); + var image = Image.Identify(source); - return new ImageInfo(image.Width, image.Height); - } - catch + if (image != null) { - return null; + result = new ImageInfo(image.Width, image.Height); } - }); + } + catch + { + result = null; + } + + return Task.FromResult(result); } } }