2023-12-15 20:29:28 +08:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
use actix_multipart::Multipart;
|
|
|
|
|
use actix_web::{get, HttpResponse, post, web};
|
|
|
|
|
use chrono::Local;
|
2023-12-18 15:52:52 +08:00
|
|
|
use futures_util::StreamExt;
|
2023-12-15 20:29:28 +08:00
|
|
|
use serde::Deserialize;
|
|
|
|
|
use std::io::Write;
|
|
|
|
|
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-15 20:29:28 +08:00
|
|
|
use crate::service::doc_info::{Mutation, Query};
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
|
pub struct Params {
|
|
|
|
|
pub uid: i64,
|
|
|
|
|
pub filter: FilterParams,
|
|
|
|
|
pub sortby: String,
|
|
|
|
|
pub page: u64,
|
|
|
|
|
pub per_page: u64,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[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>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
|
pub struct MvParams {
|
|
|
|
|
pub dids: Vec<i64>,
|
|
|
|
|
pub dest_did: i64,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[get("/v1.0/docs")]
|
2023-12-18 15:52:52 +08:00
|
|
|
async fn list(params: web::Json<Params>, data: web::Data<AppState>) -> Result<HttpResponse, AppError> {
|
2023-12-15 20:29:28 +08:00
|
|
|
let docs = Query::find_doc_infos_by_params(&data.conn, params.into_inner())
|
2023-12-18 15:52:52 +08:00
|
|
|
.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,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Ok(HttpResponse::Ok()
|
|
|
|
|
.content_type("application/json")
|
2023-12-18 15:52:52 +08:00
|
|
|
.body(serde_json::to_string(&json_response)?))
|
2023-12-15 20:29:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[post("/v1.0/upload")]
|
2023-12-18 15:52:52 +08:00
|
|
|
async fn upload(mut payload: Multipart, filename: web::Data<String>, did: web::Data<i64>, uid: web::Data<i64>, data: web::Data<AppState>) -> Result<HttpResponse, AppError> {
|
2023-12-15 20:29:28 +08:00
|
|
|
let mut size = 0;
|
|
|
|
|
|
|
|
|
|
while let Some(item) = payload.next().await {
|
|
|
|
|
let mut field = item.unwrap();
|
|
|
|
|
|
|
|
|
|
let filepath = format!("./uploads/{}", filename.as_str());
|
|
|
|
|
|
|
|
|
|
let mut file = web::block(|| std::fs::File::create(filepath))
|
|
|
|
|
.await
|
2023-12-18 15:52:52 +08:00
|
|
|
.unwrap()?;
|
2023-12-15 20:29:28 +08:00
|
|
|
|
|
|
|
|
while let Some(chunk) = field.next().await {
|
|
|
|
|
let data = chunk.unwrap();
|
|
|
|
|
size += data.len() as u64;
|
|
|
|
|
file = web::block(move || file.write_all(&data).map(|_| file))
|
|
|
|
|
.await
|
2023-12-18 15:52:52 +08:00
|
|
|
.unwrap()?;
|
2023-12-15 20:29:28 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let _ = Mutation::create_doc_info(&data.conn, Model {
|
|
|
|
|
did: *did.into_inner(),
|
|
|
|
|
uid: *uid.into_inner(),
|
|
|
|
|
doc_name: filename.to_string(),
|
|
|
|
|
size,
|
|
|
|
|
kb_infos: Vec::new(),
|
|
|
|
|
kb_progress: 0.0,
|
|
|
|
|
location: "".to_string(),
|
|
|
|
|
r#type: "".to_string(),
|
|
|
|
|
created_at: Local::now().date_naive(),
|
|
|
|
|
updated_at: Local::now().date_naive(),
|
2023-12-18 15:52:52 +08:00
|
|
|
}).await?;
|
2023-12-15 20:29:28 +08:00
|
|
|
|
|
|
|
|
Ok(HttpResponse::Ok().body("File uploaded successfully"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[post("/v1.0/delete_docs")]
|
2023-12-18 15:52:52 +08:00
|
|
|
async fn delete(doc_ids: web::Json<Vec<i64>>, data: web::Data<AppState>) -> Result<HttpResponse, AppError> {
|
2023-12-15 20:29:28 +08:00
|
|
|
for doc_id in doc_ids.iter() {
|
2023-12-18 15:52:52 +08:00
|
|
|
let _ = Mutation::delete_doc_info(&data.conn, *doc_id).await?;
|
2023-12-15 20:29:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let json_response = JsonResponse {
|
|
|
|
|
code: 200,
|
|
|
|
|
err: "".to_owned(),
|
|
|
|
|
data: (),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Ok(HttpResponse::Ok()
|
|
|
|
|
.content_type("application/json")
|
2023-12-18 15:52:52 +08:00
|
|
|
.body(serde_json::to_string(&json_response)?))
|
2023-12-15 20:29:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[post("/v1.0/mv_docs")]
|
2023-12-18 15:52:52 +08:00
|
|
|
async fn mv(params: web::Json<MvParams>, data: web::Data<AppState>) -> Result<HttpResponse, AppError> {
|
|
|
|
|
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: (),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Ok(HttpResponse::Ok()
|
|
|
|
|
.content_type("application/json")
|
2023-12-18 15:52:52 +08:00
|
|
|
.body(serde_json::to_string(&json_response)?))
|
2023-12-15 20:29:28 +08:00
|
|
|
}
|