5 changed files with 28 additions and 33 deletions
@ -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 = "<file>")] |
||||
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 })) |
||||
} |
||||
|
Loading…
Reference in new issue