files

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2026 License: BSD-3-Clause Imports: 15 Imported by: 0 modules, 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 files stores the attachments on disk under the data directory.

Index

Constants

View Source
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"
)
View Source
const (
	// DownloadCSP is the Content-Security-Policy of a download: nothing may
	// load or run if the file is opened as a page (SPEC §7.5, §17), and the
	// sandbox turns off scripts, forms and the origin of the page.
	DownloadCSP = "default-src 'none'; img-src 'self'; style-src 'unsafe-inline'; sandbox"
	// DownloadCacheControl lets the client of the reader keep a file for a day;
	// shared caches never see it (SPEC D36).
	DownloadCacheControl = "private, max-age=86400"
	// OctetStream is the type of every download that is not an inline image.
	OctetStream = "application/octet-stream"
)

Variables

This section is empty.

Functions

func ContentDisposition added in v1.1.0

func ContentDisposition(disposition, name string) string

ContentDisposition makes the header Content-Disposition for a file name (RFC 6266): the plain filename with the characters that need care replaced, and for a name that has more than that, the exact name as filename* (RFC 5987, UTF-8, percent-encoded).

func ServeDownload added in v1.1.0

func ServeDownload(w http.ResponseWriter, r *http.Request, d Download, f io.ReadSeeker)

ServeDownload sends the file with the headers of a download (SPEC §7.5, D36): X-Content-Type-Options: nosniff, the sandbox policy DownloadCSP, DownloadCacheControl, Content-Disposition with the name of the file, and the type of the file only for an inline image (application/octet-stream otherwise). http.ServeContent handles ranges and If-Modified-Since. The caller closes f.

Types

type Download added in v1.1.0

type Download struct {
	// FileName is the name the file had when it was uploaded; the client
	// saves the file under it.
	FileName string
	// ContentType is the type found by sniffing the content. It is used only
	// when Inline is true.
	ContentType string
	// Inline is true for the image types that SPEC §7.5 shows inline (PNG,
	// JPEG, GIF, WebP). Every other file is sent as an attachment of the type
	// application/octet-stream.
	Inline bool
	// ModTime is when the file was attached, for If-Modified-Since.
	ModTime time.Time
}

Download describes the file that ServeDownload sends.

type NoteInfo added in v1.1.0

type NoteInfo struct {
	// Key is the key of the file the note is for.
	Key string
	// ModTime is when the note was last written.
	ModTime time.Time
}

NoteInfo is a note that exists, as Notes lists it.

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

func New(dir string) (*Store, error)

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: those not modified for an hour (staleTmpAge), because another process that shares the directory may be writing one just now. It fails with TagDirCreateFailed when a directory cannot be made and with TagTmpCleanFailed when dir/tmp cannot be emptied.

func (*Store) Note added in v1.1.0

func (s *Store) Note(key string) ([]byte, error)

Note returns the note kept for the key. A key that is not 64 lowercase hex characters fails with TagBadKey, a key with no note with TagNotFound.

func (*Store) Notes added in v1.1.0

func (s *Store) Notes() ([]NoteInfo, error)

Notes lists the notes that exist, in no particular order. Only entries named like a key count: a note being written and anything else in the directory are left out. A directory that does not exist yet has no notes. A note removed by another process while the list is made is left out. It fails with FileNoteListFailed when the directory cannot be read.

func (*Store) Open

func (s *Store) Open(key string) (*os.File, error)

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) PutNote added in v1.1.0

func (s *Store) PutNote(key string, data []byte) error

PutNote keeps a small piece of data about the file stored under key, beside the file, replacing the note that was there. A note is how an upload that has not become an attachment yet is remembered between two requests, by this process and by any other that shares the directory (SPEC §8.1). A note is written whole and renamed to its place. A key that is not 64 lowercase hex characters fails with TagBadKey before the disk is touched.

func (*Store) Remove

func (s *Store) Remove(key string) error

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) RemoveNote added in v1.1.0

func (s *Store) RemoveNote(key string) error

RemoveNote deletes the note kept for the key. A key with no note is not an error. A key that is not 64 lowercase hex characters fails with TagBadKey.

func (*Store) Save

func (s *Store) Save(ctx context.Context, r io.Reader, limit int64) (Saved, error)

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).

func (*Store) Size

func (s *Store) Size(key string) (int64, error)

Size returns the size in bytes of the file stored under key. A key that is not 64 lowercase hex characters fails with TagBadKey, a key with no file with TagNotFound.

Jump to

Keyboard shortcuts

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