Browse Source

stop use ipfs

master
hole-thu 3 years ago
parent
commit
7016718673
  1. 2
      .env.sample
  2. 1
      Rocket.toml
  3. 2
      src/api/mod.rs
  4. 51
      src/api/upload.rs
  5. 5
      src/main.rs

2
.env.sample

@ -6,6 +6,8 @@ MAST_SCOPE="read:accounts"
AUTH_BACKEND_URL="http://hole.localhost"
FRONTEND_WHITELIST="https://thuhollow.github.io"
UPLOAD_DIR="user_files"
DATABASE_URL="postgres://hole:hole_pass@localhost/hole_v2"
MIGRATION_DIRECTORY=migrations/postgres
REDIS_URL="redis://127.0.0.1:6379"

1
Rocket.toml

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

2
src/api/mod.rs

@ -25,6 +25,7 @@ macro_rules! code0 {
);
}
/*
macro_rules! code1 {
($msg:expr) => (
Ok(json!({
@ -33,6 +34,7 @@ macro_rules! code1 {
}))
);
}
*/
macro_rules! e2s {
($e:expr) => (json!({

51
src/api/upload.rs

@ -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 }))
}

5
src/main.rs

@ -84,7 +84,10 @@ async fn main() -> Result<(), rocket::Error> {
cors::options_handler,
],
)
.mount("/_api/v2", routes![api::comment::add_comment])
.mount(
"/_api/v2",
routes![api::comment::add_comment, api::upload::local_upload,],
)
.mount(
"/_login",
[

Loading…
Cancel
Save