putioarr 0.3.4

put.io to sonarr/radarr 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
use crate::{
    arr,
    putio::{self, PutIOTransfer},
    AppData,
};
use actix_web::web::Data;
use anyhow::{Context, Result};
use async_channel::{Receiver, Sender};
use async_recursion::async_recursion;
use file_owner::PathExt;
use futures::StreamExt;
use log::{debug, info};
use nix::unistd::Uid;
use serde::{Deserialize, Serialize};
use std::{fs, path::Path, time::Duration};
use tokio::{fs::metadata, time::sleep};

static NUM_WORKERS: usize = 10;

pub async fn start_download_system(app_data: Data<AppData>) -> Result<()> {
    let (sender, receiver) = async_channel::unbounded();
    let data = app_data.clone();
    let tx = sender.clone();
    actix_rt::spawn(async { produce_transfers(data, tx).await });

    for id in 0..NUM_WORKERS {
        let data = app_data.clone();
        let tx = sender.clone();
        let rx = receiver.clone();
        Worker::start(id, data, tx, rx);
    }
    Ok(())
}

// Check for new putio transfers and if they qualify, send them on for download
async fn produce_transfers(app_data: Data<AppData>, tx: Sender<MessageType>) -> Result<()> {
    let ten_seconds = std::time::Duration::from_secs(app_data.config.polling_interval);
    let mut seen = Vec::<u64>::new();

    info!("Checking if there are 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 transfer in &putio::list_transfers(&app_data.config.putio.api_key)
        .await?
        .transfers
    {
        let mut message: Message = transfer.into();
        if transfer.is_downloadable() {
            let targets = get_download_targets(&app_data, message.file_id.unwrap()).await?;
            message.targets = Some(targets);
            if is_imported(&app_data, &message).await {
                info!("{} already imported. Notifying of import.", &message.name);
                seen.push(message.transfer_id);
                tx.send(MessageType::Imported(message)).await?;
            } else {
                info!("{} not imported yet. Continuing as normal.", &message.name);
            }
        }
    }
    info!("Done checking for unfinished transfers.");
    loop {
        let transfers = putio::list_transfers(&app_data.config.putio.api_key)
            .await?
            .transfers;

        if !transfers.is_empty() {
            debug!("Active transfers: {:?}", transfers);
        }
        for transfer in &transfers {
            if seen.contains(&transfer.id) || !transfer.is_downloadable() {
                continue;
            }

            let msg: Message = transfer.into();

            info!("Queueing {} for download", msg.name);
            tx.send(MessageType::QueuedForDownload(msg)).await?;
            seen.push(transfer.id);
        }

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

        sleep(ten_seconds).await;
    }
}

#[derive(Clone)]
struct Worker {
    _id: usize,
    app_data: Data<AppData>,
    tx: Sender<MessageType>,
    rx: Receiver<MessageType>,
}

impl Worker {
    pub fn start(
        id: usize,
        app_data: Data<AppData>,
        tx: Sender<MessageType>,
        rx: Receiver<MessageType>,
    ) {
        let s = Self {
            _id: id,
            app_data,
            tx,
            rx,
        };
        let _join_handle = actix_rt::spawn(async move { s.work().await });
    }

    async fn work(&self) -> Result<()> {
        loop {
            let msg = self.rx.recv().await?;
            let app_data = self.app_data.clone();
            match msg {
                MessageType::QueuedForDownload(m) => {
                    info!("Downloading {}", m.name);
                    let targets = Some(download(&self.app_data, m.file_id.unwrap()).await?);
                    info!("Downloading {} done", m.name);
                    self.tx
                        .send(MessageType::Downloaded(Message { targets, ..m }))
                        .await?;
                }
                MessageType::Downloaded(m) => {
                    info!("Watching imports {}", m.name);
                    let tx = self.tx.clone();
                    actix_rt::spawn(async { watch_for_import(app_data, tx, m).await });
                }
                MessageType::Imported(m) => {
                    info!("Watching seeding {}", m.name);

                    actix_rt::spawn(async { watch_seeding(app_data, m).await });
                }
            }
        }
    }
}

#[derive(Debug, Clone)]
struct Message {
    name: String,
    file_id: Option<u64>,
    transfer_id: u64,
    targets: Option<Vec<DownloadTarget>>,
}

impl From<&PutIOTransfer> for Message {
    fn from(transfer: &PutIOTransfer) -> Self {
        Self {
            transfer_id: transfer.id,
            name: transfer.name.clone(),
            file_id: transfer.file_id,
            targets: None,
        }
    }
}

#[derive(Debug, Clone)]
enum MessageType {
    QueuedForDownload(Message),
    Downloaded(Message),
    Imported(Message),
}

async fn watch_for_import(
    app_data: Data<AppData>,
    tx: Sender<MessageType>,
    message: Message,
) -> Result<()> {
    loop {
        if is_imported(&app_data, &message).await {
            info!("{} has been imported. Deleting files.", message.name);
            let targets = &message.targets.clone().unwrap();
            let top_level_path = get_top_level(targets);

            match metadata(&top_level_path).await {
                Ok(m) if m.is_dir() => {
                    info!("Deleting everyting in {}", &top_level_path);
                    fs::remove_dir_all(top_level_path).unwrap();
                }
                Ok(m) if m.is_file() => {
                    info!("Deleting {}", &top_level_path);
                    fs::remove_file(top_level_path).unwrap();
                }
                Ok(_) | Err(_) => {
                    panic!("Don't know how to handle {}", &top_level_path)
                }
            };
            let m = message.clone();
            tx.send(MessageType::Imported(m)).await?;

            break;
        }
        sleep(Duration::from_secs(app_data.config.polling_interval)).await;
    }
    info!("{} deleted. Stop watching.", message.name);
    Ok(())
}

async fn is_imported(app_data: &Data<AppData>, message: &Message) -> bool {
    let targets = &message.targets.as_ref().unwrap().clone();
    // all_targets_hardlinked(targets);
    all_targets_imported(app_data, targets).await
}

async fn all_targets_imported(app_data: &Data<AppData>, targets: &[DownloadTarget]) -> bool {
    let mut check_services = Vec::<(&str, String, String)>::new();
    if let Some(a) = &app_data.config.sonarr {
        check_services.push(("Sonarr", a.url.clone(), a.api_key.clone()))
    }
    if let Some(a) = &app_data.config.radarr {
        check_services.push(("Radarr", a.url.clone(), a.api_key.clone()))
    }

    let paths = targets
        .iter()
        .filter(|t| t.target_type == DownloadType::File)
        .map(|t| t.to.clone())
        .collect::<Vec<String>>();

    let mut results = Vec::<bool>::new();
    for path in paths {
        info!("Checking if {} has been imported yet", &path);
        let mut service_results = vec![];
        for (service_name, url, key) in &check_services {
            let service_result = arr::is_imported(&path, key, url).await.unwrap();
            if service_result {
                info!("Found {} imported by {}", &path, service_name);
            }
            service_results.push(service_result)
        }
        // Check if ANY of the service_results are true and put the outcome in results
        results.push(service_results.into_iter().any(|x| x));
    }
    // Check if all targets have been imported
    results.into_iter().all(|x| x)
}

fn get_top_level(targets: &[DownloadTarget]) -> &str {
    targets.iter().find(|t| t.top_level).unwrap().to.as_str()
}

async fn watch_seeding(app_data: Data<AppData>, message: Message) -> Result<()> {
    loop {
        let transfer = putio::get_transfer(&app_data.config.putio.api_key, message.transfer_id)
            .await?
            .transfer;
        if transfer.status != "SEEDING" {
            info!(
                "Transfer {} is no longer seeding. Removing..",
                transfer.name
            );
            putio::remove_transfer(&app_data.config.putio.api_key, message.transfer_id).await?;
            break;
        }
    }
    sleep(Duration::from_secs(app_data.config.polling_interval)).await;

    info!("{} removed. Stop watching.", message.name);
    Ok(())
}

/// Downloads all files belonging to a file_id
async fn download(app_data: &Data<AppData>, file_id: u64) -> Result<Vec<DownloadTarget>> {
    let targets = get_download_targets(app_data, file_id).await?;
    download_targets(&targets, app_data).await?;

    // TODO: Cleanup when stuff goes bad
    Ok(targets)
}

async fn download_targets(targets: &Vec<DownloadTarget>, app_data: &Data<AppData>) -> Result<()> {
    for target in targets {
        match target.target_type {
            DownloadType::Directory => {
                if !Path::new(&target.to).exists() {
                    fs::create_dir(&target.to)?;
                    if Uid::effective().is_root() {
                        target.to.clone().set_owner(app_data.config.uid)?;
                    }
                }
            }
            DownloadType::File => {
                // Delete file if already exists
                if !Path::new(&target.to).exists() {
                    let url = target.from.clone().context("No URL found")?;
                    fetch(&url, &target.to, app_data.config.uid).await?
                } else {
                    info!("{} already exists. Skipping download.", &target.to);
                    // fs::remove_file(&target.to)?;
                }
            }
        }
    }
    Ok(())
}

async fn get_download_targets(
    app_data: &Data<AppData>,
    file_id: u64,
) -> Result<Vec<DownloadTarget>> {
    recurse_download_targets(app_data, file_id, None, true).await
}

#[async_recursion]
async fn recurse_download_targets(
    app_data: &Data<AppData>,
    file_id: u64,
    override_base_path: Option<String>,
    top_level: bool,
) -> Result<Vec<DownloadTarget>> {
    let base_path = override_base_path.unwrap_or(app_data.config.download_directory.clone());
    let mut targets = Vec::<DownloadTarget>::new();
    let response = putio::list_files(&app_data.config.putio.api_key, file_id).await?;

    match response.parent.file_type.as_str() {
        "FOLDER" => {
            let target = Path::new(&app_data.config.download_directory)
                .join(&response.parent.name)
                .to_string_lossy()
                .to_string();

            targets.push(DownloadTarget {
                from: None,
                target_type: DownloadType::Directory,
                to: target.clone(),
                top_level,
            });
            let new_base = format!("{}/", &target);
            for file in response.files {
                targets.append(
                    &mut recurse_download_targets(app_data, file.id, Some(new_base.clone()), false)
                        .await?,
                );
            }
        }
        "VIDEO" => {
            // Get download URL for file
            let url = putio::url(&app_data.config.putio.api_key, response.parent.id).await?;

            let to = Path::new(&base_path)
                .join(&response.parent.name)
                .to_string_lossy()
                .to_string();

            targets.push(DownloadTarget {
                from: Some(url),
                target_type: DownloadType::File,
                to,
                top_level,
            });
        }
        _ => {}
    }

    Ok(targets)
}

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

async fn fetch(url: &str, to: &str, uid: u32) -> Result<()> {
    info!("Downloading {} started...", &to);
    let tmp_path = format!("{}.downloading", &to);
    let mut tmp_file = tokio::fs::File::create(&tmp_path).await?;
    let mut byte_stream = reqwest::get(url).await?.bytes_stream();

    while let Some(item) = byte_stream.next().await {
        tokio::io::copy(&mut item?.as_ref(), &mut tmp_file).await?;
    }
    if Uid::effective().is_root() {
        tmp_path.clone().set_owner(uid)?;
    }

    fs::rename(&tmp_path, to)?;
    info!("Downloading {} finished...", &to);
    Ok(())
}

#[derive(Debug, Serialize, Deserialize, Eq, PartialEq)]
pub(crate) enum DownloadStatus {
    New,
    Downloading,
    Downloaded,
    Imported,
}

#[derive(Debug, Serialize, Deserialize)]
pub(crate) struct Download {
    hash: String,
    pub status: DownloadStatus,
    file_id: Option<u64>,
    targets: Option<Vec<DownloadTarget>>,
}

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