Documentation
¶
Overview ¶
Package file checks file uploads against an upload policy: the allowed content types, the size limit and the file name. It has no storage and no database: where the file goes, who may upload it, the object key and the upload record stay in the application.
Index ¶
Examples ¶
Constants ¶
const MaxFilenameLength = 255
MaxFilenameLength is the longest file name ValidFilename accepts, in characters.
Variables ¶
var ErrInvalid = errors.New("file: invalid upload")
ErrInvalid is wrapped by every error of Policy.Check.
var ErrNotFound = errors.New("file: not found")
ErrNotFound is returned by ServeAttachment when name is missing, is a directory, or is outside root.
Functions ¶
func DocumentTypes ¶
DocumentTypes returns the document types: pdf and the Office Open XML formats without macros (docx, xlsx, pptx). The legacy binary formats (.doc, .xls, .ppt) and the macro-enabled ones (.docm, ...) are left out.
func ImageTypes ¶
ImageTypes returns the image types safe to show inline: png, jpeg, webp and gif. SVG is left out because it can carry scripts.
func IsBinary ¶
func IsBinary(f io.ReadSeeker) (bool, error)
IsBinary reports whether f looks like a binary file, judging by its first 512 bytes: a NUL byte means binary; valid UTF-8 means text; otherwise more than 5% of bytes outside printable ASCII (tab, LF and CR count as text) means binary. A multi-byte UTF-8 character cut at the 512-byte boundary does not make text look binary. An empty file is text.
f is read from its start and left at its start, so a multipart.File or an *os.File can be read again afterwards.
func ServeAttachment ¶
ServeAttachment sends the file name, relative to the directory root, as a download (Content-Disposition: attachment, with the file name encoded for non-ASCII characters). It supports Range and If-Modified-Since requests. A Content-Type the caller set on w is kept; otherwise it is detected.
name cannot leave root: "..", absolute paths and symbolic links that point outside are refused (os.Root). On an error nothing has been written, so the caller writes the error response in its own format: ErrNotFound for a missing file, a directory or a name outside root; any other error means root itself cannot be opened.
In Gin: file.ServeAttachment(c.Writer, c.Request, dir, c.Query("file")).
func ValidFilename ¶
ValidFilename accepts an empty name, or valid UTF-8 of at most MaxFilenameLength characters without control characters, '/' or '\'. The name is for display and download only; never build a path or an object key from it.
Types ¶
type Policy ¶
type Policy struct {
Types map[string]string // content type -> file extension, without the dot
MaxSize int64 // largest file in bytes
Disposition string // Content-Disposition of the stored object, e.g. "attachment"; empty shows it inline
}
Policy is what one kind of upload accepts.
func (Policy) Check ¶
Check validates one upload and returns the file extension of contentType. size must be 1 to MaxSize bytes. filename is optional and must pass ValidFilename. Every error wraps ErrInvalid.
Example ¶
ExamplePolicy_Check checks two uploads against a document policy.
package main
import (
"errors"
"fmt"
"github.com/phcp-tech/common-library-golang/file"
)
func main() {
docs := file.Policy{Types: file.DocumentTypes(), MaxSize: 20_000_000, Disposition: "attachment"}
ext, err := docs.Check("application/pdf", 123456, "report.pdf")
fmt.Println(ext, err)
_, err = docs.Check("application/msword", 123456, "old.doc")
fmt.Println(errors.Is(err, file.ErrInvalid))
}
Output: pdf <nil> true