Skip to content

Commit 4d70b76

Browse files
committed
Improve by cargo fmt
Signed-off-by: Denys Fedoryshchenko <denys.f@collabora.com>
1 parent 94e0361 commit 4d70b76

4 files changed

Lines changed: 46 additions & 41 deletions

File tree

src/azure.rs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,24 @@ impl AzureDriver {
1010
}
1111
}
1212

13-
use crate::{ReceivedFile, get_config_content};
13+
use crate::{get_config_content, ReceivedFile};
1414
use axum::http::{HeaderName, HeaderValue};
1515
use azure_storage::StorageCredentials;
16+
use azure_storage_blobs::container::operations::BlobItem;
1617
use azure_storage_blobs::prelude::{BlobBlockType, BlockId, BlockList, ClientBuilder, Tags};
1718
use chksum_hash_sha2_512 as sha2_512;
19+
use futures::stream::StreamExt;
1820
use headers::HeaderMap;
1921
use hex;
2022
use reqwest::Client;
2123
use serde::Deserialize;
24+
use std::fs::read_to_string;
2225
use std::fs::File;
2326
use std::io::Read;
2427
use std::io::Write;
2528
use std::sync::Arc;
2629
use tempfile::Builder;
2730
use toml::Table;
28-
use std::fs::read_to_string;
29-
use futures::stream::StreamExt;
30-
use azure_storage_blobs::container::operations::BlobItem;
3131

3232
#[derive(Deserialize)]
3333
struct AzureConfig {
@@ -59,7 +59,6 @@ fn calculate_checksum(filename: &String, data: &[u8]) {
5959
let hash = sha2_512::default().update(data).finalize();
6060
let digest = hash.digest();
6161
println!("File: {} Checksum: {}", filename, digest.to_hex_lowercase());
62-
6362
}
6463

6564
/// Write file to Azure blob storage
@@ -230,7 +229,10 @@ async fn get_file_from_blob(filename: String) -> ReceivedFile {
230229
break;
231230
}
232231
std::thread::sleep(std::time::Duration::from_millis(1000));
233-
println!("Waiting for headers file {} to exist: {} seconds", cache_filename_headers, seconds);
232+
println!(
233+
"Waiting for headers file {} to exist: {} seconds",
234+
cache_filename_headers, seconds
235+
);
234236
}
235237

236238
if !headers_file_exists {
@@ -309,7 +311,10 @@ async fn get_file_from_blob(filename: String) -> ReceivedFile {
309311

310312
// Implement set tags for Azure blob storage
311313
// tags are in format "key=value"
312-
async fn azure_set_filename_tags(filename: String, user_tags: Vec<(String, String)>) -> Result<String, String> {
314+
async fn azure_set_filename_tags(
315+
filename: String,
316+
user_tags: Vec<(String, String)>,
317+
) -> Result<String, String> {
313318
let azure_cfg = Arc::new(get_azure_credentials("azure"));
314319
let storage_account = azure_cfg.account.as_str();
315320
let storage_key = azure_cfg.key.clone();
@@ -341,8 +346,8 @@ async fn azure_list_files(directory: String) -> Vec<String> {
341346
let storage_key = azure_cfg.key.clone();
342347
let storage_container = azure_cfg.container.as_str();
343348
let storage_credential = StorageCredentials::access_key(storage_account, storage_key);
344-
let container_r = ClientBuilder::new(storage_account, storage_credential)
345-
.container_client(storage_container);
349+
let container_r =
350+
ClientBuilder::new(storage_account, storage_credential).container_client(storage_container);
346351
let listbldr = container_r.list_blobs();
347352
let mut liststream = listbldr.into_stream();
348353
let mut listing = Vec::new();
@@ -354,7 +359,6 @@ async fn azure_list_files(directory: String) -> Vec<String> {
354359
BlobItem::BlobPrefix(blob_prefix) => blob_prefix.name,
355360
};
356361
listing.push(blob_name.clone());
357-
358362
}
359363
println!("Listing count: {}", listing.len());
360364
}
@@ -373,7 +377,11 @@ impl super::Driver for AzureDriver {
373377
});
374378
return filenameret;
375379
}
376-
fn tag_file(&self, filename: String, user_tags: Vec<(String, String)>) -> Result<String, String> {
380+
fn tag_file(
381+
&self,
382+
filename: String,
383+
user_tags: Vec<(String, String)>,
384+
) -> Result<String, String> {
377385
let rt = tokio::runtime::Runtime::new().unwrap();
378386
let ret = rt.block_on(azure_set_filename_tags(filename, user_tags));
379387
return ret;

src/main.rs

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
*/
1212

1313
mod azure;
14-
mod storjwt;
1514
mod storcaching;
15+
mod storjwt;
1616

1717
use axum::{
1818
body::Body,
@@ -29,9 +29,8 @@ use std::path;
2929
use std::{net::SocketAddr, path::PathBuf};
3030
use tokio::io::AsyncSeekExt;
3131
use tokio_util::io::ReaderStream;
32-
use tower::ServiceBuilder;
3332
use toml::Table;
34-
33+
use tower::ServiceBuilder;
3534

3635
#[derive(Parser, Debug)]
3736
#[command(version, about, long_about = None)]
@@ -55,19 +54,10 @@ struct Args {
5554
)]
5655
config_file: String,
5756

58-
#[clap(
59-
short,
60-
long,
61-
default_value = "false",
62-
help = "Generate JWT secret"
63-
)]
57+
#[clap(short, long, default_value = "false", help = "Generate JWT secret")]
6458
generate_jwt_secret: bool,
6559

66-
#[clap(
67-
long,
68-
default_value = "",
69-
help = "Generate JWT token for email"
70-
)]
60+
#[clap(long, default_value = "", help = "Generate JWT token for email")]
7161
generate_jwt_token: String,
7262
}
7363

@@ -81,7 +71,11 @@ struct ReceivedFile {
8171
trait Driver {
8272
fn write_file(&self, filename: String, data: Vec<u8>, cont_type: String) -> String;
8373
fn get_file(&self, filename: String) -> ReceivedFile;
84-
fn tag_file(&self, filename: String, user_tags: Vec<(String, String)>) -> Result<String, String>;
74+
fn tag_file(
75+
&self,
76+
filename: String,
77+
user_tags: Vec<(String, String)>,
78+
) -> Result<String, String>;
8579
fn list_files(&self, directory: String) -> Vec<String>;
8680
}
8781

@@ -146,7 +140,7 @@ async fn initial_setup() -> Option<RustlsConfig> {
146140
std::fs::create_dir(download_dir).unwrap();
147141
}
148142

149-
let cfg_file : PathBuf;
143+
let cfg_file: PathBuf;
150144
// is ENV KCI_STORAGE_CONFIG set?
151145
if let Ok(cfg_file_env) = std::env::var("KCI_STORAGE_CONFIG") {
152146
cfg_file = PathBuf::from(&cfg_file_env);
@@ -155,7 +149,7 @@ async fn initial_setup() -> Option<RustlsConfig> {
155149
cfg_file = PathBuf::from(&args.config_file);
156150
println!("Using config file from args: {}", cfg_file.display());
157151
}
158-
152+
159153
if !cfg_file.exists() {
160154
eprintln!("Config file {} does not exist", &args.config_file);
161155
std::process::exit(1);
@@ -176,8 +170,6 @@ async fn initial_setup() -> Option<RustlsConfig> {
176170
None
177171
}
178172
}
179-
180-
181173
}
182174

183175
#[tokio::main]
@@ -309,7 +301,10 @@ fn verify_upload_permissions(owner: &str, path: &str) -> Result<(), String> {
309301
}
310302
}
311303
}
312-
Err(format!("User {} has no upload permissions for path {}", owner, path))
304+
Err(format!(
305+
"User {} has no upload permissions for path {}",
306+
owner, path
307+
))
313308
}
314309

315310
/*
@@ -353,7 +348,6 @@ async fn ax_post_file(headers: HeaderMap, mut multipart: Multipart) -> (StatusCo
353348
Err(e) => return (StatusCode::FORBIDDEN, e.to_string().into_bytes()),
354349
}
355350

356-
357351
while let Some(field) = multipart.next_field().await.unwrap() {
358352
let name = field.name().unwrap().to_string();
359353
//let filename = field.file_name();

src/storcaching.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use tokio::time::Duration;
2-
use std::time::SystemTime;
3-
use std::fs;
41
use fs2;
2+
use std::fs;
3+
use std::time::SystemTime;
4+
use tokio::time::Duration;
55

66
struct Files {
77
file: String,
@@ -59,7 +59,10 @@ fn delete_cache_file(file: String) {
5959
// Truncate from filename .content, and add .headers, delete both files
6060
let content_filename = file.clone();
6161
let headers_filename = file.replace(".content", ".headers");
62-
println!("Deleting files: {} {}", &content_filename, &headers_filename);
62+
println!(
63+
"Deleting files: {} {}",
64+
&content_filename, &headers_filename
65+
);
6366
let res = fs::remove_file(&content_filename);
6467
match res {
6568
Ok(_) => {}
@@ -93,7 +96,7 @@ async fn clean_disk(cache_dir: String) {
9396
/// Cache housekeeping loop
9497
/// This function will check the disk space every and clean with some hysteresis
9598
pub async fn cache_loop(cache_dir: &str) {
96-
let mut cleaning_on : bool = false;
99+
let mut cleaning_on: bool = false;
97100
loop {
98101
let free_space = freediskspace_percent(cache_dir.to_string()).await;
99102
if free_space < 12 && !cleaning_on {
@@ -115,4 +118,4 @@ pub async fn cache_loop(cache_dir: &str) {
115118
tokio::time::sleep(Duration::from_secs(10)).await;
116119
}
117120
}
118-
}
121+
}

src/storjwt.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::get_config_content;
22
use hmac::{Hmac, Mac};
3-
use jwt::{Header, Token, VerifyWithKey, SignWithKey};
3+
use jwt::{Header, SignWithKey, Token, VerifyWithKey};
44
use sha2::Sha256;
55
use std::collections::BTreeMap;
66
use toml::value::Table;
@@ -36,7 +36,7 @@ pub fn verify_jwt_token(token_str: &str) -> Result<BTreeMap<String, String>, jwt
3636
pub fn generate_jwt_secret() {
3737
// generate a random 32 bytes alphanumeric string
3838
use rand::{distributions::Alphanumeric, Rng};
39-
39+
4040
let secret: String = rand::thread_rng()
4141
.sample_iter(&Alphanumeric)
4242
.take(32)
@@ -54,4 +54,4 @@ pub fn generate_jwt_token(email: &str) -> Result<String, jwt::Error> {
5454
claims.insert("email".to_string(), email.to_string());
5555
let token_str = claims.sign_with_key(&key)?;
5656
Ok(token_str)
57-
}
57+
}

0 commit comments

Comments
 (0)