Files
Chat/ChatController/Utilities/FileUtils.cs

159 lines
4.3 KiB
C#
Raw Normal View History

using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
namespace ChatController.Utilities
{
public class FileUtils
{
public static string ScaleImage(string pFile, string pFormat, long maxUploadSize)
{
2023-03-03 21:23:48 +01:00
var isImageFile = Constants.ImageFileExtensions.Contains(Path.GetExtension(pFile).ToUpperInvariant());
2023-03-03 21:23:48 +01:00
if(!isImageFile)
{
return pFile;
}
2023-03-03 21:23:48 +01:00
byte[] mediaFile;
using(Stream reader = File.OpenRead(pFile))
{
mediaFile = Utils.ReadFully(reader);
}
2023-03-03 21:23:48 +01:00
var memoryStream = new MemoryStream(mediaFile);
var image = Image.FromStream(memoryStream);
2023-03-03 21:23:48 +01:00
float width, height;
2023-03-03 21:23:48 +01:00
if(image.Height < image.Width)
{
if(image.Height > 1080)
{
2023-03-03 21:23:48 +01:00
var factor = (float)image.Height / 1080;
height = 1080;
width = image.Width / factor;
}
2023-03-03 21:23:48 +01:00
else
{
return pFile;
}
2023-03-03 21:23:48 +01:00
}
else if(image.Height < 1080 && image.Width < 1080)
{
return pFile;
}
else
{
if(image.Width > 1080)
{
var factor = (float)image.Width / 1080;
width = 1080;
height = image.Height / factor;
}
else
{
2023-03-03 21:23:48 +01:00
return pFile;
}
2023-03-03 21:23:48 +01:00
}
2023-03-03 21:23:48 +01:00
var scaledBitmap = new Bitmap(image, new Size((int)width, (int)height));
2023-03-03 21:23:48 +01:00
const int orientationId = 0x0112;
2023-03-03 21:23:48 +01:00
if(image.PropertyIdList.Contains(orientationId))
{
var item = image.GetPropertyItem(orientationId);
2023-03-03 21:23:48 +01:00
scaledBitmap.SetPropertyItem(item);
}
2023-03-03 21:23:48 +01:00
using(var graphics = Graphics.FromImage(image))
{
graphics.Clear(Color.Transparent);
2023-03-03 21:23:48 +01:00
graphics.InterpolationMode = InterpolationMode.Low;
2023-03-03 21:23:48 +01:00
graphics.DrawImage(scaledBitmap, (int)width, (int)height);
}
2023-03-03 21:23:48 +01:00
// tmp_img_owch_
var filePath = $"{Path.GetTempPath()}{Guid.NewGuid()}_tmp_img_owch{pFormat}";
2023-03-03 21:23:48 +01:00
if(pFormat.Equals(".JPG") || pFormat.Equals(".JPE") || pFormat.Equals(".JPEG") || pFormat.Equals(".BMP"))
{
scaledBitmap.Save(filePath, ImageFormat.Jpeg);
}
else if(pFormat.Equals(".PNG"))
{
scaledBitmap.Save(filePath, ImageFormat.Png);
}
else if(pFormat.Equals(".GIF"))
{
scaledBitmap.Save(filePath, ImageFormat.Gif);
}
2023-03-03 21:23:48 +01:00
if(File.Exists(filePath))
{
var fileInfo = new FileInfo(filePath);
var fileInfoLength = fileInfo.Length;
if(maxUploadSize < fileInfoLength)
{
2023-03-03 21:23:48 +01:00
File.Delete(filePath);
return string.Empty;
}
}
2023-03-03 21:23:48 +01:00
else
{
2023-03-03 21:23:48 +01:00
return string.Empty;
}
2023-03-03 21:23:48 +01:00
memoryStream.Dispose();
memoryStream.Close();
return filePath;
}
public static bool CheckFileSize(string pathToFile, long maxFileSize)
{
var fileInfo = new FileInfo(pathToFile);
return fileInfo.Length > maxFileSize;
}
public static bool IsFileLocked(string path)
{
if(!File.Exists(path))
{
return false;
}
try
{
var file = new FileInfo(path);
using (var stream = file.Open(FileMode.Open, FileAccess.Read, FileShare.None))
{
stream.Close();
}
}
catch(IOException ioException)
{
Debug.WriteLine($"Error (FileUtils.IsFileLocked): {ioException.Message}");
return true;
}
return false;
}
}
}