20230514
This commit is contained in:
17
anbs_cpfn/Classes/NetFileExtension.cs
Normal file
17
anbs_cpfn/Classes/NetFileExtension.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
namespace anbs_cp.ForNet.Classes;
|
||||
|
||||
/// <summary>
|
||||
/// Класс -- расширение для класса File
|
||||
/// </summary>
|
||||
public class NetFileExtension
|
||||
{
|
||||
/// <summary>
|
||||
/// Получает MIME-тип файла
|
||||
/// </summary>
|
||||
/// <param name="file">Загружаемый файл</param>
|
||||
/// <returns>MIME-тип файла</returns>
|
||||
public static string MIMEType (IFormFile file) =>
|
||||
file.ContentType;
|
||||
}
|
38
anbs_cpfn/Classes/NetFileHash.cs
Normal file
38
anbs_cpfn/Classes/NetFileHash.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using System.Security.Cryptography;
|
||||
|
||||
using anbs_cp.Classes;
|
||||
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
namespace anbs_cp.ForNet.Classes;
|
||||
|
||||
public static class NetFileHash
|
||||
{
|
||||
/// <summary>
|
||||
/// Получение md5-хэша загружаемого файла.
|
||||
/// Взято с https://stackoverflow.com/a/67081012/16469671
|
||||
/// </summary>
|
||||
/// <param name="file">Загружаемый файл</param>
|
||||
/// <returns>Массив хэша</returns>
|
||||
public static FileHash GetFileHash (IFormFile file)
|
||||
{
|
||||
//Создаю md5
|
||||
using MD5 md5 = MD5.Create();
|
||||
|
||||
//Создаю поток для чтения
|
||||
using StreamReader streamReader = new(file.OpenReadStream());
|
||||
|
||||
//Получаю строковый хэш
|
||||
string hash = BitConverter.ToString(md5.ComputeHash(streamReader.BaseStream)).Replace("-", "")
|
||||
.ToLowerInvariant();
|
||||
|
||||
//Создаю результат
|
||||
FileHash fileHash = new();
|
||||
|
||||
//Вношу в него данные
|
||||
fileHash.FromString(hash);
|
||||
|
||||
//Возвращаю результат
|
||||
return fileHash;
|
||||
}
|
||||
}
|
35
anbs_cpfn/Classes/NetTypeConverter.cs
Normal file
35
anbs_cpfn/Classes/NetTypeConverter.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using System.Text.Encodings.Web;
|
||||
|
||||
using Microsoft.AspNetCore.Html;
|
||||
|
||||
namespace anbs_cp.ForNet.Classes;
|
||||
|
||||
/// <summary>
|
||||
/// Расширение конвертера типов на манер Delphi
|
||||
/// </summary>
|
||||
public static class NetTypeConverter
|
||||
{
|
||||
#region Конвернтация IHtmlContent
|
||||
/// <summary>
|
||||
/// Преобразует тип <see cref="IHtmlContent"/> в строку <see cref="string"/>.
|
||||
/// </summary>
|
||||
/// <param name="content">Значение, которое нужно преобразовать.</param>
|
||||
/// <returns><see cref="string"/></returns>
|
||||
public static string HtmlContentToString(IHtmlContent content)
|
||||
{
|
||||
//Создаём writer
|
||||
using StringWriter writer = new();
|
||||
//Конвертируем IHtmlContent в string
|
||||
content.WriteTo(writer, HtmlEncoder.Default);
|
||||
//Возвращаем результат
|
||||
return writer.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Преобразует строку <see cref="string"/> в тип <see cref="IHtmlContent"/>.
|
||||
/// </summary>
|
||||
/// <param name="content">Значение, которое нужно преобразовать.</param>
|
||||
/// <returns><see cref="IHtmlContent"/></returns>
|
||||
public static IHtmlContent StringToHtmlContent(string content) => new HtmlContentBuilder().AppendHtml(content);
|
||||
#endregion
|
||||
}
|
Reference in New Issue
Block a user