Skip to content

Commit c5106f2

Browse files
committed
style(file scanner): make a separated to not repeat the same code and add USE_DATABASE variable support
1 parent 78a1bb3 commit c5106f2

1 file changed

Lines changed: 22 additions & 22 deletions

File tree

src/scanning/file_scanner.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,7 @@ pub async fn scan_file(path: String) {
9191
if found_batch.len() >= 50 {
9292
let batch_to_insert = std::mem::take(&mut found_batch);
9393

94-
match database::server::insert_servers(&batch_to_insert).await {
95-
Err(e) => logger::error(
96-
format!("Failed to insert server to database: {}", e.hex(DefaultColor::Highlight.hex()))
97-
).prefix("File Scanner").send().await,
98-
Ok(_) => logger::success(
99-
format!(
100-
"Saved {} servers to the database!",
101-
batch_to_insert.len().hex(DefaultColor::Highlight.hex())
102-
)
103-
).prefix("File Scanner").send().await
104-
}
94+
save_server(&batch_to_insert).await;
10595
}
10696
}
10797

@@ -127,17 +117,7 @@ pub async fn scan_file(path: String) {
127117
}
128118

129119
if !found_batch.is_empty() {
130-
match database::server::insert_servers(&found_batch).await {
131-
Err(e) => logger::error(
132-
format!("Failed to insert server to database: {}", e.hex(DefaultColor::Highlight.hex()))
133-
).prefix("File Scanner").send().await,
134-
Ok(_) => logger::success(
135-
format!(
136-
"Saved {} servers to the database!",
137-
found_batch.len().hex(DefaultColor::Highlight.hex())
138-
)
139-
).prefix("File Scanner").send().await
140-
}
120+
save_server(&found_batch).await;
141121
}
142122

143123
// Finished
@@ -160,4 +140,24 @@ pub async fn scan_file(path: String) {
160140
)
161141
).send().await;
162142
}).await;
143+
}
144+
145+
pub async fn save_server(results: &Vec<(ServerInfo, ServerHistory)>) {
146+
let use_db = crate::USE_DATABASE.get().map(|a| **a).unwrap_or(true);
147+
148+
if !use_db {
149+
return;
150+
}
151+
152+
match database::server::insert_servers(results).await {
153+
Err(e) => logger::error(
154+
format!("Failed to insert server to database: {}", e.hex(DefaultColor::Highlight.hex()))
155+
).prefix("File Scanner").send().await,
156+
Ok(_) => logger::success(
157+
format!(
158+
"Saved {} servers to the database!",
159+
results.len().hex(DefaultColor::Highlight.hex())
160+
)
161+
).prefix("File Scanner").send().await
162+
}
163163
}

0 commit comments

Comments
 (0)