Skip to content

Commit

Permalink
add UpdateFileMetadata api
Browse files Browse the repository at this point in the history
also made sure to check for uploading or modifying specific files in the
UploadFileMetadata API. This is probably worthless, but on the 1% chance
that we bind a token to a file we're also uploading(?), then this is necessary.
  • Loading branch information
billyb2 committed Oct 10, 2024
1 parent 73e2c11 commit c8d2b4b
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 4 deletions.
2 changes: 1 addition & 1 deletion 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 Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ COPY --from=builder-sqlx /build/bin/sqlx /usr/bin/sqlx
COPY migrations /app/migrations
COPY fly-airship-client ./fly-airship-client

CMD ["./fly-airship-client", "--", "/app/bin/file_server"]
CMD ["/app/bin/file_server"]
39 changes: 37 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ use bfsp::{
file_server_message::Message::{
ChunksUploadedQuery, DeleteChunksQuery, DeleteFileMetadataQuery, DownloadChunkQuery,
DownloadFileMetadataQuery, GetUsageQuery, ListChunkMetadataQuery, ListFileMetadataQuery,
UploadChunk, UploadFileMetadata,
UpdateFileMetadata, UploadChunk, UploadFileMetadata,
},
ChunkID, ChunkMetadata, ChunksUploadedQueryResp, DownloadChunkResp, FileServerMessage, Message,
};
Expand Down Expand Up @@ -497,6 +497,13 @@ pub async fn handle_message<M: MetaDB + 'static, C: ChunkDB + 'static>(
.encode_to_vec(),
Err(_) => todo!(),
},
UpdateFileMetadata(query) => {
let enc_meta = query.encrypted_file_metadata.unwrap();
match handle_update_file_metadata(meta_db.as_ref(), &token, enc_meta).await {
Ok(_) => bfsp::UpdateFileMetadataResp { err: None }.encode_to_vec(),
Err(_) => todo!(),
}
}
_ => todo!(),
}
.prepend_len())
Expand Down Expand Up @@ -660,7 +667,7 @@ pub async fn handle_upload_file_metadata<D: MetaDB>(
token: &Biscuit,
enc_file_meta: EncryptedFileMetadata,
) -> Result<(), UploadMetadataError> {
let user_id = authorize(Right::Write, token, Vec::new(), meta_db)
let user_id = authorize(Right::Write, token, vec![enc_file_meta.id.clone()], meta_db)
.await
.unwrap();

Expand All @@ -682,6 +689,34 @@ pub async fn handle_upload_file_metadata<D: MetaDB>(
Ok(())
}

#[tracing::instrument(err, skip(token, meta_db, enc_file_meta))]
pub async fn handle_update_file_metadata<D: MetaDB>(
meta_db: &D,
token: &Biscuit,
enc_file_meta: EncryptedFileMetadata,
) -> Result<(), UploadMetadataError> {
let user_id = authorize(Right::Write, token, vec![enc_file_meta.id.clone()], meta_db)
.await
.unwrap();

let storage_usages = meta_db.total_usages(&[user_id]).await.unwrap();
let storage_usage = *storage_usages.get(&user_id).unwrap();

let storage_caps = meta_db.storage_caps(&[user_id]).await.unwrap();
let storage_cap = *storage_caps.get(&user_id).unwrap();

if storage_usage + enc_file_meta.metadata.len() as u64 > storage_cap {
todo!("Deny uploads that exceed storage cap");
}

meta_db
.update_file_meta(enc_file_meta, user_id)
.await
.unwrap();

Ok(())
}

#[tracing::instrument(err, skip(token, meta_db))]
pub async fn handle_download_file_metadata<D: MetaDB>(
meta_db: &D,
Expand Down
24 changes: 24 additions & 0 deletions src/meta_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ pub trait MetaDB: Sized + Send + Sync + std::fmt::Debug {
enc_metadata: EncryptedFileMetadata,
user_id: i64,
) -> impl Future<Output = Result<()>> + Send;
fn update_file_meta(
&self,
enc_metadata: EncryptedFileMetadata,
user_id: i64,
) -> impl Future<Output = Result<()>> + Send;

fn get_file_meta(
&self,
meta_id: String,
Expand Down Expand Up @@ -297,6 +303,24 @@ impl MetaDB for PostgresMetaDB {
Ok(())
}

#[tracing::instrument(err, skip(enc_file_meta))]
async fn update_file_meta(
&self,
enc_file_meta: EncryptedFileMetadata,
user_id: i64,
) -> Result<()> {
sqlx::query(
"update file_metadata set id = $1, encrypted_metadata = $2, user_id = $3 where id = $1",
)
.bind(enc_file_meta.id)
.bind(enc_file_meta.metadata)
.bind(user_id)
.execute(&self.pool)
.await?;

Ok(())
}

#[tracing::instrument(err)]
async fn get_file_meta(
&self,
Expand Down

0 comments on commit c8d2b4b

Please sign in to comment.