3 changed files with 39 additions and 38 deletions
@ -1,2 +1,3 @@ |
|||||||
[default] |
[default] |
||||||
limits = { file = "200MB" } |
limits = { file = "200MB" } |
||||||
|
temp_dir = "user_files" |
||||||
|
@ -1,46 +1,37 @@ |
|||||||
use crate::api::{CurrentUser, JsonAPI}; |
use crate::api::{CurrentUser, JsonAPI}; |
||||||
use chrono::offset::Local; |
|
||||||
use rocket::fs::TempFile; |
use rocket::fs::TempFile; |
||||||
use rocket::serde::json::json; |
use rocket::serde::json::json; |
||||||
use std::fs; |
|
||||||
use std::path::Path; |
|
||||||
use std::process::Command; |
use std::process::Command; |
||||||
|
|
||||||
#[post("/upload", data = "<file>")] |
#[post("/upload", data = "<file>")] |
||||||
pub async fn ipfs_upload(user: CurrentUser, mut file: TempFile<'_>) -> JsonAPI { |
pub async fn ipfs_upload(_user: CurrentUser, file: TempFile<'_>) -> JsonAPI { |
||||||
let file_dir = Path::new("user_files").join(&user.namehash); |
|
||||||
fs::create_dir_all(&file_dir)?; |
|
||||||
// dbg!(&file);
|
// dbg!(&file);
|
||||||
let filename = format!( |
|
||||||
"{}-{}.{}", |
// dbg!(&file.path());
|
||||||
Local::now().timestamp(), |
if let Some(filepath) = file.path() { |
||||||
&file.content_type().map_or("unknow", |ct| ct.top().as_str()), |
let output = Command::new("ipfs") |
||||||
&file.content_type().map_or("file", |ct| ct.sub().as_str()) |
.args([ |
||||||
); |
"add", |
||||||
debug!("dir: {}", &file_dir.to_str().unwrap()); |
"-q", |
||||||
file.persist_to(file_dir.join(&filename)).await?; |
"-r", |
||||||
// dbg!(&file_dir);
|
"-cid-version=1", |
||||||
// dbg!(file_dir.with_file_name(&filename));
|
filepath.to_str().unwrap(), |
||||||
let output = Command::new("ipfs") |
]) |
||||||
.args([ |
.output()?; |
||||||
"add", |
// dbg!(&output);
|
||||||
"-q", |
let hash = std::str::from_utf8(&output.stdout) |
||||||
"-r", |
.unwrap() |
||||||
"-cid-version=1", |
.split_terminator("\n") |
||||||
file_dir.to_str().unwrap(), |
.last() |
||||||
]) |
.unwrap_or_else(|| { |
||||||
.output()?; |
dbg!(&output); |
||||||
// dbg!(&output);
|
dbg!(&file.path()); |
||||||
let hash = std::str::from_utf8(&output.stdout) |
panic!("get ipfs output error"); |
||||||
.unwrap() |
}); |
||||||
.split_terminator("\n") |
code0!(json!({ |
||||||
.last() |
"hash": hash, |
||||||
.unwrap_or_else(|| { |
})) |
||||||
dbg!(&output.stdout); |
} else { |
||||||
panic!("get ipfs output error"); |
code1!("文件丢失") |
||||||
}); |
} |
||||||
code0!(json!({ |
|
||||||
"hash": hash, |
|
||||||
"filename": filename, |
|
||||||
})) |
|
||||||
} |
} |
||||||
|
Loading…
Reference in new issue