merge upstream
This commit is contained in:
commit
f11213f973
2 changed files with 41 additions and 92 deletions
|
|
@ -1,9 +1,9 @@
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::io::BufReader;
|
use std::io::BufReader;
|
||||||
use actix_multipart_extract::{File, Multipart, MultipartForm};
|
use actix_multipart_extract::{ File, Multipart, MultipartForm };
|
||||||
use actix_web::{HttpResponse, post, web};
|
use actix_web::{ HttpResponse, post, web };
|
||||||
use chrono::{Utc, FixedOffset};
|
use chrono::{ Utc, FixedOffset };
|
||||||
use minio::s3::args::{BucketExistsArgs, MakeBucketArgs, PutObjectArgs};
|
use minio::s3::args::{ BucketExistsArgs, MakeBucketArgs, PutObjectArgs };
|
||||||
use sea_orm::DbConn;
|
use sea_orm::DbConn;
|
||||||
use crate::api::JsonResponse;
|
use crate::api::JsonResponse;
|
||||||
use crate::AppState;
|
use crate::AppState;
|
||||||
|
|
@ -12,7 +12,6 @@ use crate::errors::AppError;
|
||||||
use crate::service::doc_info::{ Mutation, Query };
|
use crate::service::doc_info::{ Mutation, Query };
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
|
|
||||||
fn now() -> chrono::DateTime<FixedOffset> {
|
fn now() -> chrono::DateTime<FixedOffset> {
|
||||||
Utc::now().with_timezone(&FixedOffset::east_opt(3600 * 8).unwrap())
|
Utc::now().with_timezone(&FixedOffset::east_opt(3600 * 8).unwrap())
|
||||||
}
|
}
|
||||||
|
|
@ -72,63 +71,71 @@ async fn upload(
|
||||||
) -> Result<HttpResponse, AppError> {
|
) -> Result<HttpResponse, AppError> {
|
||||||
let uid = payload.uid;
|
let uid = payload.uid;
|
||||||
let file_name = payload.file_field.name.as_str();
|
let file_name = payload.file_field.name.as_str();
|
||||||
async fn add_number_to_filename(file_name: &str, conn:&DbConn, uid:i64, parent_id:i64) -> String {
|
async fn add_number_to_filename(
|
||||||
|
file_name: &str,
|
||||||
|
conn: &DbConn,
|
||||||
|
uid: i64,
|
||||||
|
parent_id: i64
|
||||||
|
) -> String {
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
let mut new_file_name = file_name.to_string();
|
let mut new_file_name = file_name.to_string();
|
||||||
let arr: Vec<&str> = file_name.split(".").collect();
|
let arr: Vec<&str> = file_name.split(".").collect();
|
||||||
let suffix = String::from(arr[arr.len()-1]);
|
let suffix = String::from(arr[arr.len() - 1]);
|
||||||
let preffix = arr[..arr.len()-1].join(".");
|
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();
|
let mut docs = Query::find_doc_infos_by_name(
|
||||||
while docs.len()>0 {
|
conn,
|
||||||
|
uid,
|
||||||
|
&new_file_name,
|
||||||
|
Some(parent_id)
|
||||||
|
).await.unwrap();
|
||||||
|
while docs.len() > 0 {
|
||||||
i += 1;
|
i += 1;
|
||||||
new_file_name = format!("{}_{}.{}", preffix, i, suffix);
|
new_file_name = format!("{}_{}.{}", preffix, i, suffix);
|
||||||
docs = Query::find_doc_infos_by_name(conn, uid, &new_file_name, Some(parent_id)).await.unwrap();
|
docs = Query::find_doc_infos_by_name(
|
||||||
|
conn,
|
||||||
|
uid,
|
||||||
|
&new_file_name,
|
||||||
|
Some(parent_id)
|
||||||
|
).await.unwrap();
|
||||||
}
|
}
|
||||||
new_file_name
|
new_file_name
|
||||||
}
|
}
|
||||||
let fnm = add_number_to_filename(file_name, &data.conn, uid, payload.did).await;
|
let fnm = add_number_to_filename(file_name, &data.conn, uid, payload.did).await;
|
||||||
|
|
||||||
let bucket_name = format!("{}-upload", payload.uid);
|
let bucket_name = format!("{}-upload", payload.uid);
|
||||||
let s3_client:&minio::s3::client::Client = &data.s3_client;
|
let s3_client: &minio::s3::client::Client = &data.s3_client;
|
||||||
let buckets_exists = s3_client
|
let buckets_exists = s3_client
|
||||||
.bucket_exists(&BucketExistsArgs::new(&bucket_name).unwrap())
|
.bucket_exists(&BucketExistsArgs::new(&bucket_name).unwrap()).await
|
||||||
.await
|
|
||||||
.unwrap();
|
.unwrap();
|
||||||
if !buckets_exists {
|
if !buckets_exists {
|
||||||
print!("Create bucket: {}", bucket_name.clone());
|
print!("Create bucket: {}", bucket_name.clone());
|
||||||
s3_client
|
s3_client.make_bucket(&MakeBucketArgs::new(&bucket_name).unwrap()).await.unwrap();
|
||||||
.make_bucket(&MakeBucketArgs::new(&bucket_name).unwrap())
|
} else {
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
print!("Existing bucket: {}", bucket_name.clone());
|
print!("Existing bucket: {}", bucket_name.clone());
|
||||||
}
|
}
|
||||||
|
|
||||||
let location = format!("/{}/{}", payload.did, fnm);
|
let location = format!("/{}/{}", payload.did, fnm);
|
||||||
print!("===>{}", location.clone());
|
print!("===>{}", location.clone());
|
||||||
s3_client
|
s3_client.put_object(
|
||||||
.put_object(
|
&mut PutObjectArgs::new(
|
||||||
&mut PutObjectArgs::new(
|
&bucket_name,
|
||||||
&bucket_name,
|
&location,
|
||||||
&location,
|
&mut BufReader::new(payload.file_field.bytes.as_slice()),
|
||||||
&mut BufReader::new(payload.file_field.bytes.as_slice()),
|
Some(payload.file_field.bytes.len()),
|
||||||
Some(payload.file_field.bytes.len()),
|
None
|
||||||
None,
|
)?
|
||||||
)?
|
).await?;
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
let doc = Mutation::create_doc_info(&data.conn, Model {
|
let doc = Mutation::create_doc_info(&data.conn, Model {
|
||||||
did:Default::default(),
|
did: Default::default(),
|
||||||
uid: uid,
|
uid: uid,
|
||||||
doc_name: fnm,
|
doc_name: fnm,
|
||||||
size: payload.file_field.bytes.len() as i64,
|
size: payload.file_field.bytes.len() as i64,
|
||||||
location,
|
location,
|
||||||
r#type: "doc".to_string(),
|
r#type: "doc".to_string(),
|
||||||
created_at: now(),
|
created_at: now(),
|
||||||
updated_at: now(),
|
updated_at: now(),
|
||||||
is_deleted:Default::default(),
|
is_deleted: Default::default(),
|
||||||
}).await?;
|
}).await?;
|
||||||
|
|
||||||
let _ = Mutation::place_doc(&data.conn, payload.did, doc.did.unwrap()).await?;
|
let _ = Mutation::place_doc(&data.conn, payload.did, doc.did.unwrap()).await?;
|
||||||
|
|
|
||||||
|
|
@ -1,58 +0,0 @@
|
||||||
use std::collections::HashMap;
|
|
||||||
use actix_web::{get, HttpResponse, post, web};
|
|
||||||
use actix_web::http::Error;
|
|
||||||
use crate::api::JsonResponse;
|
|
||||||
use crate::AppState;
|
|
||||||
use crate::entity::tag_info;
|
|
||||||
use crate::service::tag_info::{Mutation, Query};
|
|
||||||
|
|
||||||
#[post("/v1.0/create_tag")]
|
|
||||||
async fn create(model: web::Json<tag_info::Model>, data: web::Data<AppState>) -> Result<HttpResponse, Error> {
|
|
||||||
let model = Mutation::create_tag(&data.conn, model.into_inner()).await.unwrap();
|
|
||||||
|
|
||||||
let mut result = HashMap::new();
|
|
||||||
result.insert("tid", model.tid.unwrap());
|
|
||||||
|
|
||||||
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).unwrap()))
|
|
||||||
}
|
|
||||||
|
|
||||||
#[post("/v1.0/delete_tag")]
|
|
||||||
async fn delete(model: web::Json<tag_info::Model>, data: web::Data<AppState>) -> Result<HttpResponse, Error> {
|
|
||||||
let _ = Mutation::delete_tag(&data.conn, model.tid).await.unwrap();
|
|
||||||
|
|
||||||
let json_response = JsonResponse {
|
|
||||||
code: 200,
|
|
||||||
err: "".to_owned(),
|
|
||||||
data: (),
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(HttpResponse::Ok()
|
|
||||||
.content_type("application/json")
|
|
||||||
.body(serde_json::to_string(&json_response).unwrap()))
|
|
||||||
}
|
|
||||||
|
|
||||||
#[get("/v1.0/tags")]
|
|
||||||
async fn list(data: web::Data<AppState>) -> Result<HttpResponse, Error> {
|
|
||||||
let tags = Query::find_tag_infos(&data.conn).await.unwrap();
|
|
||||||
|
|
||||||
let mut result = HashMap::new();
|
|
||||||
result.insert("tags", tags);
|
|
||||||
|
|
||||||
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).unwrap()))
|
|
||||||
}
|
|
||||||
Loading…
Add table
Reference in a new issue