media

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2022 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrUnconfiguredDisk is returned when trying to get a StorageDisk that
	// isn't configured.
	ErrUnconfiguredDisk = errors.New("unconfigured disk")

	// ErrFileNotFound is returned when a file cannot be found in a Storage.
	ErrFileNotFound = errors.New("file not found")

	// ErrUploadFailed is returned when an upload of a file fails.
	ErrUploadFailed = errors.New("upload failed")

	// ErrEmptyName is returned when providing an empty string as a name.
	// Whitespce-only strings count as empty.
	ErrEmptyName = errors.New("empty name")
)

Functions

This section is empty.

Types

type Document

type Document struct {
	File
}

Document is an arbitrary storage file.

func NewDocument

func NewDocument(name, disk, path string, filesize int) Document

NewDocument returns a Document with the given data.

func (Document) Replace

func (doc Document) Replace(ctx context.Context, r io.Reader, storage Storage) (Document, error)

Replace replaces the document in Storage with the document in r and returns the updated Document.

func (Document) Upload

func (doc Document) Upload(ctx context.Context, r io.Reader, storage Storage) (Document, error)

Upload uploads the document to storage and returns the Document with updated Filesize.

func (Document) WithTag

func (doc Document) WithTag(tags ...string) Document

WithTag adds the given tags and returns the updated Document.

func (Document) WithoutTag

func (doc Document) WithoutTag(tags ...string) Document

WithoutTags removes the given tags and returns the updated Document.

type File

type File struct {
	Name     string   `json:"name"`
	Disk     string   `json:"disk"`
	Path     string   `json:"path"`
	Filesize int      `json:"filesize"`
	Tags     []string `json:"tags"`
}

File is a file that is stored in a storage backend.

func NewFile

func NewFile(name, disk, path string, size int, tags ...string) File

NewFile returns a File with the specified data. NewFile ensures that returned File f has a non-nil f.Tags slice.

func (File) Delete

func (f File) Delete(ctx context.Context, storage Storage) error

Delete deletes the file from storage.

func (File) Download

func (f File) Download(ctx context.Context, storage Storage) ([]byte, error)

Download downloads the file from the provided Storage.

func (File) HasTag

func (f File) HasTag(tags ...string) bool

HasTag returns whether the File has the given tags. HasTag returns true if the File has all provided tags or if len(tags) == 0.

func (File) Replace

func (f File) Replace(ctx context.Context, r io.Reader, storage Storage) (File, error)

Replace replaces the file in storage with the contents in r and returns the updated File.

func (File) Same

func (f File) Same(other File) bool

Same returns true if other points to the same file as f (same Disk and Path).

func (File) Upload

func (f File) Upload(ctx context.Context, r io.Reader, storage Storage) (File, error)

Upload uploads the file to storage and returns the File with updated Filesize.

func (File) WithTag

func (f File) WithTag(tags ...string) File

WithTag adds the given tags and returns the updated File.

func (File) WithoutTag

func (f File) WithoutTag(tags ...string) File

WithoutTags removes the given tags and returns the updated File.

type Image

type Image struct {
	File

	Width  int `json:"width"`
	Height int `json:"height"`
}

Image is storage image.

func NewImage

func NewImage(width, height int, name, disk, path string, filesize int) Image

NewImage returns an Image with the given data.

func (Image) Download

func (img Image) Download(ctx context.Context, storage Storage) (image.Image, string, error)

Download downloads the image from the provided Storage and returns the decoded Image and its format. If you want to download the image without decoding it into an image.Image, use img.File.Download instead.

func (Image) Replace

func (img Image) Replace(ctx context.Context, r io.Reader, storage Storage) (Image, error)

Replace replaces the image in Storage with the image in r and returns the updated Image.

func (Image) Upload

func (img Image) Upload(ctx context.Context, r io.Reader, storage Storage) (Image, error)

Upload uploads the image to storage and returns the Image with updated Filesize, Width and Height.

func (Image) WithTag

func (img Image) WithTag(tags ...string) Image

WithTag adds the given tags and returns the updated Image.

func (Image) WithoutTag

func (img Image) WithoutTag(tags ...string) Image

WithoutTags removes the given tags and returns the updated Image.

type Storage

type Storage interface {
	// Disk returns the StorageDisk that was configured with the given name or
	// ErrUnconfiguredDisk if the disk wasn't configured.
	Disk(string) (StorageDisk, error)
}

Storage is a storage for files.

func GoDriveStorage

func GoDriveStorage(manager *godrive.Manager) Storage

GoDriveStorage returns a Storage that uses `godrive` as the storage engine.

func NewStorage

func NewStorage(opts ...StorageOption) Storage

NewStorage returns a Storage, configured by opts.

type StorageDisk

type StorageDisk interface {
	// Put uploads a file to the specified storage path or ErrFileNotFound if
	// the file does not exist.
	Put(context.Context, string, []byte) error

	// Get returns the contents of the file at the specified path or
	// ErrFileNotFound if the file does not exist.
	Get(context.Context, string) ([]byte, error)

	// Delete deletes the file at the specified path. Delete returns no error
	// if the specified file does not exist.
	Delete(context.Context, string) error
}

StorageDisk is a disk of a Storage.

func MemoryDisk

func MemoryDisk() StorageDisk

MemoryDisk returns an in-memory StorageDisk.

type StorageOption

type StorageOption func(*storage)

StorageOption is an option for creating a Storage.

func ConfigureDisk

func ConfigureDisk(name string, disk StorageDisk) StorageOption

ConfigureDisk returns a StorageOption that configures a StorageDisk under the provided name.

Directories

Path Synopsis
mock_document
Package mock_document is a generated GoMock package.
Package mock_document is a generated GoMock package.
gallery/mock_gallery
Package mock_gallery is a generated GoMock package.
Package mock_gallery is a generated GoMock package.
mock_image
Package mock_image is a generated GoMock package.
Package mock_image is a generated GoMock package.
Package mock_media is a generated GoMock package.
Package mock_media is a generated GoMock package.

Jump to

Keyboard shortcuts

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