You've already forked AstralRinth
forked from didirus/AstralRinth
Switch to Postgres (#39)
* WIP Switch to Postgres * feat(postgres): more work on porting to postgres, now compiles * feat(docker-compose): Changed the docker-compose.yml file to use postgres. * Update docker, documentation, gh actions... * Remove bson dependency * Remove bson import * feat: move mock filehost to trait rather than cargo feature * feat(postgres): transactions for mod creation, multipart refactor * fix: Add Cargo.lock so that sqlx functions * Update sqlx offline build data * fix: Use SQLX_OFFLINE to force sqlx into offline mode for CI * Default release channels * feat(postgres): refactor database models to fit postgres models * fix: Fix sqlx prepare, fix double allocation in indexing * Add dockerfile (#40) Co-authored-by: Charalampos Fanoulis <charalampos.fanoulis@gmail.com> Co-authored-by: Aeledfyr <aeledfyr@gmail.com> Co-authored-by: redblueflame <contact@redblueflame.com> Co-authored-by: Jai A <jai.a@tuta.io> Co-authored-by: Valentin Ricard <redblueflame1@gmail.Com> Co-authored-by: Charalampos Fanoulis <charalampos.fanoulis@gmail.com>
This commit is contained in:
135
src/file_hosting/backblaze.rs
Normal file
135
src/file_hosting/backblaze.rs
Normal file
@@ -0,0 +1,135 @@
|
||||
use super::{DeleteFileData, FileHost, FileHostingError, UploadFileData};
|
||||
use async_trait::async_trait;
|
||||
|
||||
mod authorization;
|
||||
mod delete;
|
||||
mod upload;
|
||||
|
||||
pub struct BackblazeHost {
|
||||
upload_url_data: authorization::UploadUrlData,
|
||||
authorization_data: authorization::AuthorizationData,
|
||||
}
|
||||
|
||||
impl BackblazeHost {
|
||||
pub async fn new(key_id: &str, key: &str, bucket_id: &str) -> Self {
|
||||
let authorization_data = authorization::authorize_account(key_id, key).await.unwrap();
|
||||
let upload_url_data = authorization::get_upload_url(&authorization_data, bucket_id)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
BackblazeHost {
|
||||
authorization_data,
|
||||
upload_url_data,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl FileHost for BackblazeHost {
|
||||
async fn upload_file(
|
||||
&self,
|
||||
content_type: &str,
|
||||
file_name: &str,
|
||||
file_bytes: Vec<u8>,
|
||||
) -> Result<UploadFileData, FileHostingError> {
|
||||
let upload_data =
|
||||
upload::upload_file(&self.upload_url_data, content_type, file_name, file_bytes).await?;
|
||||
Ok(UploadFileData {
|
||||
file_id: upload_data.file_id,
|
||||
file_name: upload_data.file_name,
|
||||
content_length: upload_data.content_length,
|
||||
content_sha1: upload_data.content_sha1,
|
||||
content_md5: upload_data.content_md5,
|
||||
content_type: upload_data.content_type,
|
||||
upload_timestamp: upload_data.upload_timestamp,
|
||||
})
|
||||
}
|
||||
|
||||
/*
|
||||
async fn upload_file_streaming(
|
||||
&self,
|
||||
content_type: &str,
|
||||
file_name: &str,
|
||||
stream: reqwest::Body
|
||||
) -> Result<UploadFileData, FileHostingError> {
|
||||
use futures::stream::StreamExt;
|
||||
|
||||
let mut data = Vec::new();
|
||||
while let Some(chunk) = stream.next().await {
|
||||
data.extend_from_slice(&chunk.map_err(|e| FileHostingError::Other(e))?);
|
||||
}
|
||||
self.upload_file(content_type, file_name, data).await
|
||||
}
|
||||
*/
|
||||
|
||||
async fn delete_file_version(
|
||||
&self,
|
||||
file_id: &str,
|
||||
file_name: &str,
|
||||
) -> Result<DeleteFileData, FileHostingError> {
|
||||
let delete_data =
|
||||
delete::delete_file_version(&self.authorization_data, file_id, file_name).await?;
|
||||
Ok(DeleteFileData {
|
||||
file_id: delete_data.file_id,
|
||||
file_name: delete_data.file_name,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use authorization::*;
|
||||
use delete::*;
|
||||
use upload::*;
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_authorization() {
|
||||
println!("{}", dotenv::var("BACKBLAZE_BUCKET_ID").unwrap());
|
||||
let authorization_data = authorize_account(
|
||||
&dotenv::var("BACKBLAZE_KEY_ID").unwrap(),
|
||||
&dotenv::var("BACKBLAZE_KEY").unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
get_upload_url(
|
||||
&authorization_data,
|
||||
&dotenv::var("BACKBLAZE_BUCKET_ID").unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_file_management() {
|
||||
let authorization_data = authorize_account(
|
||||
&dotenv::var("BACKBLAZE_KEY_ID").unwrap(),
|
||||
&dotenv::var("BACKBLAZE_KEY").unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
let upload_url_data = get_upload_url(
|
||||
&authorization_data,
|
||||
&dotenv::var("BACKBLAZE_BUCKET_ID").unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
let upload_data = upload_file(
|
||||
&upload_url_data,
|
||||
"text/plain",
|
||||
"test.txt",
|
||||
"test file".to_string().into_bytes(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
delete_file_version(
|
||||
&authorization_data,
|
||||
&upload_data.file_id,
|
||||
&upload_data.file_name,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user