file

package
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: Apache-2.0 Imports: 12 Imported by: 1 module, 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

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

View Source
const MaxFilenameLength = 255

MaxFilenameLength is the longest file name ValidFilename accepts, in characters.

Variables

View Source
var ErrInvalid = errors.New("file: invalid upload")

ErrInvalid is wrapped by every error of Policy.Check.

View Source
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

func DocumentTypes() map[string]string

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

func ImageTypes() map[string]string

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

func ServeAttachment(w http.ResponseWriter, r *http.Request, root, name string) error

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

func ValidFilename(name string) bool

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

func (p Policy) Check(contentType string, size int64, filename string) (string, error)

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

Jump to

Keyboard shortcuts

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