1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
use std::{env, error::Error, fs::{self, remove_file}, io::Write, path::PathBuf, time::Duration};
use base64::{Engine, engine::general_purpose};
use bt_logger::{get_error, log_error};
use once_cell::sync;
use reqwest::{Client, Url};
use sha3::{Digest, Sha3_512};
use crate::folder_manager::get_local_usr_data_path;
static DEFAULT_USER_AGENT: sync::Lazy<String> = sync::Lazy::new(||{
format!("Mozilla/5.0 ({}; {}; {}) {}/{}", env::consts::FAMILY, env::consts::OS, env::consts::ARCH, option_env!("CARGO_PKG_NAME").unwrap_or("bt_file_cache"), option_env!("CARGO_PKG_VERSION").unwrap_or("0.0.1b"))
});
static HTTP_CLIENT: sync::Lazy<Client> = sync::Lazy::new(||{
const CLIENT_REQUEST_TIMEOUT: u64 = 10;
if let Ok(c) = reqwest::Client::builder()
.timeout(Duration::from_secs(CLIENT_REQUEST_TIMEOUT))
.user_agent(DEFAULT_USER_AGENT.clone())
.build(){
c
}else{
reqwest::Client::new()
}
});
///BTCache provides a caching mechanism for downloading and storing files from URLs.
///It generates SHA3-512 hashes of URLs to create unique file names and manages local storage of cached files.
pub struct BTCache{
///folder_path: A PathBuf containing the directory path where cached files are stored.
folder_path: PathBuf,
}
impl BTCache {
///Constructor
/// Creates a new BTCache instance by determining the local user data path for the cache directory.
/// The method uses get_local_usr_data_path to construct the appropriate directory path based on the application folder name and cache subdirectory.
///
/// #Parameters:
/// * app_folder_name: An optional string slice that specifies the application folder name. If None, a default folder name will be used.
///
/// #Returns"
/// * Result<Self, Box<dyn Error>>: Returns a BTCache instance on success, or an error if the local data path cannot be determined.
pub fn new(app_folder_name: Option<&str>) -> Result<Self, Box<dyn Error>>{
let local_path = get_local_usr_data_path(app_folder_name, Some("cache"), true)?;
Ok(
Self { folder_path: PathBuf::from(local_path) }
)
}
///Generate a Sha3_512 hash for the given String encoded with base64 URLSAFE no padding
///This ensures a consistent, unique identifier for each URL that can be safely used as a filename.
///
/// #Parameters
/// * input: A string slice containing the URL to be hashed.
///
/// #Returns
/// * String: A base64 URL-safe encoded SHA3-512 hash of the input string.
fn get_hash_string_base64(input: &str) -> String {
let mut hasher = Sha3_512::new();
hasher.update(input.as_bytes());
let result = hasher.finalize();
general_purpose::URL_SAFE_NO_PAD.encode(result)
}
///ASYNC Helper Method. Downloads a file from the specified URL and saves it to the given file path.
///Uses reqwest for HTTP requests and writes the response bytes directly to a file.
///
///#Parameters
/// * url: A string slice containing the URL to download.
/// * int_file_path: A reference to a PathBuf specifying where the downloaded file should be saved.
///#Returns
/// * Result<(), Box<dyn Error>>: Returns Ok(()) on successful download, or an error if the download or file creation fails.
async fn download_file_async(url: &str, int_file_path: &PathBuf) -> Result<(), Box<dyn Error>>{
let parsed_url = Url::parse(url)?;
let download_response = HTTP_CLIENT.get(parsed_url).send().await?; //reqwest::blocking::get(url)?;
let bytes = download_response.bytes().await?;
let mut file = fs::File::create(int_file_path)?;
file.write_all(&bytes)?;
Ok(())
}
///ASYNC Function that attempts to retrieve a local file path for a given URL. The method:
///The method handles both cases where the file path check fails entirely (logging an error and attempting to download)
///and where the file doesn't exist at the expected location (performing a download).
///
/// #Parameters:
/// * url: A string slice containing the URL of the file to retrieve from cache.
///
/// #Returns:
/// * Result<String, Box<dyn Error>>: Returns the full local file path as a string on success, or an error if:
/// The file path cannot be retrieved due to invalid Unicode
/// File operations fail during download or path checking
pub async fn get_local_file_path_async(&self, url: &str) -> Result<String,Box<dyn Error>> {
//let int_file_path = self.folder_path.join(Self::get_hash_string_base64(url));
self.get_local_file_path_with_name_async(url, url).await
/*let path_check = int_file_path.try_exists();
if path_check.is_err() {
log_error!("get_local_file_path","Issue finding file '{:?}' trying downloading again",int_file_path);
Self::download_file_async(url, &int_file_path).await?;
}else{
if !path_check.unwrap(){
//File not found
Self::download_file_async(url, &int_file_path).await?;
}
}
if let Some(full_path) = int_file_path.to_str(){
return Ok(full_path.to_owned())
}else{
return Err(get_error!("get_local_file_path","Unable to retrieve cached file path. Invalid Unicode Path").into())
}*/
}
///ASYNC Function that attempts to retrieve a local file path for a given URL. The method:
///The method handles both cases where the file path check fails entirely (logging an error and attempting to download)
///and where the file doesn't exist at the expected location (performing a download).
///
/// #Parameters:
/// * url: A string slice containing the URL of the file to retrieve from cache.
/// * file_name: desire file name or file id. Useful when file may associuted to multiple URLs
///
/// #Returns:
/// * Result<String, Box<dyn Error>>: Returns the full local file path as a string on success, or an error if:
/// The file path cannot be retrieved due to invalid Unicode
/// File operations fail during download or path checking
pub async fn get_local_file_path_with_name_async(&self, url: &str, file_name: &str) -> Result<String,Box<dyn Error>> {
let int_file_path = self.folder_path.join(Self::get_hash_string_base64(file_name));
let path_check = int_file_path.try_exists();
if path_check.is_err() {
log_error!("get_local_file_path","Issue finding file '{:?}' trying downloading again",int_file_path);
Self::download_file_async(url, &int_file_path).await?;
}else{
if !path_check.unwrap(){
//File not found
Self::download_file_async(url, &int_file_path).await?;
}
}
if let Some(full_path) = int_file_path.to_str(){
return Ok(full_path.to_owned())
}else{
return Err(get_error!("get_local_file_path","Unable to retrieve cached file path. Invalid Unicode Path").into())
}
}
///Async function that encodes the file bytes using standard base64 encoding
///
///#Parameters
/// * url: A string slice (&str) containing the URL of the file to retrieve
///
///#Returns
/// Result<String, Box<dyn Error>>: Returns a String containing the base64-encoded file data on success, or an error if:
/// The local file path cannot be determined
/// The file cannot be read from the local cache
/// Base64 encoding fails
pub async fn get_file_data_base64_async(&self, url: &str) -> Result<String,Box<dyn Error>> {
let full_file_path = self.get_local_file_path_with_name_async(url, url).await?;
let file_data_bytes = fs::read(full_file_path)?;
Ok(general_purpose::STANDARD.encode(file_data_bytes))
}
///Async function that encodes the file bytes using standard base64 encoding
///
///#Parameters
/// * url: A string slice (&str) containing the URL of the file to retrieve
/// * file_name: desire file name or file id. Useful when file may associuted to multiple URLs
///
///#Returns
/// Result<String, Box<dyn Error>>: Returns a String containing the base64-encoded file data on success, or an error if:
/// The local file path cannot be determined
/// The file cannot be read from the local cache
/// Base64 encoding fails
pub async fn get_file_data_base64_with_name_async(&self, url: &str, file_name: &str) -> Result<String,Box<dyn Error>> {
let full_file_path = self.get_local_file_path_with_name_async(url, file_name).await?;
let file_data_bytes = fs::read(full_file_path)?;
Ok(general_purpose::STANDARD.encode(file_data_bytes))
}
///Helper Method. Downloads a file from the specified URL and saves it to the given file path.
///Uses reqwest for HTTP requests and writes the response bytes directly to a file.
///
///#Parameters
/// * url: A string slice containing the URL to download.
/// * int_file_path: A reference to a PathBuf specifying where the downloaded file should be saved.
///#Returns
/// * Result<(), Box<dyn Error>>: Returns Ok(()) on successful download, or an error if the download or file creation fails.
fn download_file(url: &str, int_file_path: &PathBuf) -> Result<(), Box<dyn Error>>{
let parsed_url = Url::parse(url)?;
let download_response = reqwest::blocking::get(parsed_url)?;
let bytes = download_response.bytes()?;
let mut file = fs::File::create(int_file_path)?;
file.write_all(&bytes)?;
Ok(())
}
///Attempts to retrieve a local file path for a given URL. The method:
///The method handles both cases where the file path check fails entirely (logging an error and attempting to download)
///and where the file doesn't exist at the expected location (performing a download).
///
/// #Parameters:
/// * url: A string slice containing the URL of the file to retrieve from cache.
///
/// #Returns:
/// * Result<String, Box<dyn Error>>: Returns the full local file path as a string on success, or an error if:
/// The file path cannot be retrieved due to invalid Unicode
/// File operations fail during download or path checking
pub fn get_local_file_path(&self, url: &str) -> Result<String,Box<dyn Error>> {
let int_file_path = self.folder_path.join(Self::get_hash_string_base64(url));
let path_check = int_file_path.try_exists();
if path_check.is_err() {
log_error!("get_local_file_path","Issue finding file '{:?}' trying downloading again",int_file_path);
Self::download_file(url, &int_file_path)?;
}else{
if !path_check.unwrap(){
//File not found
Self::download_file(url, &int_file_path)?;
}
}
if let Some(full_path) = int_file_path.to_str(){
return Ok(full_path.to_owned())
}else{
return Err(get_error!("get_local_file_path","Unable to retrieve cached file path. Invalid Unicode Path").into())
}
}
///Encodes the file bytes using standard base64 encoding
///
///#Parameters
/// * url: A string slice (&str) containing the URL of the file to retrieve
///
///#Returns
/// Result<String, Box<dyn Error>>: Returns a String containing the base64-encoded file data on success, or an error if:
/// The local file path cannot be determined
/// The file cannot be read from the local cache
/// Base64 encoding fails
pub fn get_file_data_base64(&self, url: &str) -> Result<String,Box<dyn Error>> {
let full_file_path = self.get_local_file_path(url)?;
let file_data_bytes = fs::read(full_file_path)?;
Ok(general_purpose::STANDARD.encode(file_data_bytes))
}
///The invalidate_cache function is responsible for removing a cached file from the local filesystem.
///This function is typically used to clear stale or outdated cache entries, ensuring that subsequent requests for the specified URL
///will fetch fresh data rather than using cached content.
///
/// #Parameters
/// * url: &str, A string slice representing the URL of the cached resource to be invalidated. This parameter is used to determine which cached file should be removed
///
/// #Returns
/// Result<(), Box<dyn Error>>
/// * Success: Ok(()) - Indicates that the cache file was successfully removed
/// * Error: Err(Box<dyn Error>) - Contains a boxed error object describing what went wrong during the cache invalidation process
pub fn invalidate_cache(&self, url: &str)-> Result<(),Box<dyn Error>> {
let full_file_path = self.get_local_file_path(url)?;
let _r = remove_file(full_file_path)?;
Ok(())
}
///ASYNC The invalidate_cache function is responsible for removing a cached file from the local filesystem.
///This function is typically used to clear stale or outdated cache entries, ensuring that subsequent requests for the specified URL
///will fetch fresh data rather than using cached content.
///
/// #Parameters
/// * url_name_id: &str, A string slice representing the URL or Name ID of the cached resource to be invalidated.
/// This parameter is used to determine which cached file should be removed
///
/// #Returns
/// Result<(), Box<dyn Error>>
/// * Success: Ok(()) - Indicates that the cache file was successfully removed
/// * Error: Err(Box<dyn Error>) - Contains a boxed error object describing what went wrong during the cache invalidation process
pub async fn invalidate_cache_async(&self, url_name_id: &str)-> Result<(),Box<dyn Error>> {
let full_file_path = self.get_local_file_path_with_name_async(url_name_id,url_name_id).await?;
let _r = remove_file(full_file_path)?;
Ok(())
}
///The refresh_cache function is designed to refresh or revalidate a cached resource by first invalidating the existing cache entry
///and then returning the local file path where the refreshed content is stored.
///
/// #Parameters
/// * url: &str, A string slice representing the URL of the cached resource to be invalidated. This parameter is used to determine which cached file should be removed
///
/// #Returns
/// Result<(), Box<dyn Error>>:
/// * Success: Ok(String) - Returns the local file path where the refreshed cache content is stored
/// * Error: Err(Box<dyn Error>) - Contains a boxed error object describing what went wrong during the cache refresh process
pub fn refresh_cache(&self, url: &str)-> Result<String,Box<dyn Error>> {
self.invalidate_cache(url)?;
let file_path = self.get_local_file_path(url)?;
Ok(file_path)
}
///ASYNC The refresh_cache function is designed to refresh or revalidate a cached resource by first invalidating the existing cache entry
///and then returning the local file path where the refreshed content is stored.
///
/// #Parameters
/// * url_name_id: &str, A string slice representing the URL or Name ID of the cached resource to be invalidated. This parameter is used to determine which cached file should be removed
///
/// #Returns
/// Result<(), Box<dyn Error>>:
/// * Success: Ok(String) - Returns the local file path where the refreshed cache content is stored
/// * Error: Err(Box<dyn Error>) - Contains a boxed error object describing what went wrong during the cache refresh process
pub async fn refresh_cache_async(&self, url_name_id: &str)-> Result<String,Box<dyn Error>> {
self.invalidate_cache_async(url_name_id).await?;
let file_path = self.get_local_file_path_with_name_async(url_name_id,url_name_id).await?;
Ok(file_path)
}
}
//************** */
//UNIT TEST **/
//************* */
#[cfg(test)]
mod bt_cache_tests {
use std::sync::Once;
use bt_logger::{LogLevel, LogTarget, build_logger, log_verbose};
use regex::Regex;
use super::*;
const FILE_URL: &str = "https://avatars.githubusercontent.com/u/188628667?v=4";
//const FILE_URL: &str = "https://www.google.com/s2/favicons?sz=64&domain=indeed.com";
const APP_NAME: &str = "bt_cache";
static INIT: Once = Once::new();
fn ini_log() {
INIT.call_once(|| {
build_logger("BACHUETECH", "UNIT TEST RUST CACHE", LogLevel::VERBOSE, LogTarget::STD_ERROR, None );
});
}
#[test]
fn test_get_file_path_success() {
let local_cache = BTCache::new(Some(APP_NAME)).unwrap();
let p = local_cache.get_local_file_path(FILE_URL).unwrap();
let re = Regex::new(r"^/home/.*/\.local/share/bt_cache/cache/Pe2MEfGkJXVt54yoLZ2ziRh9v4fGIJcRWQE98MtwcYTSNgJyE4ec6lZ4tSdolTCN9SA-wVrhmtP-8HJ-7jVWGg").unwrap();
assert!(re.is_match(&p));
}
#[test]
fn test_get_file_data_success() {
ini_log();
let local_cache = BTCache::new(Some(APP_NAME)).unwrap();
let p = local_cache.get_file_data_base64(FILE_URL);
log_verbose!("test_get_file_data_success","Content: {:?}",p);
assert!(p.is_ok());
}
#[test]
fn test_get_file_data_fail() {
let local_cache = BTCache::new(Some(APP_NAME)).unwrap();
let p = local_cache.get_file_data_base64("http://invalidurl.com/fake_file.unknown");
assert!(p.is_err());
}
#[test]
fn test_invaldiate_success() {
let local_cache = BTCache::new(Some(APP_NAME)).unwrap();
let _ = local_cache.get_file_data_base64(FILE_URL);
let r = local_cache.invalidate_cache(FILE_URL);
assert!(r.is_ok())
}
#[test]
fn test_invaldiate_fail() {
let local_cache = BTCache::new(Some(APP_NAME)).unwrap();
let r = local_cache.invalidate_cache("http://invalidurl.com/fake_file.unknown");
assert!(r.is_err())
}
#[test]
fn test_refresh_success() {
ini_log();
let local_cache = BTCache::new(Some(APP_NAME)).unwrap();
let _ = local_cache.get_local_file_path(FILE_URL);
let r = local_cache.refresh_cache(FILE_URL);
log_verbose!("test_refresh_success","Result {:?}",r);
assert!(r.is_ok())
}
}
#[cfg(test)]
mod bt_cache_with_name_async_tests {
use std::sync::Once;
use regex::Regex;
use bt_logger::{LogLevel, LogTarget, build_logger, log_verbose};
use super::*;
static INIT: Once = Once::new();
fn ini_log() {
INIT.call_once(|| {
build_logger("BACHUETECH", "UNIT TEST RUST CACHE", LogLevel::VERBOSE, LogTarget::STD_ERROR, None );
});
}
const FILE_URL: &str = "https://www.google.com/s2/favicons?sz=64&domain=google.com";
const FILE_URL2: &str = "https://www.google.com/s2/favicons?sz=64&domain=walmart.com";
const FILE_URL3: &str = "https://www.google.com/s2/favicons?sz=64&domain=cnn.com";
const APP_NAME: &str = "bt_cache";
#[tokio::test]
async fn test_get_file_path_success_async() {
let local_cache = BTCache::new(Some(APP_NAME)).unwrap();
let p = local_cache.get_local_file_path_with_name_async(FILE_URL3,"file1").await.unwrap();
let re = Regex::new(r"^/home/.*/\.local/share/bt_cache/cache/S6ZZaVaoYQsQDYaR7piO7byg3ThvoCZk4Gg4GWCVxac-qy1RvtBWTmdbS0OeEhJMviRfOG7fsVqGQcPoGIVD-w").unwrap();
assert!(re.is_match(&p));
}
#[tokio::test]
async fn test_get_file_data_success_async() {
ini_log();
let local_cache = BTCache::new(Some(APP_NAME)).unwrap();
let p = local_cache.get_file_data_base64_with_name_async(FILE_URL,"file2").await;
log_verbose!("test_get_file_data_success_async","File: {:?}",p);
assert!(p.is_ok());
}
#[tokio::test]
async fn test_get_file_data_fail_async() {
let local_cache = BTCache::new(Some(APP_NAME)).unwrap();
let p = local_cache.get_file_data_base64_with_name_async("http://invalidurl.com/fake_file.unknown","useless").await;
assert!(p.is_err());
}
#[tokio::test]
async fn test_invalidate_success_async() {
let local_cache = BTCache::new(Some(APP_NAME)).unwrap();
let _ = local_cache.get_file_data_base64_with_name_async(FILE_URL2,"file4").await;
let r = local_cache.invalidate_cache_async("file4").await;
assert!(r.is_ok())
}
#[tokio::test]
async fn test_refresh_success_async() {
ini_log();
let local_cache = BTCache::new(Some(APP_NAME)).unwrap();
let r = local_cache.refresh_cache_async(FILE_URL).await;
log_verbose!("test_refresh_success_async","Res: {:?}",r);
assert!(r.is_ok())
}
}
#[cfg(test)]
mod bt_cache_async_tests {
use std::sync::Once;
use regex::Regex;
use bt_logger::{LogLevel, LogTarget, build_logger, log_verbose};
use super::*;
static INIT: Once = Once::new();
fn ini_log() {
INIT.call_once(|| {
build_logger("BACHUETECH", "UNIT TEST RUST CACHE", LogLevel::VERBOSE, LogTarget::STD_ERROR, None );
});
}
//const FILE_URL: &str = "https://s.gravatar.com/avatar/5ff5566fb3d2f67fe25a9ed89953d876?s=480&r=pg&d=https%3A%2F%2Fcdn.auth0.com%2Favatars%2Fce.png";
const FILE_URL: &str = "https://www.google.com/s2/favicons?sz=64&domain=indeed.com";
const FILE_URL2: &str = "https://www.google.com/s2/favicons?sz=64&domain=monster.com";
const FILE_URL3: &str = "https://www.google.com/s2/favicons?sz=64&domain=quora.com";
const APP_NAME: &str = "bt_cache";
#[tokio::test]
async fn test_get_file_path_success_async() {
let local_cache = BTCache::new(Some(APP_NAME)).unwrap();
let p = local_cache.get_local_file_path_async(FILE_URL3).await.unwrap();
let re = Regex::new(r"^/home/.*/\.local/share/bt_cache/cache/JV0VrQPUrmKposXxQFD0TwLbPHK1fQb_hOouSb3BvaG-xqIm_Lw9GKyCyTvVDlc2v29sT-5lQgytuJbEVv2eyA").unwrap();
assert!(re.is_match(&p));
}
#[tokio::test]
async fn test_get_file_data_success_async() {
ini_log();
let local_cache = BTCache::new(Some(APP_NAME)).unwrap();
let p = local_cache.get_file_data_base64_async(FILE_URL).await;
log_verbose!("test_get_file_data_success_async","File: {:?}",p);
assert!(p.is_ok());
}
#[tokio::test]
async fn test_get_file_data_fail_async() {
let local_cache = BTCache::new(Some(APP_NAME)).unwrap();
let p = local_cache.get_file_data_base64_async("http://invalidurl.com/fake_file.unknown").await;
assert!(p.is_err());
}
#[tokio::test]
async fn test_invalidate_success_async() {
let local_cache = BTCache::new(Some(APP_NAME)).unwrap();
let _ = local_cache.get_file_data_base64_async(FILE_URL2).await;
let r = local_cache.invalidate_cache_async(FILE_URL2).await;
assert!(r.is_ok())
}
#[tokio::test]
async fn test_refresh_success_async() {
ini_log();
let local_cache = BTCache::new(Some(APP_NAME)).unwrap();
let r = local_cache.refresh_cache_async(FILE_URL).await;
log_verbose!("test_refresh_success_async","Res: {:?}",r);
assert!(r.is_ok())
}
}