direct_play_nice 1.1.0-beta.2

CLI program that converts video files to direct-play-compatible formats.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
//! Sonarr/Radarr integration workflow for event handling, path planning, and atomic media replacement.

use anyhow::{bail, Context, Result};
use log::{info, warn};
use std::env;
use std::ffi::CString;
use std::path::{Path, PathBuf};

mod api;
mod cache;
mod env_helpers;
mod language;
mod media_paths;
mod path_policy;

pub use api::{run_language_audit, ApiSettings, AuditOptions, RedownloadOptions};
pub use language::{parse_language_list, LanguageRequirements, UntaggedRetagOptions};

#[cfg(test)]
mod servarr_tests;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
/// Supported Servarr integration sources.
pub enum IntegrationKind {
    Sonarr,
    Radarr,
}

impl IntegrationKind {
    fn label(self) -> &'static str {
        match self {
            IntegrationKind::Sonarr => "Sonarr",
            IntegrationKind::Radarr => "Radarr",
        }
    }

    fn episode_path_var(self) -> &'static str {
        match self {
            IntegrationKind::Sonarr => "sonarr_episodefile_path",
            IntegrationKind::Radarr => "radarr_moviefile_path",
        }
    }

    fn title_var(self) -> &'static str {
        match self {
            IntegrationKind::Sonarr => "sonarr_series_title",
            IntegrationKind::Radarr => "radarr_movie_title",
        }
    }

    fn is_upgrade_var(self) -> &'static str {
        match self {
            IntegrationKind::Sonarr => "sonarr_isupgrade",
            IntegrationKind::Radarr => "radarr_isupgrade",
        }
    }
}

#[derive(Debug, Clone)]
/// Fully-resolved atomic replacement plan for one media file.
///
/// Replacement is always staged through `temp_output_path` and `backup_path` so
/// failures can roll back to the original source file.
pub struct ReplacePlan {
    pub kind: IntegrationKind,
    pub event_type: String,
    pub display_name: Option<String>,
    pub is_upgrade: Option<bool>,
    pub input_path: PathBuf,
    pub final_output_path: PathBuf,
    pub temp_output_path: PathBuf,
    pub backup_path: PathBuf,
    pub input_cstring: CString,
    pub temp_output_cstring: CString,
}

impl ReplacePlan {
    pub fn assign_to_args(
        &self,
        input_slot: &mut Option<CString>,
        output_slot: &mut Option<CString>,
    ) {
        if input_slot.is_none() {
            *input_slot = Some(self.input_cstring.clone());
        }
        if output_slot.is_none() {
            *output_slot = Some(self.temp_output_cstring.clone());
        }
    }

    pub fn log_summary(&self) {
        let title = self.display_name.as_deref().unwrap_or_else(|| {
            self.input_path
                .file_name()
                .and_then(|n| n.to_str())
                .unwrap_or("<unknown>")
        });
        let final_display = self
            .final_output_path
            .file_name()
            .and_then(|n| n.to_str())
            .map(|s| s.to_owned())
            .unwrap_or_else(|| self.final_output_path.to_string_lossy().into_owned());

        if self.final_output_path == self.input_path {
            info!(
                "{} {} event: converting '{}' in place",
                self.kind.label(),
                self.event_type,
                title
            );
        } else {
            info!(
                "{} {} event: converting '{}' -> '{}'",
                self.kind.label(),
                self.event_type,
                title,
                final_display
            );
        }
        if let Some(upgrade) = self.is_upgrade {
            info!("{} reported upgrade flag: {}", self.kind.label(), upgrade);
        }
    }

    pub fn finalize_success(self) -> Result<PathBuf> {
        use std::fs;

        let had_original = self.input_path.exists();

        // Move the original aside first, so final promotion can be an atomic rename.
        if had_original {
            fs::rename(&self.input_path, &self.backup_path).with_context(|| {
                format!(
                    "{} integration failed to move original file '{}' to backup '{}'",
                    self.kind.label(),
                    self.input_path.display(),
                    self.backup_path.display()
                )
            })?;
        }

        // Promote the converted temp file into place; restore backup on failure.
        if let Err(promote_err) = fs::rename(&self.temp_output_path, &self.final_output_path) {
            if had_original && self.backup_path.exists() && !self.input_path.exists() {
                if let Err(restore_err) = fs::rename(&self.backup_path, &self.input_path) {
                    return Err(promote_err).with_context(|| {
                        format!(
                            "{} integration could not promote '{}' to '{}' and failed to restore backup '{}': {}",
                            self.kind.label(),
                            self.temp_output_path.display(),
                            self.final_output_path.display(),
                            self.input_path.display(),
                            restore_err
                        )
                    });
                }
            }
            return Err(promote_err).with_context(|| {
                format!(
                    "{} integration could not promote '{}' to '{}'",
                    self.kind.label(),
                    self.temp_output_path.display(),
                    self.final_output_path.display()
                )
            });
        }

        match fs::remove_file(&self.backup_path) {
            Ok(_) => {}
            Err(err) if err.kind() == std::io::ErrorKind::NotFound => {}
            Err(err) => {
                warn!(
                    "{} integration left a backup file at '{}': {}",
                    self.kind.label(),
                    self.backup_path.display(),
                    err
                );
            }
        }

        Ok(self.final_output_path)
    }

    pub fn abort_on_failure(&self) -> Result<()> {
        use std::fs;

        if self.temp_output_path.exists() {
            fs::remove_file(&self.temp_output_path).with_context(|| {
                format!(
                    "{} integration failed to remove temporary file '{}'",
                    self.kind.label(),
                    self.temp_output_path.display()
                )
            })?;
        }

        if self.backup_path.exists() && !self.input_path.exists() {
            fs::rename(&self.backup_path, &self.input_path).with_context(|| {
                format!(
                    "{} integration could not restore backup '{}' to '{}'",
                    self.kind.label(),
                    self.backup_path.display(),
                    self.input_path.display()
                )
            })?;
        }

        Ok(())
    }
}

#[derive(Debug, Clone)]
/// Borrowed view of CLI path-related arguments used by Servarr planning.
pub struct ArgsView<'a> {
    pub has_input: bool,
    pub has_output: bool,
    pub desired_extension: &'a str,
    pub desired_suffix: &'a str,
    pub language_requirements: LanguageRequirements,
    pub untagged_retag: language::UntaggedRetagOptions,
    pub api_settings: ApiSettings,
    pub redownload_options: RedownloadOptions,
}

#[derive(Debug, Clone)]
/// Preparation result for integration-triggered runs.
pub enum IntegrationPreparation {
    None,
    Skip { reason: String },
    Replace(ReplacePlan),
    Batch(Vec<ReplacePlan>),
}

pub fn prepare_from_env(view: ArgsView<'_>) -> Result<IntegrationPreparation> {
    if let Some(event) = env::var("sonarr_eventtype").ok().filter(|v| !v.is_empty()) {
        return handle_event(IntegrationKind::Sonarr, event, view);
    }

    if let Some(event) = env::var("radarr_eventtype").ok().filter(|v| !v.is_empty()) {
        return handle_event(IntegrationKind::Radarr, event, view);
    }

    Ok(IntegrationPreparation::None)
}

fn handle_event(
    kind: IntegrationKind,
    event_type: String,
    view: ArgsView<'_>,
) -> Result<IntegrationPreparation> {
    match event_type.as_str() {
        "Download" => prepare_download(kind, event_type, view),
        "Test" => Ok(IntegrationPreparation::Skip {
            reason: format!(
                "{} test event detected; exiting without conversion.",
                kind.label()
            ),
        }),
        other => Ok(IntegrationPreparation::Skip {
            reason: format!(
                "{} event '{}' does not trigger conversion; exiting cleanly.",
                kind.label(),
                other
            ),
        }),
    }
}

fn prepare_download(
    kind: IntegrationKind,
    event_type: String,
    view: ArgsView<'_>,
) -> Result<IntegrationPreparation> {
    crate::logging::log_relevant_env(kind);

    if view.has_input && view.has_output {
        // User supplied explicit paths; allow normal CLI behaviour.
        info!(
            "{} Download event detected but CLI input/output were provided; using CLI paths.",
            kind.label()
        );
        return Ok(IntegrationPreparation::None);
    }

    if view.has_input ^ view.has_output {
        bail!(
            "Detected {} Download event but only one of <INPUT_FILE>/<OUTPUT_FILE> was provided. Provide both or rely on integration defaults.",
            kind.label()
        );
    }

    if view.has_input {
        return Ok(IntegrationPreparation::None);
    }

    let input_paths = resolve_media_paths(kind).with_context(|| {
        format!(
            "{} integration requires ${} to be set for Download events.",
            kind.label(),
            kind.episode_path_var()
        )
    })?;

    if input_paths.is_empty() {
        bail!(
            "{} integration did not receive any media file paths.",
            kind.label()
        );
    }

    let effective_suffix = if view.desired_suffix.trim().is_empty() {
        match kind {
            IntegrationKind::Sonarr => ".fixed",
            IntegrationKind::Radarr => "",
        }
    } else {
        view.desired_suffix
    };

    let display_name = get_env_ignore_case(kind.title_var());
    let is_upgrade = get_env_ignore_case(kind.is_upgrade_var()).and_then(parse_boolish);

    if view.language_requirements.enabled
        && view.language_requirements.audio.is_empty()
        && view.language_requirements.subtitles.is_empty()
    {
        warn!(
            "{} language check is enabled but no required audio/subtitle languages were configured; continuing conversion.",
            kind.label()
        );
    }

    let mut language_mismatches = Vec::new();
    if view.language_requirements.is_effective() {
        for input_path in &input_paths {
            let mut report = language::check_file(input_path, &view.language_requirements)
                .with_context(|| format!("checking languages for '{}'", input_path.display()))?;
            if !report.satisfied() && view.untagged_retag.enabled() {
                match language::retag_unknown_streams(
                    input_path,
                    &view.language_requirements,
                    &view.untagged_retag,
                ) {
                    Ok(retag) if retag.changed() => {
                        info!(
                            "{} {}retagged unknown stream language metadata for '{}': audio={}, subtitles={}",
                            kind.label(),
                            if retag.dry_run { "would have " } else { "" },
                            input_path.display(),
                            retag.audio_streams,
                            retag.subtitle_streams
                        );
                        if !retag.dry_run {
                            report = language::check_file(input_path, &view.language_requirements)
                                .with_context(|| {
                                    format!("re-checking languages for '{}'", input_path.display())
                                })?;
                        }
                    }
                    Ok(_) => {}
                    Err(err) => warn!(
                        "{} failed to retag unknown stream language metadata for '{}': {}",
                        kind.label(),
                        input_path.display(),
                        err
                    ),
                }
            }
            if let Err(err) =
                cache::record_assessment(kind, input_path, &view.language_requirements, &report)
            {
                warn!(
                    "Failed to update DPN language assessment cache for '{}': {}",
                    input_path.display(),
                    err
                );
            }
            if !report.satisfied() {
                language_mismatches.push((input_path.clone(), report));
            }
        }
    }

    if !language_mismatches.is_empty() {
        for (path, report) in &language_mismatches {
            warn!(
                "{} language requirements not met for '{}': missing {} (present audio [{}], subtitles [{}]).",
                kind.label(),
                path.display(),
                report.describe_missing(),
                report.present_audio.join(", "),
                report.present_subtitles.join(", ")
            );
        }
        match api::trigger_verified_redownload(
            kind,
            &view.api_settings,
            &view.language_requirements,
            view.redownload_options,
        )? {
            api::RedownloadOutcome::Grabbed { title } => {
                return Ok(IntegrationPreparation::Skip {
                    reason: format!(
                        "{} language requirements were not met; blacklisted the current download and grabbed verified replacement '{}'.",
                        kind.label(), title
                    ),
                });
            }
            api::RedownloadOutcome::NoVerifiedRelease { reason } => {
                return Ok(IntegrationPreparation::Skip {
                    reason: format!(
                        "{} language requirements were not met, but no replacement was grabbed: {}. Leaving current file untouched.",
                        kind.label(), reason
                    ),
                });
            }
            api::RedownloadOutcome::DryRun { summary } => {
                return Ok(IntegrationPreparation::Skip {
                    reason: format!(
                        "{} language requirements were not met; dry-run only: {}. Leaving current file untouched.",
                        kind.label(), summary
                    ),
                });
            }
        }
    }

    let mut plans = Vec::new();

    for input_path in input_paths {
        if !input_path.exists() {
            bail!(
                "{} integration could not find media file at '{}'.",
                kind.label(),
                input_path.display()
            );
        }

        let final_output_path =
            resolve_output_path(&input_path, view.desired_extension, effective_suffix)?;
        let temp_output_path = append_suffix(&final_output_path, ".direct-play-nice.tmp");
        let backup_path = append_suffix(&input_path, ".direct-play-nice.bak");

        let input_cstring = path_to_cstring(&input_path)?;
        let temp_output_cstring = path_to_cstring(&temp_output_path)?;

        let plan = ReplacePlan {
            kind,
            event_type: event_type.clone(),
            display_name: display_name.clone(),
            is_upgrade,
            input_path,
            final_output_path,
            temp_output_path,
            backup_path,
            input_cstring,
            temp_output_cstring,
        };
        plan.log_summary();
        plans.push(plan);
    }

    if plans.len() == 1 {
        Ok(IntegrationPreparation::Replace(plans.remove(0)))
    } else {
        Ok(IntegrationPreparation::Batch(plans))
    }
}

fn resolve_output_path(
    input_path: &Path,
    desired_ext: &str,
    desired_suffix: &str,
) -> Result<PathBuf> {
    path_policy::resolve_output_path(input_path, desired_ext, desired_suffix)
}

fn append_suffix(path: &Path, suffix: &str) -> PathBuf {
    path_policy::append_suffix(path, suffix)
}

fn resolve_media_paths(kind: IntegrationKind) -> Result<Vec<PathBuf>> {
    media_paths::resolve_media_paths(kind)
}

fn path_to_cstring(path: &Path) -> Result<CString> {
    path_policy::path_to_cstring(path)
}

fn parse_boolish(value: String) -> Option<bool> {
    env_helpers::parse_boolish(value)
}

fn get_env_ignore_case(key: &str) -> Option<String> {
    env_helpers::get_env_ignore_case(key)
}