Documentation
¶
Overview ¶
Package files stores the attachments on disk under the data directory.
Index ¶
Constants ¶
const ( // TagTooLarge is the tag of a file larger than the limit given to Save. // The metadata "limit" is the limit in bytes. TagTooLarge = "FileTooLarge" // TagBadKey is the tag of a key that is not 64 lowercase hex characters. TagBadKey = "FileKeyInvalid" // TagNotFound is the tag of a key with no file in the store. TagNotFound = "FileNotFound" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Saved ¶
type Saved struct {
// Key is the name of the file in the store: 64 lowercase hex characters.
Key string
// Size is the size in bytes.
Size int64
// ContentType is the type sniffed from the first 512 bytes of the file
// (http.DetectContentType). For an image (PNG, JPEG, GIF or WebP) whose
// header cannot be read it is "application/octet-stream": a file that
// claims to be an image and is not is stored as an opaque file.
ContentType string
// Width and Height are the size in pixels of a PNG, JPEG, GIF or WebP
// image; 0 for any other file.
Width, Height int
}
Saved is what Save knows about a file it stored.
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store keeps files on disk under one directory, each under a random key. Make it with New. It is safe for concurrent use.
func New ¶
New makes the store in the directory dir, and dir/tmp, creating them with the permissions 0750 when they are missing. It removes what an earlier run left in dir/tmp, files that were being written when the process died. It fails with TagDirCreateFailed when a directory cannot be made and with TagTmpCleanFailed when dir/tmp cannot be emptied.
func (*Store) Open ¶
Open opens the file stored under key for reading; the caller closes it. A key that is not 64 lowercase hex characters fails with TagBadKey before the disk is touched, a key with no file with TagNotFound.
func (*Store) Remove ¶
Remove deletes the file stored under key. A key with no file is not an error. A key that is not 64 lowercase hex characters fails with TagBadKey before the disk is touched.
func (*Store) Save ¶
Save stores what r gives, at most limit bytes, under a new random key. The bytes go to a temporary file that is synced and renamed to its key, so a file under a key is always whole. A reader that gives one byte more than limit fails with TagTooLarge (metadata "limit"), a canceled ctx with TagCanceled; in every failure the temporary file is removed. Besides the size, Save finds the content type by sniffing and, for PNG, JPEG, GIF and WebP images, the dimensions (see Saved).