uploader

package
v2.4.4 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2025 License: MIT Imports: 25 Imported by: 0

Documentation

Overview

Package uploader manages media files uploaded to a server. The files are expected to belong to a parent database object, such as a slideshow, with the files being uploaded asynchronously from a form that modifies the parent. The parent need not exist at the time of upload, and an extended transaction model is used to maintain consistency between the database and the media files.

Images are resized to fit within limits specified by the server. Audio files are converted to M4A format and videos to MP4 format. Thumbnails are generated for both images and videos. Deleted media files are retained for a specified period, allowing that they may still be referenced from cached web pages.

Uploader maintains consistency across server restarts between the parent object and the media files it references. Operation is intended to be robust for processing that may take a long time, such as converting a video file. To prevent overloading a modest server, on a server restart the workload for recovery is limited to that similar to normal operation.

Index

Constants

View Source
const (
	MediaImage    = 1
	MediaVideo    = 2
	MediaAudio    = 3
	MediaDocument = 4
)
View Source
const (
	OpOrphansV1Type = 0
	OpCancelType    = 1
	OpDeleteType    = 2
)

Operation types

Variables

View Source
var WebFiles embed.FS

WebFiles are the package's web resources (templates and static files)

Functions

func CleanName

func CleanName(name string) string

CleanName removes unwanted characters from a filename, to make it safe for display and storage. From https://stackoverflow.com/questions/54461423/efficient-way-to-remove-all-non-alphanumeric-characters-from-large-text. ## This is far more restrictive than we need.

func FileFromName

func FileFromName(id etx.TxId, version int, name string) string

FileFromName returns the stored file name for a newly uploaded file.

func FixOrientation added in v2.1.0

func FixOrientation(img image.Image, o orientation) image.Image

FixOrientation applies a transform to img corresponding to the given orientation flag.

func NameFromFile

func NameFromFile(fileName string) string

NameFromFile returns the media file name from a file name.

func OrientationSize added in v2.1.0

func OrientationSize(size image.Point, o orientation) image.Point

OrientationSize returns the dimensions of the image after orientation is changed.

func ReadOrientation added in v2.1.0

func ReadOrientation(r io.Reader) orientation

ReadOrientation tries to read the orientation EXIF flag from image data in r. If the EXIF data block is not found or the orientation flag is not found or any other error occures while reading the data, it returns the orientationUnspecified (0) value.

func Status

func Status(filename string) int

Working returns the processed status for an image. 0 = no file, 1 = processing, 100 = ready. Percent processed and errors may be implemented in future.

func Thumbnail

func Thumbnail(filename string) string

Thumbnail returns the prefixed name for a thumbnail.

Types

type Bind

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

Context for a sequence of bind calls.

func (*Bind) End

func (b *Bind) End() error

End completes the linking a parent object, and deletes any files for the transaction that are not required.

func (*Bind) File

func (b *Bind) File(fileName string) (string, error)

File is called to check if a file has changed. If so, it returns the new filename. An empty string indicates no change.

type Claim

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

Context for a sequence of claim calls and subsequent processing.

func (*Claim) End

func (c *Claim) End(fn Uploaded)

End requests notification when uploads are done. Any unclaimed temporary files are deleted.

func (*Claim) EndV1

func (c *Claim) EndV1()

EndV1 deletes any unclaimed files.

func (*Claim) File

func (c *Claim) File(name string)

File specifies a file that is referenced by the client. It is legal to specify files from other transactions; these will be ignored.

func (*Claim) FileV1

func (c *Claim) FileV1(name string)

FileV1 specifies a file that is referenced by the client. It is legal to specify files from other transactions; these will be ignored.

type DB

type DB interface {
	Begin() func() // start transaction and return commit function
}

DB is an interface to the database manager that handles parent transactions.

type OpCancel

type OpCancel struct {
}

type OpDelete

type OpDelete struct {
	Name string
}

type Uploaded

type Uploaded func(error)

Callback when all claimed files are ready for use.

type Uploader

type Uploader struct {

	// parameters
	FilePath        string
	MaxW            int
	MaxH            int
	MaxDecoded      int // maximum decoded image size (bytes)
	MaxSize         int // maximum size for AV to use original without processing
	ThumbW          int
	ThumbH          int
	MaxAge          time.Duration // maximum time for a parent update
	DeleteAfter     time.Duration // delay before deleting a file
	SnapshotAt      time.Duration // snapshot time in video (-ve for none)
	AudioTypes      []string
	DocumentTypes   []string
	ImageQuality    int    // JPEG quality, 1-100
	VideoPackage    string // software for video processing: ffmpeg, or a docker-hosted implementation of ffmpeg, for debugging
	VideoResolution int
	VideoTypes      []string
	// contains filtered or unexported fields
}

Uploader holds the parameters and state for uploading files. Typically only one is needed.

func (*Uploader) Begin

func (up *Uploader) Begin() (string, error)

Begin returns a transaction code for an update that may include a set of uploads. It expects that a database transaction (needed to write redo records) has been started.

func (*Uploader) Commit

func (up *Uploader) Commit(tx etx.TxId) error

Commit makes temporary uploaded files permanent. It returns false if the transaction ID for a set of uploads has expired.

func (*Uploader) Delete

func (up *Uploader) Delete(tx etx.TxId, filename string) error

Delete removes a media file and deletes it after a delay to allow for cached web pages holding references to be aged.

func (*Uploader) DeleteNow added in v2.0.1

func (up *Uploader) DeleteNow(filename string) error

DeleteNow removes a media file immediately, together with its thumbnail. Operation is idempotent. I.e. no error is returned if the file has already been deleted.

func (*Uploader) ForOperation

func (up *Uploader) ForOperation(opType int) etx.Op

func (*Uploader) Initialise

func (up *Uploader) Initialise(log *log.Logger, db DB, tm *etx.TM)

Initialise starts the file uploader.

func (*Uploader) MediaType

func (up *Uploader) MediaType(name string) int

MediaType returns the media type. It is 0 if not accepted.

func (*Uploader) Name

func (up *Uploader) Name() string

func (*Uploader) Operation

func (up *Uploader) Operation(id etx.TxId, opType int, op etx.Op)

Do operation requested via TM.

func (*Uploader) Save

func (up *Uploader) Save(fh *multipart.FileHeader, tx etx.TxId, version int) (error, bool)

Save creates a temporary copy of an uploaded file, to be processed later. With an error it returns true if it is the client's fault, false if it is the server's fault.

func (*Uploader) StartBind

func (up *Uploader) StartBind(tx etx.TxId) *Bind

StartBind initiates linking a parent object to a set of uploaded files, returning a context for calls to Bind and EndBind.

func (*Uploader) StartClaim

func (up *Uploader) StartClaim(tx etx.TxId) *Claim

StartClaim prepares for the client to identify the files it references.

func (*Uploader) StartClaimV1

func (up *Uploader) StartClaimV1(tx etx.TxId) *Claim

StartClaimV1 prepares for the client to identify the files it references.

func (*Uploader) Stop

func (up *Uploader) Stop()

Stop shuts down the uploader.

func (*Uploader) V1

func (up *Uploader) V1()

V1 requests the uploader to handle V1 extended transactions.

func (*Uploader) ValidCode

func (up *Uploader) ValidCode(tx etx.TxId) bool

Deprecated as a misleading name. Use the equivalent Uploader.Commit instead.

Jump to

Keyboard shortcuts

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