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 ¶
- Variables
- type DBMetadataManager
- func (m *DBMetadataManager) Delete(ctx context.Context, tenantID, id string) error
- func (m *DBMetadataManager) Get(ctx context.Context, tenantID, id string) (*File, error)
- func (m *DBMetadataManager) GetByHash(ctx context.Context, tenantID, hash string) (*File, error)
- func (m *DBMetadataManager) GetByPath(ctx context.Context, tenantID, p string) (*File, error)
- func (m *DBMetadataManager) List(ctx context.Context, query Query) ([]*File, int64, error)
- func (m *DBMetadataManager) ListAll(ctx context.Context, query Query) ([]*File, int64, error)
- func (m *DBMetadataManager) Save(ctx context.Context, file *File) error
- func (m *DBMetadataManager) UpdateAccessTime(ctx context.Context, tenantID, id string) error
- type DBMetadataOption
- type File
- type LocalStorage
- func (s *LocalStorage) Copy(ctx context.Context, srcPath, dstPath string) error
- func (s *LocalStorage) Delete(ctx context.Context, path string) error
- func (s *LocalStorage) Exists(ctx context.Context, path string) (bool, error)
- func (s *LocalStorage) Get(ctx context.Context, path string) (io.ReadCloser, error)
- func (s *LocalStorage) GetURL(ctx context.Context, path string, expiry time.Duration) (string, error)
- func (s *LocalStorage) List(ctx context.Context, prefix string, limit int) ([]*storefile.FileStat, error)
- func (s *LocalStorage) Put(ctx context.Context, opts PutOptions) (*File, error)
- func (s *LocalStorage) Stat(ctx context.Context, path string) (*storefile.FileStat, error)
- type MetadataManager
- type PutOptions
- type Query
- type S3Config
- type S3Signer
- type S3Storage
- func (s *S3Storage) Copy(ctx context.Context, srcPath, dstPath string) error
- func (s *S3Storage) Delete(ctx context.Context, p string) error
- func (s *S3Storage) Exists(ctx context.Context, p string) (bool, error)
- func (s *S3Storage) Get(ctx context.Context, p string) (io.ReadCloser, error)
- func (s *S3Storage) GetURL(ctx context.Context, p string, expiry time.Duration) (string, error)
- func (s *S3Storage) List(ctx context.Context, prefix string, limit int) ([]*storefile.FileStat, error)
- func (s *S3Storage) Put(ctx context.Context, opts PutOptions) (*File, error)
- func (s *S3Storage) Stat(ctx context.Context, p string) (*storefile.FileStat, error)
- type Storage
Constants ¶
This section is empty.
Variables ¶
var ErrNilMetadataDB = errors.New("file metadata: database cannot be nil")
ErrNilMetadataDB is returned when DBMetadataManager has no database handle.
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) GetByHash ¶
GetByHash retrieves tenant-scoped file metadata by hash for deduplication.
func (*DBMetadataManager) ListAll ¶
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) 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.
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 ¶
NewS3Signer creates a new S3 signer.
func (*S3Signer) PresignRequest ¶
PresignRequest generates a presigned URL for the request.
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) List ¶
func (s *S3Storage) List(ctx context.Context, prefix string, limit int) ([]*storefile.FileStat, error)
List returns files in S3 storage matching the prefix.
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).