Skip to content

Commit

Permalink
switch to uuid for user instead of string (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
thebino committed Oct 27, 2023
1 parent e20ee49 commit e3fda67
Show file tree
Hide file tree
Showing 13 changed files with 76 additions and 51 deletions.
9 changes: 3 additions & 6 deletions crates/common/src/auth/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

use std::fmt;
use time::OffsetDateTime;
use uuid::Uuid;

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct User {
Expand All @@ -43,12 +42,10 @@ impl fmt::Display for User {
}

impl User {
pub(crate) fn new(email: String) -> User {
pub(crate) fn new() -> User {
User {
uuid: Uuid::parse_str("808c78e4-34bc-486a-902f-929e8b146d20")
.unwrap()
.to_string(),
email,
uuid: "808c78e4-34bc-486a-902f-929e8b146d20".to_string(),
email: "[email protected]".to_string(),
password: Option::None,
lastname: Option::None,
firstname: Option::None,
Expand Down
2 changes: 1 addition & 1 deletion crates/common/src/http/extractors/optuser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ where
let _auth_token = header.to_str().ok();

// TODO: verify auth token
Some(Self(Some(User::new("[email protected]".to_string()))))
Some(Self(Some(User::new())))
})
.ok_or("".to_string())
}
Expand Down
2 changes: 1 addition & 1 deletion crates/common/src/http/extractors/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ where
.ok_or((StatusCode::UNAUTHORIZED, "Unauthorized"))?;

// TODO: get user for Authtoken
Ok(User::new("[email protected]".to_string()))
Ok(User::new())
// TODO: verify Token
// verify_auth_token(auth_header)
// .await
Expand Down
5 changes: 2 additions & 3 deletions crates/database/src/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,9 @@ impl Database for SqliteDatabase {

async fn create_user(&self, user: &User) -> Result<()> {
let query = "INSERT INTO users (uuid, email, password, lastname, firstname) VALUES ($1, $2, $3, $4, $5)";
let id = Uuid::new_v4().hyphenated().to_string();
info!("create new user with id `{}`.", id);
info!("create new user with id `{}`.", &user.uuid);
sqlx::query(query)
.bind(id)
.bind(&user.uuid)
.bind(&user.email)
.bind(&user.password)
.bind(&user.lastname)
Expand Down
2 changes: 1 addition & 1 deletion crates/media/src/api/routes/post_media.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub(crate) async fn post_media(
);

Ok((
StatusCode::OK,
StatusCode::CREATED,
Json(ResponseId {
id: uuid.hyphenated().to_string(),
}),
Expand Down
10 changes: 9 additions & 1 deletion crates/media/src/api/routes/post_media_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
use axum::extract::{Multipart, Path, State};
use axum::http::StatusCode;
use axum::response::{IntoResponse, Redirect};
use axum::Json;
use common::auth::user::User;
use tracing::{debug, info};
use uuid::Uuid;

use bytes::Bytes;

use crate::api::routes::post_media::ResponseId;
use crate::data::error::DataAccessError;
use crate::repository::MediaRepositoryState;

Expand Down Expand Up @@ -71,7 +73,13 @@ pub(crate) async fn post_media_id(
Ok(uuid) => {
debug!("reference added. uuid={}", uuid.hyphenated().to_string());

Ok(uuid.hyphenated().to_string().into_response())
Ok((
StatusCode::CREATED,
Json(ResponseId {
id: uuid.hyphenated().to_string(),
}),
)
.into_response())
}
Err(error) => match error {
DataAccessError::AlreadyExist(id) => {
Expand Down
27 changes: 21 additions & 6 deletions crates/media/src/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ use std::fs;
use std::path::Path;
use std::sync::Arc;
use time::OffsetDateTime;
use tracing::log::warn;
use tracing::{debug, info};
use tracing::{debug, error, info, warn};
use uuid::Uuid;

#[allow(dead_code)]
Expand Down Expand Up @@ -112,6 +111,7 @@ impl MediaRepositoryTrait for MediaRepository {
name: String,
date_taken: OffsetDateTime,
) -> Result<Uuid, DataAccessError> {
debug!("user_id: {}", user_id.hyphenated().to_string());
let db_result = &self
.database
.create_media_item(
Expand Down Expand Up @@ -143,6 +143,7 @@ impl MediaRepositoryTrait for MediaRepository {

info!("target {}", path.clone().to_str().unwrap().to_string());
debug!("got {} bytes to handle", bytes.len());
let size = bytes.len();

let file_result = tokio::fs::write(&path.join(&name), &bytes).await;
match file_result {
Expand All @@ -161,16 +162,30 @@ impl MediaRepositoryTrait for MediaRepository {
uuid: Uuid::new_v4().hyphenated().to_string(),
filepath: path.clone().to_str().unwrap().to_string(),
filename: name.to_string(),
size: 0u64,
size: size.try_into().unwrap(),
description: "",
last_modified: OffsetDateTime::now_utc(),
is_missing: false,
};
let _ = &self
let db_result = &self
.database
.add_reference(media_id, name.as_str(), &reference)
.add_reference(
user_id.hyphenated().to_string().as_str(),
media_id,
&reference,
)
.await;
Err(DataAccessError::OtherError)

match db_result {
Ok(uuid) => {
info!("added reference with id {}", uuid.clone());
Ok(Uuid::parse_str(uuid.as_str()).unwrap())
}
Err(e) => {
error!("Could not write reference to database! {:?}", e);
Err(DataAccessError::OtherError)
}
}
}
}

Expand Down
File renamed without changes.
35 changes: 35 additions & 0 deletions documentation/end-to-end/file_upload.hurl
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# create a new media item
POST http://127.0.0.1:7777/media
Authorization: FakeToken

[MultipartFormData]
name: DSC_1234
date_taken: 1985-04-12T23:20:50.52Z

HTTP 201

[Asserts]
jsonpath "$.id" != null

[Captures]
media_id: jsonpath "$.id"


# upload reference to media item
POST http://127.0.0.1:7777/media/{{media_id}}
Authorization: FakeToken

[MultipartFormData]
name: DSC_1234.NEF
file: file,DSC_1234.NEF;

HTTP 201


# check media item
GET http://127.0.0.1:7777/media/{{media_id}}
Authorization: FakeToken

HTTP 200
[Asserts]
jsonpath "$.{{media_id}}.references" count == 1
5 changes: 0 additions & 5 deletions documentation/http/get_media.hurl

This file was deleted.

12 changes: 0 additions & 12 deletions documentation/http/post_media.hurl

This file was deleted.

9 changes: 0 additions & 9 deletions documentation/http/post_media_id.hurl

This file was deleted.

9 changes: 3 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,13 @@ pub async fn start_server() -> Result<()> {
let users = db.get_users().await?;
if users.is_empty() {
info!("No user found, create a default admin user. Please check `data/credentials.txt` for details.");
let default_user = "photo@photos.network";
let default_user = "noreply@photos.network";
let default_pass = "unsecure";
let path = Path::new(DATA_PATH).join("credentials.txt");
let _ = fs::write(path, format!("{}\n{}", default_user, default_pass));
// let mut output = File::create(path)?;
// let line = "hello";
// write!(output, "{}\n{}", default_user, default_pass);

let user = User {
uuid: "".to_string(),
uuid: "808c78e4-34bc-486a-902f-929e8b146d20".to_string(),
email: default_user.to_string(),
password: Some(default_pass.to_string()),
lastname: Some("Admin".to_string()),
Expand Down Expand Up @@ -260,7 +257,7 @@ async fn status() -> Json<Status> {

// TODO: print loaded plugins from appState
let status = Status {
message: String::from("API running"),
message: String::from("API running."),
};
Json(status)
}
Expand Down

0 comments on commit e3fda67

Please sign in to comment.