Add Backblaze Driver (#32)

* Backblaze Driver

* Update action to work with new tests

* Fix minor issues

* Run Formatter + Switch to reqwest json parser
This commit is contained in:
Geometrically
2020-07-02 05:00:04 -07:00
committed by GitHub
parent 6d16b68f11
commit 91305262f1
20 changed files with 253 additions and 97 deletions

View File

@@ -0,0 +1,41 @@
use crate::file_hosting::authorization::UploadUrlData;
use crate::file_hosting::FileHostingError;
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: String,
file_name: String,
file_bytes: Vec<u8>,
) -> Result<UploadFileData, FileHostingError> {
Ok(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?
.json()
.await?)
}