-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10494 from abpframework/EngincanV/blogging-images…
…harp Blogging: Replace System.Drawing with ImageSharp package
- Loading branch information
Showing
4 changed files
with
35 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 11 additions & 7 deletions
18
modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/Files/FileUploadConsts.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
26 changes: 16 additions & 10 deletions
26
modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/Files/ImageFormatHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |