internal

package
v0.0.0-...-84c2a97 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2021 License: GPL-3.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DirDatabase = "db"
	DirStorage  = "data"
)
View Source
const (
	IDSize    = 4
	KeySize   = 32
	NonceSize = 24
)
View Source
const MimeDrop = "DROP"

Variables

View Source
var (
	ErrLifetimeToLong = errors.New("lifetime is greater than maximum lifetime")

	ErrFileToBig = errors.New("file size is greater than maximum filesize")
)
View Source
var ErrDecryptionError = errors.New("decryption error")

ErrDecryptionError is returned by `Store.Get` if the decryption failed. This may be because the wrong key has been provided, or because the data has been tampered with.

View Source
var ErrMimeDrop = errors.New("MIME must be dropped")
View Source
var (
	ErrNoMatch = errors.New("input does not match pattern")
)
View Source
var ErrNotFound = errors.New("no Item found for this ID")

ErrNotFound is returned by the `Store.Get` method if there is no Item for the requested ID.

Functions

func NewOwnerTypes

func NewOwnerTypes(r *http.Request) (owners map[OwnerType]net.IP, err error)

NewOwnerTypes creates a map of OwnerTypes to IP addresses based on a Request.

func ParseBytesize

func ParseBytesize(s string) (size int64, err error)

ParseBytesize parses a positive, human readable and whole byte amount in the binary prefix notation. Legit values might be "1B", "23KiB"/"23KB" etc.

func ParseDuration

func ParseDuration(s string) (d time.Duration, err error)

ParseDuration parses a (positive) duration string, similar to the `time.ParseDuration` method. A duration string is sequence of decimal numbers and a unit suffix. Valid time units are "s", "m", "h", "d", "w", "mo", "y".

func PrettyBytesize

func PrettyBytesize(bs int64) string

PrettyBytesize returns a human readable representation of a byte size.

func PrettyDuration

func PrettyDuration(d time.Duration) string

PrettyDuration returns a human readable representation of a time.Duration.

func WebProtocol

func WebProtocol(r *http.Request) string

WebProtocol returns "http" or "https", based on the X-Forwarded-Proto header.

Types

type Item

type Item struct {
	ID string `badgerhold:"key"`

	BurnAfterReading bool

	Filename      string
	FilenameNonce [NonceSize]byte

	ContentType string

	Chunks      uint64
	ChunkSize   uint64
	ChunkNonces [][NonceSize]byte

	Created time.Time
	Expires time.Time `badgerholdIndex:"Expires"`

	Owner map[OwnerType]net.IP
}

Item describes an uploaded file.

func NewItem

func NewItem(r *http.Request, maxSize int64, maxLifetime time.Duration, chunkSize uint64) (item Item, file io.ReadCloser, err error)

NewItem creates a new Item based on a Request. The ID will be left empty. Furthermore, if no error has occurred, a file is returned from which the file content should be read. This file must be closed afterwards.

func (Item) DeleteContent

func (i Item) DeleteContent(directory string) error

DeleteContent removes the content of an Item from the given directory.

func (Item) ReadEncryptedFile

func (i Item) ReadEncryptedFile(directory string, secretKey [KeySize]byte) (io.ReadCloser, error)

ReadEncryptedFile takes the chunks written by WriteEncryptedFile, decrypts and verifies each chunk and reassembles the original file in memory (the decrypted contents are never written to disk).

func (Item) ReadFile

func (i Item) ReadFile(directory string) (io.ReadCloser, error)

ReadFile deserializes the file of an Item from the given directory into a ReadCloser.

func (Item) WriteEncryptedFile

func (i Item) WriteEncryptedFile(file io.ReadCloser, secretKey [KeySize]byte, directory string) (uint64, [][NonceSize]byte, error)

WriteEncryptedFile splits the file into chunks of size ChunkSize and encrypts each chunk using nacl secretbox. Files will be put into a folder named with the Item ID

func (Item) WriteFile

func (i Item) WriteFile(file io.ReadCloser, directory string) error

WriteFile serializes the file of an Item in the given directory. The file name will be the ID of the Item.

type MimeMap

type MimeMap map[string]string

MimeMap replaces predefined MIME types with others or requires them to be dropped.

# An example MimeMap could look like this, comment included:
text/html        text/plain
text/javascript  text/plain
text/mp4         DROP

func NewMimeMap

func NewMimeMap(file io.Reader) (mm MimeMap, err error)

NewMimeMap creates a new MimeMap based on the Reader's data.

func (MimeMap) MustDrop

func (mm MimeMap) MustDrop(mime string) bool

MustDrop indicates if a MIME type must be dropped.

func (MimeMap) Substitute

func (mm MimeMap) Substitute(mime string) (mimeOut string, err error)

Substitute returns the replaced MIME type and indicates with an error, if the input MIME type must be dropped.

type OwnerType

type OwnerType string

OwnerType describes a possible type of an owner, as an IP address. This can be the remote address as well as some header field.

const (
	RemoteAddr    OwnerType = "RemoteAddr"
	Forwarded     OwnerType = "Forwarded"
	XForwardedFor OwnerType = "X-Forwarded-For"
)

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server implements a http.Handler for up- and download.

func NewServer

func NewServer(storeDirectory string, maxSize int64, maxLifetime time.Duration,
	contactMail string, mimeMap MimeMap, chunkSize uint64) (s *Server, err error)

NewServer creates a new Server with a given database directory, and configuration values. The Server must be started as a http.Handler.

func (*Server) Close

func (serv *Server) Close() error

Close the Server and its components.

func (*Server) ServeHTTP

func (serv *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store stores an index of all Items as well as the pure files.

func NewStore

func NewStore(baseDir string, backgroundCleanup bool) (s *Store, err error)

NewStore opens or initializes a Store in the given directory. A background task for continuous cleaning can be activated.

func (*Store) BadgerHold

func (s *Store) BadgerHold() *badgerhold.Store

BadgerHold returns a reference to the underlying BadgerHold instance.

func (*Store) Close

func (s *Store) Close() error

Close the Store and its database.

func (*Store) Delete

func (s *Store) Delete(i Item) (err error)

Delete an Item. Both the database entry and the file will be removed.

func (*Store) DeleteExpired

func (s *Store) DeleteExpired() error

DeleteExpired checks the Store for expired Items and deletes them.

func (*Store) Get

func (s *Store) Get(id string, delExpired bool) (i Item, err error)

Get an Item by its ID. The Item's content can be accessed with GetFile.

func (*Store) GetDecrypted

func (s *Store) GetDecrypted(id string, secretKey [KeySize]byte, delExpired bool) (i Item, err error)

GetDecrypted gets an Item by its ID and decrypts the filename. The Item's content can be accessed with GetFile.

func (*Store) GetFile

func (s *Store) GetFile(i Item, secretKey [KeySize]byte) (io.ReadCloser, error)

GetFile creates a ReadCloser to the Item's file.

func (*Store) Put

func (s *Store) Put(i Item, file io.ReadCloser) (id string, secretKey [KeySize]byte, err error)

Put a new Item inside the Store. Both a database entry and a file will be created.

Jump to

Keyboard shortcuts

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