downloadcache

package
v0.0.13 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	FormatOriginal = "original"
	FormatKepub    = "kepub"
)

Download format constants.

View Source
const CleanupThreshold = 0.8

CleanupThreshold is the percentage of maxSize to reduce the cache to during cleanup. For example, 0.8 means cleanup will reduce the cache to 80% of maxSize.

Variables

This section is empty.

Functions

func DeleteCachedFile

func DeleteCachedFile(cacheDir string, fileID int, ext string) error

DeleteCachedFile removes both the cached file and its metadata.

func DeleteKepubCachedFile

func DeleteKepubCachedFile(cacheDir string, fileID int) error

DeleteKepubCachedFile removes both the cached KePub file and its metadata.

func DeletePluginCachedFile

func DeletePluginCachedFile(cacheDir string, fileID int, formatID string) error

DeletePluginCachedFile removes both the cached plugin file and its metadata.

func FormatDownloadFilename

func FormatDownloadFilename(book *models.Book, file *models.File) string

FormatDownloadFilename generates a formatted filename for downloading a file. Format: [Author] Series #Number - Title.ext For audiobooks with narrators: [Author] Series #Number - Title {Narrator}.ext If no series: [Author] Title.ext. If no author: Title.ext.

func FormatKepubDownloadFilename

func FormatKepubDownloadFilename(book *models.Book, file *models.File) string

FormatKepubDownloadFilename generates a formatted filename for downloading a KePub file. Uses Kobo-safe characters only to ensure compatibility with Kobo e-readers. Format: Author - Series Number - Title.kepub.epub (no brackets, no hash symbols).

func FormatPluginDownloadFilename

func FormatPluginDownloadFilename(book *models.Book, file *models.File, formatID string) string

FormatPluginDownloadFilename generates a formatted filename for a plugin-generated download. It replaces the file extension with the plugin's format ID.

func GetCachedFilePath

func GetCachedFilePath(cacheDir string, fileID int, ext string, currentHash string) (string, error)

GetCachedFilePath returns the path to a cached file if it exists and is valid. Returns empty string if the cache is invalid or doesn't exist.

func GetKepubCachedFilePath

func GetKepubCachedFilePath(cacheDir string, fileID int, currentHash string) (string, error)

GetKepubCachedFilePath returns the path to a cached KePub file if it exists and is valid. Returns empty string if the cache is invalid or doesn't exist.

func GetPluginCachedFilePath

func GetPluginCachedFilePath(cacheDir string, fileID int, formatID, currentHash string) (string, error)

GetPluginCachedFilePath returns the path to a cached plugin file if it exists and is valid. Returns empty string if the cache is invalid or doesn't exist.

func GetTotalCacheSize

func GetTotalCacheSize(cacheDir string) (int64, error)

GetTotalCacheSize returns the total size of all cached files in bytes.

func RunCleanup

func RunCleanup(cacheDir string, maxSizeBytes int64) error

RunCleanup removes cached files until the total size is below the threshold. Files are removed in LRU (least recently used) order.

func UpdateKepubLastAccessed

func UpdateKepubLastAccessed(cacheDir string, fileID int) error

UpdateKepubLastAccessed updates the last accessed time for a cached KePub file.

func UpdateLastAccessed

func UpdateLastAccessed(cacheDir string, fileID int) error

UpdateLastAccessed updates the last accessed time for a cached file.

func UpdatePluginLastAccessed

func UpdatePluginLastAccessed(cacheDir string, fileID int, formatID string) error

UpdatePluginLastAccessed updates the last accessed time for a cached plugin file.

func WriteKepubMetadata

func WriteKepubMetadata(cacheDir string, meta *CacheMetadata) error

WriteKepubMetadata writes the cache metadata for a KePub cache entry.

func WriteMetadata

func WriteMetadata(cacheDir string, meta *CacheMetadata) error

WriteMetadata writes the cache metadata for a file ID.

func WritePluginMetadata

func WritePluginMetadata(cacheDir string, fileID int, formatID string, meta *CacheMetadata) error

WritePluginMetadata writes the cache metadata for a plugin cache entry.

Types

type Cache

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

Cache manages the download cache for generated files.

func NewCache

func NewCache(dir string, maxSizeBytes int64) *Cache

NewCache creates a new Cache with the given directory and max size.

func (*Cache) Dir

func (c *Cache) Dir() string

Dir returns the cache directory path.

func (*Cache) GetCachedPath

func (c *Cache) GetCachedPath(fileID int, fileType string, book *models.Book, file *models.File) (string, error)

GetCachedPath returns the path to a cached file if it exists and is valid. Returns empty string if the cache doesn't exist or is invalid.

func (*Cache) GetOrGenerate

func (c *Cache) GetOrGenerate(ctx context.Context, book *models.Book, file *models.File) (cachedPath string, downloadFilename string, err error)

GetOrGenerate returns the path to a cached file, generating it if necessary. It returns the cached file path, the formatted download filename, and any error.

func (*Cache) GetOrGenerateKepub

func (c *Cache) GetOrGenerateKepub(ctx context.Context, book *models.Book, file *models.File) (cachedPath string, downloadFilename string, err error)

GetOrGenerateKepub returns the path to a cached KePub file, generating it if necessary. It returns the cached file path, the formatted download filename, and any error. Returns ErrKepubNotSupported if the file type cannot be converted to KePub.

func (*Cache) GetOrGeneratePlugin

func (c *Cache) GetOrGeneratePlugin(ctx context.Context, book *models.Book, file *models.File, generator *plugins.PluginGenerator) (cachedPath string, downloadFilename string, err error)

GetOrGeneratePlugin returns the path to a cached plugin-generated file. The pluginGenerator handles both generation and fingerprinting.

func (*Cache) Invalidate

func (c *Cache) Invalidate(fileID int, fileType string) error

Invalidate removes the cached file for a given file ID.

func (*Cache) InvalidateKepub

func (c *Cache) InvalidateKepub(fileID int) error

InvalidateKepub removes the cached KePub file for a given file ID.

func (*Cache) InvalidatePlugin

func (c *Cache) InvalidatePlugin(fileID int, formatID string) error

InvalidatePlugin removes the cached plugin file for a given file ID and format.

func (*Cache) MaxSize

func (c *Cache) MaxSize() int64

MaxSize returns the maximum cache size in bytes.

func (*Cache) TriggerCleanup

func (c *Cache) TriggerCleanup()

TriggerCleanup runs cache cleanup if the cache exceeds the max size. This runs in the current goroutine - call with `go` to run in background.

type CacheMetadata

type CacheMetadata struct {
	FileID          int       `json:"file_id"`
	Format          string    `json:"format,omitempty"` // "original" or "kepub"
	FingerprintHash string    `json:"fingerprint_hash"`
	GeneratedAt     time.Time `json:"generated_at"`
	LastAccessedAt  time.Time `json:"last_accessed_at"`
	SizeBytes       int64     `json:"size_bytes"`
}

CacheMetadata stores information about a cached file.

func ListCacheEntries

func ListCacheEntries(cacheDir string) ([]*CacheMetadata, error)

ListCacheEntries returns all cache entries in the directory.

func ReadKepubMetadata

func ReadKepubMetadata(cacheDir string, fileID int) (*CacheMetadata, error)

ReadKepubMetadata reads the cache metadata for a KePub cache entry. Returns nil if the metadata file doesn't exist.

func ReadMetadata

func ReadMetadata(cacheDir string, fileID int) (*CacheMetadata, error)

ReadMetadata reads the cache metadata for a file ID. Returns nil if the metadata file doesn't exist.

func ReadPluginMetadata

func ReadPluginMetadata(cacheDir string, fileID int, formatID string) (*CacheMetadata, error)

ReadPluginMetadata reads the cache metadata for a plugin cache entry. Returns nil if the metadata file doesn't exist.

type CleanupStats

type CleanupStats struct {
	FilesRemoved  int
	BytesRemoved  int64
	FilesRemained int
	BytesRemained int64
}

CleanupStats holds statistics about a cleanup operation.

func RunCleanupWithStats

func RunCleanupWithStats(cacheDir string, maxSizeBytes int64) (*CleanupStats, error)

RunCleanupWithStats performs cleanup and returns statistics.

type Fingerprint

type Fingerprint struct {
	Title             string                  `json:"title"`
	Subtitle          *string                 `json:"subtitle,omitempty"`
	Description       *string                 `json:"description,omitempty"`
	Authors           []FingerprintAuthor     `json:"authors"`
	Narrators         []FingerprintNarrator   `json:"narrators"`
	Series            []FingerprintSeries     `json:"series"`
	Genres            []string                `json:"genres"`
	Tags              []string                `json:"tags"`
	Identifiers       []FingerprintIdentifier `json:"identifiers,omitempty"`
	URL               *string                 `json:"url,omitempty"`
	Publisher         *string                 `json:"publisher,omitempty"`
	Imprint           *string                 `json:"imprint,omitempty"`
	ReleaseDate       *time.Time              `json:"release_date,omitempty"`
	Cover             *FingerprintCover       `json:"cover,omitempty"`
	CoverPage         *int                    `json:"cover_page,omitempty"` // For CBZ files: page index of cover
	Chapters          []FingerprintChapter    `json:"chapters,omitempty"`
	Format            string                  `json:"format,omitempty"`             // Download format: original, kepub, or plugin:<id>
	Name              *string                 `json:"name,omitempty"`               // File name (edition name)
	PluginFingerprint string                  `json:"plugin_fingerprint,omitempty"` // Plugin-specific fingerprint for cache invalidation
}

Fingerprint represents the metadata that affects file generation. Changes to any of these fields should invalidate the cached file.

func ComputeFingerprint

func ComputeFingerprint(book *models.Book, file *models.File) (*Fingerprint, error)

ComputeFingerprint creates a fingerprint from a book and file.

func (*Fingerprint) Equal

func (fp *Fingerprint) Equal(other *Fingerprint) bool

Equal compares two fingerprints for equality.

func (*Fingerprint) Hash

func (fp *Fingerprint) Hash() (string, error)

Hash computes a SHA256 hash of the fingerprint.

type FingerprintAuthor

type FingerprintAuthor struct {
	Name      string  `json:"name"`
	Role      *string `json:"role,omitempty"` // CBZ author role (writer, penciller, etc.)
	SortOrder int     `json:"sort_order"`
}

FingerprintAuthor represents author information for fingerprinting.

type FingerprintChapter

type FingerprintChapter struct {
	Title            string               `json:"title"`
	SortOrder        int                  `json:"sort_order"`
	StartPage        *int                 `json:"start_page,omitempty"`
	StartTimestampMs *int64               `json:"start_timestamp_ms,omitempty"`
	Href             *string              `json:"href,omitempty"`
	Children         []FingerprintChapter `json:"children,omitempty"`
}

FingerprintChapter represents chapter information for fingerprinting.

type FingerprintCover

type FingerprintCover struct {
	Path     string    `json:"path"`
	MimeType string    `json:"mime_type"`
	ModTime  time.Time `json:"mod_time"`
}

FingerprintCover represents cover image information for fingerprinting.

type FingerprintIdentifier

type FingerprintIdentifier struct {
	Type  string `json:"type"`
	Value string `json:"value"`
}

FingerprintIdentifier represents identifier information for fingerprinting.

type FingerprintNarrator

type FingerprintNarrator struct {
	Name      string `json:"name"`
	SortOrder int    `json:"sort_order"`
}

FingerprintNarrator represents narrator information for fingerprinting.

type FingerprintSeries

type FingerprintSeries struct {
	Name      string   `json:"name"`
	Number    *float64 `json:"number,omitempty"`
	SortOrder int      `json:"sort_order"`
}

FingerprintSeries represents series information for fingerprinting.

Jump to

Keyboard shortcuts

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