* typos :help_me: * (part 1/?) massive cleanup to make the code more Rust-ic and cut down heap allocations. * (part 2/?) massive cleanup to make the code more Rust-ic and cut down heap allocations. * (part 3/?) cut down some pretty major heap allocations here - more Bytes and BytesMuts, less Vec<u8>s also I don't really understand why you need to `to_vec` when you don't really use it again afterwards * (part 4/?) deduplicate error handling in backblaze logic * (part 5/?) fixes, cleanups, refactors, and reformatting * (part 6/?) cleanups and refactors * remove loads of `as_str` in types that already are `Display` * Revert "remove loads of `as_str` in types that already are `Display`" This reverts commit 4f974310cfb167ceba03001d81388db4f0fbb509. * reformat and move routes util to the util module * use streams * Run prepare + formatting issues Co-authored-by: Jai A <jaiagr+gpg@pm.me> Co-authored-by: Geometrically <18202329+Geometrically@users.noreply.github.com>
80 lines
2.2 KiB
Rust
80 lines
2.2 KiB
Rust
use crate::file_hosting::{DeleteFileData, FileHost, FileHostingError, UploadFileData};
|
|
use async_trait::async_trait;
|
|
use bytes::{Buf, Bytes};
|
|
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 mut bucket = Bucket::new(
|
|
bucket_name,
|
|
Region::Custom {
|
|
region: bucket_region.to_string(),
|
|
endpoint: url.to_string(),
|
|
},
|
|
Credentials::new(Some(access_token), Some(secret), None, None, None)?,
|
|
)?;
|
|
|
|
bucket.add_header("x-amz-acl", "public-read");
|
|
|
|
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.bytes()));
|
|
|
|
self.bucket
|
|
.put_object_with_content_type(
|
|
format!("/{}", file_name),
|
|
file_bytes.bytes(),
|
|
content_type,
|
|
)
|
|
.await?;
|
|
|
|
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: chrono::Utc::now().timestamp_millis() 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?;
|
|
|
|
Ok(DeleteFileData {
|
|
file_id: file_id.to_string(),
|
|
file_name: file_name.to_string(),
|
|
})
|
|
}
|
|
}
|