20221203
This commit is contained in:
71
anbs_cp/Classes/FileExtensions.cs
Normal file
71
anbs_cp/Classes/FileExtensions.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
using System.Security.Cryptography;
|
||||
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
using MimeKit;
|
||||
|
||||
namespace anbs_cp.Classes;
|
||||
|
||||
/// <summary>
|
||||
/// Класс -- расширение для класса File
|
||||
/// </summary>
|
||||
public static class FileExtension
|
||||
{
|
||||
/// <summary>
|
||||
/// Получение md5-хэша файла.
|
||||
/// Взято с https://stackoverflow.com/a/24031467/16469671
|
||||
/// </summary>
|
||||
/// <param name="fileName">Имя файла</param>
|
||||
/// <returns>Массив хэша</returns>
|
||||
public static byte[] Hash (string fileName)
|
||||
{
|
||||
using MD5 md5 = MD5.Create();
|
||||
byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(fileName);
|
||||
byte[] result = md5.ComputeHash(inputBytes);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Получение md5-хэша загружаемого файла.
|
||||
/// Взято с https://stackoverflow.com/a/67081012/16469671
|
||||
/// </summary>
|
||||
/// <param name="file">Загружаемый файл</param>
|
||||
/// <returns>Массив хэша</returns>
|
||||
public static byte[] Hash (IFormFile file)
|
||||
{
|
||||
using MD5 md5 = MD5.Create();
|
||||
using StreamReader streamReader = new(file.OpenReadStream());
|
||||
return md5.ComputeHash(streamReader.BaseStream);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Получение md5-хэша файла и вывод в строке.
|
||||
/// </summary>
|
||||
/// <param name="fileName">Имя файла</param>
|
||||
/// <returns>Срока с хэшем файла</returns>
|
||||
public static string HashString (string fileName) => Convert.ToHexString(Hash(fileName));
|
||||
|
||||
/// <summary>
|
||||
/// Получение md5-хэша файла и вывод в строке.
|
||||
/// </summary>
|
||||
/// <param name="file">Загружаемый файл</param>
|
||||
/// <returns>Срока с хэшем файла</returns>
|
||||
public static string HashString (IFormFile file) => Convert.ToHexString(Hash(file));
|
||||
|
||||
/// <summary>
|
||||
/// Получает MIME-тип файла
|
||||
/// </summary>
|
||||
/// <param name="filename">Имя файла</param>
|
||||
/// <returns>MIME-тип файла</returns>
|
||||
public static string MIMEType (string filename) =>
|
||||
MimeTypes.GetMimeType(filename);
|
||||
|
||||
/// <summary>
|
||||
/// Получает MIME-тип файла
|
||||
/// </summary>
|
||||
/// <param name="file">Загружаемый файл</param>
|
||||
/// <returns>MIME-тип файла</returns>
|
||||
public static string MIMEType (IFormFile file) =>
|
||||
file.ContentType;
|
||||
}
|
@@ -2,7 +2,7 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<Version>1.2022.1130</Version>
|
||||
<Version>2022.1203.1</Version>
|
||||
<Authors>Alexander Babaev</Authors>
|
||||
<Product>ANB Software Components Pack</Product>
|
||||
<Description>Library of some useful functions in C# language.</Description>
|
||||
@@ -15,8 +15,8 @@
|
||||
<SignAssembly>True</SignAssembly>
|
||||
<PackageProjectUrl>https://git.babaev-an.ru/babaev-an/anbsoftware_componentspack</PackageProjectUrl>
|
||||
<RepositoryUrl>https://git.babaev-an.ru/babaev-an/anbsoftware_componentspack</RepositoryUrl>
|
||||
<AssemblyVersion>1.2022.1130</AssemblyVersion>
|
||||
<FileVersion>1.2022.1130</FileVersion>
|
||||
<AssemblyVersion>2022.1203.1</AssemblyVersion>
|
||||
<FileVersion>2022.1203.1</FileVersion>
|
||||
<PackageId>ANBSoftware.ComponentsPack</PackageId>
|
||||
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
||||
<AnalysisLevel>6.0</AnalysisLevel>
|
||||
@@ -27,16 +27,20 @@
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
|
||||
<WarningLevel>7</WarningLevel>
|
||||
<CheckForOverflowUnderflow>True</CheckForOverflowUnderflow>
|
||||
<DebugType>none</DebugType>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
|
||||
<WarningLevel>7</WarningLevel>
|
||||
<CheckForOverflowUnderflow>True</CheckForOverflowUnderflow>
|
||||
<DebugType>none</DebugType>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Html.Abstractions" Version="2.2.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Http.Features" Version="5.0.17" />
|
||||
<PackageReference Include="Microsoft.VisualStudio.Shell.Interop" Version="17.4.33103.184" />
|
||||
<PackageReference Include="MimeKitLite" Version="3.4.3" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
Reference in New Issue
Block a user