You've already forked AstralRinth
forked from didirus/AstralRinth
move to monorepo dir
This commit is contained in:
95
apps/labrinth/src/file_hosting/backblaze.rs
Normal file
95
apps/labrinth/src/file_hosting/backblaze.rs
Normal file
@@ -0,0 +1,95 @@
|
||||
use super::{DeleteFileData, FileHost, FileHostingError, UploadFileData};
|
||||
use async_trait::async_trait;
|
||||
use bytes::Bytes;
|
||||
use reqwest::Response;
|
||||
use serde::Deserialize;
|
||||
use sha2::Digest;
|
||||
|
||||
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 {
|
||||
upload_url_data,
|
||||
authorization_data,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl FileHost for BackblazeHost {
|
||||
async fn upload_file(
|
||||
&self,
|
||||
content_type: &str,
|
||||
file_name: &str,
|
||||
file_bytes: Bytes,
|
||||
) -> Result<UploadFileData, FileHostingError> {
|
||||
let content_sha512 = format!("{:x}", sha2::Sha512::digest(&file_bytes));
|
||||
|
||||
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_sha512,
|
||||
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,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn process_response<T>(response: Response) -> Result<T, FileHostingError>
|
||||
where
|
||||
T: for<'de> Deserialize<'de>,
|
||||
{
|
||||
if response.status().is_success() {
|
||||
Ok(response.json().await?)
|
||||
} else {
|
||||
Err(FileHostingError::BackblazeError(response.json().await?))
|
||||
}
|
||||
}
|
||||
75
apps/labrinth/src/file_hosting/backblaze/authorization.rs
Normal file
75
apps/labrinth/src/file_hosting/backblaze/authorization.rs
Normal file
@@ -0,0 +1,75 @@
|
||||
use crate::file_hosting::FileHostingError;
|
||||
use base64::Engine;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct AuthorizationPermissions {
|
||||
bucket_id: Option<String>,
|
||||
bucket_name: Option<String>,
|
||||
capabilities: Vec<String>,
|
||||
name_prefix: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct AuthorizationData {
|
||||
pub absolute_minimum_part_size: i32,
|
||||
pub account_id: String,
|
||||
pub allowed: AuthorizationPermissions,
|
||||
pub api_url: String,
|
||||
pub authorization_token: String,
|
||||
pub download_url: String,
|
||||
pub recommended_part_size: i32,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct UploadUrlData {
|
||||
pub bucket_id: String,
|
||||
pub upload_url: String,
|
||||
pub authorization_token: String,
|
||||
}
|
||||
|
||||
pub async fn authorize_account(
|
||||
key_id: &str,
|
||||
application_key: &str,
|
||||
) -> Result<AuthorizationData, FileHostingError> {
|
||||
let combined_key = format!("{key_id}:{application_key}");
|
||||
let formatted_key = format!(
|
||||
"Basic {}",
|
||||
base64::engine::general_purpose::STANDARD.encode(combined_key)
|
||||
);
|
||||
|
||||
let response = reqwest::Client::new()
|
||||
.get("https://api.backblazeb2.com/b2api/v2/b2_authorize_account")
|
||||
.header(reqwest::header::CONTENT_TYPE, "application/json")
|
||||
.header(reqwest::header::AUTHORIZATION, formatted_key)
|
||||
.send()
|
||||
.await?;
|
||||
|
||||
super::process_response(response).await
|
||||
}
|
||||
|
||||
pub async fn get_upload_url(
|
||||
authorization_data: &AuthorizationData,
|
||||
bucket_id: &str,
|
||||
) -> Result<UploadUrlData, FileHostingError> {
|
||||
let response = reqwest::Client::new()
|
||||
.post(format!("{}/b2api/v2/b2_get_upload_url", authorization_data.api_url).to_string())
|
||||
.header(reqwest::header::CONTENT_TYPE, "application/json")
|
||||
.header(
|
||||
reqwest::header::AUTHORIZATION,
|
||||
&authorization_data.authorization_token,
|
||||
)
|
||||
.body(
|
||||
serde_json::json!({
|
||||
"bucketId": bucket_id,
|
||||
})
|
||||
.to_string(),
|
||||
)
|
||||
.send()
|
||||
.await?;
|
||||
|
||||
super::process_response(response).await
|
||||
}
|
||||
38
apps/labrinth/src/file_hosting/backblaze/delete.rs
Normal file
38
apps/labrinth/src/file_hosting/backblaze/delete.rs
Normal file
@@ -0,0 +1,38 @@
|
||||
use super::authorization::AuthorizationData;
|
||||
use crate::file_hosting::FileHostingError;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct DeleteFileData {
|
||||
pub file_id: String,
|
||||
pub file_name: String,
|
||||
}
|
||||
|
||||
pub async fn delete_file_version(
|
||||
authorization_data: &AuthorizationData,
|
||||
file_id: &str,
|
||||
file_name: &str,
|
||||
) -> Result<DeleteFileData, FileHostingError> {
|
||||
let response = reqwest::Client::new()
|
||||
.post(format!(
|
||||
"{}/b2api/v2/b2_delete_file_version",
|
||||
authorization_data.api_url
|
||||
))
|
||||
.header(reqwest::header::CONTENT_TYPE, "application/json")
|
||||
.header(
|
||||
reqwest::header::AUTHORIZATION,
|
||||
&authorization_data.authorization_token,
|
||||
)
|
||||
.body(
|
||||
serde_json::json!({
|
||||
"fileName": file_name,
|
||||
"fileId": file_id
|
||||
})
|
||||
.to_string(),
|
||||
)
|
||||
.send()
|
||||
.await?;
|
||||
|
||||
super::process_response(response).await
|
||||
}
|
||||
45
apps/labrinth/src/file_hosting/backblaze/upload.rs
Normal file
45
apps/labrinth/src/file_hosting/backblaze/upload.rs
Normal file
@@ -0,0 +1,45 @@
|
||||
use super::authorization::UploadUrlData;
|
||||
use crate::file_hosting::FileHostingError;
|
||||
use bytes::Bytes;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct UploadFileData {
|
||||
pub file_id: String,
|
||||
pub file_name: String,
|
||||
pub account_id: String,
|
||||
pub bucket_id: String,
|
||||
pub content_length: u32,
|
||||
pub content_sha1: String,
|
||||
pub content_md5: Option<String>,
|
||||
pub content_type: String,
|
||||
pub upload_timestamp: u64,
|
||||
}
|
||||
|
||||
//Content Types found here: https://www.backblaze.com/b2/docs/content-types.html
|
||||
pub async fn upload_file(
|
||||
url_data: &UploadUrlData,
|
||||
content_type: &str,
|
||||
file_name: &str,
|
||||
file_bytes: Bytes,
|
||||
) -> Result<UploadFileData, FileHostingError> {
|
||||
let response = reqwest::Client::new()
|
||||
.post(&url_data.upload_url)
|
||||
.header(
|
||||
reqwest::header::AUTHORIZATION,
|
||||
&url_data.authorization_token,
|
||||
)
|
||||
.header("X-Bz-File-Name", file_name)
|
||||
.header(reqwest::header::CONTENT_TYPE, content_type)
|
||||
.header(reqwest::header::CONTENT_LENGTH, file_bytes.len())
|
||||
.header(
|
||||
"X-Bz-Content-Sha1",
|
||||
sha1::Sha1::from(&file_bytes).hexdigest(),
|
||||
)
|
||||
.body(file_bytes)
|
||||
.send()
|
||||
.await?;
|
||||
|
||||
super::process_response(response).await
|
||||
}
|
||||
58
apps/labrinth/src/file_hosting/mock.rs
Normal file
58
apps/labrinth/src/file_hosting/mock.rs
Normal file
@@ -0,0 +1,58 @@
|
||||
use super::{DeleteFileData, FileHost, FileHostingError, UploadFileData};
|
||||
use async_trait::async_trait;
|
||||
use bytes::Bytes;
|
||||
use chrono::Utc;
|
||||
use sha2::Digest;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct MockHost(());
|
||||
|
||||
impl MockHost {
|
||||
pub fn new() -> Self {
|
||||
MockHost(())
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl FileHost for MockHost {
|
||||
async fn upload_file(
|
||||
&self,
|
||||
content_type: &str,
|
||||
file_name: &str,
|
||||
file_bytes: Bytes,
|
||||
) -> Result<UploadFileData, FileHostingError> {
|
||||
let path = std::path::Path::new(&dotenvy::var("MOCK_FILE_PATH").unwrap())
|
||||
.join(file_name.replace("../", ""));
|
||||
std::fs::create_dir_all(path.parent().ok_or(FileHostingError::InvalidFilename)?)?;
|
||||
let content_sha1 = sha1::Sha1::from(&file_bytes).hexdigest();
|
||||
let content_sha512 = format!("{:x}", sha2::Sha512::digest(&file_bytes));
|
||||
|
||||
std::fs::write(path, &*file_bytes)?;
|
||||
Ok(UploadFileData {
|
||||
file_id: String::from("MOCK_FILE_ID"),
|
||||
file_name: file_name.to_string(),
|
||||
content_length: file_bytes.len() as u32,
|
||||
content_sha512,
|
||||
content_sha1,
|
||||
content_md5: None,
|
||||
content_type: content_type.to_string(),
|
||||
upload_timestamp: Utc::now().timestamp() as u64,
|
||||
})
|
||||
}
|
||||
|
||||
async fn delete_file_version(
|
||||
&self,
|
||||
file_id: &str,
|
||||
file_name: &str,
|
||||
) -> Result<DeleteFileData, FileHostingError> {
|
||||
let path = std::path::Path::new(&dotenvy::var("MOCK_FILE_PATH").unwrap())
|
||||
.join(file_name.replace("../", ""));
|
||||
if path.exists() {
|
||||
std::fs::remove_file(path)?;
|
||||
}
|
||||
Ok(DeleteFileData {
|
||||
file_id: file_id.to_string(),
|
||||
file_name: file_name.to_string(),
|
||||
})
|
||||
}
|
||||
}
|
||||
59
apps/labrinth/src/file_hosting/mod.rs
Normal file
59
apps/labrinth/src/file_hosting/mod.rs
Normal file
@@ -0,0 +1,59 @@
|
||||
use async_trait::async_trait;
|
||||
use thiserror::Error;
|
||||
|
||||
mod backblaze;
|
||||
mod mock;
|
||||
mod s3_host;
|
||||
|
||||
pub use backblaze::BackblazeHost;
|
||||
use bytes::Bytes;
|
||||
pub use mock::MockHost;
|
||||
pub use s3_host::S3Host;
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum FileHostingError {
|
||||
#[error("Error while accessing the data from backblaze")]
|
||||
HttpError(#[from] reqwest::Error),
|
||||
#[error("Backblaze error: {0}")]
|
||||
BackblazeError(serde_json::Value),
|
||||
#[error("S3 error: {0}")]
|
||||
S3Error(String),
|
||||
#[error("File system error in file hosting: {0}")]
|
||||
FileSystemError(#[from] std::io::Error),
|
||||
#[error("Invalid Filename")]
|
||||
InvalidFilename,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct UploadFileData {
|
||||
pub file_id: String,
|
||||
pub file_name: String,
|
||||
pub content_length: u32,
|
||||
pub content_sha512: String,
|
||||
pub content_sha1: String,
|
||||
pub content_md5: Option<String>,
|
||||
pub content_type: String,
|
||||
pub upload_timestamp: u64,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct DeleteFileData {
|
||||
pub file_id: String,
|
||||
pub file_name: String,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
pub trait FileHost {
|
||||
async fn upload_file(
|
||||
&self,
|
||||
content_type: &str,
|
||||
file_name: &str,
|
||||
file_bytes: Bytes,
|
||||
) -> Result<UploadFileData, FileHostingError>;
|
||||
|
||||
async fn delete_file_version(
|
||||
&self,
|
||||
file_id: &str,
|
||||
file_name: &str,
|
||||
) -> Result<DeleteFileData, FileHostingError>;
|
||||
}
|
||||
93
apps/labrinth/src/file_hosting/s3_host.rs
Normal file
93
apps/labrinth/src/file_hosting/s3_host.rs
Normal file
@@ -0,0 +1,93 @@
|
||||
use crate::file_hosting::{DeleteFileData, FileHost, FileHostingError, UploadFileData};
|
||||
use async_trait::async_trait;
|
||||
use bytes::Bytes;
|
||||
use chrono::Utc;
|
||||
use s3::bucket::Bucket;
|
||||
use s3::creds::Credentials;
|
||||
use s3::region::Region;
|
||||
use sha2::Digest;
|
||||
|
||||
pub struct S3Host {
|
||||
bucket: Bucket,
|
||||
}
|
||||
|
||||
impl S3Host {
|
||||
pub fn new(
|
||||
bucket_name: &str,
|
||||
bucket_region: &str,
|
||||
url: &str,
|
||||
access_token: &str,
|
||||
secret: &str,
|
||||
) -> Result<S3Host, FileHostingError> {
|
||||
let bucket = Bucket::new(
|
||||
bucket_name,
|
||||
if bucket_region == "r2" {
|
||||
Region::R2 {
|
||||
account_id: url.to_string(),
|
||||
}
|
||||
} else {
|
||||
Region::Custom {
|
||||
region: bucket_region.to_string(),
|
||||
endpoint: url.to_string(),
|
||||
}
|
||||
},
|
||||
Credentials::new(Some(access_token), Some(secret), None, None, None).map_err(|_| {
|
||||
FileHostingError::S3Error("Error while creating credentials".to_string())
|
||||
})?,
|
||||
)
|
||||
.map_err(|_| {
|
||||
FileHostingError::S3Error("Error while creating Bucket instance".to_string())
|
||||
})?;
|
||||
|
||||
Ok(S3Host { bucket })
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl FileHost for S3Host {
|
||||
async fn upload_file(
|
||||
&self,
|
||||
content_type: &str,
|
||||
file_name: &str,
|
||||
file_bytes: Bytes,
|
||||
) -> Result<UploadFileData, FileHostingError> {
|
||||
let content_sha1 = sha1::Sha1::from(&file_bytes).hexdigest();
|
||||
let content_sha512 = format!("{:x}", sha2::Sha512::digest(&file_bytes));
|
||||
|
||||
self.bucket
|
||||
.put_object_with_content_type(format!("/{file_name}"), &file_bytes, content_type)
|
||||
.await
|
||||
.map_err(|_| {
|
||||
FileHostingError::S3Error("Error while uploading file to S3".to_string())
|
||||
})?;
|
||||
|
||||
Ok(UploadFileData {
|
||||
file_id: file_name.to_string(),
|
||||
file_name: file_name.to_string(),
|
||||
content_length: file_bytes.len() as u32,
|
||||
content_sha512,
|
||||
content_sha1,
|
||||
content_md5: None,
|
||||
content_type: content_type.to_string(),
|
||||
upload_timestamp: Utc::now().timestamp() as u64,
|
||||
})
|
||||
}
|
||||
|
||||
async fn delete_file_version(
|
||||
&self,
|
||||
file_id: &str,
|
||||
file_name: &str,
|
||||
) -> Result<DeleteFileData, FileHostingError> {
|
||||
self.bucket
|
||||
.delete_object(format!("/{file_name}"))
|
||||
.await
|
||||
.map_err(|_| {
|
||||
FileHostingError::S3Error("Error while deleting file from S3".to_string())
|
||||
})?;
|
||||
|
||||
Ok(DeleteFileData {
|
||||
file_id: file_id.to_string(),
|
||||
file_name: file_name.to_string(),
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user