Migrate file storage to S3
This commit is contained in:
parent
f6897c30ac
commit
bb0c06faf4
10 changed files with 181 additions and 165 deletions
|
|
@ -4,6 +4,8 @@ use rocket::{Data, State};
|
|||
use rocket::response::{Response, Responder};
|
||||
use crate::Pool;
|
||||
use crate::database::get_wup_task_file_by_data_id;
|
||||
use std::env;
|
||||
use aws_sdk_s3::Client as S3Client;
|
||||
|
||||
#[derive(Responder)]
|
||||
pub struct DataResponder<T> {
|
||||
|
|
@ -27,8 +29,10 @@ impl<'r, 'o: 'r, T: Responder<'r, 'o>> DataResponder<T> {
|
|||
}
|
||||
|
||||
#[rocket::get("/p01/data/1/<boss_app_id>/<data_id>/<file_hash>")]
|
||||
pub async fn data(pool: &State<Pool>, boss_app_id: String, data_id: i64, file_hash: String) -> Result<DataResponder<Vec<u8>>, Status> {
|
||||
pub async fn data(pool: &State<Pool>, s3: &State<S3Client>,boss_app_id: String, data_id: i64, file_hash: String) -> Result<DataResponder<Vec<u8>>, Status> {
|
||||
let pool = pool.inner();
|
||||
let s3 = s3.inner();
|
||||
let bucket_name = env::var("S3_BUCKET_NAME").unwrap_or_else(|_| "bossdata".to_string());
|
||||
|
||||
let file_wup = get_wup_task_file_by_data_id(pool, data_id).await;
|
||||
|
||||
|
|
@ -37,20 +41,25 @@ pub async fn data(pool: &State<Pool>, boss_app_id: String, data_id: i64, file_ha
|
|||
None => return Err(Status::NotFound),
|
||||
};
|
||||
|
||||
if file_wup.hash != file_hash || !file_wup.boss_app_ids.contains(&boss_app_id) { return Err(Status::NotFound); }
|
||||
if file_wup.hash != file_hash || !file_wup.boss_app_ids.contains(&boss_app_id) {
|
||||
return Err(Status::NotFound);
|
||||
}
|
||||
|
||||
let file = sqlx::query_scalar!(
|
||||
"SELECT data FROM files WHERE key = $1",
|
||||
file_wup.file_key,
|
||||
)
|
||||
.fetch_optional(pool)
|
||||
.await;
|
||||
let s3_output = s3.get_object()
|
||||
.bucket(bucket_name)
|
||||
.key(&file_wup.file_key)
|
||||
.send()
|
||||
.await;
|
||||
|
||||
let file = match file {
|
||||
Ok(Some(file)) => { file },
|
||||
Ok(None) => return Err(Status::NotFound),
|
||||
let object = match s3_output {
|
||||
Ok(output) => output,
|
||||
Err(_) => return Err(Status::NotFound),
|
||||
};
|
||||
|
||||
Ok(DataResponder::new(file, file_wup.size.to_string()))
|
||||
let data = object.body.collect().await
|
||||
.map_err(|_| Status::InternalServerError)?
|
||||
.into_bytes()
|
||||
.to_vec();
|
||||
|
||||
Ok(DataResponder::new(data, file_wup.size.to_string()))
|
||||
}
|
||||
|
|
@ -10,6 +10,8 @@ use crate::Pool;
|
|||
// TODO:
|
||||
// - Use database for policy lists for easier modification if needed
|
||||
|
||||
// Do we really need to though? Policylists are usually static and if they need updating a commit is warranted anyways
|
||||
|
||||
#[derive(Serialize)]
|
||||
enum TaskLevel {
|
||||
STOPPED,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue