blaze

package
v0.31.1 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2021 License: MIT Imports: 23 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidHandle = xo.BF("invalid handle")

ErrInvalidHandle is returned if the provided handle is invalid.

View Source
var ErrInvalidPosition = xo.BF("invalid position")

ErrInvalidPosition is returned if a seek resulted in an invalid position.

View Source
var ErrNotFound = xo.BF("not found")

ErrNotFound is returned if there is no blob for the provided handle.

View Source
var ErrUsedHandle = xo.BF("used handle")

ErrUsedHandle is returned if the provided handle has already been used.

Functions

func AddFileIndexes added in v0.26.0

func AddFileIndexes(catalog *coal.Catalog)

AddFileIndexes will add files indexes to the specified catalog.

func DownloadTo added in v0.28.1

func DownloadTo(download Download, writer io.Writer) error

DownloadTo will download a blob to the provided writer.

func UploadFrom added in v0.28.1

func UploadFrom(upload Upload, reader io.Reader) (int64, error)

UploadFrom will upload a blob from the provided reader.

func ValidateType added in v0.29.0

func ValidateType(str string, whitelist ...string) error

ValidateType will validate a media type.

Types

type Binding added in v0.29.0

type Binding struct {
	// The name e.g. "user-avatar".
	Name string

	// The owner model.
	Owner coal.Model

	// The link field on the model.
	Field string

	// The file size limit.
	Limit int64

	// The allowed media types.
	Types []string

	// The forced filename for downloads.
	Filename string
}

Binding describes the binding if a file to a model.

type ClaimKey added in v0.26.0

type ClaimKey struct {
	heat.Base `json:"-" heat:"fire/blaze.claim,1h"`

	// The claimable file.
	File coal.ID `json:"file"`

	// The files name.
	Name string `json:"name"`

	// The files size.
	Size int64 `json:"size"`

	// The files media type.
	Type string `json:"type"`
}

ClaimKey is used to authorize file claims.

func (*ClaimKey) Validate added in v0.26.0

func (k *ClaimKey) Validate() error

Validate will validate the claim key.

type CleanupJob added in v0.28.0

type CleanupJob struct {
	axe.Base `json:"-" axe:"fire/blaze.cleanup"`
	stick.NoValidation
}

CleanupJob is the periodic job enqueued to cleanup a storage.

type Download added in v0.28.1

type Download interface {
	Seek(offset int64, whence int) (int64, error)
	Read(buf []byte) (int, error)
	Close() error
}

Download handles the download of a blob.

type File added in v0.26.0

type File struct {
	coal.Base `json:"-" bson:",inline" coal:"files"`

	// The current state of the file e.g. "uploading".
	State State `json:"state"`

	// The last time the file was updated.
	Updated time.Time `json:"updated-at" bson:"updated_at"`

	// The name of the file e.g. "logo.png".
	Name string `json:"name"`

	// The media type of the file e.g. "image/png".
	Type string `json:"type"`

	// The size of the file.
	Size int64 `json:"size"`

	// The service specific blob handle.
	Handle Handle `json:"handle"`

	// The binding of the file.
	Binding string `json:"binding"`

	// The owner of the file.
	Owner *coal.ID `json:"owner"`
}

File tracks uploaded files and their state.

func (*File) Validate added in v0.28.0

func (f *File) Validate() error

Validate will validate the model.

type GridFS added in v0.26.0

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

GridFS stores blobs in a GridFS bucket.

func NewGridFS added in v0.26.0

func NewGridFS(bucket *lungo.Bucket) *GridFS

NewGridFS creates a new GridFS service.

func (*GridFS) Cleanup added in v0.26.0

func (g *GridFS) Cleanup(_ context.Context) error

Cleanup implements the Service interface.

func (*GridFS) Delete added in v0.26.0

func (g *GridFS) Delete(ctx context.Context, handle Handle) error

Delete implements the Service interface.

func (*GridFS) Download added in v0.26.0

func (g *GridFS) Download(ctx context.Context, handle Handle) (Download, error)

Download implements the Service interface.

func (*GridFS) Initialize added in v0.28.2

func (g *GridFS) Initialize(ctx context.Context) error

Initialize will initialize the GridFS bucket.

func (*GridFS) Prepare added in v0.26.0

func (g *GridFS) Prepare(_ context.Context) (Handle, error)

Prepare implements the Service interface.

func (*GridFS) Upload added in v0.26.0

func (g *GridFS) Upload(ctx context.Context, handle Handle, _, _ string) (Upload, error)

Upload implements the Service interface.

type Handle added in v0.26.0

type Handle map[string]interface{}

Handle is a reference to a blob stored in a service.

type Link struct {
	// The user definable reference of the link.
	Ref string `json:"ref"`

	// The name of the linked files.
	Name string `json:"name" bson:"-"`

	// The media type of the linked file.
	Type string `json:"type" bson:"-"`

	// The size of the linked file.
	Size int64 `json:"size" bson:"-"`

	// The key for claiming a file.
	ClaimKey string `json:"claim-key" bson:"-"`

	// The key for viewing the linked file.
	ViewKey string `json:"view-key" bson:"-"`

	// The internal reference to the linked file.
	File coal.ID `json:"-" bson:"file_id"`

	// The internal information about the linked file.
	FileName string `json:"-" bson:"name"`
	FileType string `json:"-" bson:"type"`
	FileSize int64  `json:"-" bson:"size"`

	stick.BasicAccess `json:"-" bson:"-"`
}

Link is used to link a file to a model.

func (*Link) Validate added in v0.29.0

func (l *Link) Validate(requireFilename bool, whitelist ...string) error

Validate will validate the link.

type Links []Link

Links is a set of links.

func (*Links) UnmarshalBSONValue added in v0.29.1

func (l *Links) UnmarshalBSONValue(typ bsontype.Type, bytes []byte) error

UnmarshalBSONValue implements the bson.ValueUnmarshaler interface.

func (*Links) UnmarshalJSON added in v0.29.1

func (l *Links) UnmarshalJSON(bytes []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

func (Links) Validate added in v0.29.1

func (l Links) Validate(uniqueFilenames bool, whitelist ...string) error

Validate will validate the links.

type Memory added in v0.26.0

type Memory struct {
	// The stored blobs.
	Blobs map[string]*MemoryBlob

	// The next id.
	Next int
}

Memory is a service for testing purposes that stores blobs in memory.

func NewMemory added in v0.26.0

func NewMemory() *Memory

NewMemory will create a new memory service.

func (*Memory) Cleanup added in v0.26.0

func (m *Memory) Cleanup(_ context.Context) error

Cleanup implements the Service interface.

func (*Memory) Delete added in v0.26.0

func (m *Memory) Delete(_ context.Context, handle Handle) error

Delete implements the Service interface.

func (*Memory) Download added in v0.26.0

func (m *Memory) Download(_ context.Context, handle Handle) (Download, error)

Download implements the Service interface.

func (*Memory) Prepare added in v0.26.0

func (m *Memory) Prepare(_ context.Context) (Handle, error)

Prepare implements the Service interface.

func (*Memory) Upload added in v0.26.0

func (m *Memory) Upload(_ context.Context, handle Handle, name, mediaType string) (Upload, error)

Upload implements the Service interface.

type MemoryBlob added in v0.29.0

type MemoryBlob struct {
	Name  string
	Type  string
	Bytes []byte
}

MemoryBlob is a blob stored by the memory service.

type Register added in v0.29.0

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

Register manages multiple bindings.

func NewRegister added in v0.29.0

func NewRegister() *Register

NewRegister creates and returns a new register.

func (*Register) Add added in v0.29.0

func (r *Register) Add(binding *Binding)

Add will add the specified binding to the register. The name of the binding must be unique among all registered bindings.

func (*Register) Get added in v0.29.0

func (r *Register) Get(name string) *Binding

Get will get the binding with the specified name.

func (*Register) Lookup added in v0.29.0

func (r *Register) Lookup(owner coal.Model, field string) *Binding

Lookup will lookup the binding for the field on the specified owner.

type Service added in v0.26.0

type Service interface {
	// Prepare should return a new handle for uploading a blob.
	Prepare(ctx context.Context) (Handle, error)

	// Upload should initiate the upload of a blob.
	Upload(ctx context.Context, handle Handle, name, mediaType string) (Upload, error)

	// Download should initiate the download of a blob.
	Download(ctx context.Context, handle Handle) (Download, error)

	// Delete should delete the blob.
	Delete(ctx context.Context, handle Handle) error

	// Cleanup is called periodically and allows the service to cleanup its
	// storage until the context is cancelled.
	Cleanup(ctx context.Context) error
}

Service is responsible for managing blobs.

type State added in v0.26.0

type State string

State describes the current state of a file. Usually, the state of a file always moves forward by one step but it may also jump directly to "deleting".

const (
	Uploading State = "uploading"
	Uploaded  State = "uploaded"
	Claimed   State = "claimed"
	Released  State = "released"
	Deleting  State = "deleting"
)

The individual states.

func (State) Valid added in v0.28.0

func (s State) Valid() bool

Valid returns whether the state is valid.

type Storage added in v0.26.0

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

Storage provides file storage services.

func NewStorage added in v0.26.0

func NewStorage(store *coal.Store, notary *heat.Notary, service Service, register *Register) *Storage

NewStorage creates a new storage.

func (*Storage) Claim added in v0.28.1

func (s *Storage) Claim(ctx context.Context, model coal.Model, field string) error

Claim will claim the link at the field on the provided model. The claimed link must be persisted in the same transaction as the claim to ensure consistency.

func (*Storage) ClaimFile added in v0.29.0

func (s *Storage) ClaimFile(ctx context.Context, claimKey, binding string, owner coal.ID) (*File, error)

ClaimFile will claim the file referenced by the provided claim key using the specified binding and owner.

func (s *Storage) ClaimLink(ctx context.Context, link *Link, binding string, owner coal.ID) error

ClaimLink will claim the provided link under the specified binding. The claimed link must be persisted in the same transaction as the claim to ensure consistency.

func (*Storage) Cleanup added in v0.26.0

func (s *Storage) Cleanup(ctx context.Context, retention time.Duration) error

Cleanup will remove obsolete files and remove their blobs. Files in the states "uploading" or "uploaded" are removed after the specified retention which defaults to one hour if zero. Files in the states "released" and "deleting" are removed immediately. It will also allow the service to cleanup.

func (*Storage) CleanupTask added in v0.26.0

func (s *Storage) CleanupTask(lifetime, timeout, periodicity, retention time.Duration) *axe.Task

CleanupTask will return a periodic task that can be run to periodically cleanup obsolete files.

func (*Storage) Decorate added in v0.28.1

func (s *Storage) Decorate(link *Link) error

Decorate will populate the view key of the provided link if a file is available.

func (*Storage) Decorator added in v0.26.0

func (s *Storage) Decorator(fields ...string) *fire.Callback

Decorator will generate view keys for all or just the specified link fields on the returned model or models.

func (*Storage) Download added in v0.28.1

func (s *Storage) Download(ctx context.Context, viewKey string) (Download, *File, error)

Download will initiate a download for the file referenced by the provided view key.

func (*Storage) DownloadAction added in v0.26.0

func (s *Storage) DownloadAction() *fire.Action

DownloadAction returns an action that allows downloading files using view keys. This action is usually publicly accessible.

func (*Storage) DownloadFile added in v0.29.1

func (s *Storage) DownloadFile(ctx context.Context, id coal.ID) (Download, *File, error)

DownloadFile will initiate a download for the specified file.

func (*Storage) Modifier added in v0.29.0

func (s *Storage) Modifier(fields ...string) *fire.Callback

Modifier will handle modifications on all or just the specified link fields on the model.

func (*Storage) Release added in v0.28.1

func (s *Storage) Release(ctx context.Context, model coal.Model, field string) error

Release will release the link at the field on the provided model. The released link must be persisted in the same transaction as the release to ensure consistency.

func (*Storage) ReleaseFile added in v0.29.0

func (s *Storage) ReleaseFile(ctx context.Context, file coal.ID) error

ReleaseFile will release the file with the provided id.

func (s *Storage) ReleaseLink(ctx context.Context, link *Link) error

ReleaseLink will release the provided link. The released link must be persisted in the same transaction as the release to ensure consistency.

func (*Storage) Upload added in v0.28.1

func (s *Storage) Upload(ctx context.Context, name, mediaType string, cb func(Upload) (int64, error)) (string, *File, error)

Upload will initiate and perform an upload using the provided callback and return a claim key and the uploaded file. Upload must be called outside of a transaction to ensure the uploaded file is tracked in case of errors.

func (*Storage) UploadAction added in v0.26.0

func (s *Storage) UploadAction(limit int64) *fire.Action

UploadAction returns an action that provides an upload service that stores files and returns claim keys. The action should be protected and only allow authorized clients.

type Upload added in v0.28.1

type Upload interface {
	// Resume() (int64, error)
	Write(data []byte) (int, error)
	// Suspend() (int64, error)
	Abort() error
	Close() error
}

Upload handles the upload of a blob.

type ViewKey added in v0.26.0

type ViewKey struct {
	heat.Base `json:"-" heat:"fire/blaze.view,24h"`

	// The viewable file.
	File coal.ID `json:"file"`
}

ViewKey is used to authorize file views.

func (*ViewKey) Validate added in v0.26.0

func (k *ViewKey) Validate() error

Validate will validate the view key.

Jump to

Keyboard shortcuts

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