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.
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
- Variables
- func CleanName(name string) string
- func FileFromName(id etx.TxId, version int, name string) string
- func NameFromFile(fileName string) string
- func PermanentName(tempName string, ext string) string
- func SmallName(name string, ext string) string
- type Bind
- type Claim
- type DB
- type OpCancel
- type OpDelete
- type ReqSave
- type UpMedia
- type Uploaded
- type Uploader
- func (up *Uploader) AddTypes(types []string, mediaCode int, upMedia UpMedia, chDone chan bool)
- func (up *Uploader) Begin() (string, error)
- func (up *Uploader) Commit(tx etx.TxId) error
- func (up *Uploader) Delete(tx etx.TxId, filename string) error
- func (up *Uploader) DeleteNow(filename string) error
- func (up *Uploader) ForOperation(opType int) etx.Op
- func (up *Uploader) MediaType(filename string) int
- func (up *Uploader) Name() string
- func (up *Uploader) Operation(id etx.TxId, opType int, op etx.Op)
- func (up *Uploader) Save(fh *multipart.FileHeader, tx etx.TxId, version int) (error, bool)
- func (up *Uploader) StartBind(tx etx.TxId) *Bind
- func (up *Uploader) StartClaim(tx etx.TxId) *Claim
- func (up *Uploader) Status(filename string) int
- func (up *Uploader) Stop()
- func (up *Uploader) Thumbnail(filename string) string
Constants ¶
const ( OpCancelType = 1 OpDeleteType = 2 )
Operation types
Variables ¶
var WebFiles embed.FS
WebFiles are the package's web resources (templates and static files)
Functions ¶
func CleanName ¶
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 ¶
FileFromName returns the stored file name for a newly uploaded file.
func NameFromFile ¶
NameFromFile returns the media file name from a file name.
func PermanentName ¶
PermanentName returns a stored file name with the prefix for permanent status, and optionally a different extension.
Types ¶
type Bind ¶
type Bind struct {
// contains filtered or unexported fields
}
Context for a sequence of bind calls.
type Claim ¶
type Claim struct {
// contains filtered or unexported fields
}
Context for a sequence of claim calls and subsequent processing.
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 ReqSave ¶
type UpMedia ¶
type UpMedia interface {
Save(req ReqSave) (bool, error) // save media file, returning true if saving completed.
Thumbnail(string) string // return thumbnail name corresponding to media file name.
}
UpMedia is an interface to a processor for a set of media formats.
Save() is called from the main worker thread. It may rename the temporary file to be permanent of perform a required conversion, save the result as a permanent file, and delete the temporary file. It returns true if conversion and saving has been performed synchronously. If conversion will be slow, such as for a video file, send the request to a separate worker thread, return false, and call ReqSave.Done when processing is complete.
Thumbnail identifies any associated file to be deleted with the main file.
type Uploader ¶
type Uploader struct {
// parameters
DeleteAfter time.Duration // delay before deleting a file
KeepNames bool // keep user's name for file in stored file name
MaxAge time.Duration // maximum time for a parent update
// components
ErrorLog *log.Logger
// contains filtered or unexported fields
}
Uploader holds the parameters and state for uploading files. Typically only one is needed.
func NewUploader ¶
NewUploader starts and returns the file uploader. Public parameters may be changed before the first call to Begin().
func (*Uploader) Begin ¶
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 ¶
Commit makes temporary uploaded files permanent. It returns false if the transaction ID for a set of uploads has expired.
func (*Uploader) Delete ¶
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 ¶
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) Save ¶
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 ¶
StartBind initiates linking a parent object to a set of uploaded files, returning a context for calls to Bind and EndBind.
func (*Uploader) StartClaim ¶
StartClaim prepares for the client to identify the files it references.