This repository has been archived by the owner on Aug 19, 2024. It is now read-only.
forked from vimpunk/cratetorrent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.rs
69 lines (56 loc) · 1.82 KB
/
handlers.rs
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
use crate::models::FileJob;
use crate::torrent_executor::execute_torrent;
use reqwest::StatusCode;
use std::io::Cursor;
use std::process::Command;
use std::{convert::Infallible, fs::File, io::copy};
use warp::Reply;
pub async fn handle_download_file(
file_job: FileJob,
) -> Result<impl Reply, Infallible> {
let torrent_dir =
String::from("/home/admin/git/cratetorrent/var/torrents/");
let full_torrent_path = torrent_dir + &file_job.file_name;
println!("{}", full_torrent_path);
let mut output_file = create_file(&full_torrent_path);
let mut file_buffer = download_file(file_job.download_url).await;
copy(&mut file_buffer, &mut output_file).unwrap();
let downloaded_file_name = execute_torrent(&full_torrent_path).await.unwrap();
//TODO read file path from torrent
let file_dir =
String::from("/home/admin/git/cratetorrent/var/downloads/");
let full_file_path = file_dir + &downloaded_file_name;
run_docker_cmds(&full_file_path);
Ok(StatusCode::OK)
}
//TODO run command from incoming request
fn run_docker_cmds(full_file_path: &String)
{
let image_load_status = Command::new("docker")
.arg("load")
.arg("-i")
.arg(full_file_path)
.status()
.unwrap();
println!("{}", image_load_status);
let docker_run_status = Command::new("docker")
.arg("run")
.arg("-d")
.arg("--rm")
.arg("--name")
.arg("hello-world")
.arg("-p")
.arg("8080:80")
.arg("nginxdemos/hello")
.status()
.unwrap();
println!("{}", docker_run_status);
}
async fn download_file(download_path: String) -> Cursor<bytes::Bytes> {
let response = reqwest::get(download_path).await.unwrap();
Cursor::new(response.bytes().await.unwrap())
}
fn create_file(file_path: &String) -> File {
let file = std::fs::File::create(file_path).unwrap();
file
}