storage

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2026 License: AGPL-3.0 Imports: 7 Imported by: 0

Documentation

Overview

Package storage provides an abstraction layer for storing snapshots and resources.

This package defines the Storage interface which can be implemented by various backends (filesystem, S3, etc.). Currently, only filesystem storage is implemented.

Snapshots are compressed HTML archives of bookmarked web pages, while resources are embedded assets like images, stylesheets, and scripts extracted from pages.

All stored content is compressed with gzip to save disk space. Files are organized in a two-character prefix directory structure based on their hash to avoid filesystem limitations with too many files in a single directory.

The package provides a global storage instance that is initialized at startup and used throughout the application. All functions panic if storage is accessed before initialization.

Example usage:

err := storage.Init(cfg.Storage)
if err != nil {
    log.Fatal(err)
}

// Save a snapshot
key := storage.Hash(htmlContent) + ".html"
err = storage.SaveSnapshot(key, htmlContent)

// Retrieve a snapshot
reader, err := storage.GetSnapshot(key)
if err != nil {
    return err
}
defer reader.Close()

Index

Constants

This section is empty.

Variables

View Source
var ErrResourceNotFound = errors.New("resource not found")

ErrResourceNotFound is returned when a resource cannot be found.

View Source
var ErrSnapshotNotFound = errors.New("snapshot not found")

ErrSnapshotNotFound is returned when a snapshot cannot be found.

View Source
var ErrStreamNotFound = errors.New("streamable content not found")

ErrStreamNotFound is returned when a streamable content cannot be found.

View Source
var ErrUninitialized = errors.New("uninitialized storage")

ErrUninitialized is returned when storage is accessed before initialization.

View Source
var ErrUnknownStorage = errors.New("unknown storage type")

ErrUnknownStorage is returned when an unknown storage type is configured.

Functions

func FS added in v0.5.0

func FS() iofs.FS

FS returns the storage backend as an io/fs.FS interface for file system operations. This allows reading stored files using the standard library's fs.FS methods. Panics if storage has not been initialized.

func GetResource added in v0.2.0

func GetResource(key string) (io.ReadCloser, error)

GetResource retrieves a resource by its key and returns a reader for its contents. Resources are typically images, stylesheets, or other embedded assets from web pages. The returned reader must be closed by the caller when done. Returns ErrUninitialized if storage is not initialized, or ErrResourceNotFound if the resource doesn't exist.

func GetResourceSize added in v0.2.0

func GetResourceSize(key string) uint

GetResourceSize returns the size in bytes of a stored resource. Returns 0 if the resource doesn't exist. Panics if storage has not been initialized.

func GetResourceURL added in v0.3.0

func GetResourceURL(key string) string

GetResourceURL returns the URL path for accessing a resource via HTTP. This is typically used to generate URLs for embedded images and assets in HTML. Panics if storage has not been initialized.

func GetSnapshot

func GetSnapshot(key string) (io.ReadCloser, error)

GetSnapshot retrieves a snapshot by its key and returns a reader for its contents. The returned reader must be closed by the caller when done. Returns ErrUninitialized if storage is not initialized, or ErrSnapshotNotFound if the snapshot doesn't exist.

func GetSnapshotSize added in v0.2.0

func GetSnapshotSize(key string) uint

GetSnapshotSize returns the size in bytes of a stored snapshot. Returns 0 if the snapshot doesn't exist. Panics if storage has not been initialized.

func GetStream added in v0.9.0

func GetStream(key string) (io.ReadCloser, error)

GetStream retrieves a streamable content by its key and returns a reader for its contents. The returned reader must be closed by the caller when done. Returns ErrUninitialized if storage is not initialized, or ErrStreamNotFound if the resource doesn't exist.

func GetStreamSize added in v0.9.0

func GetStreamSize(key string) uint

GetStreamSize returns the size in bytes of a stored streamable content. Returns 0 if the resource doesn't exist. Panics if storage has not been initialized.

func GetStreamURL added in v0.9.0

func GetStreamURL(key string) string

GetStreamURL returns the URL path for accessing a streamable content via HTTP. Panics if storage has not been initialized.

func Hash

func Hash(x []byte) string

Hash computes a SHA256 hash of the given bytes and returns it as a hexadecimal string. This is used to generate unique keys for storing snapshots and resources.

func Init

func Init(sCfg config.Storage) error

Init initializes the storage backend with the given configuration. This must be called before using any other storage functions. Returns an error if the storage configuration is invalid or initialization fails.

func SaveResource added in v0.2.0

func SaveResource(ext string, resource io.Reader) (string, error)

SaveResource stores a resource with the given file extension. Resources are typically images, stylesheets, or other embedded assets. The resource data is compressed before storage to save disk space. Returns with the key required to access the resource on success. Returns ErrUninitialized if storage is not initialized, or an error if saving fails.

func SaveSnapshot

func SaveSnapshot(key string, snapshot []byte) error

SaveSnapshot stores a snapshot with the given key. The snapshot data is compressed before storage to save disk space. Returns ErrUninitialized if storage is not initialized, or an error if saving fails.

func SaveStream added in v0.9.0

func SaveStream(ext string, resource io.Reader) (string, error)

SaveResource stores a streamable content with the given file extension. Returns with the key required to access the resource on success. Returns ErrUninitialized if storage is not initialized, or an error if saving fails.

Types

type Storage

type Storage interface {
	FS() (iofs.FS, error)
	GetSnapshot(string) io.ReadCloser
	GetSnapshotSize(string) uint
	SaveSnapshot(string, []byte) error
	SaveResource(string, io.Reader) (string, error)
	SaveStream(string, io.Reader) (string, error)
	GetResource(string) io.ReadCloser
	GetStream(string) io.ReadCloser
	GetResourceSize(string) uint
	GetStreamSize(string) uint
	GetResourceURL(string) string
	GetStreamURL(string) string
}

Storage defines the interface for snapshot and resource storage backends.

Directories

Path Synopsis
Package fs implements filesystem-based storage for Omnom snapshots and resources.
Package fs implements filesystem-based storage for Omnom snapshots and resources.

Jump to

Keyboard shortcuts

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