storage

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2025 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrFileNotFound  = errors.New("file not found")
	ErrDiskNotFound  = errors.New("disk not found")
	ErrInvalidPath   = errors.New("invalid file path")
	ErrQuotaExceeded = errors.New("storage quota exceeded")
	ErrAccessDenied  = errors.New("access denied")
	ErrNotSupported  = errors.New("operation not supported by this driver")
)

Common errors

Functions

func AllDirectories

func AllDirectories(directory string) ([]string, error)

AllDirectories lists all directories recursively using the default disk

func AllFiles

func AllFiles(directory string) ([]string, error)

AllFiles lists all files recursively in a directory using the default disk

func Configure

func Configure(config Config) error

Configure configures the global storage manager

func Copy

func Copy(from, to string) error

Copy copies a file from one path to another using the default disk

func Delete

func Delete(paths ...string) error

Delete removes files at the given paths using the default disk

func DeleteDirectory

func DeleteDirectory(directory string) error

DeleteDirectory deletes a directory using the default disk

func Directories

func Directories(directory string) ([]string, error)

Directories lists directories using the default disk

func Exists

func Exists(path string) bool

Exists checks if a file exists at the given path using the default disk

func Files

func Files(directory string) ([]string, error)

Files lists files in a directory using the default disk

func Get

func Get(path string) ([]byte, error)

Get retrieves content from the given path using the default disk

func GetStream

func GetStream(path string) (io.ReadCloser, error)

GetStream retrieves a stream from the given path using the default disk

func LastModified

func LastModified(path string) (time.Time, error)

LastModified returns the last modified time of a file using the default disk

func MakeDirectory

func MakeDirectory(path string) error

MakeDirectory creates a directory using the default disk

func MimeType

func MimeType(path string) (string, error)

MimeType returns the MIME type of a file using the default disk

func Move

func Move(from, to string) error

Move moves a file from one path to another using the default disk

func Put

func Put(path string, contents []byte) error

Put stores content at the given path using the default disk

func PutStream

func PutStream(path string, stream io.Reader) error

PutStream stores a stream at the given path using the default disk

func ReloadFromEnv

func ReloadFromEnv()

ReloadFromEnv reloads storage configuration from environment Useful for testing or runtime reconfiguration

func SetGlobalManager

func SetGlobalManager(manager *Manager)

SetGlobalManager sets the global storage manager (mainly for testing)

func Size

func Size(path string) (int64, error)

Size returns the size of a file at the given path using the default disk

func TemporaryURL

func TemporaryURL(path string, expiration time.Duration) (string, error)

TemporaryURL returns a temporary URL for a file using the default disk

func URL

func URL(path string) string

URL returns the public URL for a file using the default disk

Types

type Config

type Config struct {
	Default string                // Default disk name
	Disks   map[string]DiskConfig // Configured disks
}

Config holds storage configuration

type DiskConfig

type DiskConfig struct {
	Driver string // "local", "s3", "memory"

	// Local driver config
	Root       string // Root directory for local storage
	URL        string // Base URL for file access
	Visibility string // Default visibility (public/private)

	// S3 driver config
	Key    string // AWS Access Key
	Secret string // AWS Secret Key
	Region string // AWS Region
	Bucket string // S3 Bucket name

	// Memory driver config
	MaxSize int64 // Maximum memory usage in bytes
}

DiskConfig holds configuration for a storage disk

type Driver

type Driver interface {
	// Basic operations
	Put(path string, contents []byte) error
	PutStream(path string, stream io.Reader) error
	Get(path string) ([]byte, error)
	GetStream(path string) (io.ReadCloser, error)

	// File management
	Exists(path string) bool
	Delete(paths ...string) error
	Copy(from, to string) error
	Move(from, to string) error

	// File information
	Size(path string) (int64, error)
	LastModified(path string) (time.Time, error)
	MimeType(path string) (string, error)

	// Directory operations
	Files(directory string) ([]string, error)
	AllFiles(directory string) ([]string, error)
	Directories(directory string) ([]string, error)
	AllDirectories(directory string) ([]string, error)
	MakeDirectory(path string) error
	DeleteDirectory(directory string) error

	// URL operations
	URL(path string) string
	TemporaryURL(path string, expiration time.Duration) (string, error)
}

Driver defines the storage driver interface

func Disk

func Disk(name string) Driver

Disk returns a specific disk from the global manager

type FileInfo

type FileInfo struct {
	Path         string
	Size         int64
	LastModified time.Time
	MimeType     string
	Visibility   Visibility
}

FileInfo contains file metadata

type LocalDriver

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

LocalDriver implements the Driver interface for local filesystem storage

func NewLocalDriver

func NewLocalDriver(config DiskConfig) *LocalDriver

NewLocalDriver creates a new local storage driver

func (*LocalDriver) AllDirectories

func (d *LocalDriver) AllDirectories(directory string) ([]string, error)

AllDirectories lists all directories recursively

func (*LocalDriver) AllFiles

func (d *LocalDriver) AllFiles(directory string) ([]string, error)

AllFiles lists all files recursively in a directory

func (*LocalDriver) Copy

func (d *LocalDriver) Copy(from, to string) error

Copy copies a file from one path to another

func (*LocalDriver) Delete

func (d *LocalDriver) Delete(paths ...string) error

Delete removes files at the given paths

func (*LocalDriver) DeleteDirectory

func (d *LocalDriver) DeleteDirectory(directory string) error

DeleteDirectory deletes a directory and all its contents

func (*LocalDriver) Directories

func (d *LocalDriver) Directories(directory string) ([]string, error)

Directories lists directories

func (*LocalDriver) Exists

func (d *LocalDriver) Exists(path string) bool

Exists checks if a file exists at the given path

func (*LocalDriver) Files

func (d *LocalDriver) Files(directory string) ([]string, error)

Files lists files in a directory

func (*LocalDriver) Get

func (d *LocalDriver) Get(path string) ([]byte, error)

Get retrieves content from the given path

func (*LocalDriver) GetStream

func (d *LocalDriver) GetStream(path string) (io.ReadCloser, error)

GetStream retrieves a stream from the given path

func (*LocalDriver) LastModified

func (d *LocalDriver) LastModified(path string) (time.Time, error)

LastModified returns the last modified time of a file

func (*LocalDriver) MakeDirectory

func (d *LocalDriver) MakeDirectory(path string) error

MakeDirectory creates a directory

func (*LocalDriver) MimeType

func (d *LocalDriver) MimeType(path string) (string, error)

MimeType returns the MIME type of a file

func (*LocalDriver) Move

func (d *LocalDriver) Move(from, to string) error

Move moves a file from one path to another

func (*LocalDriver) Put

func (d *LocalDriver) Put(path string, contents []byte) error

Put stores content at the given path

func (*LocalDriver) PutStream

func (d *LocalDriver) PutStream(path string, stream io.Reader) error

PutStream stores a stream at the given path

func (*LocalDriver) Size

func (d *LocalDriver) Size(path string) (int64, error)

Size returns the size of a file at the given path

func (*LocalDriver) TemporaryURL

func (d *LocalDriver) TemporaryURL(path string, expiration time.Duration) (string, error)

TemporaryURL returns a temporary URL for a file (not supported for local)

func (*LocalDriver) URL

func (d *LocalDriver) URL(path string) string

URL returns the public URL for a file

type Manager

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

Manager manages multiple storage disks

func NewManager

func NewManager(config Config) *Manager

NewManager creates a new storage manager

func (*Manager) AddDisk

func (m *Manager) AddDisk(name string, driver Driver)

AddDisk adds a new disk to the manager

func (*Manager) Configure

func (m *Manager) Configure(config Config) error

Configure configures the storage manager with the given configuration

func (*Manager) Default

func (m *Manager) Default() Driver

Default returns the default disk driver

func (*Manager) Disk

func (m *Manager) Disk(name string) Driver

Disk returns a specific disk driver

func (*Manager) SetDefault

func (m *Manager) SetDefault(name string) error

SetDefault sets the default disk

type MemoryDriver

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

MemoryDriver implements the Driver interface for in-memory storage

func NewMemoryDriver

func NewMemoryDriver(config DiskConfig) *MemoryDriver

NewMemoryDriver creates a new memory storage driver

func (*MemoryDriver) AllDirectories

func (d *MemoryDriver) AllDirectories(directory string) ([]string, error)

AllDirectories lists all directories recursively

func (*MemoryDriver) AllFiles

func (d *MemoryDriver) AllFiles(directory string) ([]string, error)

AllFiles lists all files recursively in a directory

func (*MemoryDriver) Clear

func (d *MemoryDriver) Clear()

Clear removes all files from memory

func (*MemoryDriver) Copy

func (d *MemoryDriver) Copy(from, to string) error

Copy copies a file from one path to another

func (*MemoryDriver) Delete

func (d *MemoryDriver) Delete(paths ...string) error

Delete removes files at the given paths

func (*MemoryDriver) DeleteDirectory

func (d *MemoryDriver) DeleteDirectory(directory string) error

DeleteDirectory deletes a directory and all its contents

func (*MemoryDriver) Directories

func (d *MemoryDriver) Directories(directory string) ([]string, error)

Directories lists directories

func (*MemoryDriver) Exists

func (d *MemoryDriver) Exists(path string) bool

Exists checks if a file exists at the given path

func (*MemoryDriver) Files

func (d *MemoryDriver) Files(directory string) ([]string, error)

Files lists files in a directory

func (*MemoryDriver) Get

func (d *MemoryDriver) Get(path string) ([]byte, error)

Get retrieves content from the given path

func (*MemoryDriver) GetStream

func (d *MemoryDriver) GetStream(path string) (io.ReadCloser, error)

GetStream retrieves a stream from the given path

func (*MemoryDriver) LastModified

func (d *MemoryDriver) LastModified(path string) (time.Time, error)

LastModified returns the last modified time of a file

func (*MemoryDriver) MakeDirectory

func (d *MemoryDriver) MakeDirectory(path string) error

MakeDirectory creates a directory (no-op for memory driver)

func (*MemoryDriver) MimeType

func (d *MemoryDriver) MimeType(path string) (string, error)

MimeType returns the MIME type of a file

func (*MemoryDriver) Move

func (d *MemoryDriver) Move(from, to string) error

Move moves a file from one path to another

func (*MemoryDriver) Put

func (d *MemoryDriver) Put(path string, contents []byte) error

Put stores content at the given path

func (*MemoryDriver) PutStream

func (d *MemoryDriver) PutStream(path string, stream io.Reader) error

PutStream stores a stream at the given path

func (*MemoryDriver) Size

func (d *MemoryDriver) Size(path string) (int64, error)

Size returns the size of a file at the given path

func (*MemoryDriver) Stats

func (d *MemoryDriver) Stats() (used int64, max int64, fileCount int)

Stats returns memory usage statistics

func (*MemoryDriver) TemporaryURL

func (d *MemoryDriver) TemporaryURL(path string, expiration time.Duration) (string, error)

TemporaryURL returns a temporary URL for a file (not supported for memory)

func (*MemoryDriver) URL

func (d *MemoryDriver) URL(path string) string

URL returns the public URL for a file (not supported for memory)

type MemoryFile

type MemoryFile struct {
	Content      []byte
	LastModified time.Time
	MimeType     string
}

MemoryFile represents a file in memory

type Option

type Option func(*PutOptions)

Option is a function that modifies PutOptions

func WithMetadata

func WithMetadata(metadata map[string]string) Option

WithMetadata sets metadata for a file

func WithMimeType

func WithMimeType(mimeType string) Option

WithMimeType sets the MIME type for a file

func WithVisibility

func WithVisibility(v Visibility) Option

WithVisibility sets the visibility for a file

type PutOptions

type PutOptions struct {
	Visibility Visibility
	MimeType   string
	Metadata   map[string]string
}

PutOptions contains options for Put operations

type S3Driver

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

S3Driver implements the Driver interface for AWS S3 storage

func NewS3Driver

func NewS3Driver(config DiskConfig) (*S3Driver, error)

NewS3Driver creates a new S3 storage driver

func (*S3Driver) AllDirectories

func (d *S3Driver) AllDirectories(directory string) ([]string, error)

AllDirectories lists all directories recursively

func (*S3Driver) AllFiles

func (d *S3Driver) AllFiles(directory string) ([]string, error)

AllFiles lists all files recursively in a directory

func (*S3Driver) Copy

func (d *S3Driver) Copy(from, to string) error

Copy copies a file from one path to another

func (*S3Driver) Delete

func (d *S3Driver) Delete(paths ...string) error

Delete removes files at the given paths

func (*S3Driver) DeleteDirectory

func (d *S3Driver) DeleteDirectory(directory string) error

DeleteDirectory deletes a directory and all its contents

func (*S3Driver) Directories

func (d *S3Driver) Directories(directory string) ([]string, error)

Directories lists directories

func (*S3Driver) Exists

func (d *S3Driver) Exists(path string) bool

Exists checks if a file exists at the given path

func (*S3Driver) Files

func (d *S3Driver) Files(directory string) ([]string, error)

Files lists files in a directory

func (*S3Driver) Get

func (d *S3Driver) Get(path string) ([]byte, error)

Get retrieves content from the given path

func (*S3Driver) GetStream

func (d *S3Driver) GetStream(path string) (io.ReadCloser, error)

GetStream retrieves a stream from the given path

func (*S3Driver) LastModified

func (d *S3Driver) LastModified(path string) (time.Time, error)

LastModified returns the last modified time of a file

func (*S3Driver) MakeDirectory

func (d *S3Driver) MakeDirectory(path string) error

MakeDirectory creates a directory (no-op for S3)

func (*S3Driver) MimeType

func (d *S3Driver) MimeType(path string) (string, error)

MimeType returns the MIME type of a file

func (*S3Driver) Move

func (d *S3Driver) Move(from, to string) error

Move moves a file from one path to another

func (*S3Driver) Put

func (d *S3Driver) Put(path string, contents []byte) error

Put stores content at the given path

func (*S3Driver) PutStream

func (d *S3Driver) PutStream(path string, stream io.Reader) error

PutStream stores a stream at the given path

func (*S3Driver) Size

func (d *S3Driver) Size(path string) (int64, error)

Size returns the size of a file at the given path

func (*S3Driver) TemporaryURL

func (d *S3Driver) TemporaryURL(path string, expiration time.Duration) (string, error)

TemporaryURL returns a temporary URL for a file

func (*S3Driver) URL

func (d *S3Driver) URL(path string) string

URL returns the public URL for a file

type Visibility

type Visibility string

Visibility defines file visibility

const (
	Public  Visibility = "public"
	Private Visibility = "private"
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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