Skip to content

Commit

Permalink
Format
Browse files Browse the repository at this point in the history
  • Loading branch information
james58899 committed Dec 17, 2024
1 parent 9e9c2c7 commit b15bfca
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 73 deletions.
1 change: 1 addition & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
group_imports = "StdExternalCrate"
imports_granularity = "Crate"
max_width = 140
fn_call_width = 140
overflow_delimited_expr = true
reorder_impl_items = true
use_field_init_shorthand = true
Expand Down
25 changes: 4 additions & 21 deletions src/cache_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,7 @@ impl CacheManager {

// Check file exists
if !metadata.is_file() || metadata.len() != info.size() as u64 {
warn!(
"Unexcepted cache file metadata: type={:?}, size={}",
metadata.file_type(),
metadata.len()
);
warn!("Unexcepted cache file metadata: type={:?}, size={}", metadata.file_type(), metadata.len());
return None;
}

Expand Down Expand Up @@ -169,12 +165,7 @@ impl CacheManager {

let hash: [u8; 20] = hasher.finalize().into();
if hash != info.hash() {
warn!(
"Delete corrupt cache file: path={:?}, hash={:x?}, actual={:x?}",
&path,
info.hash(),
hash
);
warn!("Delete corrupt cache file: path={:?}, hash={:x?}, actual={:x?}", &path, info.hash(), hash);
cache_manager.remove_cache(&info).await;
}
});
Expand Down Expand Up @@ -339,12 +330,7 @@ impl CacheManager {

let size = metadata.len();
if size != info.size() as u64 {
warn!(
"Delete corrupt cache file: path={:?}, size={:x?}, actual={:x?}",
&path,
&info.size(),
size
);
warn!("Delete corrupt cache file: path={:?}, size={:x?}, actual={:x?}", &path, &info.size(), size);

if let Err(err) = remove_file(&path).await {
error!("Delete corrupt cache file error: path={:?}, err={}", &path, err);
Expand Down Expand Up @@ -409,10 +395,7 @@ impl CacheManager {
}
let actual_hash: [u8; 20] = hasher.finalize().into();
if actual_hash != info.hash {
warn!(
"Delete corrupt cache file: path={:?}, hash={:x?}, actual={:x?}",
path, &info.hash, &actual_hash
);
warn!("Delete corrupt cache file: path={:?}, hash={:x?}, actual={:x?}", path, &info.hash, &actual_hash);
if let Err(err) = remove_file(&path).await {
error!("Delete corrupt cache file error: path={:?}, err={}", path, err);
}
Expand Down
39 changes: 11 additions & 28 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
}
let args = args.unwrap();

create_dirs(vec![
&args.data_dir,
&args.cache_dir,
&args.log_dir,
&args.temp_dir,
&args.download_dir,
])
.await?;
create_dirs(vec![&args.data_dir, &args.cache_dir, &args.log_dir, &args.temp_dir, &args.download_dir]).await?;

// Init logger
let mut logger = Logger::init(args.log_dir).unwrap();
Expand All @@ -186,13 +179,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
Some(i) => i,
None => setup(&args.data_dir).await?,
};
let client = Arc::new(RPCClient::new(
id,
&key,
args.disable_ip_origin_check,
args.max_connection,
args.rpc_server_ip.as_deref(),
));
let client = Arc::new(RPCClient::new(id, &key, args.disable_ip_origin_check, args.max_connection, args.rpc_server_ip.as_deref()));
let init_settings = client.login().await?;

let (shutdown_send, shutdown_recv) = mpsc::unbounded_channel::<()>();
Expand Down Expand Up @@ -226,19 +213,15 @@ async fn main() -> Result<(), Box<dyn Error>> {
let (tx, mut rx) = mpsc::channel::<Command>(1);

info!("Starting HTTP server...");
let server = Server::new(
args.port.unwrap_or_else(|| init_settings.client_port()),
client.get_cert().await.unwrap(),
AppState {
runtime: Handle::current(),
reqwest: create_http_client(Duration::from_secs(30), proxy.clone()),
rpc: client.clone(),
download_state: Default::default(),
cache_manager: cache_manager.clone(),
command_channel: tx.clone(),
has_proxy,
},
);
let server = Server::new(args.port.unwrap_or_else(|| init_settings.client_port()), client.get_cert().await.unwrap(), AppState {
runtime: Handle::current(),
reqwest: create_http_client(Duration::from_secs(30), proxy.clone()),
rpc: client.clone(),
download_state: Default::default(),
cache_manager: cache_manager.clone(),
command_channel: tx.clone(),
has_proxy,
});
let server_handle = server.handle();

info!("Notifying the server that we have finished starting up the client...");
Expand Down
18 changes: 3 additions & 15 deletions src/route/server_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,23 +69,11 @@ pub(super) async fn servercmd(
// Switch to MT tokio runtime
let runtime = data.runtime.enter();

let mut rand = SmallRng::from_entropy();
let mut rng = SmallRng::from_entropy();
let mut requests = Vec::new();
for _ in 1..=count {
let url = Url::parse(
format!(
"{}://{}:{}/t/{}/{}/{}/{}",
protocol,
host,
port,
size,
time,
token,
rand.gen::<u32>()
)
.as_str(),
)
.unwrap();
let random: u32 = rng.gen();
let url = Url::parse(&format!("{protocol}://{host}:{port}/t/{size}/{time}/{token}/{random}")).unwrap();
debug!("Speedtest thread start: {}", url);
let reqwest = create_http_client(Duration::from_secs(60), None); // No proxy http client
requests.push(tokio::spawn(async move {
Expand Down
11 changes: 2 additions & 9 deletions src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,11 +325,7 @@ The program will now terminate.

let srv_res = self.send_action("dlfails", Some(&list.join(";"))).await;

debug!(
"Reported {} download failures with response {}.",
list.len(),
if srv_res.is_ok() { "OK" } else { "Fail" }
);
debug!("Reported {} download failures with response {}.", list.len(), if srv_res.is_ok() { "OK" } else { "Fail" });
}

pub async fn fetch_queue(&self, gallery: Option<&GalleryMeta>) -> Option<Vec<String>> {
Expand Down Expand Up @@ -482,10 +478,7 @@ The program will now terminate.
fn build_url(&self, action: &str, additional: &str, endpoint: Option<&str>) -> Url {
let mut url = self.api_base.read().clone();
let timestamp = &self.get_timestemp().to_string();
let hash = string_to_hash(format!(
"hentai@home-{}-{}-{}-{}-{}",
action, additional, self.id, timestamp, self.key
));
let hash = string_to_hash(format!("hentai@home-{}-{}-{}-{}-{}", action, additional, self.id, timestamp, self.key));

if let Some(endpoint) = endpoint {
url.path_segments_mut().unwrap().pop().push(endpoint);
Expand Down

0 comments on commit b15bfca

Please sign in to comment.