file

package
v0.0.0-...-7b21f04 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package file ...

Index

Constants

View Source
const (
	// B base byte units.
	B ByteSize = 1
	// KB ...
	KB = B << 10
	// MB ...
	MB = KB << 10
	// GB ...
	GB = MB << 10
)
View Source
const InMemoryFSName = "in-memory-fs"

InMemoryFSName is the driver name for the in-memory storage implementation.

View Source
const LocalFSName = "local-fs"

LocalFSName is the driver name used to select the local filesystem storage implementation.

Variables

View Source
var (
	// ErrInvalidFilename is returned when an empty or invalid filename is provided.
	ErrInvalidFilename = errors.New("invalid filename")

	// ErrFileNotFound is returned when the requested file does not exist in storage.
	ErrFileNotFound = errors.New("file not found")
)
View Source
var (
	// ErrStoragePathNotSet indicates missing local storage base directory in config.
	ErrStoragePathNotSet = errors.New("[local-fs] storage_path is not set")

	// ErrInitStorage is a generic error prefix used for wrapping initialization failures.
	ErrInitStorage = errors.New("[local-fs] create driver")
)
View Source
var (
	// ErrPreviewNotSupported ...
	ErrPreviewNotSupported = errors.New("preview not supported")

	// ErrInvalidImageDimensions ...
	ErrInvalidImageDimensions = errors.New("invalid image dimensions")

	// ErrIImageResolutionIsTooLarge ...
	ErrIImageResolutionIsTooLarge = errors.New("image resolution is too large")
)
View Source
var ResizableImages = []string{".jpg", ".jpeg", ".png", ".gif", ".webp"}

ResizableImages is the list of file extensions for which we can generate previews.

Functions

func CheckImageDimensions

func CheckImageDimensions(r io.ReadSeeker) error

CheckImageDimensions validates image resolution.

func CreatePreview

func CreatePreview(ctx context.Context, source string) (string, error)

CreatePreview generates a preview image for the given source object using default settings from config (preview width/height and JPEG quality).

The preview is stored next to the source in a "preview/" subdirectory using the same base name. It returns the destination key/path for the generated preview.

func CreatePreviewCustom

func CreatePreviewCustom(ctx context.Context, srcKey, dstKey string, maxW, maxH int, quality int) error

CreatePreviewCustom creates an image preview with the provided settings.

Input formats: JPEG, PNG, GIF, WEBP (decoded via image.Decode; supported formats are enabled by the blank imports above). Output format: JPEG.

The image is resized to fit within maxW x maxH while preserving aspect ratio. If maxW/maxH are invalid, they are clamped to configured maximums.

func Delete

func Delete(ctx context.Context, filename string) error

Delete removes a file from the configured storage backend.

func IsResizableImage

func IsResizableImage(filename string) bool

IsResizableImage reports whether a preview can be generated for the given filename by checking its extension against ResizableImages.

func Load

func Load(ctx context.Context, filename string) ([]byte, error)

Load reads the entire stored file into memory and returns its contents.

func SafeName

func SafeName(filename string) string

SafeName generates a collision-resistant filename using the current timestamp and a short UUID, preserving the extension from provided filename.

func Store

func Store(ctx context.Context, r io.Reader, filename string) (string, error)

Store writes data from r into the configured storage backend under filename.

Types

type ByteSize

type ByteSize uint64

ByteSize represents a size in bytes with helper conversion and formatting methods.

func (ByteSize) Bytes

func (b ByteSize) Bytes() uint64

Bytes returns the raw size in bytes.

func (ByteSize) GBytes

func (b ByteSize) GBytes() float64

GBytes returns the size in gigabytes (base 1024) as a float.

func (ByteSize) KBytes

func (b ByteSize) KBytes() float64

KBytes returns the size in kilobytes (base 1024) as a float.

func (ByteSize) MBytes

func (b ByteSize) MBytes() float64

MBytes returns the size in megabytes (base 1024) as a float.

func (ByteSize) String

func (b ByteSize) String() string

String returns a human-readable representation of the byte size using binary (IEC-style) units.

type Driver

type Driver interface {
	// Store writes data from r into storage under the given filename.
	// It returns the normalized storage key used to reference the file.
	Store(ctx context.Context, r io.Reader, filename string) (string, error)

	// Open opens a stored file for reading and seeking.
	// It returns a ReadSeekCloser, the file size in bytes, and an error.
	Open(ctx context.Context, filename string) (fh ReadSeekCloser, size int64, err error)

	// Load reads the entire stored file into memory and returns its contents.
	// Prefer Open for large files.
	Load(ctx context.Context, filename string) ([]byte, error)

	// Delete removes a file from storage.
	// Deleting a non-existent file should not be considered an error.
	Delete(ctx context.Context, filename string) error
}

Driver defines the common interface for file storage backends.

func NewInMemoryFSStorage

func NewInMemoryFSStorage() (Driver, error)

NewInMemoryFSStorage constructs a new in-memory storage driver.

func NewLocalFSStorage

func NewLocalFSStorage() (Driver, error)

NewLocalFSStorage constructs the local filesystem driver from config.

func Storage

func Storage() Driver

Storage returns the initialized storage driver. The driver is selected based on application configuration and initialized lazily on first access.

type InMemoryFSStorage

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

InMemoryFSStorage implements Driver using an in-memory map. It is intended for tests and ephemeral storage, not for persistence.

func (*InMemoryFSStorage) Delete

func (s *InMemoryFSStorage) Delete(_ context.Context, filename string) error

Delete removes a file from in-memory storage.

func (*InMemoryFSStorage) Load

func (s *InMemoryFSStorage) Load(_ context.Context, filename string) ([]byte, error)

Load returns the full contents of the stored file as a byte slice.

func (*InMemoryFSStorage) Open

func (s *InMemoryFSStorage) Open(ctx context.Context, filename string) (ReadSeekCloser, int64, error)

Open opens a stored file for reading and seeking.

func (*InMemoryFSStorage) Store

func (s *InMemoryFSStorage) Store(_ context.Context, r io.Reader, filename string) (string, error)

Store reads all data from r and stores it in memory under the given filename.

type LocalFSStorage

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

LocalFSStorage implements Driver using the local OS filesystem.

func (*LocalFSStorage) Delete

func (s *LocalFSStorage) Delete(_ context.Context, filename string) error

Delete removes a stored file. It is not an error if the file does not exist.

func (*LocalFSStorage) Load

func (s *LocalFSStorage) Load(_ context.Context, filename string) ([]byte, error)

Load reads the entire stored file into memory and returns its bytes. Prefer Open for streaming large files.

func (*LocalFSStorage) Open

func (s *LocalFSStorage) Open(_ context.Context, filename string) (ReadSeekCloser, int64, error)

Open opens a stored file for reading and seeking. It returns an os.File (implements ReadSeekCloser), the file size, and an error.

Caller is responsible for closing the returned reader.

func (*LocalFSStorage) Store

func (s *LocalFSStorage) Store(_ context.Context, r io.Reader, filename string) (string, error)

Store writes content from r into the local storage under filename. For local FS, Store must be atomic internally (tmp + rename).

type ReadSeekCloser

type ReadSeekCloser interface {
	io.Reader
	io.Seeker
	io.Closer
}

ReadSeekCloser is a convenience interface combining io.Reader, io.Seeker, and io.Closer.

func Open

func Open(ctx context.Context, filename string) (ReadSeekCloser, int64, error)

Open opens a stored file for reading and seeking.

type Type

type Type string

Type represents a file category derived from its extension.

const (
	// TypeOther is used when file type cannot be determined
	// or does not belong to any known category.
	TypeOther Type = "other"

	// TypeImage represents image files.
	TypeImage Type = "image"

	// TypeVideo represents video files.
	TypeVideo Type = "video"
)

func ResolveFileType

func ResolveFileType(path string) Type

ResolveFileType determines the file Type based on the file extension. The lookup is case-insensitive. If the extension is unknown or missing, TypeOther is returned.

func (Type) Valid

func (t Type) Valid() bool

Valid ...

Jump to

Keyboard shortcuts

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