Skip to content

Commit bab5d03

Browse files
committed
feat: replace Pdf module with Download for improved file handling and add download functionality
1 parent 086bbf3 commit bab5d03

File tree

4 files changed

+109
-87
lines changed

4 files changed

+109
-87
lines changed

src/cmd/tasks.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use crate::{
3636
},
3737

3838
system::{
39-
pdf::Pdf,
39+
download::Download,
4040
markdown::Markdown,
4141
reporting::Reporting,
4242
},
@@ -113,15 +113,15 @@ impl Tasks {
113113

114114
if line_url.contains(Uris::PROVIDERS_DOMAINS[6]) {
115115
let scihub_url = SciHub::new(&line_url).get_pdf().await?;
116-
Pdf.download_doi(&scihub_url, &scihub_url, path).await?;
116+
Download.download_doi(&scihub_url, &scihub_url, path).await?;
117117
}
118118

119119
if line_url.contains(Uris::PROVIDERS_DOMAINS[7]) {
120120
ChatGPT::new(&line_url, &path).convert().await?;
121121
}
122122

123123
if !Providers::new(&line_url).check_provider_domain() {
124-
Pdf.download_line(&line_url, url, path).await?;
124+
Download.download_line(&line_url, url, path).await?;
125125
}
126126

127127
Ok(())

src/system/download.rs

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
use indicatif::ProgressBar;
2+
3+
use std::{
4+
fs::File,
5+
error::Error,
6+
7+
io::{
8+
Read,
9+
Write,
10+
Cursor,
11+
},
12+
};
13+
14+
use crate::{
15+
system::{
16+
pdf::Pdf,
17+
providers::Providers,
18+
},
19+
20+
ui::{
21+
ui_base::UI,
22+
errors_alerts::ErrorsAlerts,
23+
success_alerts::SuccessAlerts,
24+
},
25+
26+
utils::{
27+
url::UrlMisc,
28+
remote::Remote,
29+
file::FileUtils,
30+
validation::Validate,
31+
},
32+
};
33+
34+
pub struct Download;
35+
36+
impl Download {
37+
38+
async fn download(&self, url: &str, path: &str) -> Result<String, Box<dyn Error>> {
39+
UrlMisc::check_url_status(url).await?;
40+
41+
let (request_uri, filename) = Providers::new(url).get_from_provider().await?;
42+
let response = reqwest::get(&request_uri).await?;
43+
let total_size = Remote.get_file_size(&request_uri).await?;
44+
45+
let pb = ProgressBar::new(total_size);
46+
pb.set_style(UI::pb_template());
47+
48+
let output_path = FileUtils.get_output_path(path, &filename);
49+
let mut dest = File::create(&output_path)?;
50+
let content = response.bytes().await?;
51+
let mut reader = Cursor::new(content);
52+
53+
let _ = Validate::file_type(&filename, ".pdf");
54+
55+
let mut buffer = [0; 8192];
56+
while let Ok(size) = reader.read(&mut buffer) {
57+
if size == 0 { break; }
58+
59+
dest.write_all(&buffer[..size])?;
60+
pb.inc(size as u64);
61+
}
62+
63+
Ok(filename)
64+
}
65+
66+
pub async fn download_line(&self, line_url: &str, url: &str, path: &str) -> Result<String, Box<dyn Error>> {
67+
if Pdf.is_pdf_file(&line_url).await? || Providers::new(url).valid_provider_domain() && !line_url.contains(".md") {
68+
let result = self.download(&line_url, path).await;
69+
70+
match result {
71+
Ok(file) => {
72+
let file_path = &format!("{}{}", &path, &file);
73+
let password = Pdf.is_pdf_encrypted(&file_path);
74+
75+
SuccessAlerts::download(&file, url, password);
76+
return Ok(file_path.to_string())
77+
},
78+
79+
Err(e) => ErrorsAlerts::download(e, url),
80+
}
81+
}
82+
83+
Ok("".to_string())
84+
}
85+
86+
pub async fn download_doi(&self, line_url: &str, url: &str, path: &str) -> Result<String, Box<dyn Error>> {
87+
let result = self.download(&line_url, path).await;
88+
89+
match result {
90+
Ok(file) => {
91+
let file_path = &format!("{}{}", &path, &file);
92+
SuccessAlerts::download(&file, url, false);
93+
return Ok(file_path.to_string())
94+
},
95+
96+
Err(e) => ErrorsAlerts::download(e, url),
97+
}
98+
99+
Ok("".to_string())
100+
}
101+
102+
}

src/system/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ pub mod doi;
33
pub mod general;
44
pub mod scripts;
55
pub mod markdown;
6+
pub mod download;
67
pub mod providers;
78
pub mod reporting;
89
pub mod plataforms;

src/system/pdf.rs

Lines changed: 3 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,7 @@ use reqwest::header::CONTENT_TYPE as MimeType;
55
use std::{
66
error::Error,
77
path::PathBuf,
8-
9-
io::{
10-
Read,
11-
Write,
12-
Cursor,
13-
BufReader
14-
},
8+
io::BufReader,
159

1610
fs::{
1711
self,
@@ -20,21 +14,10 @@ use std::{
2014
};
2115

2216
use crate::{
17+
ui::ui_base::UI,
18+
utils::remote::Remote,
2319
render::render::Render,
24-
system::providers::Providers,
25-
26-
ui::{
27-
ui_base::UI,
28-
errors_alerts::ErrorsAlerts,
29-
success_alerts::SuccessAlerts,
30-
},
3120

32-
utils::{
33-
url::UrlMisc,
34-
remote::Remote,
35-
file::FileUtils,
36-
validation::Validate,
37-
},
3821
};
3922

4023
pub struct Pdf;
@@ -75,34 +58,6 @@ impl Pdf {
7558
Ok(())
7659
}
7760

78-
pub async fn download(&self, url: &str, path: &str) -> Result<String, Box<dyn Error>> {
79-
UrlMisc::check_url_status(url).await?;
80-
81-
let (request_uri, filename) = Providers::new(url).get_from_provider().await?;
82-
let response = reqwest::get(&request_uri).await?;
83-
let total_size = Remote.get_file_size(&request_uri).await?;
84-
85-
let pb = ProgressBar::new(total_size);
86-
pb.set_style(UI::pb_template());
87-
88-
let output_path = FileUtils.get_output_path(path, &filename);
89-
let mut dest = File::create(&output_path)?;
90-
let content = response.bytes().await?;
91-
let mut reader = Cursor::new(content);
92-
93-
let _ = Validate::file_type(&filename, ".pdf");
94-
95-
let mut buffer = [0; 8192];
96-
while let Ok(size) = reader.read(&mut buffer) {
97-
if size == 0 { break; }
98-
99-
dest.write_all(&buffer[..size])?;
100-
pb.inc(size as u64);
101-
}
102-
103-
Ok(filename)
104-
}
105-
10661
pub fn is_pdf_encrypted(&self, file_path: &str) -> bool {
10762
if let Ok(file) = File::open(file_path) {
10863
let reader = BufReader::new(file);
@@ -115,40 +70,4 @@ impl Pdf {
11570
false
11671
}
11772

118-
pub async fn download_line(&self, line_url: &str, url: &str, path: &str) -> Result<String, Box<dyn Error>> {
119-
if self.is_pdf_file(&line_url).await? || Providers::new(url).valid_provider_domain() && !line_url.contains(".md") {
120-
let result = self.download(&line_url, path).await;
121-
122-
match result {
123-
Ok(file) => {
124-
let file_path = &format!("{}{}", &path, &file);
125-
let password = self.is_pdf_encrypted(&file_path);
126-
127-
SuccessAlerts::download(&file, url, password);
128-
return Ok(file_path.to_string())
129-
},
130-
131-
Err(e) => ErrorsAlerts::download(e, url),
132-
}
133-
}
134-
135-
Ok("".to_string())
136-
}
137-
138-
pub async fn download_doi(&self, line_url: &str, url: &str, path: &str) -> Result<String, Box<dyn Error>> {
139-
let result = self.download(&line_url, path).await;
140-
141-
match result {
142-
Ok(file) => {
143-
let file_path = &format!("{}{}", &path, &file);
144-
SuccessAlerts::download(&file, url, false);
145-
return Ok(file_path.to_string())
146-
},
147-
148-
Err(e) => ErrorsAlerts::download(e, url),
149-
}
150-
151-
Ok("".to_string())
152-
}
153-
15473
}

0 commit comments

Comments
 (0)