core

package
v0.1.21 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package core defines shared storage contracts, metadata, config, and small helpers.

Index

Constants

View Source
const (
	UploadModeLocalChunk   = "local_chunk"
	UploadModeDirectPut    = "direct_put"
	UploadModeMultipartPut = "multipart_put"
)
View Source
const (
	DefaultSourceTTL  = 7 * 24 * time.Hour
	DefaultSourcePath = "/storage/source"
)

Variables

View Source
var (
	ErrFileTooBig    = errors.New("文件过大")
	ErrFileExists    = errors.New("文件已存在")
	ErrFileNotFound  = errors.New("文件不存在")
	ErrNameInvalid   = errors.New("文件名不合法")
	ErrUnknownDriver = errors.New("未知存储驱动")
)
View Source
var (
	ErrSourceExpired          = errors.New("storage source url expired")
	ErrSourceSignatureInvalid = errors.New("storage source signature invalid")
	ErrSourceSecretRequired   = errors.New("storage source secret required")
)
View Source
var DefaultNamer = &Namer{
	DirRule:  "{date}",
	FileRule: "{uuid}{ext}",
}

Functions

func JoinObjectURL added in v0.1.3

func JoinObjectURL(baseURL, objectPath string) string

func NormalizeSourceTTL

func NormalizeSourceTTL(ttl time.Duration) time.Duration

func SignSourceURL

func SignSourceURL(baseURL, objectPath, secret string, ttl time.Duration, now time.Time) (string, error)

func SourceSignature

func SourceSignature(objectPath string, expires int64, secret string) string

func VerifySourceSignature

func VerifySourceSignature(objectPath, expires, signature, secret string, now time.Time) error

Types

type Config

type Config struct {
	StoreName         string        `mapstructure:"-" json:"-" yaml:"-"`
	DefaultStore      bool          `mapstructure:"-" json:"-" yaml:"-"`
	Driver            string        `mapstructure:"driver"`
	TempDir           string        `mapstructure:"temp_dir"`
	MaxSize           int64         `mapstructure:"max_size"`
	ChunkSize         int64         `mapstructure:"chunk_size"`
	TokenTTL          time.Duration `mapstructure:"token_ttl"`
	DirRule           string        `mapstructure:"dir_rule"`
	FileRule          string        `mapstructure:"file_rule"`
	AllowedExtensions []string      `mapstructure:"allowed_extensions"`
	Policy            StoragePolicy `mapstructure:"policy"`
	DriverConfig      DriverConfig  `mapstructure:",remain"`
}

func (Config) Clone

func (c Config) Clone() Config

func (Config) DriverBool

func (c Config) DriverBool(driver, key string) bool

func (Config) DriverString

func (c Config) DriverString(driver, key string) string

type DriverConfig

type DriverConfig map[string]map[string]any

type FileHeader

type FileHeader interface {
	io.Reader
	Info() FileInfo
}

FileHeader 上传文件接口

type FileInfo

type FileInfo struct {
	Name     string
	Path     string
	Size     int64
	MIMEType string
	ModTime  time.Time
	Progress func(uploaded, total int64)
	Metadata map[string]string
}

FileInfo 文件元信息

type FileStream

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

FileStream 文件流

func NewFileStream

func NewFileStream(reader io.Reader, info FileInfo) *FileStream

func (*FileStream) Close

func (f *FileStream) Close() error

func (*FileStream) Info

func (f *FileStream) Info() FileInfo

func (*FileStream) Read

func (f *FileStream) Read(p []byte) (int, error)

type Handler

type Handler interface {
	// Put 服务端上传文件
	Put(file FileHeader) error
	// Get 获取文件内容
	Get(path string) (io.ReadCloser, error)
	// Delete 删除文件
	Delete(paths ...string) error
	// List 列出目录
	List(path string) ([]Object, error)
	// Source 获取文件访问 URL
	Source(path string, ttl time.Duration) (string, error)
	// Token 生成客户端直传凭证(S3 返回 presigned URL,local 返回路径)
	Token(fileInfo FileInfo, ttl time.Duration) (*UploadCredential, error)
}

Handler 文件系统存储驱动接口

type Namer

type Namer struct {
	DirRule  string // 目录规则
	FileRule string // 文件名规则
}

Namer 文件名生成器,支持模板变量 {uuid} / {randomkey16} / {randomkey8} / {timestamp} / {datetime} {date} / {year} / {month} / {day} / {hour} / {minute} / {second} {originname} / {ext}

func (*Namer) Generate

func (n *Namer) Generate(originName string) string

Generate 根据原始文件名生成存储路径

type Object

type Object struct {
	Name    string    `json:"name"`
	Path    string    `json:"path"`
	Size    int64     `json:"size"`
	IsDir   bool      `json:"is_dir"`
	ModTime time.Time `json:"mod_time"`
}

type StoragePolicy

type StoragePolicy struct {
	Driver        string `mapstructure:"driver" json:"driver"`
	Name          string `mapstructure:"name" json:"name"`
	Bucket        string `mapstructure:"bucket" json:"bucket"`
	Endpoint      string `mapstructure:"endpoint" json:"endpoint"`
	EndpointInner string `mapstructure:"endpoint_inner" json:"endpoint_inner"`
	CDNURL        string `mapstructure:"cdn_url" json:"cdn_url"`
	Region        string `mapstructure:"region" json:"region"`
	AccessKey     string `mapstructure:"access_key" json:"access_key"`
	SecretKey     string `mapstructure:"secret_key" json:"secret_key"`
	LocalDir      string `mapstructure:"local_dir" json:"local_dir"`
}

StoragePolicy is a unified storage strategy shape for config files or DB rows. Driver decides how these common fields are mapped to a concrete SDK config.

func (StoragePolicy) Empty

func (p StoragePolicy) Empty() bool

type UploadCredential

type UploadCredential struct {
	Mode        string   `json:"mode"`         // 上传模式:local_chunk / direct_put / multipart_put
	UploadID    string   `json:"upload_id"`    // 上传会话 ID
	Gateway     string   `json:"gateway"`      // 存储类型
	ChunkSize   int64    `json:"chunk_size"`   // 分片大小,0 为不分片
	ChunkNum    int      `json:"chunk_num"`    // 总分片数
	UploadURLs  []string `json:"upload_urls"`  // 每片的直传 URL
	CompleteURL string   `json:"complete_url"` // 完成合并的 URL
	Path        string   `json:"path"`         // 存储路径
	AccessPath  string   `json:"access_path"`  // 前端访问值
}

UploadCredential 客户端直传凭证

Jump to

Keyboard shortcuts

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