File size: 4,706 Bytes
1eb186a 8a65ad8 d94c6df 6858ec5 8a65ad8 6858ec5 d94c6df 6858ec5 d94c6df 6858ec5 1eb186a d94c6df 6858ec5 8a65ad8 d94c6df 6858ec5 d94c6df 6858ec5 d94c6df 6858ec5 d94c6df 6858ec5 d94c6df 1eb186a 6858ec5 1eb186a 8a65ad8 6858ec5 8a65ad8 1eb186a 8a65ad8 6858ec5 8a65ad8 3fc700a 8a65ad8 6858ec5 8a65ad8 3fc700a 8a65ad8 6858ec5 8a65ad8 6858ec5 8a65ad8 1eb186a 6858ec5 1eb186a 6858ec5 1eb186a 6858ec5 |
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 144 145 146 147 148 149 150 |
use std::collections::HashMap;
use std::io::SeekFrom;
use std::ptr::null;
use actix_identity::Identity;
use actix_web::{ HttpResponse, post, web };
use chrono::{ FixedOffset, Utc };
use sea_orm::ActiveValue::NotSet;
use serde::{ Deserialize, Serialize };
use crate::api::JsonResponse;
use crate::AppState;
use crate::entity::{ doc_info, tag_info };
use crate::entity::user_info::Model;
use crate::errors::{ AppError, UserError };
use crate::service::user_info::Mutation;
use crate::service::user_info::Query;
fn now() -> chrono::DateTime<FixedOffset> {
Utc::now().with_timezone(&FixedOffset::east_opt(3600 * 8).unwrap())
}
pub(crate) fn create_auth_token(user: &Model) -> u64 {
use std::{ collections::hash_map::DefaultHasher, hash::{ Hash, Hasher } };
let mut hasher = DefaultHasher::new();
user.hash(&mut hasher);
hasher.finish()
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub(crate) struct LoginParams {
pub(crate) email: String,
pub(crate) password: String,
}
#[post("/v1.0/login")]
async fn login(
data: web::Data<AppState>,
identity: Identity,
input: web::Json<LoginParams>
) -> Result<HttpResponse, AppError> {
match Query::login(&data.conn, &input.email, &input.password).await? {
Some(user) => {
let _ = Mutation::update_login_status(user.uid, &data.conn).await?;
let token = create_auth_token(&user).to_string();
identity.remember(token.clone());
let json_response = JsonResponse {
code: 200,
err: "".to_owned(),
data: token.clone(),
};
Ok(
HttpResponse::Ok()
.content_type("application/json")
.append_header(("X-Auth-Token", token))
.body(serde_json::to_string(&json_response)?)
)
}
None => Err(UserError::LoginFailed.into()),
}
}
#[post("/v1.0/register")]
async fn register(
model: web::Json<Model>,
data: web::Data<AppState>
) -> Result<HttpResponse, AppError> {
let mut result = HashMap::new();
let u = Query::find_user_infos(&data.conn, &model.email).await?;
if let Some(_) = u {
let json_response = JsonResponse {
code: 500,
err: "Email registered!".to_owned(),
data: (),
};
return Ok(
HttpResponse::Ok()
.content_type("application/json")
.body(serde_json::to_string(&json_response)?)
);
}
let usr = Mutation::create_user(&data.conn, &model).await?;
result.insert("uid", usr.uid.clone().unwrap());
crate::service::doc_info::Mutation::create_doc_info(&data.conn, doc_info::Model {
did: Default::default(),
uid: usr.uid.clone().unwrap(),
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!["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)",
".*\\.(WAV|FLAC|APE|ALAC|WavPack|WV|MP3|AAC|Ogg|Vorbis|Opus)",
".*\\.(pdf|doc|ppt|yml|xml|htm|json|csv|txt|ini|xsl|wps|rtf|hlp)"
];
for i in 0..4 {
crate::service::tag_info::Mutation::create_tag(&data.conn, tag_info::Model {
tid: Default::default(),
uid: usr.uid.clone().unwrap(),
tag_name: tnm[i].to_owned(),
regx: tregx[i].to_owned(),
color: (i + 1).to_owned() as i16,
icon: (i + 1).to_owned() as i16,
folder_id: 0,
created_at: Default::default(),
updated_at: Default::default(),
}).await?;
}
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)?)
)
}
#[post("/v1.0/setting")]
async fn setting(
model: web::Json<Model>,
data: web::Data<AppState>
) -> Result<HttpResponse, AppError> {
let _ = Mutation::update_user_by_id(&data.conn, &model).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)?)
)
}
|