diff --git a/Cargo.lock b/Cargo.lock index 35b2a53..e4975cd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -427,6 +427,7 @@ dependencies = [ "thiserror 2.0.12", "time", "tokio", + "tokio-util", "toml", "tower", "tower-http", @@ -3971,9 +3972,9 @@ dependencies = [ [[package]] name = "tokio-util" -version = "0.7.15" +version = "0.7.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "66a539a9ad6d5d281510d5bd368c973d636c02dbf8a67300bfb6b950696ad7df" +checksum = "14307c986784f72ef81c89db7d9e28d6ac26d16213b109ea501696195e6e3ce5" dependencies = [ "bytes", "futures-core", diff --git a/crates/biliup-cli/Cargo.toml b/crates/biliup-cli/Cargo.toml index 59e4a44..2629fb2 100644 --- a/crates/biliup-cli/Cargo.toml +++ b/crates/biliup-cli/Cargo.toml @@ -56,6 +56,7 @@ tower-sessions = { version = "0.14.0", default-features = false, features = [ tower-sessions-sqlx-store = { version = "0.15.0", features = ["sqlite"] } async-channel = { version = "2.5.0" } nom = "8" +tokio-util = "0.7.16" [features] default = [] python-bridge = [] diff --git a/crates/biliup-cli/src/server/api/bilibili_endpoints.rs b/crates/biliup-cli/src/server/api/bilibili_endpoints.rs index e93d08d..e3dbcaf 100644 --- a/crates/biliup-cli/src/server/api/bilibili_endpoints.rs +++ b/crates/biliup-cli/src/server/api/bilibili_endpoints.rs @@ -24,7 +24,7 @@ pub async fn archive_pre_endpoint( .await .change_context(AppError::Unknown) .map_err(report_to_response)?; - + // 尝试使用每个Cookie进行登录 for cookies in configurations { if let Ok(bili) = login_by_cookies(cookies.value, None).await { @@ -36,7 +36,7 @@ pub async fn archive_pre_endpoint( )); } } - + // 没有可用的Cookie Err(report_to_response(Report::from(AppError::Custom( "无可用 cookie 文件".to_string(), @@ -52,7 +52,7 @@ pub async fn get_myinfo_endpoint( .await .change_context(AppError::Custom(params["user"].to_string())) .map_err(report_to_response)?; - + // 获取用户信息 Ok(Json( bili.my_info() @@ -68,7 +68,7 @@ pub async fn get_proxy_endpoint( Query(params): Query>, ) -> Result { info!(params = ¶ms["url"]); - + // 代理HTTP请求 client .client diff --git a/crates/biliup-cli/src/server/api/endpoints.rs b/crates/biliup-cli/src/server/api/endpoints.rs index 81527f6..43fc132 100644 --- a/crates/biliup-cli/src/server/api/endpoints.rs +++ b/crates/biliup-cli/src/server/api/endpoints.rs @@ -11,10 +11,10 @@ use crate::server::infrastructure::repositories::{ del_streamer, get_all_streamer, get_upload_config, }; use crate::server::infrastructure::service_register::ServiceRegister; -use axum::Json; use axum::extract::{Path, State}; use axum::http::StatusCode; use axum::response::{IntoResponse, Response}; +use axum::{Json, debug_handler}; use biliup::credential::Credential; use error_stack::{Report, ResultExt}; use ormlite::{Insert, Model}; @@ -44,7 +44,7 @@ pub async fn get_streamers_endpoint( .iter() .find_map(|worker| { if worker.live_streamer.id == x.id { - Some(*worker.downloader_status.read().unwrap()) + Some(format!("{:?}", *worker.downloader_status.read().unwrap())) } else { None } @@ -65,7 +65,6 @@ pub async fn post_streamers_endpoint( info!("not supported url: {}", &payload.url); return Err((StatusCode::BAD_REQUEST, "Not supported url").into_response()); }; - let monitor = manager.ensure_monitor(); // You can insert the model directly. let live_streamers = payload @@ -77,14 +76,14 @@ pub async fn post_streamers_endpoint( .await .map_err(report_to_response)?; service_register - .add_room(monitor, live_streamers.clone(), upload_config) + .add_room(&manager, live_streamers.clone(), upload_config) .await .map_err(report_to_response)?; info!(workers=?service_register.workers, "successfully inserted new live streamers"); Ok(Json(live_streamers)) } - +#[debug_handler(state = ServiceRegister)] pub async fn put_streamers_endpoint( State(service_register): State, State(pool): State, @@ -109,10 +108,8 @@ pub async fn put_streamers_endpoint( .await .map_err(report_to_response)?; - let monitor = manager.ensure_monitor(); - service_register - .add_room(monitor, streamer.clone(), upload_config) + .add_room(&manager, streamer.clone(), upload_config) .await .map_err(report_to_response)?; diff --git a/crates/biliup-cli/src/server/app.rs b/crates/biliup-cli/src/server/app.rs index 08e4e46..dd3e58e 100644 --- a/crates/biliup-cli/src/server/app.rs +++ b/crates/biliup-cli/src/server/app.rs @@ -76,7 +76,7 @@ impl ApplicationController { .allow_methods(AllowMethods::any()), ) .fallback(static_handler); // 静态文件处理回退 - + // 启动HTTP服务器 info!("routes initialized, listening on {}", addr); let listener = tokio::net::TcpListener::bind(addr) diff --git a/crates/biliup-cli/src/server/common/mod.rs b/crates/biliup-cli/src/server/common/mod.rs index a946577..4ef47b6 100644 --- a/crates/biliup-cli/src/server/common/mod.rs +++ b/crates/biliup-cli/src/server/common/mod.rs @@ -1,2 +1,3 @@ +pub mod upload; /// 通用工具函数 pub mod util; diff --git a/crates/biliup-cli/src/server/common/upload.rs b/crates/biliup-cli/src/server/common/upload.rs new file mode 100644 index 0000000..5c52749 --- /dev/null +++ b/crates/biliup-cli/src/server/common/upload.rs @@ -0,0 +1,263 @@ +use crate::server::core::download_manager::UActor; +use crate::server::core::downloader::SegmentEvent; +use crate::server::errors::{AppError, AppResult}; +use crate::server::infrastructure::context::Worker; +use crate::server::infrastructure::models::UploadStreamer; +use crate::server::infrastructure::models::hook_step::process_video; +use async_channel::Receiver; +use biliup::bilibili::{BiliBili, ResponseData, Studio, Video}; +use biliup::client::StatelessClient; +use biliup::credential::login_by_cookies; +use biliup::uploader::line::{Line, Probe}; +use biliup::uploader::util::SubmitOption; +use biliup::uploader::{VideoFile, line}; +use error_stack::ResultExt; +use futures::StreamExt; +use std::path::{Path, PathBuf}; +use std::str::FromStr; +use std::time::Instant; +use tracing::{error, info}; + +// 辅助结构体 +struct UploadContext { + bilibili: BiliBili, + client: StatelessClient, + line: Line, + threads: usize, + upload_config: UploadStreamer, +} + +#[derive(Default)] +struct UploadedVideos { + videos: Vec