Skip to content

Commit d81007a

Browse files
authored
Merge pull request #662 from ReagentX/feat/cs/export-path-filename-len
Fix #661
2 parents 98a24cd + f67860b commit d81007a

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

imessage-exporter/src/app/runtime.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,16 @@ impl Config {
140140
///
141141
/// If it does not, first try and make a flat list of its members. Failing that, use the unique `chat_identifier` field.
142142
pub fn filename(&self, chatroom: &Chat) -> String {
143+
// Calculate effective max length accounting for export path
144+
let export_path_len = self.options.export_path.as_os_str().len();
145+
let max_len = MAX_LENGTH.saturating_sub(export_path_len + 1);
146+
143147
let mut filename = match &chatroom.display_name() {
144148
// If there is a display name, use that
145149
Some(name) => {
146150
format!(
147151
"{} - {}",
148-
&name[..min(MAX_LENGTH, name.len())],
152+
&name[..min(max_len, name.len())],
149153
// Get the deduplicated chat ID to ensure the filename is unique, even if the group name is not
150154
self.real_chatrooms
151155
.get(&chatroom.rowid)
@@ -181,15 +185,19 @@ impl Config {
181185
/// - Truncated Names
182186
/// - Contact 1, Contact 2, ... Contact 13 and 4 others
183187
fn filename_from_participants(&self, participants: &BTreeSet<i32>) -> String {
188+
// Calculate effective max length accounting for export path
189+
let export_path_len = self.options.export_path.as_os_str().len();
190+
let max_len = MAX_LENGTH.saturating_sub(export_path_len + 1);
191+
184192
let mut added = 0;
185-
let mut out_s = String::with_capacity(MAX_LENGTH);
193+
let mut out_s = String::with_capacity(max_len);
186194
for participant_id in participants {
187195
let participant_details = match self.resolve_participant(*participant_id) {
188196
Some(name) => name.details.as_str(),
189197
None => UNKNOWN,
190198
};
191199

192-
if participant_details.len() + out_s.len() < MAX_LENGTH {
200+
if participant_details.len() + out_s.len() < max_len {
193201
if !out_s.is_empty() {
194202
out_s.push_str(", ");
195203
}
@@ -198,10 +206,10 @@ impl Config {
198206
} else {
199207
let extra = format!(", and {} others", participants.len() - added);
200208
let space_remaining = extra.len() + out_s.len();
201-
if space_remaining >= MAX_LENGTH {
202-
out_s.replace_range((MAX_LENGTH - extra.len()).., &extra);
209+
if space_remaining >= max_len {
210+
out_s.replace_range((max_len - extra.len()).., &extra);
203211
} else if out_s.is_empty() {
204-
out_s.push_str(&participant_details[..MAX_LENGTH]);
212+
out_s.push_str(&participant_details[..max_len]);
205213
} else {
206214
out_s.push_str(&extra);
207215
}
@@ -712,7 +720,7 @@ mod filename_tests {
712720

713721
// Get filename
714722
let filename = app.filename_from_participants(&people);
715-
assert_eq!(filename, "Person With An Extremely and Excessively Long Name 10, Person With An Extremely and Excessively Long Name 11, Person With An Extremely and Excessively Long Name 12, Person With An Extremely and Excessively Long Name 13, and 4 others".to_string());
723+
assert_eq!(filename, "Person With An Extremely and Excessively Long Name 10, Person With An Extremely and Excessively Long Name 11, Person With An Extremely and Excessively Long Name 12, Person With An Extremely and Excessively Long Name , and 4 others".to_string());
716724
assert!(filename.len() <= MAX_LENGTH);
717725
}
718726

@@ -731,7 +739,7 @@ mod filename_tests {
731739

732740
// Get filename
733741
let filename = app.filename_from_participants(&people);
734-
assert_eq!(filename, "He slipped his key into the lock, and we all very quietly entered the cell. The sleeper half turned, and then settled down once more into a deep slumber. Holmes stooped to the water-jug, moistened his sponge, and then rubbed it twice v".to_string());
742+
assert_eq!(filename, "He slipped his key into the lock, and we all very quietly entered the cell. The sleeper half turned, and then settled down once more into a deep slumber. Holmes stooped to the water-jug, moistened his sponge, and then rubbed it tw".to_string());
735743
assert!(filename.len() <= MAX_LENGTH);
736744
}
737745

@@ -748,7 +756,7 @@ mod filename_tests {
748756
let filename = app.filename(&chat);
749757
assert_eq!(
750758
filename,
751-
"Life is infinitely stranger than anything which the mind of man could invent. We would not dare to conceive the things which are really mere commonplaces of existence. If we could fly out of that window hand in hand, hover over this gr - 0.html"
759+
"Life is infinitely stranger than anything which the mind of man could invent. We would not dare to conceive the things which are really mere commonplaces of existence. If we could fly out of that window hand in hand, hover over th - 0.html"
752760
);
753761
}
754762

0 commit comments

Comments
 (0)