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
- Variables
- func Decompress(data []byte) []byte
- func Extract(data []byte, season, episode int) []byte
- func ExtractFromRAR(data []byte, season, episode int) []byte
- func ExtractFromZip(data []byte, season, episode int) []byte
- func HasArchiveSignature(data []byte) bool
- func IsValidRAREntry(hdr *rardecode.FileHeader) bool
- func IsValidSubtitleEntry(f *zip.File) bool
- func LooksLikeSubtitle(data []byte) bool
- func MatchesEpisode(name string, season, episode int) bool
- func MatchesMultiEpisodeRange(base string, episode int) bool
- func ReadZipEntry(f *zip.File) []byte
Constants ¶
const MaxExtractSize = 5 << 20
MaxExtractSize is the maximum size of a single extracted subtitle file (5 MB).
Variables ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
IsValidSubtitleEntry checks if a zip entry is a valid subtitle file, applying extension, hidden file, and zip bomb checks.
func LooksLikeSubtitle ¶
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 ¶
MatchesEpisode checks if a filename contains the target season+episode. Handles multi-episode files (S01E01E02, S01E01-E02, S01E01-02).
func MatchesMultiEpisodeRange ¶
MatchesMultiEpisodeRange checks if a filename contains a multi-episode range (E01E02, E01-E02, E01-02, E01.E02) that includes the target episode.
func ReadZipEntry ¶
ReadZipEntry reads and returns the content of a zip entry, capped at 5 MB.
Types ¶
This section is empty.