media

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package media handles media for the posts

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound     = errors.New("media not found")
	ErrUserNotOwner = errors.New("user does not own the media")
)

Set of error variables for CRUD operations.

Functions

func GenerateMediaLists

func GenerateMediaLists(mediaList []Media) ([]string, []string)

GenerateMediaLists creates lists of media names and URLs from a slice of Media

func IdToCacheKey

func IdToCacheKey(idHex string) string

Types

type Core

type Core struct {
	MaxFileSizeMb int64
	// contains filtered or unexported fields
}

Core manages the set of APIs for media access.

func NewCore

func NewCore(storer Storer, cacheStorer cachestore.CacheStore, userStore user.Storer) *Core

func (*Core) Create

func (c *Core) Create(ctx context.Context, newMedia NewMedia) (MediaData, error)

func (*Core) CreateMultiple

func (c *Core) CreateMultiple(ctx context.Context, newMediaList []NewMedia) ([]MediaData, error)

func (*Core) Delete

func (c *Core) Delete(ctx context.Context, media Media) error

func (*Core) DeleteByID

func (c *Core) DeleteByID(ctx context.Context, mediaID string) error

func (*Core) ParseMedia

func (c *Core) ParseMedia(r *http.Request) (NewMedia, error)

ParseMedia function is utility function that helps handlers to parse media from http request

func (*Core) ParseMultipleMedia

func (c *Core) ParseMultipleMedia(r *http.Request) ([]NewMedia, error)

func (*Core) QueryByID

func (c *Core) QueryByID(ctx context.Context, mediaID string) (Media, error)

func (*Core) QueryByIDs

func (c *Core) QueryByIDs(ctx context.Context, mediaIDs []string) ([]Media, error)

QueryByIDs retrieves multiple media records by their IDs, checking the cache first and querying the database for any missing records.

type Media

type Media struct {
	ID        primitive.ObjectID `json:"id,omitempty" bson:"_id,omitempty"`
	OwnerID   primitive.ObjectID `json:"owner_id,omitempty"`
	Filename  string             `json:"name"`
	Length    int64              `json:"length"`
	FileBytes []byte             `json:"fileBytes"`
}

func (Media) CacheExpiration

func (p Media) CacheExpiration() time.Duration

func (Media) CacheKey

func (m Media) CacheKey() string

func (Media) GenUrl

func (m Media) GenUrl() string

GenUrl generates part of url for downloading the mediafile

type MediaData

type MediaData struct {
	Filename string `json:"name"`
	Url      string `json:"url"`
}

func MapTo

func MapTo(media Media) MediaData

func MapToMultiple

func MapToMultiple(media []Media) []MediaData

type NewMedia

type NewMedia struct {
	Filename  string `json:"name"`
	Length    int64  `json:"length"`
	FileBytes []byte `json:"fileBytes"`
}

type Storer

type Storer interface {
	Create(ctx context.Context, media Media) (Media, error)
	CreateMultiple(ctx context.Context, media []Media) ([]Media, error)
	Update(ctx context.Context, media Media) (Media, error)
	Delete(ctx context.Context, media Media) error
	DeleteByID(ctx context.Context, mediaID string) error
	QueryByID(ctx context.Context, mediaID string) (Media, error)
	QueryByIDs(ctx context.Context, mediaIDs []string) ([]Media, error)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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