Documentation
¶
Overview ¶
Package filetype identifies a file's real type from its leading bytes.
It NEVER decodes content. Detection is a comparison of magic-byte prefixes, so the package costs almost nothing in a WASM binary — decoding a PNG merely to validate it would pull in image/png and compress/zlib, hundreds of KB.
Isomorphic: no build tags, no standard library. The same detection runs in a browser (validating before upload) and on a server (validating what arrived), so client and server can never disagree about what a file is.
The type a client declares — a Content-Type header, a file extension — is a claim, not evidence. Only the bytes are evidence.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( PNG = Type{MIME: "image/png", Ext: ".png", /* contains filtered or unexported fields */} JPEG = Type{MIME: "image/jpeg", Ext: ".jpg", /* contains filtered or unexported fields */} GIF = Type{MIME: "image/gif", Ext: ".gif", /* contains filtered or unexported fields */} WebP = Type{MIME: "image/webp", Ext: ".webp", /* contains filtered or unexported fields */} PDF = Type{MIME: "application/pdf", Ext: ".pdf", /* contains filtered or unexported fields */} SVG = Type{MIME: "image/svg+xml", Ext: ".svg", /* contains filtered or unexported fields */} HTML = Type{MIME: "text/html", Ext: ".html", /* contains filtered or unexported fields */} )
The types this package recognizes.
SVG and HTML are detected ON PURPOSE, so they can be rejected by name instead of vanishing into "unknown". Both can carry JavaScript: served from your own domain, they execute in your origin. Never put them in an Allowlist for user uploads.
var Images = NewAllowlist(PNG, JPEG, GIF, WebP)
Images is the safe default for user uploads: raster images only, nothing scriptable.
Functions ¶
This section is empty.
Types ¶
type Allowlist ¶ added in v0.0.2
type Allowlist struct {
// contains filtered or unexported fields
}
Allowlist is the upload policy: which types this site accepts. The zero value accepts nothing — a policy you forgot to configure must not be permissive.
func NewAllowlist ¶ added in v0.0.2
NewAllowlist builds a policy accepting exactly the given types.
type Type ¶ added in v0.0.2
type Type struct {
MIME string // "image/png"
Ext string // ".png"
// contains filtered or unexported fields
}
Type is a recognized file type: its real MIME type and canonical extension.
func Detect ¶ added in v0.0.2
Detect returns the type deduced from data's leading bytes. ok=false means no known signature matched — the caller must reject the content, and never fall back to what the client claimed.
func (Type) Scriptable ¶ added in v0.0.2
Scriptable reports whether the type can carry executable code. Serving a scriptable file from your own domain runs that code in your origin.