Browse code

Saving attachments to disk too!

Julio Biason authored on 20/05/2019 08:49:24
Showing 3 changed files

... ...
@@ -277,7 +277,6 @@ dependencies = [
277 277
  "elefren 0.19.4 (registry+https://github.com/rust-lang/crates.io-index)",
278 278
  "env_logger 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)",
279 279
  "html2md 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)",
280
- "hyper 0.12.28 (registry+https://github.com/rust-lang/crates.io-index)",
281 280
  "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
282 281
  "reqwest 0.9.17 (registry+https://github.com/rust-lang/crates.io-index)",
283 282
 ]
... ...
@@ -8,6 +8,5 @@ edition = "2018"
8 8
 elefren = { version = "0.19", features = ["toml"] }
9 9
 html2md = "0.2.9"
10 10
 reqwest = "0.9.17"
11
-hyper = "*"
12 11
 log = "0.4.6"
13 12
 env_logger = "0.6.1"
... ...
@@ -11,8 +11,6 @@ use elefren::prelude::*;
11 11
 
12 12
 use reqwest;
13 13
 
14
-use hyper::Uri;
15
-
16 14
 use log;
17 15
 use env_logger;
18 16
 
... ...
@@ -76,14 +74,29 @@ fn save_attachments(record: &Status) -> () {
76 74
 
77 75
 fn save_attachment(attachment: &Attachment, base_path: &PathBuf) -> () {
78 76
     log::debug!("Saving attachment {}", attachment.url);
79
-    let uri: Uri = attachment.url.parse().expect("Invalid URL");
80
-    let body = reqwest::get(&attachment.url)
81
-        .expect("Failed to connect to server")
82
-        .text()
83
-        .expect("Failed to retrieve attachment");
77
+    let filename = base_path.join(get_attachment_filename(&attachment.url));
78
+    log::debug!("Saving attachment to {:?}", filename);
79
+    if let Ok(mut fp) = File::create(filename) {
80
+        reqwest::get(&attachment.url)
81
+            .expect("Failed to connect to server")
82
+            .copy_to(&mut fp)
83
+            .expect("Failed to save attachment");
84
+    }
85
+}
84 86
 
85
-    if let Ok(mut fp) = File::create(base_path.join(uri.path())) {
86
-        fp.write_all(body.as_bytes())
87
-            .expect("Failed to save the attachment");
87
+fn get_attachment_filename(url: &str) -> String {
88
+    let mut frags = url.rsplitn(2, '/');
89
+    log::debug!("URL fragments: {:?}", frags);
90
+    if let Some(path_part) = frags.next() { 
91
+        log::debug!("Found path in the attachment URL: {:?}", path_part);
92
+        path_part
93
+            .split('?')
94
+            .next()
95
+            .unwrap_or(url)
96
+            .to_string()
97
+    } else {
98
+        // this is, most of the time, bad (due special characters -- like '?' -- and path)
99
+        log::debug!("No path in attachment, using full URL");
100
+        url.to_string()
88 101
     }
89 102
 }