71 lines
2.2 KiB
C#
71 lines
2.2 KiB
C#
using CPRNIMS.Infrastructure.ViewModel;
|
|
using SixLabors.ImageSharp.Formats.Jpeg;
|
|
using SixLabors.ImageSharp.Formats.Png;
|
|
using SixLabors.ImageSharp.Formats;
|
|
using SixLabors.ImageSharp;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
namespace CPRNIMS.Core.Facades
|
|
{
|
|
public class FacadeAttachment
|
|
{
|
|
public AttachmentVM SaveAttachment(byte[] contentBytes, string filePath,
|
|
IImageEncoder imageEncoder, string fileName, int extesionId)
|
|
{
|
|
using (var image = Image.Load(contentBytes))
|
|
{
|
|
image.Save("wwwroot/" + filePath, imageEncoder);
|
|
}
|
|
|
|
var attachment = new AttachmentVM
|
|
{
|
|
URL = filePath,
|
|
FileName = fileName,
|
|
ExtesionId = extesionId
|
|
};
|
|
return attachment;
|
|
}
|
|
public (IImageFormat imageFormat, IImageEncoder imageEncoder, string)
|
|
GetImageFormatAndEncoder(string contentValueBytes)
|
|
{
|
|
IImageFormat imageFormat;
|
|
IImageEncoder imageEncoder;
|
|
|
|
if (contentValueBytes.StartsWith("data:image/png"))
|
|
{
|
|
imageFormat = PngFormat.Instance;
|
|
imageEncoder = new PngEncoder();
|
|
}
|
|
else if (contentValueBytes.StartsWith("data:image/jpeg"))
|
|
{
|
|
imageFormat = JpegFormat.Instance;
|
|
imageEncoder = new JpegEncoder();
|
|
}
|
|
else
|
|
{
|
|
return (null, null, "UnsupportedFormat"); // Handle other formats if needed
|
|
}
|
|
|
|
return (imageFormat, imageEncoder, "Format is valid");
|
|
}
|
|
|
|
public (bool, string) CheckFileSize(byte[] contentValueBytes, int maxSizeInBytes)
|
|
{
|
|
using (var image = Image.Load(contentValueBytes))
|
|
{
|
|
// Check image size
|
|
if (contentValueBytes.Length > maxSizeInBytes)
|
|
{
|
|
return (false, "InValidFileSize"); // Image size exceeds the limit
|
|
}
|
|
return (true, "ValidFileSize");
|
|
}
|
|
}
|
|
}
|
|
}
|