@tutao/node-mimimi 262.250121.1 → 262.250124.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/Cargo.toml CHANGED
@@ -1,7 +1,7 @@
1
1
  [package]
2
2
  edition = "2021"
3
3
  name = "tutao_node-mimimi"
4
- version = "262.250121.1"
4
+ version = "262.250124.0"
5
5
 
6
6
  [lib]
7
7
  # need to have lib to be able to use this from rust tests
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tutao/node-mimimi",
3
- "version": "262.250121.1",
3
+ "version": "262.250124.0",
4
4
  "main": "./dist/binding.js",
5
5
  "types": "./dist/binding.d.ts",
6
6
  "napi": {
@@ -16,7 +16,7 @@
16
16
  },
17
17
  "license": "MIT",
18
18
  "devDependencies": {
19
- "@tutao/otest": "262.250121.1",
19
+ "@tutao/otest": "262.250124.0",
20
20
  "@napi-rs/cli": "3.0.0-alpha.65",
21
21
  "typescript": "5.3.3",
22
22
  "zx": "8.1.5"
@@ -33,8 +33,8 @@
33
33
  "version": "napi version"
34
34
  },
35
35
  "optionalDependencies": {
36
- "@tutao/node-mimimi-win32-x64-msvc": "262.250121.1",
37
- "@tutao/node-mimimi-linux-x64-gnu": "262.250121.1",
38
- "@tutao/node-mimimi-darwin-universal": "262.250121.1"
36
+ "@tutao/node-mimimi-win32-x64-msvc": "262.250124.0",
37
+ "@tutao/node-mimimi-linux-x64-gnu": "262.250124.0",
38
+ "@tutao/node-mimimi-darwin-universal": "262.250124.0"
39
39
  }
40
40
  }
@@ -50,7 +50,7 @@ impl FileImport {
50
50
  .expect("failed eml path will always have filename");
51
51
  let import_directory = failed_eml_path
52
52
  .parent()
53
- .ok_or(std::io::ErrorKind::NotFound)?;
53
+ .ok_or(std::io::ErrorKind::NotADirectory)?;
54
54
  let new_failed_eml_path = import_directory.join(FAILED_MAILS_SUB_DIR).join(filename);
55
55
 
56
56
  fs::rename(failed_eml_path, new_failed_eml_path)
@@ -85,7 +85,7 @@ impl FileImport {
85
85
  let is_eml_file = source_path.extension() == Some("eml".as_ref());
86
86
 
87
87
  if is_mbox_file {
88
- filename_producer.new_mbox(source_path.clone());
88
+ filename_producer.new_mbox(&source_path);
89
89
 
90
90
  let file_buf_reader =
91
91
  fs::File::open(&source_path)
@@ -116,7 +116,7 @@ impl FileImport {
116
116
  })?;
117
117
  }
118
118
  } else if is_eml_file {
119
- let target_eml_file_path = filename_producer.new_plain_eml(source_path.clone());
119
+ let target_eml_file_path = filename_producer.new_plain_eml(&source_path);
120
120
 
121
121
  fs::copy(&source_path, &target_eml_file_path).map_err(|copy_err| {
122
122
  log::error!("Can not copy eml: {source_path:?}. Error: {copy_err:?}");
@@ -21,45 +21,40 @@ impl<'a> FileNameProducer<'a> {
21
21
  }
22
22
  }
23
23
 
24
- pub fn new_plain_eml(&mut self, mut eml_path: PathBuf) -> PathBuf {
25
- eml_path.set_extension("");
26
- let original_filename = eml_path
27
- .file_name()
28
- .and_then(OsStr::to_str)
29
- .unwrap_or("unnamed")
30
- .to_string() + "-"
31
- + self.eml_file_count.to_string().as_str()
32
- + ".eml";
24
+ pub fn new_plain_eml(&mut self, eml_path: &Path) -> PathBuf {
25
+ let original_filename = Self::format_new_file_name(&eml_path, self.eml_file_count, ".eml");
33
26
 
34
27
  self.eml_file_count += 1;
35
28
 
36
29
  self.import_directory.join(original_filename)
37
30
  }
38
31
 
39
- pub fn new_mbox(&mut self, mut mbox_path: PathBuf) {
40
- mbox_path.set_extension("");
41
- let original_filename = mbox_path
42
- .file_name()
43
- .and_then(OsStr::to_str)
44
- .unwrap_or("unnamed")
45
- .to_string() + "-"
46
- + self.mbox_file_count.to_string().as_str()
47
- + ".mbox";
32
+ pub fn new_mbox(&mut self, mbox_path: &Path) {
33
+ let original_filename = Self::format_new_file_name(&mbox_path, self.mbox_file_count, ".mbox-item-");
48
34
 
49
35
  self.mbox_file_count += 1;
50
36
 
51
- self.current_mbox_prefix = original_filename + "-item-";
37
+ self.current_mbox_prefix = original_filename;
52
38
  self.current_mbox_iter_count = 0;
53
39
  }
54
40
 
55
41
  pub fn new_file_of_current_mbox(&mut self) -> PathBuf {
56
- let new_filename = self.current_mbox_prefix.clone()
57
- + self.current_mbox_iter_count.to_string().as_str()
58
- + ".eml";
42
+ let current_prefix = &self.current_mbox_prefix;
43
+ let count = self.current_mbox_iter_count;
44
+ let new_filename = format!("{current_prefix}{count}.eml");
45
+
59
46
  self.current_mbox_iter_count += 1;
60
47
 
61
48
  self.import_directory.join(new_filename)
62
49
  }
50
+
51
+ fn format_new_file_name(path: &Path, count: usize, suffix: &str) -> String {
52
+ let file_name = path
53
+ .file_stem()
54
+ .and_then(OsStr::to_str)
55
+ .unwrap_or("unnamed");
56
+ format!("{file_name}-{count}{suffix}")
57
+ }
63
58
  }
64
59
 
65
60
  #[cfg(test)]
@@ -72,22 +67,22 @@ mod tests {
72
67
  let import_directory = PathBuf::from("/tmp/");
73
68
  let mut producer = FileNameProducer::new(&import_directory);
74
69
  let first_name = producer
75
- .new_plain_eml(PathBuf::from("/usr/var/log/somefile.eml"))
70
+ .new_plain_eml(&PathBuf::from("/usr/var/log/somefile.eml"))
76
71
  .to_str()
77
72
  .unwrap()
78
73
  .to_string();
79
74
  let second_name = producer
80
- .new_plain_eml(PathBuf::from("/usr/var/log/someotherfile.eml"))
75
+ .new_plain_eml(&PathBuf::from("/usr/var/log/someotherfile.eml"))
81
76
  .to_str()
82
77
  .unwrap()
83
78
  .to_string();
84
79
  let third_name = producer
85
- .new_plain_eml(PathBuf::from("/usr/var/log/someotherfile"))
80
+ .new_plain_eml(&PathBuf::from("/usr/var/log/someotherfile"))
86
81
  .to_str()
87
82
  .unwrap()
88
83
  .to_string();
89
84
  let fourth_name = producer
90
- .new_plain_eml(PathBuf::from("/usr/var/log/someotherdir/.."))
85
+ .new_plain_eml(&PathBuf::from("/usr/var/log/someotherdir/.."))
91
86
  .to_str()
92
87
  .unwrap()
93
88
  .to_string();
@@ -102,7 +97,7 @@ mod tests {
102
97
  fn producer_for_multiple_mbox_only() {
103
98
  let import_directory = PathBuf::from("/tmp/");
104
99
  let mut producer = FileNameProducer::new(&import_directory);
105
- producer.new_mbox(PathBuf::from("/usr/var/log/somefile.mbox"));
100
+ producer.new_mbox(&PathBuf::from("/usr/var/log/somefile.mbox"));
106
101
  let first_name = producer
107
102
  .new_file_of_current_mbox()
108
103
  .to_str()
@@ -113,7 +108,7 @@ mod tests {
113
108
  .to_str()
114
109
  .unwrap()
115
110
  .to_string();
116
- producer.new_mbox(PathBuf::from("/usr/var/log/someotherdir/.."));
111
+ producer.new_mbox(&PathBuf::from("/usr/var/log/someotherdir/.."));
117
112
  let third_name = producer
118
113
  .new_file_of_current_mbox()
119
114
  .to_str()
@@ -124,7 +119,7 @@ mod tests {
124
119
  .to_str()
125
120
  .unwrap()
126
121
  .to_string();
127
- producer.new_mbox(PathBuf::from("/usr/var/log/somedir/.."));
122
+ producer.new_mbox(&PathBuf::from("/usr/var/log/somedir/.."));
128
123
  let fifth_name = producer
129
124
  .new_file_of_current_mbox()
130
125
  .to_str()
@@ -147,7 +142,7 @@ mod tests {
147
142
  fn producer_for_multiple_mbox_and_multiple_eml() {
148
143
  let import_directory = PathBuf::from("/tmp/");
149
144
  let mut producer = FileNameProducer::new(&import_directory);
150
- producer.new_mbox(PathBuf::from("/usr/var/log/somefile.mbox"));
145
+ producer.new_mbox(&PathBuf::from("/usr/var/log/somefile.mbox"));
151
146
  let first_name = producer
152
147
  .new_file_of_current_mbox()
153
148
  .to_str()
@@ -159,12 +154,12 @@ mod tests {
159
154
  .unwrap()
160
155
  .to_string();
161
156
  let third_name = producer
162
- .new_plain_eml(PathBuf::from("/usr/var/log/someotherfile"))
157
+ .new_plain_eml(&PathBuf::from("/usr/var/log/someotherfile"))
163
158
  .to_str()
164
159
  .unwrap()
165
160
  .to_string();
166
161
  let fourth_name = producer
167
- .new_plain_eml(PathBuf::from("/usr/var/log/someotherdir/.."))
162
+ .new_plain_eml(&PathBuf::from("/usr/var/log/someotherdir/.."))
168
163
  .to_str()
169
164
  .unwrap()
170
165
  .to_string();
@@ -181,25 +176,25 @@ mod tests {
181
176
  fn can_import_multiple_files_with_same_name() {
182
177
  let import_directory = PathBuf::from("/tmp/");
183
178
  let mut producer = FileNameProducer::new(&import_directory);
184
- producer.new_mbox(PathBuf::from("/usr/var/log/somefile.mbox"));
179
+ producer.new_mbox(&PathBuf::from("/usr/var/log/somefile.mbox"));
185
180
  let first_name = producer
186
181
  .new_file_of_current_mbox()
187
182
  .to_str()
188
183
  .unwrap()
189
184
  .to_string();
190
- producer.new_mbox(PathBuf::from("/usr/var/log/somefile.mbox"));
185
+ producer.new_mbox(&PathBuf::from("/usr/var/log/somefile.mbox"));
191
186
  let second_name = producer
192
187
  .new_file_of_current_mbox()
193
188
  .to_str()
194
189
  .unwrap()
195
190
  .to_string();
196
191
  let third_name = producer
197
- .new_plain_eml(PathBuf::from("/usr/var/log/someotherfile"))
192
+ .new_plain_eml(&PathBuf::from("/usr/var/log/someotherfile"))
198
193
  .to_str()
199
194
  .unwrap()
200
195
  .to_string();
201
196
  let fourth_name = producer
202
- .new_plain_eml(PathBuf::from("/usr/var/log/someotherfile"))
197
+ .new_plain_eml(&PathBuf::from("/usr/var/log/someotherfile"))
203
198
  .to_str()
204
199
  .unwrap()
205
200
  .to_string();
@@ -61,19 +61,12 @@ pub struct MailImportMessage {
61
61
  #[napi_derive::napi(string_enum)]
62
62
  #[repr(u8)]
63
63
  #[cfg_attr(test, derive(Debug))]
64
+ #[derive(PartialEq)]
64
65
  pub enum PreparationError {
65
- /// import state file does not exist at all
66
- NoStateFile,
67
- /// import state file exists, but it's content can not be deserialized to valid idTuple
68
- MalformedStateFile,
69
66
  /// Can not create a native Rest client
70
67
  NoNativeRestClient,
71
- /// Can not log in through sdk
72
- CanNotLoginToSdk,
73
- /// Can not create a sdk
74
- CannotCreateSdk,
75
- /// some error occurred while preparing import directory
76
- ImportDirectoryPreparation,
68
+ /// some error occurred while reading import directory
69
+ CannotReadOldStateId,
77
70
  /// Error when trying to resume the session passed from client
78
71
  LoginError,
79
72
  /// Can not read all the eml files in import directory
@@ -140,12 +133,8 @@ pub enum ProgressActionError {
140
133
  impl From<PreparationError> for napi::Error {
141
134
  fn from(prep_err: PreparationError) -> Self {
142
135
  let code = match prep_err {
143
- PreparationError::NoStateFile => "NoStateFile",
144
- PreparationError::MalformedStateFile => "MalformedStateFile",
145
136
  PreparationError::NoNativeRestClient => "NoNativeRestClient",
146
- PreparationError::CanNotLoginToSdk => "CanNotLoginToSdk",
147
- PreparationError::CannotCreateSdk => "CannotCreateSdk",
148
- PreparationError::ImportDirectoryPreparation => "ImportDirectoryPreparation",
137
+ PreparationError::CannotReadOldStateId => "ImportDirectoryPreparation",
149
138
  PreparationError::LoginError => "LoginError",
150
139
  PreparationError::FailedToReadEmls => "FailedToReadEmls",
151
140
  PreparationError::NoMailGroupKey => "NoMailGroupKey",
@@ -2,7 +2,7 @@ use super::importer::{
2
2
  ImportLoopResult, ImportMailStateId, ImportProgressAction, ImportStatus, Importer,
3
3
  };
4
4
  use crate::importer::file_reader::FileImport;
5
- use crate::importer::messages::{MailImportMessage, ProgressActionError};
5
+ use crate::importer::messages::{MailImportMessage, PreparationError, ProgressActionError};
6
6
  use napi::threadsafe_function::{ThreadsafeFunction, ThreadsafeFunctionCallMode};
7
7
  use napi::Env;
8
8
  use std::path::PathBuf;
@@ -46,7 +46,8 @@ impl ImporterApi {
46
46
  ) -> napi::Result<Option<ImporterApi>> {
47
47
  let target_owner_group = GeneratedId(target_owner_group);
48
48
  let import_directory = FileImport::make_import_directory(&config_directory, &mailbox_id);
49
- let existing_import = Importer::get_existing_import_id(&import_directory)?;
49
+ let existing_import = Importer::get_existing_import_id(&import_directory)
50
+ .map_err(|_| PreparationError::CannotReadOldStateId)?;
50
51
 
51
52
  match existing_import {
52
53
  None => Ok(None),