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,73 @@
use crate::file_hosting::FileHostingError;
use base64::encode;
use serde::{Deserialize, Serialize};
use serde_json::json;
#[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)]
#[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: String,
application_key: String,
) -> Result<AuthorizationData, FileHostingError> {
let combined_key = format!("{}:{}", key_id, application_key);
let formatted_key = format!("Basic {}", encode(combined_key));
Ok(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?
.json()
.await?)
}
pub async fn get_upload_url(
authorization_data: AuthorizationData,
bucket_id: String,
) -> Result<UploadUrlData, FileHostingError> {
Ok(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(
json!({
"bucketId": bucket_id,
})
.to_string(),
)
.send()
.await?
.json()
.await?)
}

View File

@@ -0,0 +1,41 @@
use crate::file_hosting::{AuthorizationData, FileHostingError};
use serde::{Deserialize, Serialize};
use serde_json::json;
#[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: String,
file_name: String,
) -> Result<DeleteFileData, FileHostingError> {
Ok(reqwest::Client::new()
.post(
&format!(
"{}/b2api/v2/b2_delete_file_version",
authorization_data.api_url
)
.to_string(),
)
.header(reqwest::header::CONTENT_TYPE, "application/json")
.header(
reqwest::header::AUTHORIZATION,
authorization_data.authorization_token,
)
.body(
json!({
"fileName": file_name,
"fileId": file_id
})
.to_string(),
)
.send()
.await?
.json()
.await?)
}

79
src/file_hosting/mod.rs Normal file
View File

@@ -0,0 +1,79 @@
use thiserror::Error;
mod authorization;
mod delete;
mod upload;
pub use authorization::authorize_account;
pub use authorization::get_upload_url;
pub use authorization::AuthorizationData;
pub use authorization::AuthorizationPermissions;
pub use authorization::UploadUrlData;
pub use upload::upload_file;
pub use upload::UploadFileData;
pub use delete::delete_file_version;
pub use delete::DeleteFileData;
#[derive(Error, Debug)]
pub enum FileHostingError {
#[error("Error while accessing the data from remote")]
RemoteWebsiteError(#[from] reqwest::Error),
#[error("Error while serializing or deserializing JSON")]
SerDeError(#[from] serde_json::Error),
}
#[cfg(test)]
mod tests {
use super::*;
#[actix_rt::test]
async fn test_authorization() {
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.clone(),
dotenv::var("BACKBLAZE_BUCKET_ID").unwrap(),
)
.await
.unwrap();
let upload_data = upload_file(
upload_url_data,
"text/plain".to_string(),
"test.txt".to_string(),
"test file".to_string().into_bytes(),
)
.await
.unwrap();
delete_file_version(
authorization_data,
upload_data.file_id,
upload_data.file_name,
)
.await
.unwrap();
}
}

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?)
}

View File

@@ -7,6 +7,7 @@ use std::env;
use std::fs::File;
mod database;
mod file_hosting;
mod models;
mod routes;
mod search;

View File

@@ -1,4 +1,4 @@
use actix_web::{get, web, HttpResponse};
use actix_web::{get, HttpResponse};
use serde_json::json;
#[get("/")]

View File

@@ -65,10 +65,7 @@ pub async fn index_curseforge(
let text = &res.text().await?;
let curseforge_mods: Vec<CurseForgeMod> = serde_json::from_str(text)?;
let mut max_index = 0;
for curseforge_mod in curseforge_mods {
max_index = curseforge_mod.id;
if curseforge_mod.game_slug != "minecraft"
|| !curseforge_mod.website_url.contains("/mc-mods/")
{

View File

@@ -1,13 +1,11 @@
use bson::doc;
use bson::Bson;
use futures::StreamExt;
use log::info;
use meilisearch_sdk::client::Client;
use crate::database::models::Item;
use crate::database::{Mod, Version};
use crate::search::{SearchError, SearchMod, SearchRequest};
use crate::search::{SearchError, SearchMod};
pub async fn index_local(client: mongodb::Client) -> Result<Vec<SearchMod>, SearchError> {
info!("Indexing local mods!");

View File

@@ -2,7 +2,6 @@
pub mod curseforge_import;
pub mod local_import;
use crate::database::DatabaseError;
use crate::search::indexing::curseforge_import::index_curseforge;
use crate::search::indexing::local_import::index_local;
use crate::search::{SearchError, SearchMod};