subtitlefile

package
v0.8.0-dev.1 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2026 License: AGPL-3.0 Imports: 8 Imported by: 0

Documentation

Overview

Package subtitlefile owns the subtitle file on disk: what subflux names it, and what it will accept as its contents. Both save paths validate the downloaded bytes and then compute the Path or ManualPath they write them to. The names land on SMB and NFS shares Windows clients read, so the segment layout, the ordinal position and the extension are compatibility contract, and ManualOrdinal must stay the exact inverse of ManualPath's numbering. internal/subtitleext separately decides which extensions COUNT as subtitles; its coverage test pins ExtSRT to the writerOutput view.

Index

Constants

View Source
const ExtSRT = ".srt"

ExtSRT is the extension every subflux writer emits. The capability-scoped extension authority lives in internal/subtitleext; its coverage test pins this constant to the writerOutput view so the two cannot drift.

Variables

View Source
var ErrEmpty = errors.New("empty subtitle data")

ErrEmpty indicates the download carried no bytes at all. Exported because it is the one refusal a caller must be able to tell apart: a zero-byte body is an absent subtitle, not a malformed one, so blaming the archive format in the operator-facing text would misdiagnose it.

Functions

func CountNonText

func CountNonText(data []byte) int

CountNonText returns the number of bytes in data that are not printable text (control characters below TAB, or between CR and SPACE, excluding ESC). Used by Validate and archive extraction.

func ManualOrdinal

func ManualOrdinal(path string) int

ManualOrdinal parses the manual sibling number out of a subtitle path produced by ManualPath: the all-digit dot segment immediately before the extension (movie.fr.2.srt -> 2, movie.fr.forced.1.srt -> 1). An unnumbered path (the auto file, movie.fr.srt) returns 0. This is the inverse of ManualPath's numbering and the ordinal component of the wire FileRef: together with (media_type, media_id, language, variant, source) it uniquely addresses one stored subtitle file, so manual numbered siblings sharing a quad stay distinguishable without a client-supplied path.

func ManualPath

func ManualPath(videoPath string, n int, tags Tags) string

ManualPath computes a numbered manual subtitle path. e.g., movie.fr.1.srt, movie.fr.2.srt for regular subs; movie.fr.hi.1.srt or movie.fr.forced.1.srt when the user deliberately downloaded an HI or forced variant. The variant tag appears before the number so parseExternalSubPath continues to recognize it on the next scan.

func Path

func Path(videoPath string, tags Tags) string

Path computes the subtitle file path for a video.

func Validate

func Validate(data []byte) error

Validate checks whether data is subtitle text: non-empty, not a binary archive, and readable as text in an encoding subflux understands. Returns ErrEmpty for a zero-length payload and errBinary for a container or for bytes no supported encoding can read as text.

Rejecting empty is a contract, not a convenience: every caller is a download boundary, and accepting zero bytes turns a provider's empty 200 into a successful download of no subtitle — a file on disk and a coverage row that both claim a subtitle nothing can read.

The encoding check judges a DECODED VIEW and returns the verdict only; data is never modified. That split is the whole point. A UTF-16 SRT is half NUL bytes, so a byte-level text test calls it binary, and this gate refused four of the eight members of a real SubSource pack for being valid subtitles in an encoding it did not look at. Deciding on the decoded view fixes that without converting anything, because whether a conversion is PERSISTED belongs to post_processing.normalize_utf8 and to nothing else.

func VariantFromFlags

func VariantFromFlags(tags Tags) subflux.Variant

VariantFromFlags derives the variant from the HI/forced tags on a picked subtitle. HI wins over forced when both happen to be set.

It takes Tags rather than two adjacent bools: that is the same pair Tags was introduced to make untransposable, and leaving this one positional meant both shapes appeared in a single call chain (this function and Path were called 70 lines apart from the same two fields). The Lang field is unused here — a variant does not depend on it — which is harmless and keeps one type describing the whole segment.

Types

type Tags

type Tags struct {
	// Lang is the subtitle's language code, the segment's first element.
	Lang string
	// HearingImpaired tags the file as a hearing-impaired (SDH) subtitle.
	HearingImpaired bool
	// Forced tags the file as a forced (foreign-parts-only) subtitle.
	Forced bool
}

Tags are the variant tags a subtitle filename carries next to its language code (movie.fr.hi.srt, movie.fr.forced.srt).

The fields are named rather than positional because a filename can carry either variant, both or neither, so no shape or count distinguishes them: a transposed pair writes a forced subtitle to the hearing-impaired target's filename, which the next scan reads back as coverage for a language variant nothing downloaded, and the operator's own file listing is the only place the swap is visible.

Lang lives here rather than beside videoPath on the constructors, for the same reason. It renders as part of this segment — suffix() always took it — and as a second positional string it was transposable with videoPath, which is the hazard this type exists to remove. Only the video path is positional.

Jump to

Keyboard shortcuts

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