Table of Contents

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

string

HeaderSize

The number of bytes read from the start of a stream for detection: 16 KB.

public const int HeaderSize = 16384

Field Value

int

Properties

Aac

AAC audio (audio/aac).

public static FileType Aac { get; }

Property Value

FileType

All

Gets all types of Default. See All.

public static IReadOnlyList<FileType> All { get; }

Property Value

IReadOnlyList<FileType>

Apk

Android package (application/vnd.android.package-archive).

public static FileType Apk { get; }

Property Value

FileType

Avi

AVI video (video/x-msvideo).

public static FileType Avi { get; }

Property Value

FileType

Avif

AVIF image (image/avif).

public static FileType Avif { get; }

Property Value

FileType

Batch

Windows batch file (application/x-bat).

public static FileType Batch { get; }

Property Value

FileType

Bmp

BMP image (image/bmp).

public static FileType Bmp { get; }

Property Value

FileType

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

IReadOnlyList<FileType>

Bzip2

Bzip2 archive (application/x-bzip2).

public static FileType Bzip2 { get; }

Property Value

FileType

Calendar

iCalendar file (text/calendar).

public static FileType Calendar { get; }

Property Value

FileType

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

FileType

Css

CSS stylesheet (text/css).

public static FileType Css { get; }

Property Value

FileType

Csv

CSV file (text/csv).

public static FileType Csv { get; }

Property Value

FileType

Default

Gets the shared registry with the built-in types and the types added with Register(FileType).

public static FileTypeRegistry Default { get; }

Property Value

FileTypeRegistry

Dex

Android Dalvik executable (application/vnd.android.dex).

public static FileType Dex { get; }

Property Value

FileType

Doc

Word 97-2003 document (application/msword).

public static FileType Doc { get; }

Property Value

FileType

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

FileType

Docx

Word document (application/vnd.openxmlformats-officedocument.wordprocessingml.document).

public static FileType Docx { get; }

Property Value

FileType

Elf

ELF executable (application/x-executable).

public static FileType Elf { get; }

Property Value

FileType

Epub

EPUB e-book (application/epub+zip).

public static FileType Epub { get; }

Property Value

FileType

Flac

FLAC audio (audio/flac).

public static FileType Flac { get; }

Property Value

FileType

Flv

Flash video (video/x-flv).

public static FileType Flv { get; }

Property Value

FileType

Gif

GIF image (image/gif).

public static FileType Gif { get; }

Property Value

FileType

Gltf

glTF model (model/gltf+json).

public static FileType Gltf { get; }

Property Value

FileType

GltfBinary

glTF binary model (model/gltf-binary).

public static FileType GltfBinary { get; }

Property Value

FileType

Gzip

Gzip archive (application/gzip).

public static FileType Gzip { get; }

Property Value

FileType

Heic

HEIC image (image/heic).

public static FileType Heic { get; }

Property Value

FileType

Heif

HEIF image (image/heif).

public static FileType Heif { get; }

Property Value

FileType

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

FileType

Icon

Windows icon (image/vnd.microsoft.icon).

public static FileType Icon { get; }

Property Value

FileType

Jar

Java archive (application/java-archive).

public static FileType Jar { get; }

Property Value

FileType

JavaClass

Java class file (application/java-vm).

public static FileType JavaClass { get; }

Property Value

FileType

JavaScript

JavaScript (text/javascript).

public static FileType JavaScript { get; }

Property Value

FileType

Jpeg

JPEG image (image/jpeg).

public static FileType Jpeg { get; }

Property Value

FileType

JpegXl

JPEG XL image (image/jxl).

public static FileType JpegXl { get; }

Property Value

FileType

Json

JSON document (application/json).

public static FileType Json { get; }

Property Value

FileType

M4a

MPEG-4 audio (audio/mp4).

public static FileType M4a { get; }

Property Value

FileType

M4v

MPEG-4 video (iTunes) (video/x-m4v).

public static FileType M4v { get; }

Property Value

FileType

MachO

Mach-O executable (application/x-mach-binary).

public static FileType MachO { get; }

Property Value

FileType

Markdown

Markdown document (text/markdown).

public static FileType Markdown { get; }

Property Value

FileType

Matroska

Matroska video (video/x-matroska).

public static FileType Matroska { get; }

Property Value

FileType

Midi

MIDI audio (audio/midi).

public static FileType Midi { get; }

Property Value

FileType

Mp3

MP3 audio (audio/mpeg).

public static FileType Mp3 { get; }

Property Value

FileType

Mp4

MPEG-4 video (video/mp4).

public static FileType Mp4 { get; }

Property Value

FileType

Mpeg

MPEG video (video/mpeg).

public static FileType Mpeg { get; }

Property Value

FileType

Msg

Outlook message (application/vnd.ms-outlook).

public static FileType Msg { get; }

Property Value

FileType

Msi

Windows installer (application/x-msi).

public static FileType Msi { get; }

Property Value

FileType

Odp

OpenDocument presentation (application/vnd.oasis.opendocument.presentation).

public static FileType Odp { get; }

Property Value

FileType

Ods

OpenDocument spreadsheet (application/vnd.oasis.opendocument.spreadsheet).

public static FileType Ods { get; }

Property Value

FileType

Odt

OpenDocument text (application/vnd.oasis.opendocument.text).

public static FileType Odt { get; }

Property Value

FileType

Ogg

Ogg audio (audio/ogg).

public static FileType Ogg { get; }

Property Value

FileType

OpenType

OpenType font (font/otf).

public static FileType OpenType { get; }

Property Value

FileType

Parquet

Apache Parquet file (application/vnd.apache.parquet).

public static FileType Parquet { get; }

Property Value

FileType

Pdf

PDF document (application/pdf).

public static FileType Pdf { get; }

Property Value

FileType

Php

PHP script (application/x-httpd-php).

public static FileType Php { get; }

Property Value

FileType

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

FileType

Png

PNG image (image/png).

public static FileType Png { get; }

Property Value

FileType

PowerShell

PowerShell script (application/x-powershell).

public static FileType PowerShell { get; }

Property Value

FileType

Ppt

PowerPoint 97-2003 presentation (application/vnd.ms-powerpoint).

public static FileType Ppt { get; }

Property Value

FileType

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

FileType

Pptx

PowerPoint presentation (application/vnd.openxmlformats-officedocument.presentationml.presentation).

public static FileType Pptx { get; }

Property Value

FileType

Psd

Photoshop document (image/vnd.adobe.photoshop).

public static FileType Psd { get; }

Property Value

FileType

QuickTime

QuickTime video (video/quicktime).

public static FileType QuickTime { get; }

Property Value

FileType

Rar

RAR archive (application/vnd.rar).

public static FileType Rar { get; }

Property Value

FileType

Rtf

Rich Text Format document (application/rtf).

public static FileType Rtf { get; }

Property Value

FileType

SevenZip

7-Zip archive (application/x-7z-compressed).

public static FileType SevenZip { get; }

Property Value

FileType

ShellScript

Shell script. Content starting with "#!" (a shebang line) is detected as this type, whatever the interpreter.

public static FileType ShellScript { get; }

Property Value

FileType

Sqlite

SQLite database (application/vnd.sqlite3).

public static FileType Sqlite { get; }

Property Value

FileType

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

FileType

Tar

Tar archive (application/x-tar).

public static FileType Tar { get; }

Property Value

FileType

ThreeGp

3GPP video (video/3gpp).

public static FileType ThreeGp { get; }

Property Value

FileType

Tiff

TIFF image (image/tiff).

public static FileType Tiff { get; }

Property Value

FileType

TrueType

TrueType font (font/ttf).

public static FileType TrueType { get; }

Property Value

FileType

Tsv

TSV file (text/tab-separated-values).

public static FileType Tsv { get; }

Property Value

FileType

VCard

vCard file (text/vcard).

public static FileType VCard { get; }

Property Value

FileType

VbScript

VBScript file (text/vbscript).

public static FileType VbScript { get; }

Property Value

FileType

Wav

WAV audio (audio/wav).

public static FileType Wav { get; }

Property Value

FileType

WebAssembly

WebAssembly module (application/wasm).

public static FileType WebAssembly { get; }

Property Value

FileType

WebM

WebM video (video/webm).

public static FileType WebM { get; }

Property Value

FileType

WebP

WebP image (image/webp).

public static FileType WebP { get; }

Property Value

FileType

WindowsExecutable

Windows executable (application/vnd.microsoft.portable-executable).

public static FileType WindowsExecutable { get; }

Property Value

FileType

WindowsShortcut

Windows shortcut (application/x-ms-shortcut).

public static FileType WindowsShortcut { get; }

Property Value

FileType

Woff

WOFF font (font/woff).

public static FileType Woff { get; }

Property Value

FileType

Woff2

WOFF2 font (font/woff2).

public static FileType Woff2 { get; }

Property Value

FileType

Xls

Excel 97-2003 workbook (application/vnd.ms-excel).

public static FileType Xls { get; }

Property Value

FileType

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

FileType

Xlsx

Excel workbook (application/vnd.openxmlformats-officedocument.spreadsheetml.sheet).

public static FileType Xlsx { get; }

Property Value

FileType

Xml

XML document (application/xml).

public static FileType Xml { get; }

Property Value

FileType

Xz

XZ archive (application/x-xz).

public static FileType Xz { get; }

Property Value

FileType

Yaml

YAML document (application/yaml).

public static FileType Yaml { get; }

Property Value

FileType

Zip

ZIP archive (application/zip).

public static FileType Zip { get; }

Property Value

FileType

Zstandard

Zstandard archive (application/zstd).

public static FileType Zstandard { get; }

Property Value

FileType

Methods

CheckUpload(Stream, string)

Checks an upload with Default. See Validate(Stream, string).

public static UploadCheckResult CheckUpload(Stream stream, string fileName)

Parameters

stream Stream

The stream, positioned at the start of the file.

fileName string

The 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

stream Stream

The stream, positioned at the start of the file.

fileName string

The file name as uploaded.

policy UploadPolicy

The 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

content ReadOnlySpan<byte>

The file content, or at least its first HeaderSize bytes.

fileName string

The 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

content ReadOnlySpan<byte>

The file content, or at least its first HeaderSize bytes.

fileName string

The file name as uploaded.

policy UploadPolicy

The 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

stream Stream

The stream, positioned at the start of the file.

fileName string

The 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

header ReadOnlySpan<byte>

The first bytes of the file.

fileName string

The 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

stream Stream

The 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

header ReadOnlySpan<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

header ReadOnlySpan<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

stream Stream

The stream, positioned at the start of the file.

cancellationToken CancellationToken

A token to cancel the read.

Returns

ValueTask<FileType>

The first matching type, or null.

FromExtension(string?)

Finds a type by extension. See FromExtension(string?).

public static FileType? FromExtension(string? fileNameOrExtension)

Parameters

fileNameOrExtension string

A 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

mimeType string

The 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

fileNameOrExtension string

A 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

fileType FileType

The 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

fileName string

The file name as uploaded, or null.

fallback string

The 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.