Table of Contents

Class FileFormatDetector

Namespace
CivitaiSharp.Tools.Downloads
Assembly
CivitaiSharp.Tools.dll

Provides utility methods for detecting file formats based on magic bytes.

public static class FileFormatDetector
Inheritance
FileFormatDetector
Inherited Members

Remarks

This detector identifies common image and video formats used by Civitai by examining the magic bytes at the beginning of files. It does not rely on file extensions.

Supported formats:

  • PNG - Portable Network Graphics
  • JPEG - Joint Photographic Experts Group
  • GIF - Graphics Interchange Format
  • WebP - Modern image format by Google
  • AVIF - AV1 Image File Format
  • MP4 - MPEG-4 Part 14 video container
  • WebM - Open media container format

Methods

DetectFormat(ReadOnlySpan<byte>)

Detects the file format from a span of bytes containing the file header.

public static string? DetectFormat(ReadOnlySpan<byte> header)

Parameters

header ReadOnlySpan<byte>

The file header bytes to analyze (minimum 4 bytes recommended).

Returns

string

The detected format extension, or null if unknown.

DetectFormatAsync(Stream, CancellationToken)

Detects the file format based on magic bytes from a stream.

public static Task<string?> DetectFormatAsync(Stream stream, CancellationToken cancellationToken = default)

Parameters

stream Stream

The stream to read from (must be readable).

cancellationToken CancellationToken

Cancellation token.

Returns

Task<string>

The detected format extension (e.g., "png", "jpg"), or null if unknown.

DetectFormatAsync(string, CancellationToken)

Detects the file format based on magic bytes at the start of the file.

public static Task<string?> DetectFormatAsync(string filePath, CancellationToken cancellationToken = default)

Parameters

filePath string

Path to the file to analyze.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<string>

The detected format extension (e.g., "png", "jpg"), or null if unknown.