Table of Contents

Class FileType

Namespace
HeroKit.FileTypes
Assembly
HeroKit.FileTypes.dll

A file type: name, MIME type, extensions and the detectors that recognize its content. Built-in types are in FileTypes.

public sealed class FileType
Inheritance
FileType
Inherited Members
Extension Methods

Examples

var sketch = new FileType("Sketch document", "application/x-sketch", FileTypeCategory.Document, "sketch")
{
    Detectors = [new ZipEntryDetector("document.json")],
    Parent = FileTypes.Zip,
};
FileTypes.Register(sketch);

Remarks

Create an instance and register it with Register(FileType) or Register(FileType); registered types are checked before the built-in types.

Constructors

FileType(string, string, FileTypeCategory, params string[])

Creates a file type.

public FileType(string name, string mimeType, FileTypeCategory category, params string[] extensions)

Parameters

name string

A readable name, for example "PNG image".

mimeType string

The MIME type, for example "image/png".

category FileTypeCategory

The broad group of the type.

extensions string[]

The file extensions without a dot, the usual one first, for example "jpg", "jpeg".

Exceptions

ArgumentException

name or mimeType is empty.

Properties

Category

Gets the broad group.

public FileTypeCategory Category { get; }

Property Value

FileTypeCategory

Detectors

Gets the detectors that recognize the content. A type without detectors is known by its extension and MIME type only.

public IReadOnlyList<IFileTypeDetector> Detectors { get; init; }

Property Value

IReadOnlyList<IFileTypeDetector>

Extension

Gets the usual extension without a dot, or an empty string.

public string Extension { get; }

Property Value

string

Extensions

Gets the extensions in lower case without a dot, the usual one first.

public IReadOnlyList<string> Extensions { get; }

Property Value

IReadOnlyList<string>

HasMacros

Gets whether files of this type carry macros, for example macro-enabled Office documents (DOCM, XLSM, PPTM). UploadValidator rejects them unless AllowMacros is true.

public bool HasMacros { get; init; }

Property Value

bool

IsExecutable

Gets whether files of this type can run code: Executable or Script.

public bool IsExecutable { get; }

Property Value

bool

MimeType

Gets the MIME type in lower case.

public string MimeType { get; }

Property Value

string

Name

Gets the readable name.

public string Name { get; }

Property Value

string

OtherMimeTypes

Gets other MIME types that are used for the same files, for example "image/x-icon".

public IReadOnlyList<string> OtherMimeTypes { get; init; }

Property Value

IReadOnlyList<string>

Parent

Gets the more general type this type is built on, or null. For example, DOCX is a ZIP file and JSON is plain text.

public FileType? Parent { get; init; }

Property Value

FileType

Remarks

ContentMatchesExtension(ReadOnlySpan<byte>, string?) accepts content of a parent type for a more specific extension, because the content can only be recognized as far as the header shows.

Methods

IsA(FileType)

Checks whether this type is other or is built on it through Parent.

public bool IsA(FileType other)

Parameters

other FileType

The other type.

Returns

bool

True if this type is the other type or one of its descendants.

IsCompatibleWith(FileType)

Checks whether content of this type fits a file that claims to be other: the types are the same, have the same MIME type, or one is built on the other through Parent.

public bool IsCompatibleWith(FileType other)

Parameters

other FileType

The type the file claims to be, for example the type of its extension.

Returns

bool

True if the types fit.

IsMatch(ReadOnlySpan<byte>)

Checks whether the first bytes of a file match one of the detectors.

public bool IsMatch(ReadOnlySpan<byte> header)

Parameters

header ReadOnlySpan<byte>

The first bytes of the file.

Returns

bool

True if a detector matches.

ToString()

Returns a string that represents the current object.

public override string ToString()

Returns

string

A string that represents the current object.