stop use ipfs
This commit is contained in:
@@ -6,6 +6,8 @@ MAST_SCOPE="read:accounts"
|
|||||||
AUTH_BACKEND_URL="http://hole.localhost"
|
AUTH_BACKEND_URL="http://hole.localhost"
|
||||||
FRONTEND_WHITELIST="https://thuhollow.github.io"
|
FRONTEND_WHITELIST="https://thuhollow.github.io"
|
||||||
|
|
||||||
|
UPLOAD_DIR="user_files"
|
||||||
|
|
||||||
DATABASE_URL="postgres://hole:hole_pass@localhost/hole_v2"
|
DATABASE_URL="postgres://hole:hole_pass@localhost/hole_v2"
|
||||||
MIGRATION_DIRECTORY=migrations/postgres
|
MIGRATION_DIRECTORY=migrations/postgres
|
||||||
REDIS_URL="redis://127.0.0.1:6379"
|
REDIS_URL="redis://127.0.0.1:6379"
|
||||||
|
|||||||
@@ -1,3 +1,2 @@
|
|||||||
[default]
|
[default]
|
||||||
limits = { file = "200MB" }
|
limits = { file = "200MB" }
|
||||||
temp_dir = "user_files"
|
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ macro_rules! code0 {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
macro_rules! code1 {
|
macro_rules! code1 {
|
||||||
($msg:expr) => (
|
($msg:expr) => (
|
||||||
Ok(json!({
|
Ok(json!({
|
||||||
@@ -33,6 +34,7 @@ macro_rules! code1 {
|
|||||||
}))
|
}))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
macro_rules! e2s {
|
macro_rules! e2s {
|
||||||
($e:expr) => (json!({
|
($e:expr) => (json!({
|
||||||
|
|||||||
@@ -1,37 +1,26 @@
|
|||||||
use crate::api::{CurrentUser, JsonApi};
|
use super::PolicyError::OldApi;
|
||||||
|
use super::{ApiError, CurrentUser, JsonApi};
|
||||||
use rocket::fs::TempFile;
|
use rocket::fs::TempFile;
|
||||||
use rocket::serde::json::json;
|
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>")]
|
#[post("/upload", data = "<file>")]
|
||||||
pub async fn ipfs_upload(_user: CurrentUser, file: TempFile<'_>) -> JsonApi {
|
pub async fn local_upload(_user: CurrentUser, mut file: TempFile<'_>) -> JsonApi {
|
||||||
// dbg!(&file);
|
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")
|
||||||
|
);
|
||||||
|
|
||||||
// dbg!(&file.path());
|
file.copy_to(format!("{}/{}", var("UPLOAD_DIR").unwrap(), filename))
|
||||||
if let Some(filepath) = file.path() {
|
.await?;
|
||||||
let output = Command::new("ipfs")
|
|
||||||
.args([
|
code0!(json!({ "path": filename }))
|
||||||
"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!("文件丢失")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -84,7 +84,10 @@ async fn main() -> Result<(), rocket::Error> {
|
|||||||
cors::options_handler,
|
cors::options_handler,
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
.mount("/_api/v2", routes![api::comment::add_comment])
|
.mount(
|
||||||
|
"/_api/v2",
|
||||||
|
routes![api::comment::add_comment, api::upload::local_upload,],
|
||||||
|
)
|
||||||
.mount(
|
.mount(
|
||||||
"/_login",
|
"/_login",
|
||||||
[
|
[
|
||||||
|
|||||||
Reference in New Issue
Block a user