From 3f46948d6563ac51148b03a0cee6c54fd17760c9 Mon Sep 17 00:00:00 2001 From: Chikage Date: Tue, 17 Oct 2023 02:19:03 +0900 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96gofile=E4=B8=8A=E4=BC=A0?= =?UTF-8?q?=EF=BC=8C=E9=98=B2=E6=AD=A2=E4=B8=80=E6=AC=A1=E6=80=A7=E8=AF=BB?= =?UTF-8?q?=E5=8F=96=E5=A4=A7=E6=96=87=E4=BB=B6=E8=87=B3=E5=86=85=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.lock | 16 ++++++++++++++++ matrix_bot/src/main.rs | 7 +++++-- plugins/qbittorrent/Cargo.toml | 2 ++ plugins/qbittorrent/src/upload/gofile.rs | 21 +++++++++++++-------- 4 files changed, 36 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0223a54..5219521 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2490,6 +2490,7 @@ dependencies = [ "serde", "serde_json", "tokio", + "tokio-util", "toml 0.8.2", "url", "walkdir", @@ -2693,11 +2694,13 @@ dependencies = [ "tokio", "tokio-native-tls", "tokio-rustls", + "tokio-util", "tower-service", "trust-dns-resolver", "url", "wasm-bindgen", "wasm-bindgen-futures", + "wasm-streams", "web-sys", "winreg", ] @@ -3916,6 +3919,19 @@ version = "0.2.87" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ca6ad05a4870b2bf5fe995117d3728437bd27d7cd5f06f13c17443ef369775a1" +[[package]] +name = "wasm-streams" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4609d447824375f43e1ffbc051b50ad8f4b3ae8219680c94452ea05eb240ac7" +dependencies = [ + "futures-util", + "js-sys", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", +] + [[package]] name = "wasm-timer" version = "0.2.5" diff --git a/matrix_bot/src/main.rs b/matrix_bot/src/main.rs index 4988134..ebec983 100644 --- a/matrix_bot/src/main.rs +++ b/matrix_bot/src/main.rs @@ -61,7 +61,10 @@ async fn main() { let _ = load_plugins(&matrix_client, args.data.join("plugins"), &args.plugins); let ctrlc = tokio::signal::ctrl_c(); - let mut term= tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate()).unwrap(); + + #[cfg(unix)] + let mut term = + tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate()).unwrap(); let client = matrix_client.clone(); @@ -79,7 +82,7 @@ async fn main() { _=handle => { log::error!("Syncing stopped"); } - + _=term.recv() => { log::info!("SIGTERM received, stopping"); } diff --git a/plugins/qbittorrent/Cargo.toml b/plugins/qbittorrent/Cargo.toml index 6b0c28c..fb38980 100644 --- a/plugins/qbittorrent/Cargo.toml +++ b/plugins/qbittorrent/Cargo.toml @@ -18,10 +18,12 @@ reqwest = { version = "0.11.6", default-features = false, features = [ "json", "trust-dns", "rustls-tls-native-roots", + "stream", ] } regex = "1.10.0" once_cell = "1.8.0" url = "2.4.1" +tokio-util = { version = "0.7.9", default-features = false, features = ["io"] } serde_json = "1.0.68" walkdir = "2.4.0" diff --git a/plugins/qbittorrent/src/upload/gofile.rs b/plugins/qbittorrent/src/upload/gofile.rs index 905f0ae..c9c7b7c 100644 --- a/plugins/qbittorrent/src/upload/gofile.rs +++ b/plugins/qbittorrent/src/upload/gofile.rs @@ -1,6 +1,9 @@ use anyhow::Result; +use reqwest::Body; use serde::{Deserialize, Serialize}; -use std::{borrow::Cow, path::Path}; +use std::path::Path; +use tokio::fs::File; +use tokio_util::io::ReaderStream; use walkdir::WalkDir; #[derive(Debug, Deserialize, Serialize)] @@ -38,12 +41,13 @@ pub async fn upload>(file_path: P) -> Result { }) { let file_name = entry.file_name().to_string_lossy().to_string(); - let file = std::fs::read(entry.path())?; + let file = File::open(entry.path()).await?; + let file_stream = ReaderStream::new(file); let upload = UploadParams { server: server.clone(), token: token.clone(), folder_id: folder_id.clone(), - file, + file_stream, file_name, }; let upload = upload_file(upload).await?; @@ -69,12 +73,13 @@ pub async fn upload>(file_path: P) -> Result { .unwrap() .to_string_lossy() .to_string(); - let file = std::fs::read(file_path)?; + let file = File::open(file_path).await?; + let file_stream = ReaderStream::new(file); let upload = UploadParams { server, token: None, folder_id: None, - file, + file_stream, file_name, }; let upload = upload_file(upload).await?; @@ -87,7 +92,7 @@ struct UploadParams { server: String, token: Option, folder_id: Option, - file: Vec, + file_stream: ReaderStream, file_name: String, } @@ -113,8 +118,8 @@ async fn upload_file(parameter: UploadParams) -> Result { let url = format!("https://{}.gofile.io/uploadFile", parameter.server); // 一定要有file_name方法,且参数不能为空,否则数据上传失败 - let part = - reqwest::multipart::Part::bytes(Cow::from(parameter.file)).file_name(parameter.file_name); + let part = reqwest::multipart::Part::stream(Body::wrap_stream(parameter.file_stream)) + .file_name(parameter.file_name); let mut form = reqwest::multipart::Form::new().part("file", part); if let Some(token) = parameter.token { form = form.text("token", token);