2023-12-15 20:29:28 +08:00
|
|
|
use std::collections::HashMap;
|
2023-12-19 19:28:01 +08:00
|
|
|
use std::io::Write;
|
2023-12-25 10:17:13 +08:00
|
|
|
use actix_multipart_extract::{ File, Multipart, MultipartForm };
|
|
|
|
|
use actix_web::{ HttpResponse, post, web };
|
|
|
|
|
use chrono::{ Utc, FixedOffset };
|
2023-12-19 19:28:01 +08:00
|
|
|
use sea_orm::DbConn;
|
2023-12-15 20:29:28 +08:00
|
|
|
use crate::api::JsonResponse;
|
|
|
|
|
use crate::AppState;
|
|
|
|
|
use crate::entity::doc_info::Model;
|
2023-12-18 15:52:52 +08:00
|
|
|
use crate::errors::AppError;
|
2023-12-25 10:17:13 +08:00
|
|
|
use crate::service::doc_info::{ Mutation, Query };
|
2023-12-19 19:28:01 +08:00
|
|
|
use serde::Deserialize;
|
|
|
|
|
|
2023-12-25 10:17:13 +08:00
|
|
|
fn now() -> chrono::DateTime<FixedOffset> {
|
|
|
|
|
Utc::now().with_timezone(&FixedOffset::east_opt(3600 * 8).unwrap())
|
2023-12-22 17:57:27 +08:00
|
|
|
}
|
2023-12-15 20:29:28 +08:00
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
2023-12-22 17:57:27 +08:00
|
|
|
pub struct ListParams {
|
2023-12-15 20:29:28 +08:00
|
|
|
pub uid: i64,
|
|
|
|
|
pub filter: FilterParams,
|
|
|
|
|
pub sortby: String,
|
2023-12-22 17:57:27 +08:00
|
|
|
pub page: Option<u32>,
|
|
|
|
|
pub per_page: Option<u32>,
|
2023-12-15 20:29:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
|
pub struct FilterParams {
|
|
|
|
|
pub keywords: Option<String>,
|
|
|
|
|
pub folder_id: Option<i64>,
|
|
|
|
|
pub tag_id: Option<i64>,
|
|
|
|
|
pub kb_id: Option<i64>,
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-22 17:57:27 +08:00
|
|
|
#[post("/v1.0/docs")]
|
2023-12-25 10:17:13 +08:00
|
|
|
async fn list(
|
|
|
|
|
params: web::Json<ListParams>,
|
|
|
|
|
data: web::Data<AppState>
|
|
|
|
|
) -> Result<HttpResponse, AppError> {
|
|
|
|
|
let docs = Query::find_doc_infos_by_params(&data.conn, params.into_inner()).await?;
|
2023-12-15 20:29:28 +08:00
|
|
|
|
|
|
|
|
let mut result = HashMap::new();
|
|
|
|
|
result.insert("docs", docs);
|
|
|
|
|
|
|
|
|
|
let json_response = JsonResponse {
|
|
|
|
|
code: 200,
|
|
|
|
|
err: "".to_owned(),
|
|
|
|
|
data: result,
|
|
|
|
|
};
|
|
|
|
|
|
2023-12-25 10:17:13 +08:00
|
|
|
Ok(
|
|
|
|
|
HttpResponse::Ok()
|
|
|
|
|
.content_type("application/json")
|
|
|
|
|
.body(serde_json::to_string(&json_response)?)
|
|
|
|
|
)
|
2023-12-15 20:29:28 +08:00
|
|
|
}
|
|
|
|
|
|
2023-12-19 19:28:01 +08:00
|
|
|
#[derive(Deserialize, MultipartForm, Debug)]
|
|
|
|
|
pub struct UploadForm {
|
|
|
|
|
#[multipart(max_size = 512MB)]
|
|
|
|
|
file_field: File,
|
2023-12-25 10:17:13 +08:00
|
|
|
uid: i64,
|
|
|
|
|
did: i64,
|
2023-12-19 19:28:01 +08:00
|
|
|
}
|
2023-12-15 20:29:28 +08:00
|
|
|
|
2023-12-19 19:28:01 +08:00
|
|
|
#[post("/v1.0/upload")]
|
2023-12-25 10:17:13 +08:00
|
|
|
async fn upload(
|
|
|
|
|
payload: Multipart<UploadForm>,
|
|
|
|
|
data: web::Data<AppState>
|
|
|
|
|
) -> Result<HttpResponse, AppError> {
|
2023-12-19 19:28:01 +08:00
|
|
|
let uid = payload.uid;
|
2023-12-25 10:17:13 +08:00
|
|
|
async fn add_number_to_filename(
|
|
|
|
|
file_name: String,
|
|
|
|
|
conn: &DbConn,
|
|
|
|
|
uid: i64,
|
|
|
|
|
parent_id: i64
|
|
|
|
|
) -> String {
|
2023-12-19 19:28:01 +08:00
|
|
|
let mut i = 0;
|
|
|
|
|
let mut new_file_name = file_name.to_string();
|
|
|
|
|
let arr: Vec<&str> = file_name.split(".").collect();
|
2023-12-25 10:17:13 +08:00
|
|
|
let suffix = String::from(arr[arr.len() - 1]);
|
|
|
|
|
let preffix = arr[..arr.len() - 1].join(".");
|
|
|
|
|
let mut docs = Query::find_doc_infos_by_name(
|
|
|
|
|
conn,
|
|
|
|
|
uid,
|
|
|
|
|
&new_file_name,
|
|
|
|
|
Some(parent_id)
|
|
|
|
|
).await.unwrap();
|
|
|
|
|
while docs.len() > 0 {
|
2023-12-19 19:28:01 +08:00
|
|
|
i += 1;
|
|
|
|
|
new_file_name = format!("{}_{}.{}", preffix, i, suffix);
|
2023-12-25 10:17:13 +08:00
|
|
|
docs = Query::find_doc_infos_by_name(
|
|
|
|
|
conn,
|
|
|
|
|
uid,
|
|
|
|
|
&new_file_name,
|
|
|
|
|
Some(parent_id)
|
|
|
|
|
).await.unwrap();
|
2023-12-15 20:29:28 +08:00
|
|
|
}
|
2023-12-19 19:28:01 +08:00
|
|
|
new_file_name
|
2023-12-15 20:29:28 +08:00
|
|
|
}
|
2023-12-25 10:17:13 +08:00
|
|
|
let fnm = add_number_to_filename(
|
|
|
|
|
payload.file_field.name.clone(),
|
|
|
|
|
&data.conn,
|
|
|
|
|
uid,
|
|
|
|
|
payload.did
|
|
|
|
|
).await;
|
2023-12-19 19:28:01 +08:00
|
|
|
|
|
|
|
|
std::fs::create_dir_all(format!("./upload/{}/", uid));
|
|
|
|
|
let filepath = format!("./upload/{}/{}-{}", payload.uid, payload.did, fnm.clone());
|
2023-12-25 10:17:13 +08:00
|
|
|
let mut f = std::fs::File::create(&filepath)?;
|
2023-12-19 19:28:01 +08:00
|
|
|
f.write(&payload.file_field.bytes)?;
|
2023-12-25 10:17:13 +08:00
|
|
|
|
2023-12-19 19:28:01 +08:00
|
|
|
let doc = Mutation::create_doc_info(&data.conn, Model {
|
2023-12-25 10:17:13 +08:00
|
|
|
did: Default::default(),
|
|
|
|
|
uid: uid,
|
2023-12-19 19:28:01 +08:00
|
|
|
doc_name: fnm,
|
|
|
|
|
size: payload.file_field.bytes.len() as i64,
|
|
|
|
|
location: filepath,
|
|
|
|
|
r#type: "doc".to_string(),
|
2023-12-22 17:57:27 +08:00
|
|
|
created_at: now(),
|
|
|
|
|
updated_at: now(),
|
2023-12-25 10:17:13 +08:00
|
|
|
is_deleted: Default::default(),
|
2023-12-18 15:52:52 +08:00
|
|
|
}).await?;
|
2023-12-15 20:29:28 +08:00
|
|
|
|
2023-12-19 19:28:01 +08:00
|
|
|
let _ = Mutation::place_doc(&data.conn, payload.did, doc.did.unwrap()).await?;
|
|
|
|
|
|
2023-12-15 20:29:28 +08:00
|
|
|
Ok(HttpResponse::Ok().body("File uploaded successfully"))
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-22 17:57:27 +08:00
|
|
|
#[derive(Deserialize, Debug)]
|
|
|
|
|
pub struct RmDocsParam {
|
2023-12-25 10:17:13 +08:00
|
|
|
uid: i64,
|
|
|
|
|
dids: Vec<i64>,
|
2023-12-22 17:57:27 +08:00
|
|
|
}
|
2023-12-15 20:29:28 +08:00
|
|
|
#[post("/v1.0/delete_docs")]
|
2023-12-25 10:17:13 +08:00
|
|
|
async fn delete(
|
|
|
|
|
params: web::Json<RmDocsParam>,
|
|
|
|
|
data: web::Data<AppState>
|
|
|
|
|
) -> Result<HttpResponse, AppError> {
|
2023-12-22 17:57:27 +08:00
|
|
|
let _ = Mutation::delete_doc_info(&data.conn, ¶ms.dids).await?;
|
2023-12-15 20:29:28 +08:00
|
|
|
|
|
|
|
|
let json_response = JsonResponse {
|
|
|
|
|
code: 200,
|
|
|
|
|
err: "".to_owned(),
|
|
|
|
|
data: (),
|
|
|
|
|
};
|
|
|
|
|
|
2023-12-25 10:17:13 +08:00
|
|
|
Ok(
|
|
|
|
|
HttpResponse::Ok()
|
|
|
|
|
.content_type("application/json")
|
|
|
|
|
.body(serde_json::to_string(&json_response)?)
|
|
|
|
|
)
|
2023-12-15 20:29:28 +08:00
|
|
|
}
|
|
|
|
|
|
2023-12-22 17:57:27 +08:00
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
|
pub struct MvParams {
|
2023-12-25 10:17:13 +08:00
|
|
|
pub uid: i64,
|
2023-12-22 17:57:27 +08:00
|
|
|
pub dids: Vec<i64>,
|
|
|
|
|
pub dest_did: i64,
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-15 20:29:28 +08:00
|
|
|
#[post("/v1.0/mv_docs")]
|
2023-12-25 10:17:13 +08:00
|
|
|
async fn mv(
|
|
|
|
|
params: web::Json<MvParams>,
|
|
|
|
|
data: web::Data<AppState>
|
|
|
|
|
) -> Result<HttpResponse, AppError> {
|
2023-12-18 15:52:52 +08:00
|
|
|
Mutation::mv_doc_info(&data.conn, params.dest_did, ¶ms.dids).await?;
|
2023-12-15 20:29:28 +08:00
|
|
|
|
|
|
|
|
let json_response = JsonResponse {
|
|
|
|
|
code: 200,
|
|
|
|
|
err: "".to_owned(),
|
|
|
|
|
data: (),
|
|
|
|
|
};
|
|
|
|
|
|
2023-12-25 10:17:13 +08:00
|
|
|
Ok(
|
|
|
|
|
HttpResponse::Ok()
|
|
|
|
|
.content_type("application/json")
|
|
|
|
|
.body(serde_json::to_string(&json_response)?)
|
|
|
|
|
)
|
2023-12-19 19:28:01 +08:00
|
|
|
}
|
2023-12-22 17:57:27 +08:00
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
|
pub struct NewFoldParams {
|
|
|
|
|
pub uid: i64,
|
|
|
|
|
pub parent_id: i64,
|
2023-12-25 10:17:13 +08:00
|
|
|
pub name: String,
|
2023-12-22 17:57:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[post("/v1.0/new_folder")]
|
2023-12-25 10:17:13 +08:00
|
|
|
async fn new_folder(
|
|
|
|
|
params: web::Json<NewFoldParams>,
|
|
|
|
|
data: web::Data<AppState>
|
|
|
|
|
) -> Result<HttpResponse, AppError> {
|
2023-12-22 17:57:27 +08:00
|
|
|
let doc = Mutation::create_doc_info(&data.conn, Model {
|
2023-12-25 10:17:13 +08:00
|
|
|
did: Default::default(),
|
|
|
|
|
uid: params.uid,
|
2023-12-22 17:57:27 +08:00
|
|
|
doc_name: params.name.to_string(),
|
2023-12-25 10:17:13 +08:00
|
|
|
size: 0,
|
2023-12-22 17:57:27 +08:00
|
|
|
r#type: "folder".to_string(),
|
|
|
|
|
location: "".to_owned(),
|
|
|
|
|
created_at: now(),
|
|
|
|
|
updated_at: now(),
|
2023-12-25 10:17:13 +08:00
|
|
|
is_deleted: Default::default(),
|
2023-12-22 17:57:27 +08:00
|
|
|
}).await?;
|
|
|
|
|
let _ = Mutation::place_doc(&data.conn, params.parent_id, doc.did.unwrap()).await?;
|
|
|
|
|
|
|
|
|
|
Ok(HttpResponse::Ok().body("Folder created successfully"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
|
pub struct RenameParams {
|
|
|
|
|
pub uid: i64,
|
|
|
|
|
pub did: i64,
|
2023-12-25 10:17:13 +08:00
|
|
|
pub name: String,
|
2023-12-22 17:57:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[post("/v1.0/rename")]
|
2023-12-25 10:17:13 +08:00
|
|
|
async fn rename(
|
|
|
|
|
params: web::Json<RenameParams>,
|
|
|
|
|
data: web::Data<AppState>
|
|
|
|
|
) -> Result<HttpResponse, AppError> {
|
2023-12-22 17:57:27 +08:00
|
|
|
let docs = Query::find_doc_infos_by_name(&data.conn, params.uid, ¶ms.name, None).await?;
|
2023-12-25 10:17:13 +08:00
|
|
|
if docs.len() > 0 {
|
2023-12-22 17:57:27 +08:00
|
|
|
let json_response = JsonResponse {
|
|
|
|
|
code: 500,
|
|
|
|
|
err: "Name duplicated!".to_owned(),
|
|
|
|
|
data: (),
|
|
|
|
|
};
|
2023-12-25 10:17:13 +08:00
|
|
|
return Ok(
|
|
|
|
|
HttpResponse::Ok()
|
|
|
|
|
.content_type("application/json")
|
|
|
|
|
.body(serde_json::to_string(&json_response)?)
|
|
|
|
|
);
|
2023-12-22 17:57:27 +08:00
|
|
|
}
|
|
|
|
|
let doc = Mutation::rename(&data.conn, params.did, ¶ms.name).await?;
|
|
|
|
|
|
|
|
|
|
let json_response = JsonResponse {
|
|
|
|
|
code: 200,
|
|
|
|
|
err: "".to_owned(),
|
|
|
|
|
data: doc,
|
|
|
|
|
};
|
|
|
|
|
|
2023-12-25 10:17:13 +08:00
|
|
|
Ok(
|
|
|
|
|
HttpResponse::Ok()
|
|
|
|
|
.content_type("application/json")
|
|
|
|
|
.body(serde_json::to_string(&json_response)?)
|
|
|
|
|
)
|
2023-12-22 17:57:27 +08:00
|
|
|
}
|