103 lines
3.3 KiB
C#
103 lines
3.3 KiB
C#
using Microsoft.AspNetCore.Http;
|
|
using System.IO.Compression;
|
|
|
|
namespace ZelWiki.Library
|
|
{
|
|
public static class Utility
|
|
{
|
|
public static int PadVersionString(string versionString, int padLength = 3)
|
|
=> int.Parse(string.Join("", versionString.Split('.').Select(x => x.Trim().PadLeft(padLength, '0'))));
|
|
|
|
public static string SanitizeAccountName(string fileName, char[]? extraInvalidCharacters = null)
|
|
{
|
|
var invalidChars = Path.GetInvalidFileNameChars().ToList();
|
|
|
|
if (extraInvalidCharacters != null)
|
|
{
|
|
invalidChars.AddRange(extraInvalidCharacters);
|
|
}
|
|
|
|
foreach (char invalidChar in invalidChars)
|
|
{
|
|
fileName = fileName.Replace(invalidChar, '_');
|
|
}
|
|
|
|
return fileName.Replace("__", "_").Replace("__", "_");
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="originalWidth"></param>
|
|
/// <param name="originalHeight"></param>
|
|
/// <param name="maxSize"></param>
|
|
/// <returns></returns>
|
|
public static (int Width, int Height) ScaleToMaxOf(int originalWidth, int originalHeight, int maxSize)
|
|
{
|
|
var aspectRatio = (float)originalWidth / originalHeight;
|
|
|
|
int newWidth, newHeight;
|
|
if (originalWidth > originalHeight)
|
|
{
|
|
newWidth = maxSize;
|
|
newHeight = (int)(maxSize / aspectRatio);
|
|
}
|
|
else
|
|
{
|
|
newHeight = maxSize;
|
|
newWidth = (int)(maxSize * aspectRatio);
|
|
}
|
|
|
|
return (newWidth, newHeight);
|
|
}
|
|
|
|
public static List<string> SplitToTokens(string? tokenString)
|
|
{
|
|
var tokens = (tokenString ?? string.Empty).Trim().ToLower()
|
|
.Split([' ', ',', '\t'], StringSplitOptions.RemoveEmptyEntries).Distinct().ToList();
|
|
return tokens;
|
|
}
|
|
|
|
public static string GetMimeType(string fileName)
|
|
{
|
|
MimeTypes.TryGetContentType(fileName, out var contentType);
|
|
return contentType ?? "application/octet-stream";
|
|
}
|
|
|
|
public static byte[] ConvertHttpFileToBytes(IFormFile image)
|
|
{
|
|
using var stream = image.OpenReadStream();
|
|
using var reader = new BinaryReader(stream);
|
|
return reader.ReadBytes((int)image.Length);
|
|
}
|
|
|
|
public static byte[] Compress(byte[]? data)
|
|
{
|
|
if (data == null)
|
|
{
|
|
return [];
|
|
}
|
|
|
|
using var compressedStream = new MemoryStream();
|
|
using (var compressor = new GZipStream(compressedStream, CompressionMode.Compress))
|
|
{
|
|
compressor.Write(data, 0, data.Length);
|
|
}
|
|
|
|
return compressedStream.ToArray();
|
|
}
|
|
|
|
public static byte[] Decompress(byte[] data)
|
|
{
|
|
if (data == null)
|
|
return [];
|
|
|
|
|
|
using var compressedStream = new MemoryStream(data);
|
|
using var decompressor = new GZipStream(compressedStream, CompressionMode.Decompress);
|
|
using var decompressedStream = new MemoryStream();
|
|
decompressor.CopyTo(decompressedStream);
|
|
return decompressedStream.ToArray();
|
|
}
|
|
}
|
|
} |