attachments

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2022 License: BSD-3-Clause Imports: 19 Imported by: 0

README

attachments

Handles file attachments in applications.

Using

import "github.com/keep94/attachments"

Documentation

Overview

Package attachments is for storing and retrieving file attachments.

Index

Constants

This section is empty.

Variables

View Source
var (
	// Indicates that the Id does not exist in the database.
	ErrNoSuchId = errors.New("attachments: No such Id")
)

Functions

This section is empty.

Types

type Entry

type Entry struct {

	// The unique ID for the entry
	Id int64

	// The entry name e.g "document.pdf"
	Name string

	// The size of the file in bytes
	Size int64

	// The timestamp of the file in seconds
	Ts int64

	// The owner of the file
	OwnerId int64

	// Identifies the file contents
	Checksum string
}

Entry represents a file entry

func (*Entry) FormatSize

func (e *Entry) FormatSize() string

FormatSize returns the file size as readable string such as "1.23 MB"

func (*Entry) FormatTime

func (e *Entry) FormatTime() string

FormatTime returns the file timestamp in the form of "02-Jan-06 15:04" in the local timezone.

func (*Entry) Path

func (e *Entry) Path() string

Path returns the path to this file that ImmutableFs.Open() will accept. The returned value is of the form EntryId/EntryName e.g "12345/document.pdf"

type FS

type FS interface {
	// Open opens a file
	Open(name string) (io.ReadCloser, error)

	// Write writes a file.
	Write(name string) (io.WriteCloser, error)

	// Exists returns true if file with given path exists.
	Exists(name string) bool
}

FS is a very simple file system.

func NewFS

func NewFS(root string) (FS, error)

NewFS returns a file system backed by disk rooted at path root. If root does not exist or is not a directory, NewFS returns os.ErrNotExist.

func NewInMemoryFS added in v0.4.0

func NewInMemoryFS() FS

NewInMemoryFS returns a new in memory file system that can be used with multiple goroutines.

func NilFS added in v0.2.0

func NilFS() FS

NilFS returns an empty file system that cannot be written to.

type ImmutableFS

type ImmutableFS interface {

	// Open opens the named file. name is of the form EntryId/EntryName e.g
	// "12345/document.pdf"
	Open(name string) (fs.File, error)

	// Write writes a new file. name is the file name e.g "document.pdf."
	// Write returns the Id of the new file, e.g 12345. If this instance
	// is read-only, Write returns fs.ErrPermission.
	Write(name string, contents []byte) (int64, error)

	// List returns the files with given ids ordered by id.
	// If an id has no file associated with it, the slice returned will not
	// have an Entry for that id.
	List(t db.Transaction, ids map[int64]bool) ([]*Entry, error)

	// ReadOnly returns true if this instance is read-only.
	ReadOnly() bool
	// contains filtered or unexported methods
}

ImmutableFS represents an immutable file system featuring AES-256 encryption. Note that ImmutableFS implements io/fs.FS

func NewImmutableFS

func NewImmutableFS(fileSystem FS, store Store, owner Owner) ImmutableFS

NewImmutableFS creates a new ImmutableFS instance. fileSystem is where the contents of files from all owners are stored. store is where file meta data from all owners are stored such as size and timestamp. owner specifies the file owner. The returned instance will store and retrieve files only for that owner.

func ReadOnly

func ReadOnly(fileSystem ImmutableFS) ImmutableFS

ReadOnly creates a read-only wrapper around fileSystem. If fileSystem is already read-only, ReadOnly returns it unchanged.

type Owner

type Owner struct {

	// The owner ID
	Id int64

	// The encryption key for the owner. May be nil if no encryption is
	// to be done for the owner.
	Key []byte
}

Owner represents a file owner. Owners can see only their own files.

type Store

type Store interface {

	// AddEntry adds a new record to the datastore. AddEntry sets the
	// Id of the newly added record in entry.
	AddEntry(t db.Transaction, entry *Entry) error

	// EntryById retrieves the record with given id and ownerId storing it in
	// entry. EntryById returns ErrNoSuchId if no record found.
	EntryById(t db.Transaction, id, ownerId int64, entry *Entry) error
}

Store stores and retrieves file entries from a database.

Directories

Path Synopsis
attachmentsdb
fixture
Package fixture provides test suites to test implementations of the datastore for attachments.
Package fixture provides test suites to test implementations of the datastore for attachments.
for_sqlite
Package for_sqlite provides a sqlite implementation of the attachments database.
Package for_sqlite provides a sqlite implementation of the attachments database.
sqlite_setup
Package sqlite_setup sets up a sqlite database for attachments
Package sqlite_setup sets up a sqlite database for attachments

Jump to

Keyboard shortcuts

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