media

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2026 License: MIT Imports: 41 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	MaxConcurrentDownloads = 4
	MaxAttempts            = 12
)
View Source
const (
	LinkPath     = "/media/"
	LinkLifetime = 15 * time.Minute
)
View Source
const (
	KindImage    = "image"
	KindVideo    = "video"
	KindAudio    = "audio"
	KindDocument = "document"
)
View Source
const (
	MaxFileBytes = 100 << 20
	UploadTTL    = time.Hour
	UploadsDir   = "uploads"
	TempDir      = "tmp"
)
View Source
const LinkRoute = "GET " + LinkPath + "{token}"
View Source
const (
	MaxBase64Bytes = 5 << 20
)
View Source
const UploadsPath = "/uploads"
View Source
const VoiceNoteMime = "audio/ogg; codecs=opus"

Variables

View Source
var (
	ErrUnavailable   = errors.New("WhatsApp cannot deliver this file")
	ErrNotDownloaded = errors.New("the file is not downloaded yet")
	ErrStillRunning  = errors.New("the download is still running")
)
View Source
var (
	ErrTooLarge      = fmt.Errorf("the file is larger than %d MB", MaxFileBytes>>20)
	ErrEmpty         = errors.New("the file is empty")
	ErrUnknownUpload = errors.New("unknown upload")
)
View Source
var (
	ErrOneSource      = errors.New("pass exactly one of media_url, upload_id or base64")
	ErrBase64TooLarge = fmt.Errorf("the base64 file is larger than %d MB; POST it to /uploads and pass the upload_id", MaxBase64Bytes>>20)
	ErrInvalidBase64  = errors.New("the base64 value is not valid base64")
)

Functions

func ContentType

func ContentType(file store.MediaFile) string

func Convertible

func Convertible(mediaType string) bool

func DownloadName

func DownloadName(file store.MediaFile) string

func Handler

func Handler(deps HandlerDeps) http.Handler

func UploadHandler

func UploadHandler(deps UploadHandlerDeps) http.Handler

Types

type Clients

type Clients interface {
	Client(accountID int64) (whatsapp.Client, bool)
}

type Downloader

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

func NewDownloader

func NewDownloader(deps DownloaderDeps) *Downloader

func (*Downloader) Ensure

func (d *Downloader) Ensure(ctx context.Context, ref store.MediaRef) (store.MediaFile, error)

func (*Downloader) Notice

func (d *Downloader) Notice(accountID int64, evt any)

func (*Downloader) Run

func (d *Downloader) Run(ctx context.Context) error

func (*Downloader) Wake

func (d *Downloader) Wake()

type DownloaderDeps

type DownloaderDeps struct {
	Store   *store.Store
	Files   *Files
	Clients Clients
	Clock   clock.Clock
	Log     *slog.Logger
}

type ExpiredUploadError

type ExpiredUploadError struct {
	ExpiredAt time.Time
}

func (*ExpiredUploadError) Error

func (e *ExpiredUploadError) Error() string

type FFmpeg

type FFmpeg struct{}

func (FFmpeg) Duration

func (FFmpeg) Duration(ctx context.Context, path string) (time.Duration, error)

func (FFmpeg) VoiceNote

func (FFmpeg) VoiceNote(ctx context.Context, input, output string) error

type FetchConfig

type FetchConfig struct {
	Timeout time.Duration
	Allowed []netip.Prefix
}

type FetchError

type FetchError struct {
	Message string
}

func (*FetchError) Error

func (e *FetchError) Error() string

type File

type File struct {
	Path   string
	Size   int64
	SHA256 []byte
	Mime   string
	Name   string
	// contains filtered or unexported fields
}

func (File) Discard

func (f File) Discard()

func (File) Hash

func (f File) Hash() string

func (File) Kind

func (f File) Kind() string

func (File) Open

func (f File) Open() (*os.File, error)

func (File) ShortHash

func (f File) ShortHash() string

type Files

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

func OpenFiles

func OpenFiles(dir string) (*Files, error)

func (*Files) AccountBytes

func (f *Files) AccountBytes(accountID int64) (int64, error)

func (*Files) Close

func (f *Files) Close() error

func (*Files) Keep

func (f *Files) Keep(accountID int64, content io.Reader) (string, error)

func (*Files) Open

func (f *Files) Open(name string) (*os.File, error)

func (*Files) Remove

func (f *Files) Remove(name string) error

func (*Files) RemoveAccount

func (f *Files) RemoveAccount(accountID int64) error

type HandlerDeps

type HandlerDeps struct {
	Store *store.Store
	Files *Files
	Links *Links
	Log   *slog.Logger
}
type Links struct {
	// contains filtered or unexported fields
}
func NewLinks(signingKey []byte, c clock.Clock, publicURL string) *Links

func (*Links) Sign

func (l *Links) Sign(path string) (string, time.Time)

type Outbox

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

func NewOutbox

func NewOutbox(deps OutboxDeps) (*Outbox, error)

func (*Outbox) Load

func (o *Outbox) Load(ctx context.Context, grantID int64, src Source) (File, error)

func (*Outbox) Prepare

func (o *Outbox) Prepare(ctx context.Context, file File, voiceNote bool) (Ready, error)

func (*Outbox) Run

func (o *Outbox) Run(ctx context.Context) error

func (*Outbox) SaveUpload

func (o *Outbox) SaveUpload(grantID int64, declaredType string, body io.Reader) (Upload, error)

func (*Outbox) Scratch

func (o *Outbox) Scratch() (*os.File, func(), error)

type OutboxDeps

type OutboxDeps struct {
	DataDir    string
	Clock      clock.Clock
	Transcoder Transcoder
	Fetch      FetchConfig
	Log        *slog.Logger
}

type Ready

type Ready struct {
	Path    string
	Mime    string
	Name    string
	Seconds int
	Width   int
	Height  int
	// contains filtered or unexported fields
}

func (Ready) Discard

func (r Ready) Discard()

func (Ready) Open

func (r Ready) Open() (*os.File, error)

type Source

type Source struct {
	URL      string
	UploadID string
	Base64   string
	FileName string
}

func (Source) Single

func (s Source) Single() bool

type Transcoder

type Transcoder interface {
	VoiceNote(ctx context.Context, input, output string) error
	Duration(ctx context.Context, path string) (time.Duration, error)
}

type Upload

type Upload struct {
	ID        string
	Size      int64
	ExpiresAt time.Time
}

type UploadHandlerDeps

type UploadHandlerDeps struct {
	Outbox *Outbox
	Store  *store.Store
	Audit  *audit.Logger
}

Jump to

Keyboard shortcuts

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