Selaa lähdekoodia

mailbox folder creating in the output directory moved to the email conversion

Yurii Sokolovskyi 1 viikko sitten
vanhempi
commit
11e4354e13
2 muutettua tiedostoa jossa 20 lisäystä ja 8 poistoa
  1. 11 5
      src/imap.rs
  2. 9 3
      src/main.rs

+ 11 - 5
src/imap.rs

@@ -1,7 +1,7 @@
 use std::collections::{HashMap, HashSet};
 use std::{io};
 use std::convert::TryFrom;
-use std::fs::File;
+use std::fs::{create_dir_all, File};
 use std::future::Future;
 use std::path::{PathBuf};
 use std::io::{BufReader, BufWriter, ErrorKind, Read, Write};
@@ -104,14 +104,17 @@ pub async fn list_imap_folders(session: &mut Session<TlsStream<TcpStream>>) -> a
 
 /// download all emails from one mailbox 
 pub async fn fetch_and_store_emails(session: &mut Session<TlsStream<TcpStream>>, list: String) -> anyhow::Result<Vec<(u32, PathBuf)>> {
+    let out_dir_name = Config::global().out_dir.clone().join(list.clone());
+    if !out_dir_name.exists() { create_dir_all(out_dir_name).ok(); }
+    
     session.select(list.clone()).await?;
 
     // Create directories for maildir
-    std::fs::create_dir_all(Config::global().maildir.clone().join(list.clone()).join("new"))
+    create_dir_all(Config::global().maildir.clone().join(list.clone()).join("new"))
         .expect(&*("Unable to create 'new' directory in ".to_owned() + &*list.clone()));
-    std::fs::create_dir_all(Config::global().maildir.clone().join(list.clone()).join("cur"))
+    create_dir_all(Config::global().maildir.clone().join(list.clone()).join("cur"))
         .expect(&*("Unable to create 'cur' directory in ".to_owned() + &*list.clone()));
-    std::fs::create_dir_all(Config::global().maildir.clone().join(list.clone()).join("tmp"))
+    create_dir_all(Config::global().maildir.clone().join(list.clone()).join("tmp"))
         .expect(&*("Unable to create 'tmp' directory in ".to_owned() + &*list.clone()));
 
     let uids_path = Config::global().maildir.clone().join(".uids.json");
@@ -144,7 +147,10 @@ pub async fn fetch_and_store_emails(session: &mut Session<TlsStream<TcpStream>>,
             if let Some(body) = msg.body() {
                 let mail_file = store(Config::global().maildir.clone().join(list.clone()), uid.clone().to_string(), "new".to_string(), body, "");
                 match mail_file {
-                    Ok(file) => stored_paths.push((uid.to_string().parse().unwrap(), file)),
+                    Ok(file) => {
+                        // TODO convert and persist html
+                        stored_paths.push((uid.to_string().parse().unwrap(), file))
+                    },
                     Err(e) => eprintln!("Failed to store email: {}", e),
                 }
             } else {

+ 9 - 3
src/main.rs

@@ -352,8 +352,6 @@ async fn run(){
         }
         
         lists.push(dir_name.clone());
-        let out_dir_name = Config::global().out_dir.clone().join(dir_name);
-        if !out_dir_name.exists() { create_dir_all(out_dir_name).ok(); }
     }
     
     // let mut uid = 1; // fake uid for tests
@@ -369,6 +367,7 @@ async fn run(){
         // uid += 1;
     }
 
+    // TODO update indexes along with persisting emails
     let threads_indexes_path = Config::global().out_dir.clone().join("threads.json");
     if !threads_indexes_path.exists() {
         Indexes::persist_threads().expect("Unable to persist threads");
@@ -410,13 +409,16 @@ async fn main() -> anyhow::Result<()> {
     
     // delete_email("Sent".to_string(), 4).await;
     
+    println!("{:?}", Instant::now() - start);
     Ok(())
 }
 
 /// Entry point for a not wasi env
 #[cfg(not(target_os = "wasi"))]
 #[tokio::main]
-async fn main() {
+async fn main() -> anyhow::Result<()>{
+    let start = Instant::now();
+    
     run().await;
     
     // let email = LettreMessage::builder()
@@ -439,4 +441,8 @@ async fn main() {
 
     
     // delete_email("lqo7m8r2.1js7w080jvr2o@express.medallia.com".to_string()).await.expect("Unable to delete an email");
+
+    println!("{:?}", Instant::now() - start);
+    
+    Ok(())
 }