Basic formatting applied. Unnecessary comments have been removed. Suspicious code is covered by TODO.
56 lines
2.2 KiB
C#
56 lines
2.2 KiB
C#
using System;
|
||
using System.IO;
|
||
using System.Linq;
|
||
|
||
namespace Hcs.ClientApi
|
||
{
|
||
public class HcsFile
|
||
{
|
||
public string FileName { get; private set; }
|
||
public string ContentType { get; private set; }
|
||
public Stream Stream { get; private set; }
|
||
|
||
public long Length => Stream.Length;
|
||
|
||
public HcsFile(string fileName, string contentType, Stream stream)
|
||
{
|
||
FileName = fileName;
|
||
ContentType = contentType ?? throw new ArgumentNullException(nameof(ContentType));
|
||
Stream = stream ?? throw new ArgumentNullException(nameof(stream));
|
||
}
|
||
|
||
/// <summary>
|
||
/// По имени файла возвращает строку MIME Content-Type или null если тип MIME не найден
|
||
/// </summary>
|
||
public static string GetMimeContentTypeForFileName(string fileName)
|
||
{
|
||
if (string.IsNullOrEmpty(fileName)) return null;
|
||
string extension = Path.GetExtension(fileName).ToLower();
|
||
var mimeType = AllowedMimeTypes.FirstOrDefault(x => x.Extension == extension);
|
||
if (mimeType == null) return null;
|
||
return mimeType.ContentType;
|
||
}
|
||
|
||
public record struct MimeType(string Extension, string ContentType);
|
||
|
||
/// <summary>
|
||
/// Типы MIME допустимые для загрузки в ГИС ЖКХ
|
||
/// </summary>
|
||
public static MimeType[] AllowedMimeTypes =
|
||
{
|
||
new MimeType(".pdf", "application/pdf"),
|
||
new MimeType(".xls", "application/excel"),
|
||
new MimeType(".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"),
|
||
new MimeType(".doc", "application/msword"),
|
||
new MimeType(".docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"),
|
||
new MimeType(".rtf", "application/rtf"),
|
||
new MimeType(".jpg", "image/jpeg"),
|
||
new MimeType(".jpeg", "image/jpeg"),
|
||
new MimeType(".tif", "image/tiff"),
|
||
new MimeType(".tiff", "image/tiff")
|
||
// TODO: Проверить комментарий
|
||
// В спецификации есть другие типы файлов .zip, .sgn и т.д.
|
||
};
|
||
}
|
||
}
|