Documentation
¶
Index ¶
- Variables
- func GetBaseMIME(mt MIMEType) string
- func ListDirectory(path, pattern string) ([]string, error)
- func MIMEForLocalFile(path string) (mimeType MIMEType, mode ExtensionMode, method MIMEDetectMethod, err error)
- func ReadFile(path string, encoding ReadEncoding, maxBytes int64) (string, error)
- func SearchFiles(ctx context.Context, root, pattern string, maxResults int) ([]string, error)
- func SniffFileMIME(path string) (mimeType MIMEType, mode ExtensionMode, err error)
- type ExtensionMode
- type FileExt
- type ImageData
- type ImageInfo
- type MIMEDetectMethod
- type MIMEType
- type PathInfo
- type ReadEncoding
Constants ¶
This section is empty.
Variables ¶
var ( ErrInvalidPath = errors.New("invalid path") ErrUnknownExtension = errors.New("unknown extension") )
var AllExtensionModes = []ExtensionMode{ ExtensionModeText, ExtensionModeImage, ExtensionModeDocument, ExtensionModeDefault, }
var BaseMIMEToMode = map[string]ExtensionMode{ "": ExtensionModeDefault, "application/octet-stream": ExtensionModeDefault, "text/plain": ExtensionModeText, "text/markdown": ExtensionModeText, "text/html": ExtensionModeText, "text/css": ExtensionModeText, "application/json": ExtensionModeText, "application/xml": ExtensionModeText, "application/x-yaml": ExtensionModeText, "application/yaml": ExtensionModeText, "application/toml": ExtensionModeText, "application/sql": ExtensionModeText, "application/javascript": ExtensionModeText, "image/jpeg": ExtensionModeImage, "image/png": ExtensionModeImage, "image/gif": ExtensionModeImage, "image/webp": ExtensionModeImage, "image/bmp": ExtensionModeImage, "image/svg+xml": ExtensionModeImage, "application/pdf": ExtensionModeDocument, "application/msword": ExtensionModeDocument, "application/vnd.ms-powerpoint": ExtensionModeDocument, "application/vnd.ms-excel": ExtensionModeDocument, "application/vnd.openxmlformats-officedocument.wordprocessingml.document": ExtensionModeDocument, "application/vnd.openxmlformats-officedocument.presentationml.presentation": ExtensionModeDocument, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": ExtensionModeDocument, "application/vnd.oasis.opendocument.text": ExtensionModeDocument, "application/vnd.oasis.opendocument.spreadsheet": ExtensionModeDocument, }
BaseMIMEToMode maps *base mime types* (no parameters) to a coarse mode.
var ExtensionToMIMEType = map[FileExt]MIMEType{ ExtTxt: MIMETextPlain, ExtMd: MIMETextMarkdown, ExtMarkdown: MIMETextMarkdown, ExtLog: MIMETextPlain, ExtJSON: MIMEApplicationJSON, ExtYAML: MIMEApplicationYAML, ExtYML: MIMEApplicationYAML, ExtTOML: MIMEApplicationTOML, ExtJS: MIMEApplicationJS, ExtTS: MIMETextPlain, ExtTSX: MIMETextPlain, ExtJSX: MIMETextPlain, ExtPY: MIMETextPlain, ExtGO: MIMETextPlain, ExtRS: MIMETextPlain, ExtJAVA: MIMETextPlain, ExtC: MIMETextPlain, ExtCPP: MIMETextPlain, ExtH: MIMETextPlain, ExtHPP: MIMETextPlain, ExtCS: MIMETextPlain, ExtRB: MIMETextPlain, ExtPHP: MIMETextPlain, ExtHTML: MIMETextHTML, ExtHTM: MIMETextHTML, ExtCSS: MIMETextCSS, ExtSCSS: MIMETextPlain, ExtLESS: MIMETextPlain, ExtSQL: MIMEApplicationSQL, ExtMod: MIMETextPlain, ExtSum: MIMETextPlain, ExtJSONL: MIMETextPlain, ExtShell: MIMETextPlain, ExtSWIFT: MIMETextPlain, ExtM: MIMETextPlain, ExtKT: MIMETextPlain, ExtPL: MIMETextPlain, ExtSCALA: MIMETextPlain, ExtHS: MIMETextPlain, ExtLUA: MIMETextPlain, ExtDART: MIMETextPlain, ExtCmake: MIMETextPlain, ExtBazel: MIMETextPlain, ExtXML: MIMEApplicationXML, ExtJPG: MIMEImageJPEG, ExtJPEG: MIMEImageJPEG, ExtPNG: MIMEImagePNG, ExtGIF: MIMEImageGIF, ExtWEBP: MIMEImageWEBP, ExtBMP: MIMEImageBMP, ExtSVG: MIMEImageSVG, ExtPDF: MIMEApplicationPDF, ExtDOC: MIMEApplicationMSWord, ExtDOCX: MIMEApplicationOpenXMLDoc, ExtPPT: MIMEApplicationMSPowerPt, ExtPPTX: MIMEApplicationOpenXMLPPT, ExtXLS: MIMEApplicationMSExcel, ExtXLSX: MIMEApplicationOpenXMLXLS, ExtODT: MIMEApplicationODT, ExtODS: MIMEApplicationODS, }
ExtensionToMIMEType is an internal registry of common/explicitly-supported extensions. This is used before falling back to mime.TypeByExtension.
var ModeToExtensions = func() map[ExtensionMode][]FileExt { m := make(map[ExtensionMode][]FileExt, len(AllExtensionModes)) for ext, mt := range ExtensionToMIMEType { mode := GetModeForMIME(mt) m[mode] = append(m[mode], ext) } return m }()
ModeToExtensions is a convenience reverse index built from ExtensionToMIMEType + BaseMIMEToMode.
Functions ¶
func GetBaseMIME ¶ added in v0.4.0
func ListDirectory ¶
ListDirectory lists files/dirs in path (default "."), pattern is an optional glob filter (filepath.Match).
func MIMEForLocalFile ¶
func MIMEForLocalFile( path string, ) (mimeType MIMEType, mode ExtensionMode, method MIMEDetectMethod, err error)
MIMEForLocalFile returns a best-effort MIME type, "file mode" (text/image/document/default) and detection method.
Behavior:
- First try extension-based detection (internal registry + stdlib).
- If extension detection is unknown or generic, sniff the file bytes.
- Sniffing uses DetectContentType + a small "isProbablyTextSample" heuristic.
Detection method can be:
- extension: a non-generic MIME type was derived from the file extension (no file IO required)
- sniff: content sniffing was used (requires opening/reading the file)
func ReadFile ¶
func ReadFile(path string, encoding ReadEncoding, maxBytes int64) (string, error)
ReadFile reads a file and returns its contents. If maxBytes > 0, it enforces a hard cap during reading.
func SearchFiles ¶
SearchFiles walks root (default ".") recursively and returns up to maxResults files whose *path* or UTF-8 text content* match the regexp pattern. If maxResults <= 0, it is treated as "no limit".
func SniffFileMIME ¶
func SniffFileMIME(path string) (mimeType MIMEType, mode ExtensionMode, err error)
SniffFileMIME inspects initial bytes of a file and returns a best-effort MIME type and mode. It will return an error if the file can't be opened/read.
Types ¶
type ExtensionMode ¶
type ExtensionMode string
const ( ExtensionModeText ExtensionMode = "text" ExtensionModeImage ExtensionMode = "image" ExtensionModeDocument ExtensionMode = "document" ExtensionModeDefault ExtensionMode = "default" )
func GetModeForMIME ¶ added in v0.4.0
func GetModeForMIME(mt MIMEType) ExtensionMode
type FileExt ¶
type FileExt string
const ( ExtTxt FileExt = ".txt" ExtMd FileExt = ".md" ExtMarkdown FileExt = ".markdown" ExtLog FileExt = ".log" ExtJSON FileExt = ".json" ExtYAML FileExt = ".yaml" ExtYML FileExt = ".yml" ExtTOML FileExt = ".toml" ExtJS FileExt = ".js" ExtTS FileExt = ".ts" ExtTSX FileExt = ".tsx" ExtJSX FileExt = ".jsx" ExtPY FileExt = ".py" ExtGO FileExt = ".go" ExtRS FileExt = ".rs" ExtJAVA FileExt = ".java" ExtC FileExt = ".c" ExtCPP FileExt = ".cpp" ExtH FileExt = ".h" ExtHPP FileExt = ".hpp" ExtCS FileExt = ".cs" ExtRB FileExt = ".rb" ExtPHP FileExt = ".php" ExtHTML FileExt = ".html" ExtHTM FileExt = ".htm" ExtCSS FileExt = ".css" ExtSCSS FileExt = ".scss" ExtLESS FileExt = ".less" ExtSQL FileExt = ".sql" ExtMod FileExt = ".mod" ExtSum FileExt = ".sum" ExtJSONL FileExt = ".jsonl" ExtShell FileExt = ".sh" ExtSWIFT FileExt = ".swift" ExtM FileExt = ".m" ExtKT FileExt = ".kt" ExtPL FileExt = ".pl" ExtSCALA FileExt = ".scala" ExtHS FileExt = ".hs" ExtLUA FileExt = ".lua" ExtDART FileExt = ".dart" ExtCmake FileExt = ".cmake" ExtBazel FileExt = ".bazel" ExtXML FileExt = ".xml" ExtJPG FileExt = ".jpg" ExtJPEG FileExt = ".jpeg" ExtPNG FileExt = ".png" ExtGIF FileExt = ".gif" ExtWEBP FileExt = ".webp" ExtBMP FileExt = ".bmp" ExtSVG FileExt = ".svg" ExtPDF FileExt = ".pdf" ExtDOC FileExt = ".doc" ExtDOCX FileExt = ".docx" ExtPPT FileExt = ".ppt" ExtPPTX FileExt = ".pptx" ExtXLS FileExt = ".xls" ExtXLSX FileExt = ".xlsx" ExtODT FileExt = ".odt" ExtODS FileExt = ".ods" )
func GetNormalizedExt ¶ added in v0.4.0
GetNormalizedExt lowercases and ensures a leading '.' for an extension.
type ImageData ¶
type ImageData struct {
ImageInfo
Base64Data string `json:"base64Data,omitempty"` // optional, if requested
}
ImageData holds metadata (and optionally content) for an image file.
func ReadImage ¶
ReadImage inspects an image file and returns its intrinsic metadata. If includeBase64 is true, Base64Data will contain the base64-encoded file contents. If the file does not exist, Exists == false and err == nil. Returns an error if the path is empty, a directory, or not a supported image.
type MIMEDetectMethod ¶ added in v0.4.0
type MIMEDetectMethod string
MIMEDetectMethod describes how MIME detection was performed. It is intentionally coarse (extension vs sniff).
const ( MIMEDetectMethodExtension MIMEDetectMethod = "extension" MIMEDetectMethodSniff MIMEDetectMethod = "sniff" )
type MIMEType ¶
type MIMEType string
const ( MIMEEmpty MIMEType = "" MIMEApplicationOctetStream MIMEType = "application/octet-stream" MIMETextPlain MIMEType = "text/plain; charset=utf-8" MIMETextMarkdown MIMEType = "text/markdown; charset=utf-8" MIMETextHTML MIMEType = "text/html; charset=utf-8" MIMETextCSS MIMEType = "text/css; charset=utf-8" MIMEApplicationJSON MIMEType = "application/json" MIMEApplicationXML MIMEType = "application/xml" MIMEApplicationYAML MIMEType = "application/x-yaml" MIMEApplicationTOML MIMEType = "application/toml" MIMEApplicationSQL MIMEType = "application/sql" MIMEApplicationJS MIMEType = "application/javascript" MIMEImageJPEG MIMEType = "image/jpeg" MIMEImagePNG MIMEType = "image/png" MIMEImageGIF MIMEType = "image/gif" MIMEImageWEBP MIMEType = "image/webp" MIMEImageBMP MIMEType = "image/bmp" MIMEImageSVG MIMEType = "image/svg+xml" MIMEApplicationPDF MIMEType = "application/pdf" MIMEApplicationMSWord MIMEType = "application/msword" MIMEApplicationMSPowerPt MIMEType = "application/vnd.ms-powerpoint" MIMEApplicationMSExcel MIMEType = "application/vnd.ms-excel" MIMEApplicationOpenXMLDoc MIMEType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document" MIMEApplicationOpenXMLPPT MIMEType = "application/vnd.openxmlformats-officedocument.presentationml.presentation" MIMEApplicationOpenXMLXLS MIMEType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" MIMEApplicationODT MIMEType = "application/vnd.oasis.opendocument.text" MIMEApplicationODS MIMEType = "application/vnd.oasis.opendocument.spreadsheet" )
func MIMEFromExtensionString ¶
MIMEFromExtensionString returns a best-known MIME for the given extension string. Accepts "png" as well as ".png" (useful because image.DecodeConfig returns "png").
Lookup order: internal registry -> stdlib mime.TypeByExtension. If the extension cannot be resolved, returns application/octet-stream and ErrUnknownExtension.
type PathInfo ¶
type ReadEncoding ¶
type ReadEncoding string
const ( ReadEncodingText ReadEncoding = "text" ReadEncodingBinary ReadEncoding = "binary" )