Documentation ¶
Index ¶
- Constants
- func NewPeriodicTaskConfigProvider() asynq.PeriodicTaskConfigProvider
- func NewPruneTask() *asynq.Task
- func NewTaskHandler(repo Repository, store Store) (string, asynq.Handler)
- type FileStore
- func (s *FileStore) Create(ctx context.Context, _ mediatype.MediaType, id uuid.UUID) (io.WriteCloser, error)
- func (s *FileStore) Delete(ctx context.Context, _ mediatype.MediaType, id uuid.UUID) (bool, error)
- func (s *FileStore) Open(ctx context.Context, _ mediatype.MediaType, id uuid.UUID) (io.ReadCloser, error)
- func (s *FileStore) Root() string
- type Repository
- type Service
- type Store
Constants ¶
const ( TaskQueue = "media" TaskTypePrune = "media:prune" )
Variables ¶
This section is empty.
Functions ¶
func NewPeriodicTaskConfigProvider ¶
func NewPeriodicTaskConfigProvider() asynq.PeriodicTaskConfigProvider
func NewPruneTask ¶
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 ¶
NewFileStore creates a new file store rooted at root. The root directory must exist and be a directory.
func (*FileStore) Create ¶
func (s *FileStore) Create(ctx context.Context, _ mediatype.MediaType, id uuid.UUID) (io.WriteCloser, error)
Create opens a writer for file. Any necessary intermediate directories are created before this method returns.
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 // GetFile fetches the file with the specified UUID from the repository. GetFile(ctx context.Context, id uuid.UUID) (model.File, error) // UpdateFile updates the fields of file in the repository. UpdateFile(ctx context.Context, file *model.File) error // DeleteFile deletes the file with the specified UUID from the repository. // If no such file exists, the first return value will be false. // // This method does not delete the file contents associated with the file. // In most cases you should use Service.DeleteFile instead. DeleteFile(ctx context.Context, id uuid.UUID) (bool, error) // FindOrphanedFiles fetches a list of files that are not associated with a song. // If you pass limit < 0, all orphaned files are returned. FindOrphanedFiles(ctx context.Context, limit int64) ([]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 updates 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) // DeleteFile removes the file with the specified UUID from the database and from the file system. // Implementations must make sure that a nil value is returned if and only if // the file was deleted from both the database and the file storage. DeleteFile(ctx context.Context, id uuid.UUID) 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 repo 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) // Delete deletes the file with the specified media type and UUID. // If the file was already absent, the first return value will be false. Delete(ctx context.Context, mediaType mediatype.MediaType, id uuid.UUID) (bool, 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 ¶
NewMockStore returns a new Store implementation that holds file contents in memory.