Return token on upload (required by JM Web)

This commit is contained in:
Timo Ley 2022-01-10 19:07:57 +01:00
parent b191626ad9
commit 6e51ece84c
4 changed files with 4 additions and 5 deletions

View file

@ -82,6 +82,7 @@ pub struct UploadResponse {
pub status: i32, pub status: i32,
pub error: Option<String>, pub error: Option<String>,
pub files: Option<Vec<String>>, pub files: Option<Vec<String>>,
pub token: String,
} }
#[derive(Serialize)] #[derive(Serialize)]

View file

@ -130,7 +130,7 @@ async fn upload(
let token = token.ok_or_else(|| APIError::Unauthorized("Missing token".to_string()))?; let token = token.ok_or_else(|| APIError::Unauthorized("Missing token".to_string()))?;
let category = category.ok_or_else(|| APIError::BadRequest("Missing category".to_string()))?; let category = category.ok_or_else(|| APIError::BadRequest("Missing category".to_string()))?;
let user = User::check_token(token, &db_pool) let user = User::check_token(&token, &db_pool)
.await? .await?
.ok_or_else(|| APIError::Forbidden("token not existing".to_string()))?; .ok_or_else(|| APIError::Forbidden("token not existing".to_string()))?;
let total = (user.dayuploads as isize) + (files.len() as isize); let total = (user.dayuploads as isize) + (files.len() as isize);
@ -167,12 +167,11 @@ async fn upload(
status: 201, status: 201,
error: None, error: None,
files: Some(links), files: Some(links),
token,
}), }),
)) ))
} }
//TODO: Implement upload endpoint
pub fn routes() -> Router<BoxRoute> { pub fn routes() -> Router<BoxRoute> {
Router::new() Router::new()
.route("/meme", get(meme)) .route("/meme", get(meme))

View file

@ -157,7 +157,7 @@ impl User {
Ok(q) Ok(q)
} }
pub async fn check_token(token: String, pool: &MySqlPool) -> Result<Option<Self>> { pub async fn check_token(token: &String, pool: &MySqlPool) -> Result<Option<Self>> {
let q: Option<User> = sqlx::query("SELECT id, name, MD5(token) AS hash, uploads FROM (SELECT id, name, IFNULL(count.uploads, 0) AS uploads FROM users LEFT JOIN (SELECT user, COUNT(*) AS uploads FROM memes WHERE DATE(timestamp) = CURDATE() GROUP BY (user)) AS count ON users.id = count.user) AS users, token WHERE users.id = token.uid AND token = ?") let q: Option<User> = sqlx::query("SELECT id, name, MD5(token) AS hash, uploads FROM (SELECT id, name, IFNULL(count.uploads, 0) AS uploads FROM users LEFT JOIN (SELECT user, COUNT(*) AS uploads FROM memes WHERE DATE(timestamp) = CURDATE() GROUP BY (user)) AS count ON users.id = count.user) AS users, token WHERE users.id = token.uid AND token = ?")
.bind(token) .bind(token)
.map(|row: MySqlRow| Self { .map(|row: MySqlRow| Self {

View file

@ -1 +0,0 @@