Files
hole-backend-rust/src/api/upload.rs
2022-07-31 23:12:57 +08:00

27 lines
758 B
Rust

use super::PolicyError::OldApi;
use super::{ApiError, CurrentUser, JsonApi};
use rocket::fs::TempFile;
use rocket::serde::json::json;
use std::env::var;
#[post("/upload")]
pub async fn ipfs_upload() -> ApiError {
OldApi.into()
}
#[post("/upload", data = "<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?;
code0!(json!({ "path": filename }))
}