diff --git a/.env.sample b/.env.sample index cb1cdd1..2feee2b 100644 --- a/.env.sample +++ b/.env.sample @@ -6,6 +6,8 @@ MAST_SCOPE="read:accounts" AUTH_BACKEND_URL="http://hole.localhost" FRONTEND_WHITELIST="https://thuhollow.github.io" +UPLOAD_DIR="user_files" + DATABASE_URL="postgres://hole:hole_pass@localhost/hole_v2" MIGRATION_DIRECTORY=migrations/postgres REDIS_URL="redis://127.0.0.1:6379" diff --git a/Rocket.toml b/Rocket.toml index 1dc04c2..be66c1e 100644 --- a/Rocket.toml +++ b/Rocket.toml @@ -1,3 +1,2 @@ [default] limits = { file = "200MB" } -temp_dir = "user_files" diff --git a/src/api/mod.rs b/src/api/mod.rs index 20c3d48..d4e3f16 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -25,6 +25,7 @@ macro_rules! code0 { ); } +/* macro_rules! code1 { ($msg:expr) => ( Ok(json!({ @@ -33,6 +34,7 @@ macro_rules! code1 { })) ); } +*/ macro_rules! e2s { ($e:expr) => (json!({ diff --git a/src/api/upload.rs b/src/api/upload.rs index f329470..7043b56 100644 --- a/src/api/upload.rs +++ b/src/api/upload.rs @@ -1,37 +1,26 @@ -use crate::api::{CurrentUser, JsonApi}; +use super::PolicyError::OldApi; +use super::{ApiError, CurrentUser, JsonApi}; use rocket::fs::TempFile; use rocket::serde::json::json; -use std::process::Command; +use std::env::var; + +#[post("/upload")] +pub async fn ipfs_upload() -> ApiError { + OldApi.into() +} #[post("/upload", data = "")] -pub async fn ipfs_upload(_user: CurrentUser, file: TempFile<'_>) -> JsonApi { - // dbg!(&file); +pub async fn local_upload(_user: CurrentUser, mut file: TempFile<'_>) -> JsonApi { + let filename: String = format!( + "file{}.{}", + file.path().unwrap().file_name().unwrap().to_str().unwrap(), + file.content_type() + .map(|ct| ct.extension().unwrap_or_else(|| ct.sub()).as_str()) + .unwrap_or("unknown") + ); + + file.copy_to(format!("{}/{}", var("UPLOAD_DIR").unwrap(), filename)) + .await?; - // dbg!(&file.path()); - if let Some(filepath) = file.path() { - let output = Command::new("ipfs") - .args([ - "add", - "-q", - "-r", - "-cid-version=1", - filepath.to_str().unwrap(), - ]) - .output()?; - // dbg!(&output); - let hash = std::str::from_utf8(&output.stdout) - .unwrap() - .split_terminator('\n') - .last() - .unwrap_or_else(|| { - dbg!(&output); - dbg!(&file.path()); - panic!("get ipfs output error"); - }); - code0!(json!({ - "hash": hash, - })) - } else { - code1!("文件丢失") - } + code0!(json!({ "path": filename })) } diff --git a/src/main.rs b/src/main.rs index 517dc21..b822068 100644 --- a/src/main.rs +++ b/src/main.rs @@ -84,7 +84,10 @@ async fn main() -> Result<(), rocket::Error> { cors::options_handler, ], ) - .mount("/_api/v2", routes![api::comment::add_comment]) + .mount( + "/_api/v2", + routes![api::comment::add_comment, api::upload::local_upload,], + ) .mount( "/_login", [