fileutil

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidPath      = errors.New("invalid path")
	ErrUnknownExtension = errors.New("unknown extension")
)

Functions

func ListDirectory

func ListDirectory(path, pattern string) ([]string, error)

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

func SearchFiles(ctx context.Context, root, pattern string, maxResults int) ([]string, error)

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 FileExt

type FileExt string
const (
	ExtPDF FileExt = ".pdf"
)

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

func ReadImage(
	path string,
	includeBase64Data bool,
) (*ImageData, error)

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 ImageInfo

type ImageInfo struct {
	PathInfo

	Width    int      `json:"width,omitempty"`
	Height   int      `json:"height,omitempty"`
	Format   string   `json:"format,omitempty"`   // e.g. "jpeg", "png"
	MIMEType MIMEType `json:"mimeType,omitempty"` // e.g. "image/jpeg"
}

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

func MIMEFromExtensionString(ext string) (MIMEType, error)

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 PathInfo struct {
	Path    string     `json:"path"`
	Name    string     `json:"name"`
	Exists  bool       `json:"exists"`
	IsDir   bool       `json:"isDir"`
	Size    int64      `json:"size,omitempty"`
	ModTime *time.Time `json:"modTime,omitempty"`
}

func StatPath

func StatPath(path string) (pathInfo *PathInfo, err error)

StatPath returns basic metadata for the supplied path without mutating the filesystem. If the path does not exist, exists == false and err == nil.

type ReadEncoding

type ReadEncoding string
const (
	ReadEncodingText   ReadEncoding = "text"
	ReadEncodingBinary ReadEncoding = "binary"
)

Jump to

Keyboard shortcuts

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