plex

package
v0.1.10 Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package plex provides media organization for Plex libraries. It handles media type detection, file renaming, and library placement.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrSourceNotFound    = errors.New("source file or directory not found")
	ErrDestinationExists = errors.New("destination already exists")
	ErrPermissionDenied  = errors.New("permission denied")
	ErrPathEscape        = errors.New("path escapes allowed directory")
)

Move operation errors.

View Source
var ErrDetectionFailed = errors.New("unable to detect media type")

ErrDetectionFailed indicates media type could not be determined.

View Source
var ErrInvalidInput = errors.New("invalid input for naming")

ErrInvalidInput indicates the input cannot be processed for naming.

Functions

func CleanupSourceDir added in v0.1.8

func CleanupSourceDir(sourceDir, downloadsPath, incompletePath string) error

CleanupSourceDir removes all files and the directory itself. Call this after user confirms cleanup of remaining files. Includes safeguards to prevent accidentally deleting important directories. downloadsPath is the configured downloads directory (e.g., ~/Downloads/torrents) incompletePath is qBittorrent's temp/incomplete path (may be empty if not configured) Both paths will be protected along with common subdirectories.

func FindAllVideos added in v0.1.9

func FindAllVideos(path string) ([]string, error)

FindAllVideos finds all video files in a directory (up to 2 levels deep), ignoring samples. Returns files sorted alphabetically. Used for TV season packs with multiple episodes.

func FindMainVideo added in v0.1.6

func FindMainVideo(path string) (string, error)

FindMainVideo finds the largest video file in a directory (top level only), ignoring samples.

func FindSubtitles added in v0.1.6

func FindSubtitles(path string) []string

FindSubtitles finds all .srt subtitle files in a directory (up to 2 levels deep).

func FindSubtitlesForVideo added in v0.1.9

func FindSubtitlesForVideo(baseDir, videoPath string) []string

FindSubtitlesForVideo finds .srt subtitle files matching a specific video file. Looks for subtitles that start with the same base name (without extension). Like script: find "$BASE_DIR" -maxdepth 2 -type f -iname "${NO_EXT}*.srt"

func FormatMoviePath

func FormatMoviePath(m MovieNaming) (string, error)

FormatMoviePath generates a Plex-compatible filename for a movie. Returns: "Title (Year).ext" (directly in Movies folder, like the bash script)

func FormatTVFilename added in v0.1.6

func FormatTVFilename(t TVNaming) string

FormatTVFilename generates a Plex-compatible filename for a TV episode. Returns: "Show Title - S##E## - Episode Title.ext" or "Show Title - S##E##.ext"

func FormatTVPath

func FormatTVPath(t TVNaming) (string, error)

FormatTVPath generates a Plex-compatible directory path for a TV episode. Returns: "Show Title/Season ##" - caller appends original filename.

func SanitizeFilename

func SanitizeFilename(name string) string

SanitizeFilename removes or replaces characters that are invalid for filesystem paths.

func ValidatePath

func ValidatePath(path, allowedBase string) error

ValidatePath checks that a path is safe and within allowed boundaries.

Types

type DetectionResult

type DetectionResult struct {
	Type       MediaType
	Title      string  // Parsed title
	Year       int     // Release year (movies) or 0 if unknown
	Season     int     // Season number (TV) or 0
	Episode    int     // Episode number (TV) or 0
	Confidence float64 // 0.0-1.0 confidence score
}

DetectionResult holds the outcome of media type detection.

func Detect

func Detect(filename string) (DetectionResult, error)

Detect analyzes a filename to determine media type. Checks TV patterns first (more specific), then falls back to movie year detection.

func DetectFromPath

func DetectFromPath(path string) (DetectionResult, error)

DetectFromPath analyzes a full path including parent directories. Uses the innermost directory or filename for detection.

type MediaType

type MediaType int

MediaType represents the detected type of media content.

const (
	MediaTypeUnknown MediaType = iota
	MediaTypeMovie
	MediaTypeTV
)

func (MediaType) String

func (m MediaType) String() string

String returns a human-readable media type name.

type MoveConfig

type MoveConfig struct {
	MovieLibraryPath string // Base path for movie library
	TVLibraryPath    string // Base path for TV library
	UseSudo          bool   // Use sudo for rsync operations
}

MoveConfig holds configuration for media file operations.

type MoveProgress added in v0.1.6

type MoveProgress struct {
	BytesCopied int64
	TotalBytes  int64
	Percentage  float64 // Overall progress (0.0-1.0)
	CurrentFile string
	Rate        string // Transfer rate (e.g., "10.5MB/s")
	ETA         string // Estimated time remaining (e.g., "0:01:23")
	// TV multi-episode fields
	EpisodeIndex    int     // Current episode index (1-based), 0 for movies
	EpisodeTotal    int     // Total episodes, 0 for movies
	EpisodeProgress float64 // Current episode progress (0.0-1.0)
}

MoveProgress reports progress during a move operation.

type MoveResult

type MoveResult struct {
	SourcePath      string // First/main source file
	DestinationPath string // Destination directory (TV) or file (movie)
	MediaType       MediaType
	BytesMoved      int64
	FilesMoved      int // Number of video files moved (1 for movies, N for TV)
	Success         bool
	Error           error
	RemainingFiles  []string // Files left in source directory (for cleanup prompt)
	SourceDir       string   // Source directory path (for cleanup)
}

MoveResult contains the outcome of a move operation.

type Mover

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

Mover handles moving completed downloads to Plex libraries.

func NewMover

func NewMover(config MoveConfig) *Mover

NewMover creates a new Mover with the given configuration.

func (*Mover) MoveAsMovie

func (m *Mover) MoveAsMovie(ctx context.Context, sourcePath string, naming MovieNaming) (*MoveResult, error)

MoveAsMovie moves a file to the movie library with specified naming.

func (*Mover) MoveAsTV

func (m *Mover) MoveAsTV(ctx context.Context, sourcePath string, naming TVNaming) (*MoveResult, error)

MoveAsTV moves a file to the TV library with specified naming.

func (*Mover) MoveToLibrary

func (m *Mover) MoveToLibrary(ctx context.Context, sourcePath string) (*MoveResult, error)

MoveToLibrary moves a completed download without progress reporting.

func (*Mover) MoveToLibraryWithProgress added in v0.1.6

func (m *Mover) MoveToLibraryWithProgress(
	ctx context.Context,
	sourcePath string,
	detection DetectionResult,
	cleanup bool,
	progress chan<- MoveProgress,
) (*MoveResult, error)

MoveToLibraryWithProgress moves a completed download to the appropriate Plex library. For movies: moves the largest video file to Movies library. For TV: moves ALL video files, each to their proper season folder based on S##E## in filename. It reports progress via the provided channel.

type MovieNaming

type MovieNaming struct {
	Title      string
	Year       int
	Resolution string // e.g., "1080p", "4K"
	Extension  string // e.g., ".mkv", ".mp4"
}

MovieNaming contains parsed movie information for file naming.

type TVNaming

type TVNaming struct {
	ShowTitle    string
	Season       int
	Episode      int
	EpisodeTitle string // Optional episode title
	Resolution   string
	Extension    string
}

TVNaming contains parsed TV show information for file naming.

Jump to

Keyboard shortcuts

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