files

package
v1.0.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: 13 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"
)

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

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. It fails with TagDirCreateFailed when a directory cannot be made and with TagTmpCleanFailed when dir/tmp cannot be emptied.

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