storage

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2026 License: MIT Imports: 22 Imported by: 0

Documentation

Overview

Package storage provides high-level file upload, download, validation, image helpers, and storage driver dispatch.

Index

Constants

View Source
const (
	OperationUpload   = "upload"
	OperationDownload = "download"
	OperationGet      = "get"
	OperationDelete   = "delete"
	OperationList     = "list"
	OperationSource   = "source"
	OperationToken    = "token"

	HookBeforeUpload        = "BeforeUpload"
	HookAfterUpload         = "AfterUpload"
	HookAfterUploadFailed   = "AfterUploadFailed"
	HookBeforeDownload      = "BeforeDownload"
	HookAfterDownload       = "AfterDownload"
	HookAfterDownloadFailed = "AfterDownloadFailed"
	HookBeforeGet           = "BeforeGet"
	HookAfterGet            = "AfterGet"
	HookAfterGetFailed      = "AfterGetFailed"
	HookBeforeDelete        = "BeforeDelete"
	HookAfterDelete         = "AfterDelete"
	HookAfterDeleteFailed   = "AfterDeleteFailed"
	HookBeforeList          = "BeforeList"
	HookAfterList           = "AfterList"
	HookAfterListFailed     = "AfterListFailed"
	HookBeforeSource        = "BeforeSource"
	HookAfterSource         = "AfterSource"
	HookAfterSourceFailed   = "AfterSourceFailed"
	HookBeforeToken         = "BeforeToken"
	HookAfterToken          = "AfterToken"
	HookAfterTokenFailed    = "AfterTokenFailed"
)

Variables

This section is empty.

Functions

func CropImage

func CropImage(reader io.Reader, writer io.Writer, rect CropRect, format string, quality int) error

func HookValidateFile

func HookValidateFile(ctx context.Context, event Event) error

func RegisterDriver added in v0.1.4

func RegisterDriver(driver string, factory DriverFactory)

func SourceHandler

func SourceHandler(fs *FileSystem, secret string) http.Handler

func ValidateImageInfo

func ValidateImageInfo(info ImageInfo, limit ImageLimit) error

Types

type CropRect

type CropRect struct {
	X      int
	Y      int
	Width  int
	Height int
}

type DeleteResult

type DeleteResult struct {
	Paths   []string
	Deleted bool
	Error   error
}

func (DeleteResult) AfterFailed

func (r DeleteResult) AfterFailed() bool

func (DeleteResult) OK

func (r DeleteResult) OK() bool

type DownloadResult

type DownloadResult struct {
	File       core.FileInfo
	Downloaded bool
	Error      error
}

func (DownloadResult) AfterFailed

func (r DownloadResult) AfterFailed() bool

func (DownloadResult) OK

func (r DownloadResult) OK() bool

type DriverFactory added in v0.1.4

type DriverFactory func(cfg core.Config) (core.Handler, error)

type Event

type Event struct {
	Store      string
	Operation  string
	Stage      string
	File       core.FileInfo
	Reader     io.ReadCloser
	Path       string
	Paths      []string
	Objects    []core.Object
	Source     string
	Credential *core.UploadCredential
	Err        error
	Metadata   map[string]any
}

type FileSystem

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

func New

func New(cfg *core.Config) (*FileSystem, error)

func NewFileSystem

func NewFileSystem(policy core.StoragePolicy, opts ...Option) (*FileSystem, error)

func NewFromPolicy

func NewFromPolicy(policy core.StoragePolicy, opts ...Option) (*FileSystem, error)

func (*FileSystem) AccessPath added in v0.1.3

func (fs *FileSystem) AccessPath(objectPath string) string

func (*FileSystem) CDNURL added in v0.1.3

func (fs *FileSystem) CDNURL() string

func (*FileSystem) CleanHooks

func (fs *FileSystem) CleanHooks(stage string)

func (*FileSystem) Close

func (fs *FileSystem) Close() error

func (*FileSystem) Config

func (fs *FileSystem) Config() core.Config

func (*FileSystem) Delete

func (fs *FileSystem) Delete(paths ...string) error

func (*FileSystem) DeleteWithHook

func (fs *FileSystem) DeleteWithHook(ctx context.Context, paths []string, opts ...HookOption) DeleteResult

func (*FileSystem) DispatchHandler

func (fs *FileSystem) DispatchHandler() error

func (*FileSystem) Download

func (fs *FileSystem) Download(ctx context.Context, path string, writer io.Writer, progress func(downloaded, total int64)) DownloadResult

func (*FileSystem) DownloadWithHook

func (fs *FileSystem) DownloadWithHook(ctx context.Context, path string, writer io.Writer, progress func(downloaded, total int64), opts ...HookOption) DownloadResult

func (*FileSystem) Get

func (fs *FileSystem) Get(path string) (io.ReadCloser, error)

func (*FileSystem) GetWithHook

func (fs *FileSystem) GetWithHook(ctx context.Context, path string, opts ...HookOption) GetResult

func (*FileSystem) Handler

func (fs *FileSystem) Handler() core.Handler

func (*FileSystem) InitUpload

func (fs *FileSystem) InitUpload(ctx context.Context, req UploadInitRequest) (*core.UploadCredential, error)

func (*FileSystem) InitUploadWithHook

func (fs *FileSystem) InitUploadWithHook(ctx context.Context, req UploadInitRequest, opts ...HookOption) (*core.UploadCredential, error)

func (*FileSystem) List

func (fs *FileSystem) List(path string) ([]core.Object, error)

func (*FileSystem) ListWithHook

func (fs *FileSystem) ListWithHook(ctx context.Context, path string, opts ...HookOption) ListResult

func (*FileSystem) ObjectPath added in v0.1.3

func (fs *FileSystem) ObjectPath(value string) string

func (*FileSystem) Put

func (fs *FileSystem) Put(file core.FileHeader) UploadResult

func (*FileSystem) PutWithHook

func (fs *FileSystem) PutWithHook(ctx context.Context, file core.FileHeader, opts ...HookOption) UploadResult

func (*FileSystem) Recycle

func (fs *FileSystem) Recycle()

func (*FileSystem) RegisterHook

func (fs *FileSystem) RegisterHook(stage string, hook Hook) error

func (*FileSystem) Source

func (fs *FileSystem) Source(path string, ttl time.Duration) (string, error)

func (*FileSystem) SourceWithHook

func (fs *FileSystem) SourceWithHook(ctx context.Context, path string, ttl time.Duration, opts ...HookOption) SourceResult

func (*FileSystem) Token

func (fs *FileSystem) Token(info core.FileInfo, ttl time.Duration) (*core.UploadCredential, error)

func (*FileSystem) TokenWithHook

func (fs *FileSystem) TokenWithHook(ctx context.Context, info core.FileInfo, ttl time.Duration, opts ...HookOption) TokenResult

func (*FileSystem) Trigger

func (fs *FileSystem) Trigger(ctx context.Context, stage string, event Event, localHooks ...Hook) error

func (*FileSystem) Upload

func (fs *FileSystem) Upload(ctx context.Context, file core.FileHeader) UploadResult

func (*FileSystem) UploadChunk

func (fs *FileSystem) UploadChunk(ctx context.Context, uploadID string, index int, reader io.Reader) (*UploadChunkResult, error)

func (*FileSystem) UploadChunkWithHook

func (fs *FileSystem) UploadChunkWithHook(ctx context.Context, uploadID string, index int, reader io.Reader, opts ...HookOption) (*UploadChunkResult, error)

func (*FileSystem) UploadCroppedImage

func (fs *FileSystem) UploadCroppedImage(ctx context.Context, reader io.Reader, info core.FileInfo, rect CropRect, format string, quality int) UploadResult

func (*FileSystem) UploadCroppedImageWithHook

func (fs *FileSystem) UploadCroppedImageWithHook(ctx context.Context, reader io.Reader, info core.FileInfo, rect CropRect, format string, quality int, opts ...HookOption) UploadResult

func (*FileSystem) UploadFromURL

func (fs *FileSystem) UploadFromURL(ctx context.Context, rawURL string, info core.FileInfo) UploadResult

func (*FileSystem) UploadFromURLWithHook

func (fs *FileSystem) UploadFromURLWithHook(ctx context.Context, rawURL string, info core.FileInfo, opts ...HookOption) UploadResult

func (*FileSystem) UploadStatus

func (fs *FileSystem) UploadStatus(uploadID string) (*UploadStatus, error)

func (*FileSystem) UploadStream

func (fs *FileSystem) UploadStream(ctx context.Context, reader io.Reader, info core.FileInfo) UploadResult

func (*FileSystem) UploadStreamWithHook

func (fs *FileSystem) UploadStreamWithHook(ctx context.Context, reader io.Reader, info core.FileInfo, opts ...HookOption) UploadResult

func (*FileSystem) UploadWithHook

func (fs *FileSystem) UploadWithHook(ctx context.Context, file core.FileHeader, opts ...HookOption) UploadResult

func (*FileSystem) ValidateExtension

func (fs *FileSystem) ValidateExtension(name, path string) bool

func (*FileSystem) ValidateFileInfo

func (fs *FileSystem) ValidateFileInfo(file core.FileInfo) error

func (*FileSystem) ValidateFileName

func (fs *FileSystem) ValidateFileName(name, path string) bool

func (*FileSystem) ValidateFileSize

func (fs *FileSystem) ValidateFileSize(size int64) bool

type GetResult

type GetResult struct {
	File   core.FileInfo
	Reader io.ReadCloser
	Opened bool
	Error  error
}

type Hook

type Hook func(ctx context.Context, event Event) error

type HookOption

type HookOption func(*operationHooks)

func AfterDelete

func AfterDelete(hook Hook) HookOption

func AfterDeleteFailed

func AfterDeleteFailed(hook Hook) HookOption

func AfterDownload

func AfterDownload(hook Hook) HookOption

func AfterDownloadFailed

func AfterDownloadFailed(hook Hook) HookOption

func AfterGet

func AfterGet(hook Hook) HookOption

func AfterGetFailed

func AfterGetFailed(hook Hook) HookOption

func AfterList

func AfterList(hook Hook) HookOption

func AfterListFailed

func AfterListFailed(hook Hook) HookOption

func AfterSource

func AfterSource(hook Hook) HookOption

func AfterSourceFailed

func AfterSourceFailed(hook Hook) HookOption

func AfterToken

func AfterToken(hook Hook) HookOption

func AfterTokenFailed

func AfterTokenFailed(hook Hook) HookOption

func AfterUpload

func AfterUpload(hook Hook) HookOption

func AfterUploadFailed

func AfterUploadFailed(hook Hook) HookOption

func BeforeDelete

func BeforeDelete(hook Hook) HookOption

func BeforeDownload

func BeforeDownload(hook Hook) HookOption

func BeforeGet

func BeforeGet(hook Hook) HookOption

func BeforeList

func BeforeList(hook Hook) HookOption

func BeforeSource

func BeforeSource(hook Hook) HookOption

func BeforeToken

func BeforeToken(hook Hook) HookOption

func BeforeUpload

func BeforeUpload(hook Hook) HookOption

func HookMetadata

func HookMetadata(key string, value any) HookOption

type ImageInfo

type ImageInfo struct {
	Width  int
	Height int
	Format string
}

func DecodeImageInfo

func DecodeImageInfo(reader io.Reader) (ImageInfo, error)

type ImageLimit

type ImageLimit struct {
	MaxWidth  int
	MaxHeight int
	MinWidth  int
	MinHeight int
}

type ListResult

type ListResult struct {
	Path    string
	Objects []core.Object
	Listed  bool
	Error   error
}

func (ListResult) AfterFailed

func (r ListResult) AfterFailed() bool

func (ListResult) OK

func (r ListResult) OK() bool

type Option

type Option func(*core.Config)

func WithAllowedExtensions

func WithAllowedExtensions(exts ...string) Option

func WithChunkSize

func WithChunkSize(chunkSize int64) Option

func WithMaxSize

func WithMaxSize(maxSize int64) Option

func WithNameRules

func WithNameRules(dirRule, fileRule string) Option

func WithTempDir

func WithTempDir(tempDir string) Option

func WithTokenTTL

func WithTokenTTL(ttl time.Duration) Option

type SourceResult

type SourceResult struct {
	Path   string
	Source string
	Signed bool
	Error  error
}

func (SourceResult) AfterFailed

func (r SourceResult) AfterFailed() bool

func (SourceResult) OK

func (r SourceResult) OK() bool

type TokenResult

type TokenResult struct {
	File       core.FileInfo
	Credential *core.UploadCredential
	Issued     bool
	Error      error
}

func (TokenResult) AfterFailed

func (r TokenResult) AfterFailed() bool

func (TokenResult) OK

func (r TokenResult) OK() bool

type UploadChunkResult

type UploadChunkResult struct {
	UploadID       string `json:"upload_id"`
	ChunkIndex     int    `json:"chunk_index"`
	Received       int    `json:"received"`
	ReceivedText   string `json:"received_text"`
	ChunkNum       int    `json:"chunk_num"`
	Done           bool   `json:"done"`
	Path           string `json:"path"`
	FilePath       string `json:"file_path"`
	AccessPath     string `json:"access_path"`
	UploadComplete bool   `json:"upload_complete"`
}

type UploadInitRequest

type UploadInitRequest struct {
	FileName  string
	Path      string
	TotalSize int64
	MIMEType  string
	Metadata  map[string]string
}

type UploadResult

type UploadResult struct {
	File       core.FileInfo
	Path       string
	ObjectPath string
	Uploaded   bool
	Error      error
}

func (UploadResult) AfterFailed

func (r UploadResult) AfterFailed() bool

func (UploadResult) OK

func (r UploadResult) OK() bool

type UploadSession

type UploadSession struct {
	UploadID  string
	FileName  string
	FilePath  string
	TotalSize int64
	MIMEType  string
	Metadata  map[string]string
	Chunks    *chunk.ChunkUpload
	// contains filtered or unexported fields
}

type UploadStatus

type UploadStatus struct {
	UploadID  string `json:"upload_id"`
	FileName  string `json:"file_name"`
	FilePath  string `json:"file_path"`
	ChunkNum  int    `json:"chunk_num"`
	Received  []int  `json:"received"`
	TotalSize int64  `json:"total_size"`
}

Directories

Path Synopsis
Package core defines shared storage contracts, metadata, config, and small helpers.
Package core defines shared storage contracts, metadata, config, and small helpers.
driver
cos
oss
s3

Jump to

Keyboard shortcuts

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