|
|
|
@ -1,33 +1,21 @@
|
|
|
|
|
use crate::api::{CurrentUser, JsonAPI}; |
|
|
|
|
use chrono::offset::Local; |
|
|
|
|
use rocket::fs::TempFile; |
|
|
|
|
use rocket::serde::json::json; |
|
|
|
|
use std::fs; |
|
|
|
|
use std::path::Path; |
|
|
|
|
use std::process::Command; |
|
|
|
|
|
|
|
|
|
#[post("/upload", data = "<file>")] |
|
|
|
|
pub async fn ipfs_upload(user: CurrentUser, mut file: TempFile<'_>) -> JsonAPI { |
|
|
|
|
let file_dir = Path::new("user_files").join(&user.namehash); |
|
|
|
|
fs::create_dir_all(&file_dir)?; |
|
|
|
|
pub async fn ipfs_upload(_user: CurrentUser, file: TempFile<'_>) -> JsonAPI { |
|
|
|
|
// dbg!(&file);
|
|
|
|
|
let filename = format!( |
|
|
|
|
"{}-{}.{}", |
|
|
|
|
Local::now().timestamp(), |
|
|
|
|
&file.content_type().map_or("unknow", |ct| ct.top().as_str()), |
|
|
|
|
&file.content_type().map_or("file", |ct| ct.sub().as_str()) |
|
|
|
|
); |
|
|
|
|
debug!("dir: {}", &file_dir.to_str().unwrap()); |
|
|
|
|
file.persist_to(file_dir.join(&filename)).await?; |
|
|
|
|
// dbg!(&file_dir);
|
|
|
|
|
// dbg!(file_dir.with_file_name(&filename));
|
|
|
|
|
|
|
|
|
|
// dbg!(&file.path());
|
|
|
|
|
if let Some(filepath) = file.path() { |
|
|
|
|
let output = Command::new("ipfs") |
|
|
|
|
.args([ |
|
|
|
|
"add", |
|
|
|
|
"-q", |
|
|
|
|
"-r", |
|
|
|
|
"-cid-version=1", |
|
|
|
|
file_dir.to_str().unwrap(), |
|
|
|
|
filepath.to_str().unwrap(), |
|
|
|
|
]) |
|
|
|
|
.output()?; |
|
|
|
|
// dbg!(&output);
|
|
|
|
@ -36,11 +24,14 @@ pub async fn ipfs_upload(user: CurrentUser, mut file: TempFile<'_>) -> JsonAPI {
|
|
|
|
|
.split_terminator("\n") |
|
|
|
|
.last() |
|
|
|
|
.unwrap_or_else(|| { |
|
|
|
|
dbg!(&output.stdout); |
|
|
|
|
dbg!(&output); |
|
|
|
|
dbg!(&file.path()); |
|
|
|
|
panic!("get ipfs output error"); |
|
|
|
|
}); |
|
|
|
|
code0!(json!({ |
|
|
|
|
"hash": hash, |
|
|
|
|
"filename": filename, |
|
|
|
|
})) |
|
|
|
|
} else { |
|
|
|
|
code1!("文件丢失") |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|