Class FileTypes
- Namespace
- HeroKit.FileTypes
- Assembly
- HeroKit.FileTypes.dll
The built-in file types and shortcuts to the shared Default registry and to UploadValidator.
public static class FileTypes
- Inheritance
-
FileTypes
- Inherited Members
Examples
using var stream = File.OpenRead("photo.bin");
FileTypes.Detect(stream)?.MimeType; // "image/png"
FileTypes.FromExtension("report.docx")?.Name; // "Word document"
FileTypes.GetMimeType("archive.tar.gz"); // "application/gzip"
FileTypes.CheckUpload(stream, "photo.png").IsSafe;
Remarks
The static methods use Default. Inject a FileTypeRegistry or an UploadValidator instead when you want your own set of types or depend on them through constructor injection.
Fields
DefaultMimeType
The MIME type for unknown binary content: "application/octet-stream".
public const string DefaultMimeType = "application/octet-stream"
Field Value
HeaderSize
The number of bytes read from the start of a stream for detection: 16 KB.
public const int HeaderSize = 16384
Field Value
Properties
Aac
AAC audio (audio/aac).
public static FileType Aac { get; }
Property Value
All
public static IReadOnlyList<FileType> All { get; }
Property Value
Apk
Android package (application/vnd.android.package-archive).
public static FileType Apk { get; }
Property Value
Avi
AVI video (video/x-msvideo).
public static FileType Avi { get; }
Property Value
Avif
AVIF image (image/avif).
public static FileType Avif { get; }
Property Value
Batch
Windows batch file (application/x-bat).
public static FileType Batch { get; }
Property Value
Bmp
BMP image (image/bmp).
public static FileType Bmp { get; }
Property Value
BuiltIn
Gets the built-in types in detection order: specific types before the types they are built on, plain text last.
public static IReadOnlyList<FileType> BuiltIn { get; }
Property Value
Bzip2
Bzip2 archive (application/x-bzip2).
public static FileType Bzip2 { get; }
Property Value
Calendar
iCalendar file (text/calendar).
public static FileType Calendar { get; }
Property Value
CompoundFile
OLE compound file: the container of legacy Office files (DOC, XLS, PPT), Outlook messages and MSI installers. The header does not show which one.
public static FileType CompoundFile { get; }
Property Value
Css
CSS stylesheet (text/css).
public static FileType Css { get; }
Property Value
Csv
CSV file (text/csv).
public static FileType Csv { get; }
Property Value
Default
Gets the shared registry with the built-in types and the types added with Register(FileType).
public static FileTypeRegistry Default { get; }
Property Value
Dex
Android Dalvik executable (application/vnd.android.dex).
public static FileType Dex { get; }
Property Value
Doc
Word 97-2003 document (application/msword).
public static FileType Doc { get; }
Property Value
Docm
Macro-enabled Word document (application/vnd.ms-word.document.macroenabled.12). Recognized by its VBA project entry; HasMacros is true.
public static FileType Docm { get; }
Property Value
Docx
Word document (application/vnd.openxmlformats-officedocument.wordprocessingml.document).
public static FileType Docx { get; }
Property Value
Elf
ELF executable (application/x-executable).
public static FileType Elf { get; }
Property Value
Epub
EPUB e-book (application/epub+zip).
public static FileType Epub { get; }
Property Value
Flac
FLAC audio (audio/flac).
public static FileType Flac { get; }
Property Value
Flv
Flash video (video/x-flv).
public static FileType Flv { get; }
Property Value
Gif
GIF image (image/gif).
public static FileType Gif { get; }
Property Value
Gltf
glTF model (model/gltf+json).
public static FileType Gltf { get; }
Property Value
GltfBinary
glTF binary model (model/gltf-binary).
public static FileType GltfBinary { get; }
Property Value
Gzip
Gzip archive (application/gzip).
public static FileType Gzip { get; }
Property Value
Heic
HEIC image (image/heic).
public static FileType Heic { get; }
Property Value
Heif
HEIF image (image/heif).
public static FileType Heif { get; }
Property Value
Html
HTML document. HTML can run script in a browser, so it is in the Script category and is rejected by the default upload policy.
public static FileType Html { get; }
Property Value
Icon
Windows icon (image/vnd.microsoft.icon).
public static FileType Icon { get; }
Property Value
Jar
Java archive (application/java-archive).
public static FileType Jar { get; }
Property Value
JavaClass
Java class file (application/java-vm).
public static FileType JavaClass { get; }
Property Value
JavaScript
JavaScript (text/javascript).
public static FileType JavaScript { get; }
Property Value
Jpeg
JPEG image (image/jpeg).
public static FileType Jpeg { get; }
Property Value
JpegXl
JPEG XL image (image/jxl).
public static FileType JpegXl { get; }
Property Value
Json
JSON document (application/json).
public static FileType Json { get; }
Property Value
M4a
MPEG-4 audio (audio/mp4).
public static FileType M4a { get; }
Property Value
M4v
MPEG-4 video (iTunes) (video/x-m4v).
public static FileType M4v { get; }
Property Value
MachO
Mach-O executable (application/x-mach-binary).
public static FileType MachO { get; }
Property Value
Markdown
Markdown document (text/markdown).
public static FileType Markdown { get; }
Property Value
Matroska
Matroska video (video/x-matroska).
public static FileType Matroska { get; }
Property Value
Midi
MIDI audio (audio/midi).
public static FileType Midi { get; }
Property Value
Mp3
MP3 audio (audio/mpeg).
public static FileType Mp3 { get; }
Property Value
Mp4
MPEG-4 video (video/mp4).
public static FileType Mp4 { get; }
Property Value
Mpeg
MPEG video (video/mpeg).
public static FileType Mpeg { get; }
Property Value
Msg
Outlook message (application/vnd.ms-outlook).
public static FileType Msg { get; }
Property Value
Msi
Windows installer (application/x-msi).
public static FileType Msi { get; }
Property Value
Odp
OpenDocument presentation (application/vnd.oasis.opendocument.presentation).
public static FileType Odp { get; }
Property Value
Ods
OpenDocument spreadsheet (application/vnd.oasis.opendocument.spreadsheet).
public static FileType Ods { get; }
Property Value
Odt
OpenDocument text (application/vnd.oasis.opendocument.text).
public static FileType Odt { get; }
Property Value
Ogg
Ogg audio (audio/ogg).
public static FileType Ogg { get; }
Property Value
OpenType
OpenType font (font/otf).
public static FileType OpenType { get; }
Property Value
Parquet
Apache Parquet file (application/vnd.apache.parquet).
public static FileType Parquet { get; }
Property Value
PDF document (application/pdf).
public static FileType Pdf { get; }
Property Value
Php
PHP script (application/x-httpd-php).
public static FileType Php { get; }
Property Value
PlainText
Plain text: UTF-8 without control characters, or text with a UTF-16 byte order mark. Detected last, when no other type matches.
public static FileType PlainText { get; }
Property Value
Png
PNG image (image/png).
public static FileType Png { get; }
Property Value
PowerShell
PowerShell script (application/x-powershell).
public static FileType PowerShell { get; }
Property Value
Ppt
PowerPoint 97-2003 presentation (application/vnd.ms-powerpoint).
public static FileType Ppt { get; }
Property Value
Pptm
Macro-enabled PowerPoint presentation (application/vnd.ms-powerpoint.presentation.macroenabled.12). Recognized by its VBA project entry; HasMacros is true.
public static FileType Pptm { get; }
Property Value
Pptx
PowerPoint presentation (application/vnd.openxmlformats-officedocument.presentationml.presentation).
public static FileType Pptx { get; }
Property Value
Psd
Photoshop document (image/vnd.adobe.photoshop).
public static FileType Psd { get; }
Property Value
QuickTime
QuickTime video (video/quicktime).
public static FileType QuickTime { get; }
Property Value
Rar
RAR archive (application/vnd.rar).
public static FileType Rar { get; }
Property Value
Rtf
Rich Text Format document (application/rtf).
public static FileType Rtf { get; }
Property Value
SevenZip
7-Zip archive (application/x-7z-compressed).
public static FileType SevenZip { get; }
Property Value
ShellScript
Shell script. Content starting with "#!" (a shebang line) is detected as this type, whatever the interpreter.
public static FileType ShellScript { get; }
Property Value
Sqlite
SQLite database (application/vnd.sqlite3).
public static FileType Sqlite { get; }
Property Value
Svg
SVG image. SVG is XML and can contain script; UploadValidator rejects SVG with script, event handlers or external content.
public static FileType Svg { get; }
Property Value
Tar
Tar archive (application/x-tar).
public static FileType Tar { get; }
Property Value
ThreeGp
3GPP video (video/3gpp).
public static FileType ThreeGp { get; }
Property Value
Tiff
TIFF image (image/tiff).
public static FileType Tiff { get; }
Property Value
TrueType
TrueType font (font/ttf).
public static FileType TrueType { get; }
Property Value
Tsv
TSV file (text/tab-separated-values).
public static FileType Tsv { get; }
Property Value
VCard
vCard file (text/vcard).
public static FileType VCard { get; }
Property Value
VbScript
VBScript file (text/vbscript).
public static FileType VbScript { get; }
Property Value
Wav
WAV audio (audio/wav).
public static FileType Wav { get; }
Property Value
WebAssembly
WebAssembly module (application/wasm).
public static FileType WebAssembly { get; }
Property Value
WebM
WebM video (video/webm).
public static FileType WebM { get; }
Property Value
WebP
WebP image (image/webp).
public static FileType WebP { get; }
Property Value
WindowsExecutable
Windows executable (application/vnd.microsoft.portable-executable).
public static FileType WindowsExecutable { get; }
Property Value
WindowsShortcut
Windows shortcut (application/x-ms-shortcut).
public static FileType WindowsShortcut { get; }
Property Value
Woff
WOFF font (font/woff).
public static FileType Woff { get; }
Property Value
Woff2
WOFF2 font (font/woff2).
public static FileType Woff2 { get; }
Property Value
Xls
Excel 97-2003 workbook (application/vnd.ms-excel).
public static FileType Xls { get; }
Property Value
Xlsm
Macro-enabled Excel workbook (application/vnd.ms-excel.sheet.macroenabled.12). Recognized by its VBA project entry; HasMacros is true.
public static FileType Xlsm { get; }
Property Value
Xlsx
Excel workbook (application/vnd.openxmlformats-officedocument.spreadsheetml.sheet).
public static FileType Xlsx { get; }
Property Value
Xml
XML document (application/xml).
public static FileType Xml { get; }
Property Value
Xz
XZ archive (application/x-xz).
public static FileType Xz { get; }
Property Value
Yaml
YAML document (application/yaml).
public static FileType Yaml { get; }
Property Value
Zip
ZIP archive (application/zip).
public static FileType Zip { get; }
Property Value
Zstandard
Zstandard archive (application/zstd).
public static FileType Zstandard { get; }
Property Value
Methods
CheckUpload(Stream, string)
Checks an upload with Default. See Validate(Stream, string).
public static UploadCheckResult CheckUpload(Stream stream, string fileName)
Parameters
streamStreamThe stream, positioned at the start of the file.
fileNamestringThe file name as uploaded.
Returns
- UploadCheckResult
The result with the reason of a rejection.
CheckUpload(Stream, string, UploadPolicy)
Checks an upload with a policy. See Validate(Stream, string).
public static UploadCheckResult CheckUpload(Stream stream, string fileName, UploadPolicy policy)
Parameters
streamStreamThe stream, positioned at the start of the file.
fileNamestringThe file name as uploaded.
policyUploadPolicyThe policy.
Returns
- UploadCheckResult
The result with the reason of a rejection.
CheckUpload(ReadOnlySpan<byte>, string)
Checks an upload with Default. See Validate(ReadOnlySpan<byte>, string).
public static UploadCheckResult CheckUpload(ReadOnlySpan<byte> content, string fileName)
Parameters
contentReadOnlySpan<byte>The file content, or at least its first HeaderSize bytes.
fileNamestringThe file name as uploaded.
Returns
- UploadCheckResult
The result with the reason of a rejection.
CheckUpload(ReadOnlySpan<byte>, string, UploadPolicy)
Checks an upload with a policy. See Validate(ReadOnlySpan<byte>, string).
public static UploadCheckResult CheckUpload(ReadOnlySpan<byte> content, string fileName, UploadPolicy policy)
Parameters
contentReadOnlySpan<byte>The file content, or at least its first HeaderSize bytes.
fileNamestringThe file name as uploaded.
policyUploadPolicyThe policy.
Returns
- UploadCheckResult
The result with the reason of a rejection.
ContentMatchesExtension(Stream, string?)
Checks whether a stream fits the extension. See ContentMatchesExtension(Stream, string?).
public static bool ContentMatchesExtension(Stream stream, string? fileName)
Parameters
streamStreamThe stream, positioned at the start of the file.
fileNamestringThe file name or extension.
Returns
- bool
True if both are known and fit.
ContentMatchesExtension(ReadOnlySpan<byte>, string?)
Checks whether content fits the extension. See ContentMatchesExtension(ReadOnlySpan<byte>, string?).
public static bool ContentMatchesExtension(ReadOnlySpan<byte> header, string? fileName)
Parameters
headerReadOnlySpan<byte>The first bytes of the file.
fileNamestringThe file name or extension.
Returns
- bool
True if both are known and fit.
Detect(Stream)
Detects the type of a file from a stream. See Detect(Stream).
public static FileType? Detect(Stream stream)
Parameters
streamStreamThe stream, positioned at the start of the file.
Returns
- FileType
The first matching type, or null.
Detect(ReadOnlySpan<byte>)
Detects the type of a file from its first bytes. See Detect(ReadOnlySpan<byte>).
public static FileType? Detect(ReadOnlySpan<byte> header)
Parameters
headerReadOnlySpan<byte>The first bytes of the file.
Returns
- FileType
The first matching type, or null.
DetectAll(ReadOnlySpan<byte>)
Returns every matching type. See DetectAll(ReadOnlySpan<byte>).
public static IReadOnlyList<FileType> DetectAll(ReadOnlySpan<byte> header)
Parameters
headerReadOnlySpan<byte>The first bytes of the file.
Returns
- IReadOnlyList<FileType>
The matching types in detection order.
DetectAsync(Stream, CancellationToken)
Detects the type of a file from a stream asynchronously. See DetectAsync(Stream, CancellationToken).
public static ValueTask<FileType?> DetectAsync(Stream stream, CancellationToken cancellationToken = default)
Parameters
streamStreamThe stream, positioned at the start of the file.
cancellationTokenCancellationTokenA token to cancel the read.
Returns
FromExtension(string?)
Finds a type by extension. See FromExtension(string?).
public static FileType? FromExtension(string? fileNameOrExtension)
Parameters
fileNameOrExtensionstringA file name, path or extension.
Returns
- FileType
The type, or null.
FromMimeType(string?)
Finds a type by MIME type. See FromMimeType(string?).
public static FileType? FromMimeType(string? mimeType)
Parameters
mimeTypestringThe MIME type.
Returns
- FileType
The type, or null.
GetMimeType(string?)
Returns the MIME type for a file name. See GetMimeType(string?).
public static string GetMimeType(string? fileNameOrExtension)
Parameters
fileNameOrExtensionstringA file name, path or extension.
Returns
- string
The MIME type, or DefaultMimeType.
Register(FileType)
Registers a type in Default. See Register(FileType).
public static void Register(FileType fileType)
Parameters
fileTypeFileTypeThe type.
ToSafeFileName(string?, string)
Turns an uploaded file name into one that is safe to save or to send in a Content-Disposition header.
public static string ToSafeFileName(string? fileName, string fallback = "file")
Parameters
fileNamestringThe file name as uploaded, or null.
fallbackstringThe name returned when nothing is left. Defaults to "file".
Returns
- string
The safe file name.
Examples
FileTypes.ToSafeFileName("../../etc/passwd"); // "passwd"
FileTypes.ToSafeFileName("C:\\temp\\rapor?.pdf"); // "rapor_.pdf"
FileTypes.ToSafeFileName("con.txt"); // "_con.txt"
FileTypes.ToSafeFileName(" Fatura Ekim .pdf "); // "Fatura Ekim .pdf"
Remarks
Keeps only the name after the last path separator. Replaces control characters and the characters Windows does not allow
(\ / : * ? " < > |) with an underscore, collapses runs of white space into one space, and trims spaces and dots at both ends.
Puts an underscore before Windows device names such as "CON" or "nul.txt", and cuts the name to 255 characters, keeping a short extension.
Letters of every language are kept. This does not check the type: use CheckUpload(Stream, string) for that,
and prefer a generated name for the stored file.