Documentation
¶
Overview ¶
Package file ...
Index ¶
- Constants
- Variables
- func CheckImageDimensions(r io.ReadSeeker) error
- func CreatePreview(ctx context.Context, source string) (string, error)
- func CreatePreviewCustom(ctx context.Context, srcKey, dstKey string, maxW, maxH int, quality int) error
- func Delete(ctx context.Context, filename string) error
- func IsResizableImage(filename string) bool
- func Load(ctx context.Context, filename string) ([]byte, error)
- func SafeName(filename string) string
- func Store(ctx context.Context, r io.Reader, filename string) (string, error)
- type ByteSize
- type Driver
- type InMemoryFSStorage
- func (s *InMemoryFSStorage) Delete(_ context.Context, filename string) error
- func (s *InMemoryFSStorage) Load(_ context.Context, filename string) ([]byte, error)
- func (s *InMemoryFSStorage) Open(ctx context.Context, filename string) (ReadSeekCloser, int64, error)
- func (s *InMemoryFSStorage) Store(_ context.Context, r io.Reader, filename string) (string, error)
- type LocalFSStorage
- func (s *LocalFSStorage) Delete(_ context.Context, filename string) error
- func (s *LocalFSStorage) Load(_ context.Context, filename string) ([]byte, error)
- func (s *LocalFSStorage) Open(_ context.Context, filename string) (ReadSeekCloser, int64, error)
- func (s *LocalFSStorage) Store(_ context.Context, r io.Reader, filename string) (string, error)
- type ReadSeekCloser
- type Type
Constants ¶
const ( // B base byte units. B ByteSize = 1 // KB ... KB = B << 10 // MB ... MB = KB << 10 // GB ... GB = MB << 10 )
const InMemoryFSName = "in-memory-fs"
InMemoryFSName is the driver name for the in-memory storage implementation.
const LocalFSName = "local-fs"
LocalFSName is the driver name used to select the local filesystem storage implementation.
Variables ¶
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") )
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") )
var ( // ErrPreviewNotSupported ... ErrPreviewNotSupported = errors.New("preview not supported") // ErrInvalidImageDimensions ... ErrInvalidImageDimensions = errors.New("invalid image dimensions") // ErrIImageResolutionIsTooLarge ... ErrIImageResolutionIsTooLarge = errors.New("image resolution is too large") )
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 ¶
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 IsResizableImage ¶
IsResizableImage reports whether a preview can be generated for the given filename by checking its extension against ResizableImages.
Types ¶
type ByteSize ¶
type ByteSize uint64
ByteSize represents a size in bytes with helper conversion and formatting methods.
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 ¶
NewInMemoryFSStorage constructs a new in-memory storage driver.
func NewLocalFSStorage ¶
NewLocalFSStorage constructs the local filesystem driver from config.
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) Open ¶
func (s *InMemoryFSStorage) Open(ctx context.Context, filename string) (ReadSeekCloser, int64, error)
Open opens a stored file for reading and seeking.
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 ¶
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.
type ReadSeekCloser ¶
ReadSeekCloser is a convenience interface combining io.Reader, io.Seeker, and io.Closer.
type Type ¶
type Type string
Type represents a file category derived from its extension.
func ResolveFileType ¶
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.