Browse Source

Proper dispose.

pull/476/head^2
Sebastian 6 years ago
parent
commit
7c6f686a9d
  1. 16
      backend/src/Squidex.Domain.Apps.Entities/Apps/AppCommandMiddleware.cs
  2. 9
      backend/src/Squidex.Domain.Apps.Entities/Assets/AssetCommandMiddleware.cs
  3. 43
      backend/src/Squidex.Domain.Apps.Entities/Assets/ImageAssetMetadataSource.cs
  4. 22
      backend/src/Squidex.Infrastructure/Assets/ImageSharp/ImageSharpAssetThumbnailGenerator.cs

16
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);
}
}
}
}

9
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();
}
}
}

43
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");
}
}
}
}

22
backend/src/Squidex.Infrastructure/Assets/ImageSharp/ImageSharpAssetThumbnailGenerator.cs

@ -77,19 +77,23 @@ namespace Squidex.Infrastructure.Assets.ImageSharp
public Task<ImageInfo?> 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);
}
}
}

Loading…
Cancel
Save