storage

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2026 License: GPL-3.0 Imports: 20 Imported by: 0

Documentation

Overview

Package storage stores downloaded chapter files on configurable backends: the local filesystem (which includes mounted network shares) and S3-compatible object storage. Files are addressed by key (a slash-separated path); every backend writes atomically and reads with random access, so the reader can open archives without downloading them whole.

Index

Constants

View Source
const (
	KindLocal = "local"
	KindS3    = "s3"
)

Kinds of storage.

Variables

View Source
var (
	ErrUnknown    = errors.New("storage: no such storage")
	ErrIDTaken    = errors.New("storage: a storage with this ID exists")
	ErrInvalidID  = errors.New("storage: the ID must be lowercase letters, digits, '-' or '_' (up to 40)")
	ErrInUse      = errors.New("storage: files are stored here; move them first")
	ErrIsDefault  = errors.New("storage: the default storage can't be removed; make another the default first")
	ErrBadConfig  = errors.New("storage: invalid settings")
	ErrNoDefault  = errors.New("storage: no default storage")
	ErrRootNeeded = errors.New("storage: a local storage needs an absolute root directory")
)

Errors callers map to responses.

View Source
var ErrNotFound = errors.New("storage: not found")

ErrNotFound is a key with no file.

Functions

This section is empty.

Types

type Config

type Config struct {
	Local *LocalConfig
	S3    *S3Config
}

Config is a storage's settings: Local for local, S3 for S3.

func ConfigOf

func ConfigOf(s db.Storage) (Config, error)

ConfigOf decodes a storage's settings (without the secret).

type File

type File interface {
	io.ReaderAt
	io.Closer
	Size() int64
}

File is an open stored file with random access.

type Local

type Local struct {
	Root string
}

Local stores files under a directory.

func (*Local) Delete

func (l *Local) Delete(_ context.Context, key string) error

Delete removes a file and then empty parent directories up to the root.

func (*Local) Open

func (l *Local) Open(_ context.Context, key string) (File, error)

Open opens a file.

func (*Local) Put

func (l *Local) Put(_ context.Context, key, src string) error

Put moves (or copies, across filesystems) the file into place through a temporary name in the target directory, then renames it.

func (*Local) Test

func (l *Local) Test(ctx context.Context) error

Test writes, reads and deletes a probe file.

type LocalConfig

type LocalConfig struct {
	Root string `json:"root"`
}

LocalConfig is a local storage's settings.

type Manager

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

Manager keeps the configured storages.

func NewManager

func NewManager(st *store.Store, box *secret.Box, log *slog.Logger) *Manager

NewManager returns the storage manager.

func (*Manager) Backend

func (m *Manager) Backend(ctx context.Context, id string) (Storage, error)

Backend returns the live backend of a storage.

func (*Manager) Create

func (m *Manager) Create(ctx context.Context, n NewStorage) (db.Storage, error)

Create checks a storage works (write, read, delete) and saves it.

func (*Manager) Default

func (m *Manager) Default(ctx context.Context) (db.Storage, Storage, error)

Default returns the default storage and its backend.

func (*Manager) Delete

func (m *Manager) Delete(ctx context.Context, id string) error

Delete removes an empty storage that isn't the default.

func (*Manager) EnsureDefault

func (m *Manager) EnsureDefault(ctx context.Context, root string) error

EnsureDefault creates a local storage at root as the default when there is no storage yet.

func (*Manager) Get

func (m *Manager) Get(ctx context.Context, id string) (db.Storage, error)

Get returns a storage.

func (*Manager) List

func (m *Manager) List(ctx context.Context) ([]db.Storage, error)

List returns every storage.

func (*Manager) SetDefault

func (m *Manager) SetDefault(ctx context.Context, id string) error

SetDefault makes a storage the default for new downloads.

func (*Manager) Test

func (m *Manager) Test(ctx context.Context, id string) error

Test checks a configured storage now.

func (*Manager) Update

func (m *Manager) Update(ctx context.Context, id, name string, cfg Config, secret string) (db.Storage, error)

Update changes a storage's name and settings (checked first). An empty secret keeps the stored one.

type NewStorage

type NewStorage struct {
	ID, Name, Kind string
	Config         Config
	// Secret is the S3 secret access key.
	Secret string
}

NewStorage is a storage to create.

type S3

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

S3 stores files in an S3-compatible bucket. Object writes are atomic.

func NewS3

func NewS3(cfg S3Config, secret string) (*S3, error)

NewS3 returns an S3 backend; secret is the secret access key.

func (*S3) Delete

func (s *S3) Delete(ctx context.Context, key string) error

Delete removes an object.

func (*S3) Open

func (s *S3) Open(ctx context.Context, key string) (File, error)

Open opens an object for ranged reads.

func (*S3) Put

func (s *S3) Put(ctx context.Context, key, src string) error

Put uploads a file.

func (*S3) Test

func (s *S3) Test(ctx context.Context) error

Test checks the bucket exists and a probe can be written, read and deleted.

type S3Config

type S3Config struct {
	// Endpoint is the service's URL, e.g. "https://s3.example.com".
	Endpoint string `json:"endpoint"`
	Region   string `json:"region,omitempty"`
	Bucket   string `json:"bucket"`
	// Prefix is prepended to every key, e.g. "panelarr/".
	Prefix    string `json:"prefix,omitempty"`
	AccessKey string `json:"access_key"`
	// PathStyle addresses the bucket in the path (most self-hosted
	// services need it).
	PathStyle bool `json:"path_style"`
}

S3Config is how to reach an S3-compatible bucket.

type Storage

type Storage interface {
	// Put stores the file at path under key, replacing nothing until it is
	// complete: readers see the old file or the new one, never a partial
	// one.
	Put(ctx context.Context, key, path string) error
	// Open opens a stored file for random access.
	Open(ctx context.Context, key string) (File, error)
	// Delete removes a file; a missing file is not an error.
	Delete(ctx context.Context, key string) error
	// Test checks the backend can write, read and delete.
	Test(ctx context.Context) error
}

Storage is one storage backend.

Jump to

Keyboard shortcuts

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