archive

package
v0.1.21 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2026 License: GPL-2.0, GPL-3.0 Imports: 13 Imported by: 0

Documentation

Overview

Package archive provides subtitle extraction from ZIP and RAR archives. It is the single source of truth for archive-related logic in the provider subsystem: format detection, subtitle extension validation, episode matching, and decompression-bomb guards.

Index

Constants

View Source
const MaxExtractSize = 5 << 20

MaxExtractSize is the maximum size of a single extracted subtitle file (5 MB).

Variables

View Source
var SubtitleExts = map[string]bool{
	".srt": true, ".ass": true, ".ssa": true, ".sub": true,
	".vtt": true,
}

SubtitleExts lists file extensions recognized as subtitle formats. Includes .vtt which is not in search/existing.go's list because VTT files are only encountered inside archives, not as standalone files on disk from Sonarr/Radarr media libraries.

Functions

func Decompress

func Decompress(data []byte) []byte

Decompress detects compressed data by magic bytes and decompresses it. Supports xz (via ulikunitz/xz) and gzip. Passes through plain data unchanged. Decompressed output is capped at 5 MB (decompression bomb guard).

func Extract

func Extract(data []byte, season, episode int) []byte

Extract attempts to extract a subtitle file from an archive. Tries ZIP first, then RAR. If the data is not a recognized archive, returns it only if it looks like valid subtitle content (SRT timing arrows, ASS headers, or WebVTT signature). Returns nil for unrecognized binary data.

When season > 0 and episode > 0, returns only files matching the target episode (S##E## pattern). Returns nil if no match is found.

func ExtractFromRAR

func ExtractFromRAR(data []byte, season, episode int) []byte

ExtractFromRAR attempts to extract a subtitle file from a RAR archive. Supports both RAR4 and RAR5 formats. When season > 0 and episode > 0, returns only files matching the target episode (S##E## pattern); returns nil if no match is found (no fallback to unmatched files). Without episode context, returns the first valid subtitle entry. Returns nil if data is not a valid RAR, contains no subtitles, or the extracted content exceeds MaxExtractSize (5 MB).

func ExtractFromZip

func ExtractFromZip(data []byte, season, episode int) []byte

ExtractFromZip attempts to extract a subtitle file from a zip archive. When season > 0 and episode > 0, returns only files matching the target episode (S##E## pattern); returns nil if no match is found (no fallback to unmatched files). Without episode context, returns the first subtitle. Returns nil if data is not a valid zip, contains no subtitles, or the matching subtitle exceeds 5 MB. Rejects zip bombs (uncompressed > 50x compressed, or zero compressed with non-zero uncompressed) and caps extracted content at 5 MB.

func HasArchiveSignature

func HasArchiveSignature(data []byte) bool

HasArchiveSignature checks whether data starts with a known archive magic number (ZIP or RAR). Used to skip expensive archive probing when the data is clearly plain text.

func IsValidRAREntry

func IsValidRAREntry(hdr *rardecode.FileHeader) bool

IsValidRAREntry checks if a RAR header represents a valid subtitle file. Includes a decompression bomb guard consistent with the ZIP extraction path.

func IsValidSubtitleEntry

func IsValidSubtitleEntry(f *zip.File) bool

IsValidSubtitleEntry checks if a zip entry is a valid subtitle file, applying extension, hidden file, and zip bomb checks.

func LooksLikeSubtitle

func LooksLikeSubtitle(data []byte) bool

LooksLikeSubtitle checks whether data appears to be a text-based subtitle file by looking for format-specific signatures in the first 4KB. Also rejects data with high concentrations of non-text bytes.

func MatchesEpisode

func MatchesEpisode(name string, season, episode int) bool

MatchesEpisode checks if a filename contains the target season+episode. Handles multi-episode files (S01E01E02, S01E01-E02, S01E01-02).

func MatchesMultiEpisodeRange

func MatchesMultiEpisodeRange(base string, episode int) bool

MatchesMultiEpisodeRange checks if a filename contains a multi-episode range (E01E02, E01-E02, E01-02, E01.E02) that includes the target episode.

func ReadZipEntry

func ReadZipEntry(f *zip.File) []byte

ReadZipEntry reads and returns the content of a zip entry, capped at 5 MB.

Types

This section is empty.

Jump to

Keyboard shortcuts

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