media

package
v0.8.1 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2023 License: AGPL-3.0 Imports: 37 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var SupportedEmojiMIMETypes = []string{
	mimeImageGif,
	mimeImagePng,
}
View Source
var SupportedMIMETypes = []string{
	mimeImageJpeg,
	mimeImageGif,
	mimeImagePng,
	mimeImageWebp,
	mimeVideoMp4,
}

Functions

This section is empty.

Types

type AdditionalEmojiInfo added in v0.2.0

type AdditionalEmojiInfo struct {
	// Time that this emoji was created; defaults to time.Now().
	CreatedAt *time.Time
	// Domain the emoji originated from. Blank for this instance's domain. Defaults to "".
	Domain *string
	// URL of this emoji on a remote instance; defaults to "".
	ImageRemoteURL *string
	// URL of the static version of this emoji on a remote instance; defaults to "".
	ImageStaticRemoteURL *string
	// Whether this emoji should be disabled (not shown) on this instance; defaults to false.
	Disabled *bool
	// Whether this emoji should be visible in the instance's emoji picker; defaults to true.
	VisibleInPicker *bool
	// ID of the category this emoji should be placed in; defaults to "".
	CategoryID *string
}

AdditionalEmojiInfo represents additional information that should be taken into account when processing an emoji.

type AdditionalMediaInfo added in v0.2.0

type AdditionalMediaInfo struct {
	// Time that this media was created; defaults to time.Now().
	CreatedAt *time.Time
	// ID of the status to which this media is attached; defaults to "".
	StatusID *string
	// URL of the media on a remote instance; defaults to "".
	RemoteURL *string
	// Image description of this media; defaults to "".
	Description *string
	// Blurhash of this media; defaults to "".
	Blurhash *string
	// ID of the scheduled status to which this media is attached; defaults to "".
	ScheduledStatusID *string
	// Mark this media as in-use as an avatar; defaults to false.
	Avatar *bool
	// Mark this media as in-use as a header; defaults to false.
	Header *bool
	// X focus coordinate for this media; defaults to 0.
	FocusX *float32
	// Y focus coordinate for this media; defaults to 0.
	FocusY *float32
}

AdditionalMediaInfo represents additional information that should be added to an attachment when processing a piece of media.

type DataFunc added in v0.2.0

type DataFunc func(ctx context.Context) (reader io.ReadCloser, fileSize int64, err error)

DataFunc represents a function used to retrieve the raw bytes of a piece of media.

type DereferenceMedia added in v0.7.0

type DereferenceMedia func(ctx context.Context, iri *url.URL) (io.ReadCloser, int64, error)

type Manager added in v0.2.0

type Manager interface {

	// PreProcessMedia begins the process of decoding and storing the given data as an attachment.
	// It will return a pointer to a ProcessingMedia struct upon which further actions can be performed, such as getting
	// the finished media, thumbnail, attachment, etc.
	//
	// data should be a function that the media manager can call to return a reader containing the media data.
	//
	// postData will be called after data has been called; it can be used to clean up any remaining resources.
	// The provided function can be nil, in which case it will not be executed.
	//
	// accountID should be the account that the media belongs to.
	//
	// ai is optional and can be nil. Any additional information about the attachment provided will be put in the database.
	//
	// Note: unlike ProcessMedia, this will NOT queue the media to be asynchronously processed.
	PreProcessMedia(ctx context.Context, data DataFunc, postData PostDataCallbackFunc, accountID string, ai *AdditionalMediaInfo) (*ProcessingMedia, error)

	// PreProcessMediaRecache refetches, reprocesses, and recaches an existing attachment that has been uncached via pruneRemote.
	//
	// Note: unlike ProcessMedia, this will NOT queue the media to be asychronously processed.
	PreProcessMediaRecache(ctx context.Context, data DataFunc, postData PostDataCallbackFunc, attachmentID string) (*ProcessingMedia, error)

	// ProcessMedia will call PreProcessMedia, followed by queuing the media to be processing in the media worker queue.
	ProcessMedia(ctx context.Context, data DataFunc, postData PostDataCallbackFunc, accountID string, ai *AdditionalMediaInfo) (*ProcessingMedia, error)

	// PreProcessEmoji begins the process of decoding and storing the given data as an emoji.
	// It will return a pointer to a ProcessingEmoji struct upon which further actions can be performed, such as getting
	// the finished media, thumbnail, attachment, etc.
	//
	// data should be a function that the media manager can call to return a reader containing the emoji data.
	//
	// postData will be called after data has been called; it can be used to clean up any remaining resources.
	// The provided function can be nil, in which case it will not be executed.
	//
	// shortcode should be the emoji shortcode without the ':'s around it.
	//
	// id is the database ID that should be used to store the emoji.
	//
	// uri is the ActivityPub URI/ID of the emoji.
	//
	// ai is optional and can be nil. Any additional information about the emoji provided will be put in the database.
	//
	// Note: unlike ProcessEmoji, this will NOT queue the emoji to be asynchronously processed.
	PreProcessEmoji(ctx context.Context, data DataFunc, postData PostDataCallbackFunc, shortcode string, id string, uri string, ai *AdditionalEmojiInfo, refresh bool) (*ProcessingEmoji, error)

	// ProcessEmoji will call PreProcessEmoji, followed by queuing the emoji to be processing in the emoji worker queue.
	ProcessEmoji(ctx context.Context, data DataFunc, postData PostDataCallbackFunc, shortcode string, id string, uri string, ai *AdditionalEmojiInfo, refresh bool) (*ProcessingEmoji, error)

	// PruneAll runs all of the below pruning/uncacheing functions, and then cleans up any resulting
	// empty directories from the storage driver. It can be called as a shortcut for calling the below
	// pruning functions one by one.
	//
	// If blocking is true, then any errors encountered during the prune will be combined + returned to
	// the caller. If blocking is false, the prune is run in the background and errors are just logged
	// instead.
	PruneAll(ctx context.Context, mediaCacheRemoteDays int, blocking bool) error
	// UncacheRemote uncaches all remote media attachments older than the given amount of days.
	//
	// In this context, uncacheing means deleting media files from storage and marking the attachment
	// as cached=false in the database.
	//
	// If 'dry' is true, then only a dry run will be performed: nothing will actually be changed.
	//
	// The returned int is the amount of media that was/would be uncached by this function.
	UncacheRemote(ctx context.Context, olderThanDays int, dry bool) (int, error)
	// PruneUnusedRemote prunes unused/out of date headers and avatars cached on this instance.
	//
	// The returned int is the amount of media that was pruned by this function.
	PruneUnusedRemote(ctx context.Context, dry bool) (int, error)
	// PruneUnusedLocal prunes unused media attachments that were uploaded by
	// a user on this instance, but never actually attached to a status, or attached but
	// later detached.
	//
	// The returned int is the amount of media that was pruned by this function.
	PruneUnusedLocal(ctx context.Context, dry bool) (int, error)
	// PruneOrphaned prunes files that exist in storage but which do not have a corresponding
	// entry in the database.
	//
	// If dry is true, then nothing will be changed, only the amount that *would* be removed
	// is returned to the caller.
	PruneOrphaned(ctx context.Context, dry bool) (int, error)

	// RefetchEmojis iterates through remote emojis (for the given domain, or all if domain is empty string).
	//
	// For each emoji, the manager will check whether both the full size and static images are present in storage.
	// If not, the manager will refetch and reprocess full size and static images for the emoji.
	//
	// The provided DereferenceMedia function will be used when it's necessary to refetch something this way.
	RefetchEmojis(ctx context.Context, domain string, dereferenceMedia DereferenceMedia) (int, error)
}

Manager provides an interface for managing media: parsing, storing, and retrieving media objects like photos, videos, and gifs.

func NewManager added in v0.2.0

func NewManager(state *state.State) Manager

NewManager returns a media manager with the given db and underlying storage.

A worker pool will also be initialized for the manager, to ensure that only a limited number of media will be processed in parallel. The numbers of workers is determined from the $GOMAXPROCS environment variable (usually no. CPU cores). See internal/concurrency.NewWorkerPool() documentation for further information.

type PostDataCallbackFunc added in v0.2.1

type PostDataCallbackFunc func(ctx context.Context) error

PostDataCallbackFunc represents a function executed after the DataFunc has been executed, and the returned reader has been read. It can be used to clean up any remaining resources.

This can be set to nil, and will then not be executed.

type ProcessingEmoji added in v0.2.0

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

ProcessingEmoji represents an emoji currently processing. It exposes various functions for retrieving data from the process.

func (*ProcessingEmoji) EmojiID added in v0.2.0

func (p *ProcessingEmoji) EmojiID() string

EmojiID returns the ID of the underlying emoji without blocking processing.

func (*ProcessingEmoji) LoadEmoji added in v0.2.0

func (p *ProcessingEmoji) LoadEmoji(ctx context.Context) (*gtsmodel.Emoji, error)

LoadEmoji blocks until the static and fullsize image has been processed, and then returns the completed emoji.

func (*ProcessingEmoji) Process added in v0.7.0

func (p *ProcessingEmoji) Process(ctx context.Context)

Process allows the receiving object to fit the runners.WorkerFunc signature. It performs a (blocking) load and logs on error.

type ProcessingMedia added in v0.2.0

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

ProcessingMedia represents a piece of media that is currently being processed. It exposes various functions for retrieving data from the process.

func (*ProcessingMedia) AttachmentID added in v0.2.0

func (p *ProcessingMedia) AttachmentID() string

AttachmentID returns the ID of the underlying media attachment without blocking processing.

func (*ProcessingMedia) LoadAttachment added in v0.2.0

func (p *ProcessingMedia) LoadAttachment(ctx context.Context) (*gtsmodel.MediaAttachment, error)

LoadAttachment blocks until the thumbnail and fullsize content has been processed, and then returns the completed attachment.

func (*ProcessingMedia) Process added in v0.7.0

func (p *ProcessingMedia) Process(ctx context.Context)

Process allows the receiving object to fit the runners.WorkerFunc signature. It performs a (blocking) load and logs on error.

type Size

type Size string
const (
	SizeSmall    Size = "small"    // SizeSmall is the key for small/thumbnail versions of media
	SizeOriginal Size = "original" // SizeOriginal is the key for original/fullsize versions of media and emoji
	SizeStatic   Size = "static"   // SizeStatic is the key for static (non-animated) versions of emoji
)

type Type

type Type string
const (
	TypeAttachment Type = "attachment" // TypeAttachment is the key for media attachments
	TypeHeader     Type = "header"     // TypeHeader is the key for profile header requests
	TypeAvatar     Type = "avatar"     // TypeAvatar is the key for profile avatar requests
	TypeEmoji      Type = "emoji"      // TypeEmoji is the key for emoji type requests
)

Jump to

Keyboard shortcuts

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