mirror of https://github.com/abpframework/abp.git
4 changed files with 35 additions and 19 deletions
@ -1,20 +1,24 @@ |
|||||
using System.Collections.Generic; |
using System.Collections.Generic; |
||||
using System.Collections.ObjectModel; |
using System.Collections.ObjectModel; |
||||
using System.Drawing.Imaging; |
|
||||
using System.Linq; |
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 |
namespace Volo.Blogging.Files |
||||
{ |
{ |
||||
public class FileUploadConsts |
public class FileUploadConsts |
||||
{ |
{ |
||||
public static readonly ICollection<ImageFormat> AllowedImageUploadFormats = new Collection<ImageFormat> |
public static readonly ICollection<IImageFormat> AllowedImageUploadFormats = new Collection<IImageFormat> |
||||
{ |
{ |
||||
ImageFormat.Jpeg, |
JpegFormat.Instance, |
||||
ImageFormat.Png, |
PngFormat.Instance, |
||||
ImageFormat.Gif, |
GifFormat.Instance, |
||||
ImageFormat.Bmp |
BmpFormat.Instance, |
||||
}; |
}; |
||||
|
|
||||
public static string AllowedImageFormatsJoint => string.Join(",", AllowedImageUploadFormats.Select(x => x.ToString())); |
public static string AllowedImageFormatsJoint => string.Join(",", AllowedImageUploadFormats.Select(x => x.Name)); |
||||
} |
} |
||||
} |
} |
||||
|
|||||
@ -1,27 +1,33 @@ |
|||||
using System.Collections.Generic; |
using System; |
||||
using System.Drawing.Imaging; |
using System.Collections.Generic; |
||||
using System.IO; |
using System.IO; |
||||
using System.Runtime.InteropServices; |
using SixLabors.ImageSharp; |
||||
|
using SixLabors.ImageSharp.Formats; |
||||
|
|
||||
namespace Volo.Blogging.Areas.Blog.Helpers |
namespace Volo.Blogging.Areas.Blog.Helpers |
||||
{ |
{ |
||||
public class ImageFormatHelper |
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
|
try |
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) |
|
||||
{ |
{ |
||||
var imageFormat = GetImageRawFormat(stream); |
var imageFormat = GetImageRawFormat(stream); |
||||
|
|
||||
return validFormats.Contains(imageFormat); |
return validFormats.Contains(imageFormat); |
||||
} |
} |
||||
|
catch (Exception e) |
||||
return true; |
{ |
||||
|
return false; |
||||
|
} |
||||
} |
} |
||||
} |
} |
||||
} |
} |
||||
|
|||||
Loading…
Reference in new issue