putioarr 0.6.10

put.io to sonarr/radarr/whisparr proxy
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
use crate::{
    services::{
        arr::ArrApp,
        putio::{self, PutIOTransfer},
    },
    state::OrphanFile,
    AppData,
};
use actix_web::web::Data;
use anyhow::{Context, Result};
use async_channel::Sender;
use async_recursion::async_recursion;
use colored::*;
use log::{debug, error, info, warn};
use serde::{Deserialize, Serialize};
use std::{
    collections::HashSet,
    fmt::Display,
    path::Path,
    time::{Duration, Instant},
};
use tokio::time::sleep;

#[derive(Clone)]
pub struct Transfer {
    pub name: String,
    pub file_id: Option<i64>,
    pub hash: Option<String>,
    pub transfer_id: u64,
    pub targets: Option<Vec<DownloadTarget>>,
    pub app_data: Data<AppData>,
    /// True if this came from a watch-folder scan (an orphaned file with no
    /// put.io transfer record) rather than `transfers/list`. Such transfers are
    /// cleaned up by deleting the file directly, since there is no transfer to
    /// remove or seeding to wait on (issue #34).
    pub is_orphan: bool,
}

impl Transfer {
    pub async fn is_imported(&self) -> bool {
        let targets = self.targets.as_ref().unwrap().clone();
        let apps: Vec<ArrApp> = self
            .app_data
            .config
            .all_arrs()
            .into_iter()
            .map(|(name, kind, c)| ArrApp::new(name, kind, c))
            .collect();

        let targets = targets
            .into_iter()
            .filter(|t| t.target_type == TargetType::File)
            .collect::<Vec<DownloadTarget>>();

        // With no files to check, `all()` below is vacuously true, and the
        // transfer's local folder and put.io copy get deleted as "imported".
        // That happens when a transfer has only files putioarr doesn't download,
        // such as a RAR release (put.io type ARCHIVE) with download_archives
        // off. Nothing was downloaded, so nothing can have been imported.
        if targets.is_empty() {
            return false;
        }

        // The transfer's own folder, if it is a folder rather than a single file.
        let transfer_dir = self
            .targets
            .as_ref()
            .unwrap()
            .iter()
            .find(|t| t.top_level && t.target_type == TargetType::Directory)
            .map(|t| t.to.clone());

        let mut results = Vec::<bool>::new();
        // Archive parts in the same transfer folder share one history lookup.
        let mut dir_import_cache = std::collections::HashMap::<String, bool>::new();
        for target in targets {
            // The *arr never imports archive parts (put.io type ARCHIVE, only
            // downloaded with download_archives), only the file an extractor
            // such as Unpackerr unpacks from them, so they never show up in
            // its history under their own path. An archive counts as imported
            // once it's gone from disk (Unpackerr's delete_orig), or once the
            // *arr imported something from this transfer's folder.
            if target.media_type == Some(MediaType::Archive) {
                if !Path::new(&target.to).exists() {
                    info!("{}: archive gone (extracted)", target);
                    results.push(true);
                    continue;
                }
                // A transfer that is a single archive file has no folder of its
                // own. Its parent is the category or download directory, which
                // nearly every import's path contains, so only its removal can
                // tell us it was extracted.
                let Some(dir) = archive_import_dir(&target, transfer_dir.as_deref()) else {
                    results.push(false);
                    continue;
                };
                if let Some(hit) = dir_import_cache.get(&dir) {
                    results.push(*hit);
                    continue;
                }
                let mut dir_imported = false;
                for app in &apps {
                    match app.check_imported_dir(&dir).await {
                        Ok(true) => {
                            dir_imported = true;
                            break;
                        }
                        Ok(false) => {}
                        Err(e) => {
                            if self.app_data.state.should_log_arr_error(&app.name).await {
                                error!(
                                    "Error retrieving history from {} (suppressing repeats for {:?}): {}",
                                    app,
                                    crate::state::StateManager::ARR_ERROR_LOG_INTERVAL,
                                    e
                                );
                            }
                        }
                    }
                }
                if dir_imported {
                    info!("{}: archive extracted and imported from {}", target, dir);
                }
                dir_import_cache.insert(dir, dir_imported);
                results.push(dir_imported);
                continue;
            }
            let mut service_results = vec![];
            for app in &apps {
                // Only ask an *arr about files matching its media type.
                if let Some(mt) = &target.media_type {
                    if *mt != app.kind.media_type() {
                        continue;
                    }
                }
                let service_result = match app.check_imported(&target.to).await {
                    Ok(r) => r,
                    Err(e) => {
                        // A misconfigured/unreachable *arr fails for every
                        // transfer on every poll; throttle the log so it doesn't
                        // fill the disk over time (issue #21). Key on the app's
                        // existing name (no allocation on the suppressed path).
                        if self.app_data.state.should_log_arr_error(&app.name).await {
                            error!(
                                "Error retrieving history from {} (suppressing repeats for {:?}): {}",
                                app,
                                crate::state::StateManager::ARR_ERROR_LOG_INTERVAL,
                                e
                            );
                        }
                        false
                    }
                };
                if service_result {
                    info!(
                        "{}: found imported by {}",
                        target,
                        app.to_string().bright_blue()
                    );
                }
                service_results.push(service_result)
            }
            // If no service was eligible for this target, treat it as imported
            // (otherwise an audio file with no Lidarr would block forever).
            let any_imported = service_results.is_empty() || service_results.into_iter().any(|x| x);
            results.push(any_imported);
        }
        // Check if all targets have been imported
        results.into_iter().all(|x| x)
    }

    pub async fn get_download_targets(&self) -> Result<Vec<DownloadTarget>> {
        self.generate_targets(None).await
    }

    /// Like [`get_download_targets`] but with an explicit base directory instead
    /// of looking one up from stored transfer state. Used for orphaned files,
    /// which have no persisted state to route them (issue #34).
    pub async fn get_download_targets_in(&self, base_path: &str) -> Result<Vec<DownloadTarget>> {
        self.generate_targets(Some(base_path.to_string())).await
    }

    async fn generate_targets(&self, base_path: Option<String>) -> Result<Vec<DownloadTarget>> {
        info!("{}: generating targets", self);
        let file_id = self.file_id.context("transfer has no file_id")?;
        let default = "0000".to_string();
        let hash = self.hash.as_ref().unwrap_or(&default).as_str();
        recurse_download_targets(&self.app_data, file_id, hash, base_path, true).await
    }

    pub fn get_top_level(&self) -> DownloadTarget {
        self.targets
            .clone()
            .unwrap()
            .into_iter()
            .find(|t| t.top_level)
            .unwrap()
    }

    pub fn from(app_data: Data<AppData>, transfer: &PutIOTransfer) -> Self {
        let default = &"Unknown".to_string();
        let name = transfer.name.as_ref().unwrap_or(default);
        Self {
            transfer_id: transfer.id,
            name: name.clone(),
            file_id: transfer.file_id,
            targets: None,
            hash: transfer.hash.clone(),
            app_data,
            is_orphan: false,
        }
    }

    /// Builds a synthetic transfer for an orphaned watch-folder file (one with
    /// no put.io transfer record). The file_id doubles as the transfer id and is
    /// formatted into a deterministic synthetic hash so the *arr can track it.
    pub fn from_orphan(app_data: Data<AppData>, file_id: i64, name: String) -> Self {
        // put.io file ids are non-negative; the watch-folder scan filters out
        // any that aren't before constructing an orphan, so the `as u64` casts
        // below are exact. Assert it to catch future misuse.
        debug_assert!(file_id >= 0, "orphan file_id must be non-negative");
        Self {
            transfer_id: file_id as u64,
            name,
            file_id: Some(file_id),
            targets: None,
            hash: Some(format!("{:040x}", file_id as u64)),
            app_data,
            is_orphan: true,
        }
    }
}

impl Display for Transfer {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let default = "0000".to_string();
        let hash = &self.hash.as_ref().unwrap_or(&default)[..4];
        let s = format!("[{}: {}]", hash, self.name).cyan();
        write!(f, "{s}")
    }
}

#[allow(clippy::double_must_use)] // the #[must_use] comes from #[async_recursion]
#[async_recursion]
async fn recurse_download_targets(
    app_data: &Data<AppData>,
    file_id: i64,
    hash: &str,
    override_base_path: Option<String>,
    top_level: bool,
) -> Result<Vec<DownloadTarget>> {
    // Check if we have stored state for this transfer to get the correct download directory
    let base_path = if let Some(path) = override_base_path {
        path
    } else {
        // Try to get the download directory from state, fallback to default
        app_data.state.get_download_dir_for_transfer(hash, &app_data.config.download_directory).await
    };
    let mut targets = Vec::<DownloadTarget>::new();
    let response = putio::list_files(&app_data.config.putio.api_key, file_id).await?;
    let to = Path::new(&base_path)
        .join(&response.parent.name)
        .to_string_lossy()
        .to_string();

    match response.parent.file_type.as_str() {
        "FOLDER" => {
            if !app_data
                .config
                .skip_directories
                .contains(&response.parent.name.to_lowercase())
            {
                let new_base_path = to.clone();

                targets.push(DownloadTarget {
                    from: None,
                    target_type: TargetType::Directory,
                    to,
                    top_level,
                    transfer_hash: hash.to_string(),
                    media_type: None,
                });

                for file in response.files {
                    targets.append(
                        &mut recurse_download_targets(
                            app_data,
                            file.id,
                            hash,
                            Some(new_base_path.clone()),
                            false,
                        )
                        .await?,
                    );
                }
            }
        }
        "VIDEO" | "AUDIO" => {
            // Get download URL for file
            let url = putio::url(&app_data.config.putio.api_key, response.parent.id).await?;
            targets.push(DownloadTarget {
                from: Some(url),
                target_type: TargetType::File,
                to,
                top_level,
                transfer_hash: hash.to_string(),
                media_type: MediaType::from_putio(response.parent.file_type.as_str()),
            });
        }
        // Archives (.rar parts, .zip, .7z) only when download_archives is on.
        // An extractor such as Unpackerr unpacks them for the *arr; see
        // is_imported for how putioarr then decides they're done.
        "ARCHIVE" if app_data.config.download_archives => {
            let url = putio::url(&app_data.config.putio.api_key, response.parent.id).await?;
            targets.push(DownloadTarget {
                from: Some(url),
                target_type: TargetType::File,
                to,
                top_level,
                transfer_hash: hash.to_string(),
                media_type: MediaType::from_putio(response.parent.file_type.as_str()),
            });
        }
        other => {
            debug!(
                "{}: skipping file type {}",
                response.parent.name, other
            );
        }
    }

    Ok(targets)
}

/// The folder name to look for in the *arr's import history for an archive
/// target: the name of the transfer's own top-level folder, when the archive is
/// inside it. `None` when the transfer is a single archive file. Only the name
/// is compared, so an archive deeper down (e.g. in `CD1/`) still matches the
/// release folder rather than a generic subfolder name.
fn archive_import_dir(target: &DownloadTarget, transfer_dir: Option<&str>) -> Option<String> {
    let dir = Path::new(transfer_dir?);
    if !Path::new(&target.to).starts_with(dir) {
        return None;
    }
    dir.file_name().map(|n| n.to_string_lossy().into_owned())
}

#[derive(Clone)]
pub enum TransferMessage {
    QueuedForDownload(Transfer),
    Downloaded(Transfer),
    Imported(Transfer),
}

#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq, Copy)]
pub enum MediaType {
    Audio,
    Video,
    /// Multi-volume RAR parts (put.io file_type ARCHIVE).
    /// Tracked separately from Video so is_imported() can use a different
    /// completion check (local file presence, since the *arr never reports
    /// a downloadFolderImported history event for a .rNN/.rar path -- only
    /// for the video file Unpackerr extracts from it).
    Archive,
}

impl MediaType {
    pub fn from_putio(file_type: &str) -> Option<Self> {
        match file_type {
            "AUDIO" => Some(Self::Audio),
            "VIDEO" => Some(Self::Video),
            "ARCHIVE" => Some(Self::Archive),
            _ => None,
        }
    }
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct DownloadTarget {
    pub from: Option<String>,
    pub to: String,
    pub target_type: TargetType,
    pub top_level: bool,
    pub transfer_hash: String,
    pub media_type: Option<MediaType>,
}

impl Display for DownloadTarget {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let hash = &self.transfer_hash.as_str()[..4];
        let s = format!("[{}: {}]", hash, self.to).magenta();
        write!(f, "{s}")
    }
}

#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq)]
pub enum TargetType {
    Directory,
    File,
}

// Check for new putio transfers and if they qualify, send them on for download
/// Returns true if this transfer is managed by putioarr, i.e. it has stored
/// category/download-dir state because *it* uploaded it on behalf of an *arr.
/// Transfers added directly on put.io (e.g. a manually-maintained "Watch List")
/// have no state. Unless the user opts in via `download_unmanaged`, those are
/// ignored so putioarr does not try to download the entire put.io account and
/// hang on seeding transfers (see upstream issue #9).
async fn is_managed(app_data: &Data<AppData>, putio_transfer: &PutIOTransfer) -> bool {
    if app_data.config.download_unmanaged {
        return true;
    }
    match &putio_transfer.hash {
        Some(hash) => app_data.state.get_transfer(hash).await.is_some(),
        None => false,
    }
}

pub async fn produce_transfers(app_data: Data<AppData>, tx: Sender<TransferMessage>) -> Result<()> {
    let putio_check_interval = Duration::from_secs(app_data.config.polling_interval);
    let mut seen = Vec::<u64>::new();
    // Watch-folder scans hit put.io once per folder, so run them on their own
    // configurable interval rather than every poll to avoid extra API traffic
    // (issue #34).
    // Clamp to >= 1s so a misconfigured 0 doesn't become a zero Duration that
    // scans on every poll. The effective floor is the polling interval anyway,
    // since the scan only runs inside this loop.
    let orphan_scan_interval =
        Duration::from_secs(app_data.config.watch_folder_interval_secs.max(1));
    let mut last_orphan_scan: Option<Instant> = None;

    info!("Checking unfinished transfers");
    // We only need to check if something has been imported. Just by looking at the filesystem we
    // can't determine if a transfer has been imported and removed or hasn't been downloaded.
    // This avoids downloading a tranfer that has already been imported. In case there is a download,
    // but it wasn't (completely) imported, we will attempt a (partial) download. Files that have
    // been completed downloading will be skipped.
    for putio_transfer in &putio::list_transfers(&app_data.config.putio.api_key)
        .await?
        .transfers
    {
        let name = putio_transfer.name.clone().unwrap_or("??".to_string());
        let mut transfer = Transfer::from(app_data.clone(), putio_transfer);
        if putio_transfer.is_downloadable() && is_managed(&app_data, putio_transfer).await {
            info!("Getting download target for {name}");
            let targets = transfer.get_download_targets().await;
            if targets.is_err() {
                // For example, if the user trashed the file in Putio
                warn!("Could not get target for {name}");
                continue;
            }
            transfer.targets = Some(targets?);
            if transfer.is_imported().await {
                info!("{}: already imported", transfer);
                seen.push(transfer.transfer_id);
                tx.send(TransferMessage::Imported(transfer)).await?;
            } else {
                info!("{}: not imported yet", transfer);
            }
        }
    }
    info!("Done checking for unfinished transfers. Starting to monitor transfers.");

    // Set the start time
    let mut start = std::time::Instant::now();

    loop {
        if let Ok(list_transfer_response) =
            putio::list_transfers(&app_data.config.putio.api_key).await
        {
            for putio_transfer in &list_transfer_response.transfers {
                if seen.contains(&putio_transfer.id) || !putio_transfer.is_downloadable() {
                    continue;
                }
                let transfer = Transfer::from(app_data.clone(), putio_transfer);

                // Skip transfers we don't manage (e.g. a manual Watch List) unless
                // `download_unmanaged` is set. This prevents putioarr from trying to
                // download the whole account and hanging on seeding transfers (#9).
                if !is_managed(&app_data, putio_transfer).await {
                    debug!(
                        "{}: not managed by putioarr (no stored category/download-dir), skipping",
                        transfer
                    );
                    seen.push(putio_transfer.id);
                    continue;
                }

                info!("{}: ready for download", transfer);
                tx.send(TransferMessage::QueuedForDownload(transfer))
                    .await?;
                seen.push(putio_transfer.id);
            }

            // Remove any transfers from seen that are not in the active transfers
            let active_ids: Vec<u64> = list_transfer_response
                .transfers
                .iter()
                .map(|t| t.id)
                .collect();
            seen.retain(|t| active_ids.contains(t));

            // Pull orphaned files from the configured watch folders (completed
            // files whose transfer record no longer exists — see issue #34),
            // throttled so it doesn't list every folder on every poll.
            if last_orphan_scan.is_none_or(|t| t.elapsed() >= orphan_scan_interval) {
                scan_watch_folders(&app_data, &tx, &list_transfer_response.transfers).await;
                last_orphan_scan = Some(Instant::now());
            }

            // Log status when 60 seconds have passed since last time
            if start.elapsed().as_secs() >= 60 {
                info!(
                    "Active transfers: {}",
                    list_transfer_response.transfers.len()
                );
                for t in &list_transfer_response.transfers {
                    let transfer = Transfer::from(app_data.clone(), t);
                    // Report combined two-stage progress (cloud 0-50%, local pull
                    // 50-100%) so a stalled download is visible in the log (#5).
                    let pct = combined_progress_percent(&app_data, t).await;
                    info!("  {} ({}%)", transfer, pct);
                }

                start = std::time::Instant::now();
            }

            sleep(putio_check_interval).await;
        } else {
            warn!("List put.io transfers failed. Retrying..");
            continue;
        };
    }
}

/// Combined two-stage download progress (0-100) for a transfer, mirroring what
/// [`crate::http::handlers`] reports to the *arr: put.io's cloud download is the
/// first 50%, pulling the files to local disk is the second 50% (issue #5). Used
/// for the periodic status log so a stuck download is distinguishable from one
/// that's still making progress.
async fn combined_progress_percent(app_data: &Data<AppData>, t: &PutIOTransfer) -> u64 {
    if app_data.state.is_local_complete(t.id).await {
        return 100;
    }
    let size = t.size.unwrap_or(0).max(0) as u64;
    if size == 0 {
        return 0;
    }
    let putio_done = t.finished_at.is_some()
        || matches!(
            t.status.to_uppercase().as_str(),
            "SEEDING" | "COMPLETED" | "STOPPED"
        );
    if putio_done {
        let local = match &t.hash {
            Some(hash) => app_data
                .state
                .local_bytes_downloaded(hash)
                .await
                .unwrap_or(0)
                .min(size),
            None => 0,
        };
        // Cap at 99 until local_complete flips (handled by the early return
        // above): the counter can reach `size` a moment before the transfer is
        // marked complete, and logging 100% then would misreport a still-active
        // pull as finished.
        (50 + 50 * local / size).min(99)
    } else {
        let downloaded = (t.downloaded.unwrap_or(0).max(0) as u64).min(size);
        50 * downloaded / size
    }
}

/// Heuristic: does this name look like a TV episode (SxxExx, or "Season")?
/// Used to route an orphaned file to the Sonarr vs Radarr category folder.
fn looks_like_episode(name: &str) -> bool {
    let b = name.as_bytes();
    let n = b.len();
    let mut i = 0;
    while i + 3 < n {
        if (b[i] == b'S' || b[i] == b's') && b[i + 1].is_ascii_digit() {
            let mut j = i + 1;
            while j < n && b[j].is_ascii_digit() {
                j += 1;
            }
            if j + 1 < n && (b[j] == b'E' || b[j] == b'e') && b[j + 1].is_ascii_digit() {
                return true;
            }
        }
        i += 1;
    }
    // Case-insensitive "season" check without allocating a lowercased copy.
    name.as_bytes()
        .windows(6)
        .any(|w| w.eq_ignore_ascii_case(b"season"))
}

/// Scans the configured `watch_folders` for orphaned completed files — files
/// with no transfer record (e.g. removed by put.io's "clear completed") that
/// `transfers/list` will never surface — and queues them for download like
/// normal transfers so they don't get stranded on put.io (issue #34).
async fn scan_watch_folders(
    app_data: &Data<AppData>,
    tx: &Sender<TransferMessage>,
    active_transfers: &[PutIOTransfer],
) {
    if app_data.config.watch_folders.is_empty() {
        return;
    }
    let api_key = &app_data.config.putio.api_key;
    // Only files backed by a *managed* active transfer are handled the normal
    // way by the main loop. Unmanaged active transfers (e.g. a manually added
    // put.io Watch List entry, or one whose state was lost) are skipped there,
    // so their completed files must be picked up here as orphans — otherwise
    // both paths ignore them and they are never downloaded/imported.
    let mut managed_active_file_ids: HashSet<i64> = HashSet::new();
    for t in active_transfers {
        if let Some(file_id) = t.file_id {
            if is_managed(app_data, t).await {
                managed_active_file_ids.insert(file_id);
            }
        }
    }

    for folder_id in &app_data.config.watch_folders {
        let resp = match putio::list_files(api_key, *folder_id).await {
            Ok(r) => r,
            Err(e) => {
                warn!("watch folder {}: listing failed: {}", folder_id, e);
                continue;
            }
        };
        for file in &resp.files {
            // Only media (and folders that may contain media); skip stray
            // images/nfos and anything with an unusable (negative) id.
            if file.id < 0 || !matches!(file.file_type.as_str(), "FOLDER" | "VIDEO" | "AUDIO") {
                continue;
            }
            // Skip the result of a *managed* active transfer (handled the normal
            // way) and any orphan we're already pulling. Files from unmanaged
            // active transfers fall through the main loop, so they are handled
            // here. Using `has_orphan` as the "in progress" marker keeps tracking
            // bounded and, since a failed orphan is dropped from it, lets a later
            // poll retry it.
            if managed_active_file_ids.contains(&file.id) || app_data.state.has_orphan(file.id).await {
                continue;
            }

            // Route to the matching *arr category folder based on the name. The
            // base dir is passed explicitly so orphans need no persisted state.
            let category = if looks_like_episode(&file.name) {
                app_data
                    .config
                    .sonarr
                    .as_ref()
                    .and_then(|c| c.category.clone())
            } else {
                app_data
                    .config
                    .radarr
                    .as_ref()
                    .and_then(|c| c.category.clone())
            };
            let download_dir = match &category {
                Some(c) => format!("{}/{}", app_data.config.download_directory, c),
                None => app_data.config.download_directory.clone(),
            };

            let mut transfer = Transfer::from_orphan(app_data.clone(), file.id, file.name.clone());
            let targets = match transfer.get_download_targets_in(&download_dir).await {
                // Leave anything without a file to download (e.g. only RAR
                // parts) on put.io: it can never be imported.
                Ok(t) if t.iter().any(|t| t.target_type == TargetType::File) => t,
                Ok(_) => continue, // no downloadable (video/audio) files
                Err(e) => {
                    warn!("{}: orphan target generation failed: {}", transfer, e);
                    continue;
                }
            };
            transfer.targets = Some(targets);

            if transfer.is_imported().await {
                // Already imported by the *arr — just clean it off put.io.
                info!("{}: orphan already imported, deleting from put.io", transfer);
                if let Err(e) = putio::delete_file(api_key, file.id).await {
                    warn!(
                        "{}: failed to delete imported orphan from put.io: {}",
                        transfer, e
                    );
                }
                continue;
            }

            info!("{}: orphan ready for download", transfer);
            // Reuse the hash already derived by `from_orphan` so there's a single
            // source of truth for the synthetic hash.
            let hash = transfer.hash.clone().unwrap_or_default();
            // Only start tracking/reporting the orphan once it's actually been
            // queued, so a failed send can't leave it advertised via torrent-get
            // as a download that never happens.
            if tx
                .send(TransferMessage::QueuedForDownload(transfer))
                .await
                .is_ok()
            {
                app_data
                    .state
                    .add_orphan(OrphanFile {
                        file_id: file.id,
                        name: file.name.clone(),
                        hash,
                        size: file.size,
                        download_dir,
                    })
                    .await;
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use figment::{
        providers::{Format, Toml},
        Figment,
    };

    /// AppData with no *arr configured, so `is_imported` makes no HTTP calls.
    fn app_data() -> Data<AppData> {
        let config: crate::Config = Figment::new()
            .merge(Toml::string(
                r#"
                bind_address = "0.0.0.0"
                download_directory = "/downloads"
                download_workers = 1
                loglevel = "info"
                orchestration_workers = 1
                password = "p"
                polling_interval = 10
                port = 9091
                skip_directories = ["sample"]
                uid = 1000
                username = "u"
                [putio]
                api_key = "k"
                "#,
            ))
            .extract()
            .unwrap();
        Data::new(AppData {
            config,
            state: crate::state::StateManager::new("k".to_string()),
            http: reqwest::Client::new(),
        })
    }

    fn target(target_type: TargetType, to: &str) -> DownloadTarget {
        DownloadTarget {
            from: None,
            to: to.to_string(),
            target_type,
            top_level: false,
            transfer_hash: "0".repeat(40),
            media_type: Some(MediaType::Video),
        }
    }

    #[actix_rt::test]
    async fn transfer_with_only_folders_is_not_imported() {
        // e.g. a RAR release: the folder is a target, the ARCHIVE parts aren't.
        let mut t = Transfer::from_orphan(app_data(), 1, "Some.Release".to_string());
        t.targets = Some(vec![target(TargetType::Directory, "/downloads/Some.Release")]);
        assert!(!t.is_imported().await);
    }

    fn archive(to: &str, top_level: bool) -> DownloadTarget {
        DownloadTarget {
            top_level,
            media_type: Some(MediaType::Archive),
            ..target(TargetType::File, to)
        }
    }

    #[test]
    fn archive_in_a_folder_matches_on_the_transfer_folder() {
        let dir = Some("/downloads/movies/Some.Release");
        let release = Some("Some.Release");
        let part = archive("/downloads/movies/Some.Release/release.part01.rar", false);
        assert_eq!(archive_import_dir(&part, dir).as_deref(), release);
        // Deeper down it still matches the release folder, not "CD1".
        let part = archive("/downloads/movies/Some.Release/CD1/cd1.rar", false);
        assert_eq!(archive_import_dir(&part, dir).as_deref(), release);
    }

    #[test]
    fn lone_archive_has_no_folder_to_match() {
        // A single-file transfer: its parent is the category folder ("movies"),
        // which almost every import path contains.
        let lone = archive("/downloads/movies/some.release.zip", true);
        assert_eq!(archive_import_dir(&lone, None), None);
    }

    #[actix_rt::test]
    async fn archive_counts_as_imported_only_once_it_is_gone() {
        let path = std::env::temp_dir().join(format!("putioarr-test-{}.zip", std::process::id()));
        std::fs::write(&path, b"zip").unwrap();
        let mut t = Transfer::from_orphan(app_data(), 1, "some.release.zip".to_string());
        t.targets = Some(vec![archive(path.to_str().unwrap(), true)]);
        assert!(!t.is_imported().await);
        std::fs::remove_file(&path).unwrap();
        assert!(t.is_imported().await);
    }

    #[actix_rt::test]
    async fn file_with_no_arr_to_ask_still_counts_as_imported() {
        // Unchanged behaviour: a file no configured *arr handles doesn't block.
        let mut t = Transfer::from_orphan(app_data(), 1, "Some.Release".to_string());
        t.targets = Some(vec![
            target(TargetType::Directory, "/downloads/Some.Release"),
            target(TargetType::File, "/downloads/Some.Release/movie.mkv"),
        ]);
        assert!(t.is_imported().await);
    }
}