file

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 15, 2026 License: MIT Imports: 25 Imported by: 0

Documentation

Overview

Package file provides tenant-aware file storage implementations backed by the store/file interfaces. It adds tenant identity to every operation and organises stored objects by tenant in both the filesystem path and the metadata database.

Index

Constants

This section is empty.

Variables

View Source
var ErrNilMetadataDB = errors.New("file metadata: database cannot be nil")

ErrNilMetadataDB is returned when DBMetadataManager has no database handle.

View Source
var ErrTenantRequired = errors.New("file metadata: tenant id is required")

ErrTenantRequired is returned when a tenant-scoped metadata operation omits tenant id.

Functions

This section is empty.

Types

type DBMetadataManager

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

DBMetadataManager implements MetadataManager using a PostgreSQL database.

func NewDBMetadataManagerE

func NewDBMetadataManagerE(db *sql.DB, opts ...DBMetadataOption) (*DBMetadataManager, error)

NewDBMetadataManagerE creates a database-backed metadata manager and reports invalid dependencies without relying on later method calls.

func (*DBMetadataManager) Delete

func (m *DBMetadataManager) Delete(ctx context.Context, tenantID, id string) error

Delete soft-deletes file metadata.

func (*DBMetadataManager) Get

func (m *DBMetadataManager) Get(ctx context.Context, tenantID, id string) (*File, error)

Get retrieves file metadata by ID.

func (*DBMetadataManager) GetByHash

func (m *DBMetadataManager) GetByHash(ctx context.Context, tenantID, hash string) (*File, error)

GetByHash retrieves tenant-scoped file metadata by hash for deduplication.

func (*DBMetadataManager) GetByPath

func (m *DBMetadataManager) GetByPath(ctx context.Context, tenantID, p string) (*File, error)

GetByPath retrieves file metadata by path.

func (*DBMetadataManager) List

func (m *DBMetadataManager) List(ctx context.Context, query Query) ([]*File, int64, error)

List retrieves tenant-scoped file metadata matching the query.

func (*DBMetadataManager) ListAll

func (m *DBMetadataManager) ListAll(ctx context.Context, query Query) ([]*File, int64, error)

ListAll retrieves file metadata across tenants. It is an explicit admin surface; tenant-facing callers should use List with Query.TenantID.

func (*DBMetadataManager) Save

func (m *DBMetadataManager) Save(ctx context.Context, file *File) error

Save stores file metadata in the database.

func (*DBMetadataManager) UpdateAccessTime

func (m *DBMetadataManager) UpdateAccessTime(ctx context.Context, tenantID, id string) error

UpdateAccessTime updates the last access timestamp.

type DBMetadataOption

type DBMetadataOption func(*DBMetadataManager)

DBMetadataOption configures DBMetadataManager.

func WithMetadataClock

func WithMetadataClock(now func() time.Time) DBMetadataOption

WithMetadataClock configures the clock used for mutation timestamps.

type File

type File struct {
	ID            string         `json:"id" db:"id"`
	TenantID      string         `json:"tenant_id" db:"tenant_id"`
	Name          string         `json:"name" db:"name"`
	Path          string         `json:"path" db:"path"`
	Size          int64          `json:"size" db:"size"`
	MimeType      string         `json:"mime_type" db:"mime_type"`
	Extension     string         `json:"extension" db:"extension"`
	Hash          string         `json:"hash" db:"hash"`
	Width         int            `json:"width,omitempty" db:"width"`
	Height        int            `json:"height,omitempty" db:"height"`
	ThumbnailPath string         `json:"thumbnail_path,omitempty" db:"thumbnail_path"`
	StorageType   string         `json:"storage_type" db:"storage_type"`
	Metadata      map[string]any `json:"metadata,omitempty" db:"metadata"`
	UploadedBy    string         `json:"uploaded_by" db:"uploaded_by"`
	CreatedAt     time.Time      `json:"created_at" db:"created_at"`
	UpdatedAt     time.Time      `json:"updated_at" db:"updated_at"`
	LastAccessAt  *time.Time     `json:"last_access_at,omitempty" db:"last_access_at"`
	DeletedAt     *time.Time     `json:"deleted_at,omitempty" db:"deleted_at"`
}

File extends the core file record with tenant identity.

type LocalStorage

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

LocalStorage implements Storage using the local filesystem. Files are organised as: basePath/{tenantID}/{YYYY}/{MM}/{DD}/{id}{ext}

func NewLocalStorage

func NewLocalStorage(basePath, baseURL string, metadata MetadataManager) (*LocalStorage, error)

NewLocalStorage creates a new local filesystem storage.

func (*LocalStorage) Copy

func (s *LocalStorage) Copy(ctx context.Context, srcPath, dstPath string) error

Copy copies a file within local storage.

func (*LocalStorage) Delete

func (s *LocalStorage) Delete(ctx context.Context, path string) error

Delete removes a file from local storage.

func (*LocalStorage) Exists

func (s *LocalStorage) Exists(ctx context.Context, path string) (bool, error)

Exists checks if a file exists in local storage.

func (*LocalStorage) Get

func (s *LocalStorage) Get(ctx context.Context, path string) (io.ReadCloser, error)

Get retrieves a file from local storage.

func (*LocalStorage) GetURL

func (s *LocalStorage) GetURL(ctx context.Context, path string, expiry time.Duration) (string, error)

GetURL returns a static URL for accessing the file.

func (*LocalStorage) List

func (s *LocalStorage) List(ctx context.Context, prefix string, limit int) ([]*storefile.FileStat, error)

List returns files in local storage matching the prefix.

func (*LocalStorage) Put

func (s *LocalStorage) Put(ctx context.Context, opts PutOptions) (*File, error)

Put uploads a file to local storage under the tenant's directory tree.

func (*LocalStorage) Stat

func (s *LocalStorage) Stat(ctx context.Context, path string) (*storefile.FileStat, error)

Stat returns file information from local storage.

type MetadataManager

type MetadataManager interface {
	Save(ctx context.Context, file *File) error
	Get(ctx context.Context, tenantID, id string) (*File, error)
	GetByPath(ctx context.Context, tenantID, path string) (*File, error)
	GetByHash(ctx context.Context, tenantID, hash string) (*File, error)
	List(ctx context.Context, query Query) ([]*File, int64, error) // requires Query.TenantID
	Delete(ctx context.Context, tenantID, id string) error
	UpdateAccessTime(ctx context.Context, tenantID, id string) error
}

MetadataManager manages tenant-scoped file metadata in a persistent store.

func NewDBMetadataManager

func NewDBMetadataManager(db *sql.DB, opts ...DBMetadataOption) MetadataManager

NewDBMetadataManager creates a new database-backed metadata manager.

type PutOptions

type PutOptions struct {
	TenantID      string         // Tenant ID for path isolation and metadata
	Reader        io.Reader      // File content
	FileName      string         // Original filename
	ContentType   string         // MIME type
	Size          int64          // File size (-1 if unknown)
	UploadedBy    string         // User ID of uploader
	GenerateThumb bool           // Whether to generate thumbnail
	ThumbWidth    int            // Thumbnail width (default 200)
	ThumbHeight   int            // Thumbnail height (default 200)
	Metadata      map[string]any // Additional metadata
}

PutOptions contains options for uploading a file, including the tenant under which the file should be stored.

type Query

type Query struct {
	TenantID   string
	UploadedBy string
	MimeType   string
	StartTime  time.Time
	EndTime    time.Time
	Page       int
	PageSize   int
	OrderBy    string
}

Query contains parameters for querying tenant-scoped file metadata.

type S3Config

type S3Config struct {
	Endpoint          string
	Region            string
	Bucket            string
	AccessKey         string
	SecretKey         string
	UseSSL            bool
	PathStyle         bool // Path-style (true) or virtual-hosted-style (false)
	TempDir           string
	MaxSinglePutBytes int64 // Maximum object size for the current single PUT implementation; 0 uses the default.
}

S3Config contains the provider-specific settings required by S3Storage.

type S3Signer

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

S3Signer implements AWS Signature Version 4 signing.

func NewS3Signer

func NewS3Signer(accessKey, secretKey, region string) *S3Signer

NewS3Signer creates a new S3 signer.

func (*S3Signer) PresignRequest

func (s *S3Signer) PresignRequest(req *http.Request, expiry time.Duration) (string, error)

PresignRequest generates a presigned URL for the request.

func (*S3Signer) SignRequest

func (s *S3Signer) SignRequest(req *http.Request, payloadHash string) error

SignRequest signs an HTTP request using AWS Signature V4.

type S3Storage

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

S3Storage implements Storage using an S3-compatible object store. Objects are organised as: {tenantID}/{YYYY}/{MM}/{DD}/{id}{ext}

func NewS3Storage

func NewS3Storage(config S3Config, metadata MetadataManager) (*S3Storage, error)

NewS3Storage creates a new S3-compatible storage.

func (*S3Storage) Copy

func (s *S3Storage) Copy(ctx context.Context, srcPath, dstPath string) error

Copy copies a file within S3 storage.

func (*S3Storage) Delete

func (s *S3Storage) Delete(ctx context.Context, p string) error

Delete removes a file from S3 storage.

func (*S3Storage) Exists

func (s *S3Storage) Exists(ctx context.Context, p string) (bool, error)

Exists checks if a file exists in S3 storage.

func (*S3Storage) Get

func (s *S3Storage) Get(ctx context.Context, p string) (io.ReadCloser, error)

Get retrieves a file from S3 storage.

func (*S3Storage) GetURL

func (s *S3Storage) GetURL(ctx context.Context, p string, expiry time.Duration) (string, error)

GetURL returns a presigned URL for accessing the file.

func (*S3Storage) List

func (s *S3Storage) List(ctx context.Context, prefix string, limit int) ([]*storefile.FileStat, error)

List returns files in S3 storage matching the prefix.

func (*S3Storage) Put

func (s *S3Storage) Put(ctx context.Context, opts PutOptions) (*File, error)

Put uploads a file to S3 storage under the tenant's key prefix.

func (*S3Storage) Stat

func (s *S3Storage) Stat(ctx context.Context, p string) (*storefile.FileStat, error)

Stat returns file information from S3 storage.

type Storage

type Storage interface {
	Put(ctx context.Context, opts PutOptions) (*File, error)
	Get(ctx context.Context, path string) (io.ReadCloser, error)
	Delete(ctx context.Context, path string) error
	Exists(ctx context.Context, path string) (bool, error)
	Stat(ctx context.Context, path string) (*storefile.FileStat, error)
	List(ctx context.Context, prefix string, limit int) ([]*storefile.FileStat, error)
	GetURL(ctx context.Context, path string, expiry time.Duration) (string, error)
	Copy(ctx context.Context, srcPath, dstPath string) error
}

Storage is a tenant-aware superset of store/file.Storage. It accepts PutOptions (which carries TenantID) and returns tenant-tagged File records. The Get/Delete/Exists/Stat/List/GetURL/Copy methods operate on paths that already encode the tenant (as set by Put).

Jump to

Keyboard shortcuts

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