media

package
v0.0.0-...-c05fae0 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2023 License: MIT Imports: 29 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TaskQueue     = "media"
	TaskTypePrune = "media:prune"
)

Variables

This section is empty.

Functions

func NewPeriodicTaskConfigProvider

func NewPeriodicTaskConfigProvider() asynq.PeriodicTaskConfigProvider

func NewPruneTask

func NewPruneTask() *asynq.Task

func NewTaskHandler

func NewTaskHandler(repo Repository, store Store) (string, asynq.Handler)

Types

type FileStore

type FileStore struct {
	FileMode fs.FileMode // mode for newly created files
	DirMode  fs.FileMode // mode for newly created folders
	// contains filtered or unexported fields
}

FileStore is an implementation of the Store interface using a directory in the local filesystem. The FileStore assumes that it has full control over the directory tree located at its root.

FileStore creates subdirectories for files based on their UUIDs. The subdirectories are nested by UUID prefixes to avoid performance degradation because of the number of files in a folder.

func NewFileStore

func NewFileStore(logger *slog.Logger, root string) (*FileStore, error)

NewFileStore creates a new file store rooted at root. The root directory must exist and be a directory.

func (*FileStore) Create

Create opens a writer for file. Any necessary intermediate directories are created before this method returns.

func (*FileStore) Open

Open opens a reader for file.

func (*FileStore) Root

func (s *FileStore) Root() string

Root returns the absolute path to the root directory for the store.

type Repository

type Repository interface {
	// CreateFile creates a new file reference based on the specified file.
	// The file.Type may be used to influence storage, other fields should not be used for this.
	// Implementations must set file.UUID, file.CreatedAt, and file.UpdatedAt.
	CreateFile(ctx context.Context, file *model.File) error

	// UpdateFile updates the fields of file in the repository.
	UpdateFile(ctx context.Context, file *model.File) error
}

Repository is a service for storing references to model.File in a database.

func NewDBRepository

func NewDBRepository(logger *slog.Logger, db pgxutil.DB) Repository

NewDBRepository returns a new Repository backed by the specified connection. db can be a single connection or a connection pool.

func NewFakeRepository

func NewFakeRepository() Repository

NewFakeRepository creates a new Repository backed by a simple in-memory storage.

type Service

type Service interface {
	// StoreFile creates a new model.File and writes the data provided by r into the file.
	// This method should update known file metadata fields during the upload.
	// Depending on the media type implementations should analyze the file set type-specific metadata as well.
	//
	// If an error occurs r may have been partially consumed.
	// If any bytes have been persisted, this method must return a valid model.File that is able to identify the (potentially partial) data.
	// If the file has not been stored successfully, an error is returned.
	StoreFile(ctx context.Context, mediaType mediatype.MediaType, r io.Reader) (model.File, error)
}

Service provides an interface for working with media files in Karman. An implementation of the Service interface implements the core logic associated with these files.

func NewFakeService

func NewFakeService(repo Repository) Service

NewFakeService creates a new fakeService instance and returns it. The placeholder will be the content of all "files".

func NewService

func NewService(logger *slog.Logger, repo Repository, store Store) Service

NewService creates a new Service instance using the supplied db and store. The default implementation will store media files in the store as well as in the DB. For each media file there will be an entry in the DB, the actual data however lives in the store.

type Store

type Store interface {
	// Create opens a writer for a file with the specified media type and UUID.
	// If no writer could be opened, an error will be returned.
	// It is the caller's responsibility to close the writer after writing the file contents.
	Create(ctx context.Context, mediaType mediatype.MediaType, id uuid.UUID) (io.WriteCloser, error)

	// Open opens a reader for the contents of the file with the specified media type and UUID.
	// If no reader could be opened, an error will be returned.
	// It is the caller's responsibility to close the reader after reading the file contents.
	Open(ctx context.Context, mediaType mediatype.MediaType, id uuid.UUID) (io.ReadCloser, error)
}

Store is an interface to an underlying storage system used by Karman.

func NewMemStore

func NewMemStore() Store

NewMemStore returns a new Store implementation that holds file contents in memory. This implementation is intended for testing purposes and should not be used in the actual application.

func NewMockStore

func NewMockStore(placeholder string) Store

NewMockStore returns a new Store implementation that holds file contents in memory.

Jump to

Keyboard shortcuts

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