Documentation
¶
Index ¶
- Variables
- func ListDirectory(path, pattern string) ([]string, error)
- func MIMEForLocalFile(path string) (mimeType MIMEType, mode ExtensionMode, 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 MIMEType
- type PathInfo
- type ReadEncoding
Constants ¶
This section is empty.
Variables ¶
var ( ErrInvalidPath = errors.New("invalid path") ErrUnknownExtension = errors.New("unknown extension") )
Functions ¶
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, err error)
MIMEForLocalFile returns a best-effort MIME type and a coarse "mode" (text/image/document/default).
Behavior is intentionally simple:
- First try extension-based detection (stdlib + minimal overrides).
- If extension detection is unknown/generic, sniff the file bytes.
- Sniffing uses DetectContentType + a small "isProbablyTextSample" heuristic.
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" )
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 MIMEType ¶
type MIMEType string
const ( MIMEEmpty MIMEType = "" MIMEApplicationOctetStream MIMEType = "application/octet-stream" MIMETextPlain MIMEType = "text/plain; 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" MIMEApplicationPDF MIMEType = "application/pdf" )
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").
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" )