File size: 4,406 Bytes
b772fd3 249b27c b772fd3 249b27c b772fd3 d94c6df 249b27c b772fd3 d94c6df b772fd3 249b27c b772fd3 d94c6df b772fd3 d94c6df b772fd3 d94c6df b772fd3 249b27c b772fd3 249b27c b772fd3 249b27c b772fd3 249b27c b772fd3 249b27c b772fd3 d94c6df b772fd3 249b27c b772fd3 d94c6df b772fd3 d94c6df b772fd3 d94c6df b772fd3 d94c6df b772fd3 d94c6df 249b27c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
use std::collections::HashMap;
use std::io::Write;
use std::slice::Chunks;
//use actix_multipart::{Multipart, MultipartError, Field};
use actix_multipart_extract::{File, Multipart, MultipartForm};
use actix_web::{get, HttpResponse, post, web};
use actix_web::web::Bytes;
use chrono::Local;
use futures_util::StreamExt;
use sea_orm::DbConn;
use crate::api::JsonResponse;
use crate::AppState;
use crate::entity::doc_info::Model;
use crate::errors::AppError;
use crate::service::doc_info::{Mutation, Query};
use serde::Deserialize;
#[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")]
async fn list(params: web::Json<Params>, data: web::Data<AppState>) -> Result<HttpResponse, AppError> {
let docs = Query::find_doc_infos_by_params(&data.conn, params.into_inner())
.await?;
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")
.body(serde_json::to_string(&json_response)?))
}
#[derive(Deserialize, MultipartForm, Debug)]
pub struct UploadForm {
#[multipart(max_size = 512MB)]
file_field: File,
uid: i64,
did: i64
}
#[post("/v1.0/upload")]
async fn upload(payload: Multipart<UploadForm>, data: web::Data<AppState>) -> Result<HttpResponse, AppError> {
let uid = payload.uid;
async fn add_number_to_filename(file_name: String, conn:&DbConn, uid:i64) -> String {
let mut i = 0;
let mut new_file_name = file_name.to_string();
let arr: Vec<&str> = file_name.split(".").collect();
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.clone()).await.unwrap();
while docs.len()>0 {
i += 1;
new_file_name = format!("{}_{}.{}", preffix, i, suffix);
docs = Query::find_doc_infos_by_name(conn, uid, new_file_name.clone()).await.unwrap();
}
new_file_name
}
let fnm = add_number_to_filename(payload.file_field.name.clone(), &data.conn, uid).await;
std::fs::create_dir_all(format!("./upload/{}/", uid));
let filepath = format!("./upload/{}/{}-{}", payload.uid, payload.did, fnm.clone());
let mut f =std::fs::File::create(&filepath)?;
f.write(&payload.file_field.bytes)?;
let doc = Mutation::create_doc_info(&data.conn, Model {
did:Default::default(),
uid: uid,
doc_name: fnm,
size: payload.file_field.bytes.len() as i64,
kb_infos: Vec::new(),
kb_progress: 0.0,
kb_progress_msg: "".to_string(),
location: filepath,
r#type: "doc".to_string(),
created_at: Local::now().date_naive(),
updated_at: Local::now().date_naive(),
}).await?;
let _ = Mutation::place_doc(&data.conn, payload.did, doc.did.unwrap()).await?;
Ok(HttpResponse::Ok().body("File uploaded successfully"))
}
#[post("/v1.0/delete_docs")]
async fn delete(doc_ids: web::Json<Vec<i64>>, data: web::Data<AppState>) -> Result<HttpResponse, AppError> {
for doc_id in doc_ids.iter() {
let _ = Mutation::delete_doc_info(&data.conn, *doc_id).await?;
}
let json_response = JsonResponse {
code: 200,
err: "".to_owned(),
data: (),
};
Ok(HttpResponse::Ok()
.content_type("application/json")
.body(serde_json::to_string(&json_response)?))
}
#[post("/v1.0/mv_docs")]
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?;
let json_response = JsonResponse {
code: 200,
err: "".to_owned(),
data: (),
};
Ok(HttpResponse::Ok()
.content_type("application/json")
.body(serde_json::to_string(&json_response)?))
}
|