repl export-chat-requires destination path

and combind the public accessible functions into one
This commit is contained in:
Simon Laux
2020-06-13 05:27:36 +02:00
parent 897d2f4a08
commit 4bf07ccc71
2 changed files with 22 additions and 26 deletions

View File

@@ -1,7 +1,6 @@
extern crate dirs; extern crate dirs;
use std::str::FromStr; use std::str::FromStr;
use std::time::{SystemTime, UNIX_EPOCH};
use anyhow::{bail, ensure, Error}; use anyhow::{bail, ensure, Error};
use async_std::path::Path; use async_std::path::Path;
@@ -15,7 +14,7 @@ use deltachat::context::*;
use deltachat::dc_receive_imf::*; use deltachat::dc_receive_imf::*;
use deltachat::dc_tools::*; use deltachat::dc_tools::*;
use deltachat::error::Error; use deltachat::error::Error;
use deltachat::export_chat::{export_chat, pack_exported_chat}; use deltachat::export_chat::export_chat_to_zip;
use deltachat::imex::*; use deltachat::imex::*;
use deltachat::location; use deltachat::location;
use deltachat::log::LogExt; use deltachat::log::LogExt;
@@ -390,7 +389,7 @@ pub async fn cmdline(context: Context, line: &str, chat_id: &mut ChatId) -> Resu
protect <chat-id>\n\ protect <chat-id>\n\
unprotect <chat-id>\n\ unprotect <chat-id>\n\
delchat <chat-id>\n\ delchat <chat-id>\n\
export-chat <chat-id>\n\ export-chat <chat-id> <destination-file>\n\
===========================Contact requests==\n\ ===========================Contact requests==\n\
decidestartchat <msg-id>\n\ decidestartchat <msg-id>\n\
decideblock <msg-id>\n\ decideblock <msg-id>\n\
@@ -1028,24 +1027,10 @@ pub async fn cmdline(context: Context, line: &str, chat_id: &mut ChatId) -> Resu
} }
"export-chat" => { "export-chat" => {
ensure!(!arg1.is_empty(), "Argument <chat-id> missing."); ensure!(!arg1.is_empty(), "Argument <chat-id> missing.");
ensure!(!arg2.is_empty(), "Argument <destination file> missing.");
let chat_id = ChatId::new(arg1.parse()?); let chat_id = ChatId::new(arg1.parse()?);
let res = export_chat(&context, chat_id).await; // todo check if path is valid (dest dir exists) and ends in .zip
// println!("{:?}", res); export_chat_to_zip(&context, chat_id, arg2).await;
let timestamp = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs();
let destination_raw = context.get_blobdir().join(format!(
"exported_{}_{}.zip",
chat_id.to_u32(),
timestamp
));
let destination = destination_raw.to_str().unwrap();
let pack_res = pack_exported_chat(&context, res, destination);
match &pack_res {
Ok(()) => println!("Exported chat successfully to {}", destination),
Err(err) => println!("Error {:?}", err),
};
} }
"msginfo" => { "msginfo" => {
ensure!(!arg1.is_empty(), "Argument <msg-id> missing."); ensure!(!arg1.is_empty(), "Argument <msg-id> missing.");

View File

@@ -38,20 +38,29 @@ use futures::future::join_all;
use serde::Serialize; use serde::Serialize;
#[derive(Debug)] #[derive(Debug)]
pub struct ExportChatResult { struct ExportChatResult {
chat_json: String, chat_json: String,
// locations_geo_json: String, // locations_geo_json: String,
message_info: Vec<(u32, String, Option<String>)>, message_info: Vec<(u32, String, Option<String>)>,
referenced_blobs: Vec<String>, referenced_blobs: Vec<String>,
} }
pub fn pack_exported_chat( pub async fn export_chat_to_zip(context: &Context, chat_id: ChatId, filename: &str) -> () {
let res = export_chat_data(&context, chat_id).await;
let destination = std::path::Path::new(filename);
let pack_res = pack_exported_chat(&context, res, destination);
match &pack_res {
Ok(()) => println!("Exported chat successfully to {}", filename),
Err(err) => println!("Error {:?}", err),
};
}
fn pack_exported_chat(
context: &Context, context: &Context,
artifact: ExportChatResult, artifact: ExportChatResult,
filename: &str, destination: &Path,
) -> zip::result::ZipResult<()> { ) -> zip::result::ZipResult<()> {
let path = std::path::Path::new(filename); let file = std::fs::File::create(&destination).unwrap();
let file = std::fs::File::create(&path).unwrap();
let mut zip = zip::ZipWriter::new(file); let mut zip = zip::ZipWriter::new(file);
@@ -88,6 +97,8 @@ pub fn pack_exported_chat(
} }
} }
// todo maybe memory optimisation -> load message source here and pack it directly into zip
zip.finish()?; zip.finish()?;
Ok(()) Ok(())
} }
@@ -169,7 +180,7 @@ impl MessageJSON {
} }
} }
pub async fn export_chat(context: &Context, chat_id: ChatId) -> ExportChatResult { async fn export_chat_data(context: &Context, chat_id: ChatId) -> ExportChatResult {
let mut blobs = Vec::new(); let mut blobs = Vec::new();
let mut chat_author_ids = Vec::new(); let mut chat_author_ids = Vec::new();
// get all messages // get all messages