Skip to content

Commit

Permalink
Fix upload file metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
billyb2 committed Apr 10, 2024
1 parent f118a87 commit 954475e
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 41 deletions.
28 changes: 14 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion migrations/20240306015917_create_file_meta_table.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CREATE TABLE file_metadata (
id serial primary key unique not null,
id integer primary key autoincrement unique,
encrypted_metadata blob not null,
nonce blob not null,
user_id int not null
Expand Down
50 changes: 27 additions & 23 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use async_trait::async_trait;
use bfsp::{ChunkHash, ChunkID, ChunkMetadata, EncryptedFileMetadata, EncryptionNonce};
use futures::StreamExt;
use log::debug;
use sqlx::{QueryBuilder, Row, SqlitePool};
use sqlx::{Execute, QueryBuilder, Row, SqlitePool};
use thiserror::Error;

#[async_trait]
Expand Down Expand Up @@ -161,12 +161,14 @@ impl ChunkDatabase for SqliteDB {
enc_file_meta: EncryptedFileMetadata,
user_id: i64,
) -> Result<()> {
sqlx::query("insert into (encrypted_metadata, user_id, nonce) values (?, ?, ?)")
.bind(enc_file_meta.metadata)
.bind(user_id)
.bind(enc_file_meta.nonce)
.execute(&self.pool)
.await?;
sqlx::query(
"insert into file_metadata (encrypted_metadata, user_id, nonce) values (?, ?, ?)",
)
.bind(enc_file_meta.metadata)
.bind(user_id)
.bind(enc_file_meta.nonce)
.execute(&self.pool)
.await?;

Ok(())
}
Expand Down Expand Up @@ -215,24 +217,26 @@ impl ChunkDatabase for SqliteDB {
}
query.push(")");
}
debug!("{user_id}");
let query = query.build().bind(user_id);
let mut rows = query.fetch(&self.pool);

let mut file_meta = HashMap::new();

while let Some(row) = rows.next().await {
let row = row?;
let meta_id: i64 = row.get("id");
let enc_meta: Vec<u8> = row.get("encrypted_metadata");
let nonce: Vec<u8> = row.get("nonce");
file_meta.insert(
meta_id,
EncryptedFileMetadata {
nonce,
metadata: enc_meta,
},
);
}
let file_meta: HashMap<_, _> = query
.fetch_all(&self.pool)
.await?
.into_iter()
.map(|row| {
let meta_id: i64 = row.get("id");
let enc_meta: Vec<u8> = row.get("encrypted_metadata");
let nonce: Vec<u8> = row.get("nonce");
(
meta_id,
EncryptedFileMetadata {
nonce,
metadata: enc_meta,
},
)
})
.collect();

debug!("Found {} file metadata", file_meta.len());

Expand Down
13 changes: 10 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ async fn main() -> Result<()> {
let db = Arc::clone(&db);

tokio::task::spawn(async move {
info!("New connection from {addr:?}");
loop {
let action_len = if let Ok(len) = sock.read_u32_le().await {
len
Expand Down Expand Up @@ -206,8 +207,9 @@ async fn main() -> Result<()> {
}
.prepend_len();

debug!("Sent response");
debug!("Sending response");
sock.write_all(&resp).await.unwrap();
debug!("Sent response");
}
});
}
Expand Down Expand Up @@ -301,7 +303,7 @@ async fn handle_upload_chunk<D: ChunkDatabase>(
let mut authorizer = authorizer!(
r#"
check if email($email);
check if rights($rights), $rights.contains("upload");
check if rights($rights), $rights.contains("write");
allow if true;
deny if false;
"#
Expand Down Expand Up @@ -392,10 +394,11 @@ pub async fn handle_upload_metadata<D: ChunkDatabase>(
token: &Biscuit,
enc_file_meta: EncryptedFileMetadata,
) -> Result<(), UploadMetadataError> {
debug!("Uploading file metadata");
let mut authorizer = authorizer!(
r#"
check if user($user);
check if rights($rights), $rights.contains("delete");
check if rights($rights), $rights.contains("write");
allow if true;
deny if false;
"#
Expand All @@ -405,6 +408,8 @@ pub async fn handle_upload_metadata<D: ChunkDatabase>(
authorizer.authorize().unwrap();

let user_id = get_user_id(&mut authorizer).unwrap();
debug!("Uploading metadata for user {}", user_id);

chunk_db
.insert_file_meta(enc_file_meta, user_id)
.await
Expand Down Expand Up @@ -442,6 +447,7 @@ pub async fn handle_list_metadata<D: ChunkDatabase>(
token: &Biscuit,
meta_ids: Vec<i64>,
) -> Result<HashMap<i64, EncryptedFileMetadata>, UploadMetadataError> {
info!("Listing metadata");
let mut authorizer = authorizer!(
r#"
check if user($user);
Expand All @@ -457,6 +463,7 @@ pub async fn handle_list_metadata<D: ChunkDatabase>(
let meta_ids: HashSet<i64> = HashSet::from_iter(meta_ids.into_iter());

let user_id = get_user_id(&mut authorizer).unwrap();
info!("Listing metadata for user {}", user_id);
let meta = chunk_db.list_file_meta(meta_ids, user_id).await.unwrap();
Ok(meta)
}
Expand Down

0 comments on commit 954475e

Please sign in to comment.