Browse Source

opt: don't save uploaded files

master
hole-thu 3 years ago
parent
commit
40f183d393
  1. 1
      Rocket.toml
  2. 9
      src/api/mod.rs
  3. 67
      src/api/upload.rs

1
Rocket.toml

@ -1,2 +1,3 @@
[default] [default]
limits = { file = "200MB" } limits = { file = "200MB" }
temp_dir = "user_files"

9
src/api/mod.rs

@ -25,6 +25,15 @@ macro_rules! code0 {
); );
} }
macro_rules! code1 {
($msg:expr) => (
Ok(json!({
"code": 1,
"msg": $msg,
}))
);
}
macro_rules! e2s { macro_rules! e2s {
($e:expr) => (json!({ ($e:expr) => (json!({
"code": -1, "code": -1,

67
src/api/upload.rs

@ -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…
Cancel
Save