filetype

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 14, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Register

func Register(engine Engine)

Register adds an engine to the default registry.

func SetFallback

func SetFallback(engine Engine)

SetFallback sets the fallback engine for the default registry. The fallback is returned by Detect when no registered engine matches. This should be called during initialization (e.g. init()) before any detection calls occur.

Types

type ChunkerSelector

type ChunkerSelector interface {
	ChunkerFor(fileSize int64) chunker.Chunker
}

ChunkerSelector chooses the chunking strategy for a file of the given size. Each engine uses its own tuned chunk sizes (engine autonomy). Returning nil signals that the caller should store the whole file as a single chunk.

type Detector

type Detector interface {
	Name() string
	DetectByMagic(header []byte) bool
	DetectByExtension(path string) bool
	DetectByHeuristic(path string, header []byte) bool
}

Detector identifies whether a file belongs to an engine via three layered signals, applied in priority order by the registry: magic bytes (strongest), file extension, and heuristic sniffing (weakest, used only as a fallback).

type Differ

type Differ interface {
	// Diff compares two files. oldPath/newPath are used for the diff header
	// and file type context; oldReader/newReader provide streaming content.
	Diff(ctx context.Context, oldPath string, oldReader io.Reader, newPath string, newReader io.Reader) (string, error)
}

Differ compares two files and returns a unified diff or summary. Implementations read content streaming from oldReader/newReader rather than receiving the whole file in memory. The context lets callers cancel a long-running diff mid-stream.

type Engine

type Engine interface {
	Detector
	ChunkerSelector
	Differ
	Previewer
	// Metadata returns the file metadata describing files handled by this
	// engine (e.g. MIME type). Returning nil means "no metadata"; callers
	// must handle nil. Pushing this into the engine (rather than switching
	// on engine.Name() in porcelain) keeps the engine self-describing and
	// preserves the pluggable-engine contract: adding a new engine requires
	// no edits outside its own package.
	Metadata() *core.FileMetadata
}

Engine bundles the per-filetype capabilities a registered engine must provide: detection, chunker selection, diffing, preview, and metadata. Composing these in one interface lets the registry treat all engines uniformly and keeps each engine self-describing — adding a new filetype requires no edits outside its own package.

func DetectEngine

func DetectEngine(path string, header []byte) Engine

DetectEngine finds the first matching engine for a file in the default registry.

Engine registration order matters for heuristic detection: text must be registered before binary, because binary's DetectByHeuristic always returns true and would short-circuit text detection. The registration order in init.go is: text → image → video → binary.

type Previewer

type Previewer interface {
	// Preview returns a short summary. header is the file head (for magic
	// detection), size is the total file size, reader streams the content,
	// and maxLines bounds the number of lines for text previews.
	Preview(header []byte, size int64, reader io.Reader, maxLines int) (string, error)
}

Previewer generates a short, human-readable preview of a file. header carries the leading bytes (for magic detection and dimension parsing), size is the total file size, and reader allows streaming access to the content. Engines that only need the header (image, video, binary) must not read from reader, keeping memory constant for large files.

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

Registry holds registered file type engines.

func (*Registry) Detect

func (r *Registry) Detect(path string, header []byte) Engine

Detect finds the best matching engine for a file using layered detection. Layer 1 (magic bytes) is queried first — the most reliable signal. Layer 2 (extension) is queried only if no magic matched. Layer 3 (heuristic) is the final fallback for unknown extensions. If no engine matches, the explicit fallback engine is returned (or nil if no fallback was set — callers should check for nil in that case).

func (*Registry) Register

func (r *Registry) Register(engine Engine)

Register adds an engine to the registry.

func (*Registry) SetFallback

func (r *Registry) SetFallback(engine Engine)

SetFallback sets the fallback engine returned by Detect when no registered engine matches. Callers should set this during initialization to avoid nil returns from Detect.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL