build dialog server; add thumbnail to docinfo; (#17)
This commit is contained in:
@@ -11,6 +11,7 @@ use crate::entity::doc_info::Model;
|
||||
use crate::errors::AppError;
|
||||
use crate::service::doc_info::{ Mutation, Query };
|
||||
use serde::Deserialize;
|
||||
use regex::Regex;
|
||||
|
||||
fn now() -> chrono::DateTime<FixedOffset> {
|
||||
Utc::now().with_timezone(&FixedOffset::east_opt(3600 * 8).unwrap())
|
||||
@@ -64,6 +65,41 @@ pub struct UploadForm {
|
||||
did: i64,
|
||||
}
|
||||
|
||||
fn file_type(filename: &String) -> String {
|
||||
let fnm = filename.to_lowercase();
|
||||
if
|
||||
let Some(_) = Regex::new(r"\.(mpg|mpeg|avi|rm|rmvb|mov|wmv|asf|dat|asx|wvx|mpe|mpa)$")
|
||||
.unwrap()
|
||||
.captures(&fnm)
|
||||
{
|
||||
return "Video".to_owned();
|
||||
}
|
||||
if
|
||||
let Some(_) = Regex::new(
|
||||
r"\.(jpg|jpeg|png|tif|gif|pcx|tga|exif|fpx|svg|psd|cdr|pcd|dxf|ufo|eps|ai|raw|WMF|webp|avif|apng)$"
|
||||
)
|
||||
.unwrap()
|
||||
.captures(&fnm)
|
||||
{
|
||||
return "Picture".to_owned();
|
||||
}
|
||||
if
|
||||
let Some(_) = Regex::new(r"\.(WAV|FLAC|APE|ALAC|WavPack|WV|MP3|AAC|Ogg|Vorbis|Opus)$")
|
||||
.unwrap()
|
||||
.captures(&fnm)
|
||||
{
|
||||
return "Music".to_owned();
|
||||
}
|
||||
if
|
||||
let Some(_) = Regex::new(r"\.(pdf|doc|ppt|yml|xml|htm|json|csv|txt|ini|xsl|wps|rtf|hlp)$")
|
||||
.unwrap()
|
||||
.captures(&fnm)
|
||||
{
|
||||
return "Document".to_owned();
|
||||
}
|
||||
"Other".to_owned()
|
||||
}
|
||||
|
||||
#[post("/v1.0/upload")]
|
||||
async fn upload(
|
||||
payload: Multipart<UploadForm>,
|
||||
@@ -114,7 +150,13 @@ async fn upload(
|
||||
print!("Existing bucket: {}", bucket_name.clone());
|
||||
}
|
||||
|
||||
let location = format!("/{}/{}", payload.did, fnm);
|
||||
let location = format!("/{}/{}", payload.did, fnm)
|
||||
.as_bytes()
|
||||
.to_vec()
|
||||
.iter()
|
||||
.map(|b| format!("{:02x}", b).to_string())
|
||||
.collect::<Vec<String>>()
|
||||
.join("");
|
||||
print!("===>{}", location.clone());
|
||||
s3_client.put_object(
|
||||
&mut PutObjectArgs::new(
|
||||
@@ -129,10 +171,11 @@ async fn upload(
|
||||
let doc = Mutation::create_doc_info(&data.conn, Model {
|
||||
did: Default::default(),
|
||||
uid: uid,
|
||||
doc_name: fnm,
|
||||
doc_name: fnm.clone(),
|
||||
size: payload.file_field.bytes.len() as i64,
|
||||
location,
|
||||
r#type: "doc".to_string(),
|
||||
r#type: file_type(&fnm),
|
||||
thumbnail_base64: Default::default(),
|
||||
created_at: now(),
|
||||
updated_at: now(),
|
||||
is_deleted: Default::default(),
|
||||
@@ -214,6 +257,7 @@ async fn new_folder(
|
||||
size: 0,
|
||||
r#type: "folder".to_string(),
|
||||
location: "".to_owned(),
|
||||
thumbnail_base64: Default::default(),
|
||||
created_at: now(),
|
||||
updated_at: now(),
|
||||
is_deleted: Default::default(),
|
||||
|
||||
@@ -90,12 +90,13 @@ async fn register(
|
||||
doc_name: "/".into(),
|
||||
size: 0,
|
||||
location: "".into(),
|
||||
thumbnail_base64: "".into(),
|
||||
r#type: "folder".to_string(),
|
||||
created_at: now(),
|
||||
updated_at: now(),
|
||||
is_deleted: Default::default(),
|
||||
}).await?;
|
||||
let tnm = vec!["视频", "图片", "音乐", "文档"];
|
||||
let tnm = vec!["Video", "Picture", "Music", "Document"];
|
||||
let tregx = vec![
|
||||
".*\\.(mpg|mpeg|avi|rm|rmvb|mov|wmv|asf|dat|asx|wvx|mpe|mpa)",
|
||||
".*\\.(png|tif|gif|pcx|tga|exif|fpx|svg|psd|cdr|pcd|dxf|ufo|eps|ai|raw|WMF|webp|avif|apng)",
|
||||
|
||||
@@ -17,6 +17,8 @@ pub struct Model {
|
||||
#[serde(skip_deserializing)]
|
||||
pub location: String,
|
||||
#[serde(skip_deserializing)]
|
||||
pub thumbnail_base64: String,
|
||||
#[serde(skip_deserializing)]
|
||||
pub created_at: DateTime<FixedOffset>,
|
||||
#[serde(skip_deserializing)]
|
||||
pub updated_at: DateTime<FixedOffset>,
|
||||
|
||||
@@ -9,28 +9,28 @@ pub struct Model {
|
||||
#[sea_orm(index)]
|
||||
pub tag_id: i64,
|
||||
#[sea_orm(index)]
|
||||
pub uid: i64,
|
||||
pub did: i64,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, EnumIter)]
|
||||
pub enum Relation {
|
||||
DocInfo,
|
||||
Tag,
|
||||
DocInfo,
|
||||
}
|
||||
|
||||
impl RelationTrait for Relation {
|
||||
fn def(&self) -> sea_orm::RelationDef {
|
||||
match self {
|
||||
Self::DocInfo =>
|
||||
Entity::belongs_to(super::doc_info::Entity)
|
||||
.from(Column::Uid)
|
||||
.to(super::doc_info::Column::Uid)
|
||||
.into(),
|
||||
Self::Tag =>
|
||||
Entity::belongs_to(super::tag_info::Entity)
|
||||
.from(Column::TagId)
|
||||
.to(super::tag_info::Column::Tid)
|
||||
.into(),
|
||||
Self::DocInfo =>
|
||||
Entity::belongs_to(super::doc_info::Entity)
|
||||
.from(Column::Did)
|
||||
.to(super::doc_info::Column::Did)
|
||||
.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -163,7 +163,7 @@ impl Query {
|
||||
);
|
||||
}
|
||||
if tag.regx.len() > 0 {
|
||||
cond.push_str(&format!(" and doc_name ~ '{}'", tag.regx));
|
||||
cond.push_str(&format!(" and (type='{}' or doc_name ~ '{}') ", tag.tag_name, tag.regx));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -254,6 +254,7 @@ impl Mutation {
|
||||
size: Set(form_data.size.to_owned()),
|
||||
r#type: Set(form_data.r#type.to_owned()),
|
||||
location: Set(form_data.location.to_owned()),
|
||||
thumbnail_base64: Default::default(),
|
||||
created_at: Set(form_data.created_at.to_owned()),
|
||||
updated_at: Set(form_data.updated_at.to_owned()),
|
||||
is_deleted: Default::default(),
|
||||
@@ -277,6 +278,7 @@ impl Mutation {
|
||||
size: Set(form_data.size.to_owned()),
|
||||
r#type: Set(form_data.r#type.to_owned()),
|
||||
location: Set(form_data.location.to_owned()),
|
||||
thumbnail_base64: doc_info.thumbnail_base64,
|
||||
created_at: doc_info.created_at,
|
||||
updated_at: Set(now()),
|
||||
is_deleted: Default::default(),
|
||||
|
||||
Reference in New Issue
Block a user