storage

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildUploadFileName added in v0.0.3

func BuildUploadFileName(fileName string, strategy UploadNamingStrategy) (string, error)

BuildUploadFileName builds the upload file name by strategy.

func DefaultKeyBuilder

func DefaultKeyBuilder(prefix string) func(fileName string, dir ...string) string

DefaultKeyBuilder creates a key builder function that generates unique file keys. It combines timestamp, MD5 hash of the filename, and preserves the file extension. The prefix is prepended to the generated key if provided. Format: [prefix_]timestamp_md5hash.ext

func JoinUploadKey added in v0.0.3

func JoinUploadKey(prefixDir string, bizDir string, fileName string) (string, error)

JoinUploadKey joins configured prefix dir, business dir and file name into a safe key.

func KeepFileNameKeyBuilder

func KeepFileNameKeyBuilder() func(fileName string, dir ...string) string

KeepFileNameKeyBuilder creates a key builder that preserves the original filename. It generates a unique directory path using timestamp and MD5 hash, then stores the file with its original name within that directory. Format: timestamp_md5hash/original_filename

Types

type CDNStorage

type CDNStorage interface {
	Storage
	URLHandler
	UploadAuthorizer
}

CDNStorage extends Storage with token generation for secure uploads. This interface is suitable for cloud storage backends that support direct client uploads.

type DownloadResult added in v0.0.3

type DownloadResult struct {
	Reader io.ReadCloser
	MIME   string
	Size   int64
}

DownloadResult is the structured output for download operations.

type FileDeleter

type FileDeleter interface {
	// DeleteFile removes a file from the storage backend.
	DeleteFile(ctx context.Context, key string) error
}

FileDeleter provides file deletion capabilities.

type FileDownloader

type FileDownloader interface {
	// IsFileExists checks whether a file exists in the storage backend.
	IsFileExists(ctx context.Context, key string) (bool, error)

	// DownloadFile retrieves a file from storage.
	// The caller is responsible for closing DownloadResult.Reader.
	DownloadFile(ctx context.Context, key string) (DownloadResult, error)
}

FileDownloader provides file download and existence checking capabilities.

type FileMoverCopier

type FileMoverCopier interface {
	// MoveFile relocates a file from source to destination key.
	// If overwrite is false, returns an error if destination already exists.
	MoveFile(ctx context.Context, sourceKey string, destinationKey string, overwrite bool) error

	// CopyFile duplicates a file from source to destination key.
	// If overwrite is false, returns an error if destination already exists.
	CopyFile(ctx context.Context, sourceKey string, destinationKey string, overwrite bool) error
}

FileMoverCopier provides file moving and copying operations within the storage backend.

type FileUploader

type FileUploader interface {
	// UploadFile uploads data from a reader to the storage backend with the specified key.
	// Returns the storage key or an error if upload fails.
	UploadFile(ctx context.Context, file io.Reader, key string) (string, error)

	// UploadLocalFile uploads a local file to the storage backend with the specified key.
	// Returns the storage key or an error if upload fails.
	UploadLocalFile(ctx context.Context, file string, key string) (string, error)
}

FileUploader provides file upload capabilities to the storage backend.

type Storage

Storage combines the core file storage operations in a single interface. This is the primary interface for most file storage use cases.

type URLHandler

type URLHandler interface {
	// GenerateURL creates a public URL for accessing the file identified by the given key.
	// Optional params are encoded into query string. When multiple values are provided,
	// only the first non-nil params is used.
	GenerateURL(key string, params ...url.Values) string

	// GenerateURLs creates public URLs for multiple files in batch.
	// Optional params are encoded into query string for each generated URL.
	GenerateURLs(keys []string, params ...url.Values) []string

	// ExtractKeyFromURL extracts the storage key from a given URL.
	ExtractKeyFromURL(uri string) string

	// ExtractKeyFromURLWithMode extracts the storage key from a URL with strict mode option.
	// When strict is true, returns an error if the URL format is invalid.
	ExtractKeyFromURLWithMode(uri string, strict bool) (string, error)
}

URLHandler provides URL generation and key extraction capabilities for storage backends. This interface enables URL-based access to stored files and reverse key lookup from URLs.

type UploadAuthRequest added in v0.0.3

type UploadAuthRequest struct {
	FileName string `json:"file_name" yaml:"file_name"`
	Dir      string `json:"dir,omitempty" yaml:"dir,omitempty"`
}

UploadAuthRequest describes the input for upload authorization generation.

type UploadAuthResult added in v0.0.3

type UploadAuthResult struct {
	Authorization UploadAuthorization `json:"authorization" yaml:"authorization"`
	File          UploadFileInfo      `json:"file" yaml:"file"`
}

UploadAuthResult is the structured result for generating upload authorization.

type UploadAuthorization added in v0.0.3

type UploadAuthorization struct {
	Type    UploadAuthorizationType `json:"type" yaml:"type"`
	Value   string                  `json:"value" yaml:"value"`
	Method  string                  `json:"method" yaml:"method"`
	Headers map[string]string       `json:"headers,omitempty" yaml:"headers,omitempty"`
}

UploadAuthorization carries the upload authorization data for client-side uploads.

type UploadAuthorizationType added in v0.0.3

type UploadAuthorizationType string

UploadAuthorizationType indicates how upload authorization should be interpreted by clients.

const (
	UploadAuthorizationTypeURL   UploadAuthorizationType = "url"
	UploadAuthorizationTypeToken UploadAuthorizationType = "token"
)

type UploadAuthorizer added in v0.0.3

type UploadAuthorizer interface {
	// GenerateUploadAuth creates upload authorization and target file information.
	GenerateUploadAuth(ctx context.Context, req UploadAuthRequest) (UploadAuthResult, error)
}

UploadAuthorizer provides secure upload authorization generation for client-side uploads. This is commonly used for direct-to-storage uploads from web browsers or mobile apps.

type UploadFileInfo added in v0.0.3

type UploadFileInfo struct {
	Key string `json:"key" yaml:"key"`
	URL string `json:"url" yaml:"url"`
}

UploadFileInfo contains the finalized storage information for an upload.

type UploadNamingStrategy added in v0.0.3

type UploadNamingStrategy string

UploadNamingStrategy controls how upload file names are generated.

const (
	UploadNamingStrategyRandomExt UploadNamingStrategy = "random_ext"
	UploadNamingStrategyHashExt   UploadNamingStrategy = "hash_ext"
	UploadNamingStrategyOriginal  UploadNamingStrategy = "original"
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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