Documentation
¶
Index ¶
- Constants
- Variables
- func IsStorageKey(storageKey string) bool
- func NormalizeIdentifier(identifier string) string
- func ValidateStorageKey(storageKey StorageKey) error
- type FileMeta
- type IFileMetaRepository
- type IStorageProvider
- type IStorageService
- type IUploadSessionTracker
- type StorageKey
- type UploadSession
Constants ¶
View Source
const Lens scene.ModuleName = "storage"
Variables ¶
View Source
var ( ErrStorageFailed = _eg.CreateError(1, "storage failed") ErrFileNotFound = _eg.CreateError(2, "file not found") ErrStorageNotFound = _eg.CreateError(3, "storage not found") ErrFailToLoad = _eg.CreateError(4, "fail to load") ErrFailToStore = _eg.CreateError(5, "fail to store") ErrFailToDelete = _eg.CreateError(6, "fail to delete") ErrLoadingMeta = _eg.CreateError(7, "loading meta") ErrInvalidStorageKey = _eg.CreateError(8, "invalid storage key") ErrStorageError = _eg.CreateError(9, "storage error") ErrInvalidOffset = _eg.CreateError(10, "invalid offset") ErrInvalidLength = _eg.CreateError(11, "invalid length") ErrUnknownError = _eg.CreateError(12, "unknown error") ErrInitPartUploadFailed = _eg.CreateError(13, "init part upload failed") ErrUploadSessionNotFound = _eg.CreateError(14, "upload session not found") ErrStorePartFailed = _eg.CreateError(15, "store part upload failed") ErrFailToAbortMultipartStore = _eg.CreateError(16, "fail to abort") ErrMetaNotFound = _eg.CreateError(17, "meta not found") ErrFailToListMeta = _eg.CreateError(18, "fail to list meta") ErrStorageKeyExists = _eg.CreateError(19, "storage key exists") ErrDirectURLUnsupported = _eg.CreateError(20, "direct URL unsupported") ErrGetDirectURLFailed = _eg.CreateError(21, "fail to get direct URL") )
View Source
var ( PermFileUpload = permission.Create("storage:file:upload") PermFileDelete = permission.Create("storage:file:delete") PermFileDownload = permission.Create("storage:file:download") PermFileList = permission.Create("storage:file:list") )
Functions ¶
func IsStorageKey ¶ added in v0.3.7
func NormalizeIdentifier ¶ added in v0.3.7
func ValidateStorageKey ¶ added in v0.3.7
func ValidateStorageKey(storageKey StorageKey) error
Types ¶
type FileMeta ¶
type FileMeta struct {
StorageKey StorageKey `json:"storage_key"`
Provider string `json:"provider"`
Identifier string `json:"identifier"`
OriginalFilename string `json:"original_filename"`
ContentType string `json:"content_type"`
ContentLength int64 `json:"content_length"`
Md5Checksum string `json:"md5_checksum"`
Finished bool `json:"finished"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
func OpenContent ¶ added in v0.3.7
func OpenContent(ctx context.Context, srv IStorageService, storageKey StorageKey) (io.ReadSeekCloser, FileMeta, error)
OpenContent opens storage content as a request-scoped seekable stream. Sequential reads share one provider reader. Seeking closes that reader and opens a new ranged reader lazily on the next Read call.
func (*FileMeta) FillMissing ¶
type IFileMetaRepository ¶
type IFileMetaRepository interface {
scene.Named
// Store will store the metadata in the repository
// will overwrite the old metadata if exists
Store(ctx context.Context, meta FileMeta) error
Load(ctx context.Context, storageKey StorageKey) (meta FileMeta, err error)
Delete(ctx context.Context, storageKey StorageKey) error
List(ctx context.Context, provider string, offset, limit int64) (model.PaginationResult[FileMeta], error)
}
type IStorageProvider ¶
type IStorageProvider interface {
scene.Named
// ProviderName return provider name in storageKey
ProviderName() string
// HealthCheck will check if this Storage provider is accessible
HealthCheck(ctx context.Context) error
// Meta will return any possible metadata can be found, as a fallback option if IFileMetaRepository failed
Meta(ctx context.Context, storageKey StorageKey) (meta FileMeta, err error)
Load(ctx context.Context, storageKey StorageKey, offset, length int64) (reader io.ReadCloser, err error)
LoadAll(ctx context.Context, storageKey StorageKey) (reader io.ReadCloser, err error)
// Store will store the data in the storage at path and return the storageKey,
// if path not exists, it will create the path.
Store(ctx context.Context, storageKey StorageKey, data io.Reader) (err error)
Delete(ctx context.Context, storageKey StorageKey) error
// Multipart related
InitMultipartStore(ctx context.Context, storageKey StorageKey) (uploadId string, err error)
StoreMultipart(ctx context.Context, uploadId string, partNumber int, data io.Reader) error
CompleteMultipart(ctx context.Context, uploadId string) error
AbortMultipart(ctx context.Context, uploadId string) error
// GetDirectURL returns a URL that accesses the provider directly.
// Providers that cannot expose direct URLs return ErrDirectURLUnsupported.
GetDirectURL(ctx context.Context, storageKey StorageKey) (url string, err error)
}
type IStorageService ¶
type IStorageService interface {
scene.Service
ListProviders() []string
// ListMeta will list meta from specific provider.
ListMeta(ctx context.Context, provider string, offset, limit int64) (model.PaginationResult[FileMeta], error)
// Meta return the meta given a storageKey
Meta(ctx context.Context, storageKey StorageKey) (meta FileMeta, err error)
// Load will load file stream at offset with length.
// Caller must close the returned reader.
Load(ctx context.Context, storageKey StorageKey, offset, length int64) (reader io.ReadCloser, err error)
// LoadAll will load full file stream.
// Caller must close the returned reader.
LoadAll(ctx context.Context, storageKey StorageKey) (reader io.ReadCloser, err error)
Delete(ctx context.Context, storageKey StorageKey) error
// Store will store data at default provider
// it calls StoreAt internally
// Store consumes data from reader until EOF.
Store(ctx context.Context, data io.Reader, meta FileMeta) (storageKey StorageKey, err error)
// StoreAt will store data using the given provider and identifier.
// If provider is empty, the service default provider will be used.
// If identifier is empty, the service will generate one internally.
StoreAt(ctx context.Context, provider, identifier string, data io.Reader, meta FileMeta) (storageKey StorageKey, err error)
// Multipart related
// InitMultipartStore will initialize a multipart upload using the given provider and identifier.
// If provider is empty, the service default provider will be used.
// If identifier is empty, the service will generate one internally.
// It returns both the resolved storage key and the upload id.
InitMultipartStore(ctx context.Context, provider, identifier string, meta FileMeta) (storageKey StorageKey, uploadId string, err error)
StoreMultipart(ctx context.Context, uploadId string, partNumber int, data io.Reader) error
// CompleteMultipart completes an upload and returns its finalized metadata.
CompleteMultipart(ctx context.Context, uploadId string) (FileMeta, error)
AbortMultipart(ctx context.Context, uploadId string) error
// GetDirectURL returns a URL that accesses the storage provider directly.
GetDirectURL(ctx context.Context, storageKey StorageKey) (url string, err error)
}
IStorageService is the storage module's service boundary. Every non-nil error returned directly by a service method must be a storage errcode; provider and repository errors, including context errors, are mapped at this boundary.
type IUploadSessionTracker ¶
type StorageKey ¶ added in v0.3.7
type StorageKey string
StorageKey is the unique identifier of a file in storage. it is composed with {Provider}://{ID} example: tos.buketName://objectName example: local.name://objectName
func NewStorageKey ¶ added in v0.3.7
func NewStorageKey(provider string, path ...string) StorageKey
func NewStorageKeyWithUUID ¶ added in v0.3.7
func NewStorageKeyWithUUID(provider string) StorageKey
func ParseStorageKey ¶ added in v0.3.7
func ParseStorageKey(storageKey string) (StorageKey, bool)
func (StorageKey) FileID ¶ added in v0.3.7
func (f StorageKey) FileID() string
func (StorageKey) Provider ¶ added in v0.3.7
func (f StorageKey) Provider() string
type UploadSession ¶
type UploadSession struct {
StorageKey StorageKey `json:"storage_key"`
Created time.Time `json:"created"`
}
UploadSession contains info to resume/complete uploads
Click to show internal directories.
Click to hide internal directories.