Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blogging: Replace System.Drawing with ImageSharp package #10494

Merged
merged 1 commit into from
Nov 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Drawing.Common" Version="4.7.0" />
<PackageReference Include="SixLabors.ImageSharp" Version="1.0.4" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Volo.Blogging.Admin.Application.Contracts\Volo.Blogging.Admin.Application.Contracts.csproj" />
<ProjectReference Include="..\Volo.Blogging.Domain\Volo.Blogging.Domain.csproj" />
<ProjectReference Include="..\..\..\..\framework\src\Volo.Abp.AutoMapper\Volo.Abp.AutoMapper.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
<PackageId>Volo.Blogging.Application</PackageId>
<RootNamespace />
</PropertyGroup>

<ItemGroup>
<PackageReference Include="SixLabors.ImageSharp" Version="1.0.4" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.Drawing.Common" Version="$(MicrosoftPackageVersion)" />
<ProjectReference Include="..\Volo.Blogging.Application.Contracts\Volo.Blogging.Application.Contracts.csproj" />
<ProjectReference Include="..\Volo.Blogging.Domain\Volo.Blogging.Domain.csproj" />
<ProjectReference Include="..\..\..\..\framework\src\Volo.Abp.AutoMapper\Volo.Abp.AutoMapper.csproj" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing.Imaging;
using System.Linq;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.Formats.Bmp;
using SixLabors.ImageSharp.Formats.Gif;
using SixLabors.ImageSharp.Formats.Jpeg;
using SixLabors.ImageSharp.Formats.Png;

namespace Volo.Blogging.Files
{
public class FileUploadConsts
{
public static readonly ICollection<ImageFormat> AllowedImageUploadFormats = new Collection<ImageFormat>
public static readonly ICollection<IImageFormat> AllowedImageUploadFormats = new Collection<IImageFormat>
{
ImageFormat.Jpeg,
ImageFormat.Png,
ImageFormat.Gif,
ImageFormat.Bmp
JpegFormat.Instance,
PngFormat.Instance,
GifFormat.Instance,
BmpFormat.Instance,
};

public static string AllowedImageFormatsJoint => string.Join(",", AllowedImageUploadFormats.Select(x => x.ToString()));
public static string AllowedImageFormatsJoint => string.Join(",", AllowedImageUploadFormats.Select(x => x.Name));
}
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
using System.Collections.Generic;
using System.Drawing.Imaging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats;

namespace Volo.Blogging.Areas.Blog.Helpers
{
public class ImageFormatHelper
{
public static ImageFormat GetImageRawFormat(Stream stream)
public static IImageFormat GetImageRawFormat(Stream stream)
{
return System.Drawing.Image.FromStream(stream).RawFormat;
using (var image = Image.Load(stream, out var imageFormat))
{
return imageFormat;
}
}

public static bool IsValidImage(Stream stream, ICollection<ImageFormat> validFormats)
public static bool IsValidImage(Stream stream, ICollection<IImageFormat> validFormats)
{
// System.Drawing only works on windows => https://docs.microsoft.com/en-us/dotnet/api/system.drawing.image?view=net-5.0#remarks
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
try
{
var imageFormat = GetImageRawFormat(stream);

return validFormats.Contains(imageFormat);
}

return true;
catch (Exception e)
{
return false;
}
}
}
}