-
Notifications
You must be signed in to change notification settings - Fork 148
/
Copy pathattachment.rs
657 lines (583 loc) · 22.2 KB
/
attachment.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
/*!
This module represents common (but not all) columns in the `attachment` table.
*/
use rusqlite::{Connection, Error, Result, Row, Statement};
use sha1::{Digest, Sha1};
use std::{
fs::File,
io::Read,
path::{Path, PathBuf},
};
use crate::{
error::{attachment::AttachmentError, table::TableError},
message_types::sticker::{get_sticker_effect, StickerEffect},
tables::{
messages::Message,
table::{Table, ATTACHMENT},
},
util::{
dates::TIMESTAMP_FACTOR,
dirs::home,
output::{done_processing, processing},
platform::Platform,
query_context::QueryContext,
size::format_file_size,
},
};
/// The default root directory for iMessage attachment data
pub const DEFAULT_ATTACHMENT_ROOT: &str = "~/Library/Messages/Attachments";
/// Represents the [MIME type](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_Types) of a message's attachment data
///
/// The interior `str` contains the subtype, i.e. `x-m4a` for `audio/x-m4a`
#[derive(Debug, PartialEq, Eq)]
pub enum MediaType<'a> {
Image(&'a str),
Video(&'a str),
Audio(&'a str),
Text(&'a str),
Application(&'a str),
Other(&'a str),
Unknown,
}
/// Represents a single row in the `attachment` table.
#[derive(Debug)]
pub struct Attachment {
pub rowid: i32,
/// The path to the file on disk
pub filename: Option<String>,
/// The [Uniform Type Identifier](https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/understanding_utis/understand_utis_intro/understand_utis_intro.html)
pub uti: Option<String>,
/// String representation of the file's MIME type
pub mime_type: Option<String>,
/// The name of the file when sent or received
pub transfer_name: Option<String>,
/// The total amount of data transferred over the network (not necessarily the size of the file)
pub total_bytes: u64,
/// `true` if the attachment was a sticker, else `false`
pub is_sticker: bool,
pub hide_attachment: i32,
/// Auxiliary data to denote that an attachment has been copied
pub copied_path: Option<PathBuf>,
}
impl Table for Attachment {
fn from_row(row: &Row) -> Result<Attachment> {
Ok(Attachment {
rowid: row.get("rowid")?,
filename: row.get("filename").unwrap_or(None),
uti: row.get("uti").unwrap_or(None),
mime_type: row.get("mime_type").unwrap_or(None),
transfer_name: row.get("transfer_name").unwrap_or(None),
total_bytes: row.get("total_bytes").unwrap_or_default(),
is_sticker: row.get("is_sticker").unwrap_or(false),
hide_attachment: row.get("hide_attachment").unwrap_or(0),
copied_path: None,
})
}
fn get(db: &Connection) -> Result<Statement, TableError> {
db.prepare(&format!("SELECT * from {ATTACHMENT}"))
.map_err(TableError::Attachment)
}
fn extract(attachment: Result<Result<Self, Error>, Error>) -> Result<Self, TableError> {
match attachment {
Ok(Ok(attachment)) => Ok(attachment),
Err(why) | Ok(Err(why)) => Err(TableError::Attachment(why)),
}
}
}
impl Attachment {
/// Gets a Vector of attachments for a single message
pub fn from_message(db: &Connection, msg: &Message) -> Result<Vec<Attachment>, TableError> {
let mut out_l = vec![];
if msg.has_attachments() {
let mut statement = db
.prepare(&format!(
"
SELECT * FROM message_attachment_join j
LEFT JOIN attachment AS a ON j.attachment_id = a.ROWID
WHERE j.message_id = {}
",
msg.rowid
))
.map_err(TableError::Attachment)?;
let iter = statement
.query_map([], |row| Ok(Attachment::from_row(row)))
.map_err(TableError::Attachment)?;
for attachment in iter {
let m = Attachment::extract(attachment)?;
out_l.push(m);
}
}
Ok(out_l)
}
/// Get the media type of an attachment
pub fn mime_type(&'_ self) -> MediaType<'_> {
match &self.mime_type {
Some(mime) => {
if let Some(mime_str) = mime.split('/').next() {
match mime_str {
"image" => MediaType::Image(mime),
"video" => MediaType::Video(mime),
"audio" => MediaType::Audio(mime),
"text" => MediaType::Text(mime),
"application" => MediaType::Application(mime),
_ => MediaType::Other(mime),
}
} else {
MediaType::Other(mime)
}
}
None => {
// Fallback to `uti` if the MIME type cannot be inferred
if let Some(uti) = &self.uti {
match uti.as_str() {
// This type is for audio messages, which are sent in `caf` format
// https://developer.apple.com/library/archive/documentation/MusicAudio/Reference/CAFSpec/CAF_overview/CAF_overview.html
"com.apple.coreaudio-format" => MediaType::Audio("x-caf; codecs=opus"),
_ => MediaType::Unknown,
}
} else {
MediaType::Unknown
}
}
}
}
/// Read the attachment from the disk into a vector of bytes in memory
pub fn as_bytes(
&self,
platform: &Platform,
db_path: &Path,
custom_attachment_root: Option<&str>,
) -> Result<Option<Vec<u8>>, AttachmentError> {
if let Some(file_path) =
self.resolved_attachment_path(platform, db_path, custom_attachment_root)
{
let mut file = File::open(&file_path)
.map_err(|err| AttachmentError::Unreadable(file_path.clone(), err))?;
let mut bytes = vec![];
file.read_to_end(&mut bytes)
.map_err(|err| AttachmentError::Unreadable(file_path.clone(), err))?;
return Ok(Some(bytes));
}
Ok(None)
}
/// Determine the [`StickerEffect`] of a sticker message
pub fn get_sticker_effect(
&self,
platform: &Platform,
db_path: &Path,
custom_attachment_root: Option<&str>,
) -> Result<Option<StickerEffect>, AttachmentError> {
// Handle the non-sticker case
if !self.is_sticker {
return Ok(None);
}
// Try to parse the HEIC data
if let Some(data) = self.as_bytes(platform, db_path, custom_attachment_root)? {
return Ok(Some(get_sticker_effect(data)));
}
// Default if the attachment is a sticker and cannot be parsed/read
Ok(Some(StickerEffect::default()))
}
/// Get the path to an attachment, if it exists
pub fn path(&self) -> Option<&Path> {
match &self.filename {
Some(name) => Some(Path::new(name)),
None => None,
}
}
/// Get the extension of an attachment, if it exists
pub fn extension(&self) -> Option<&str> {
match self.path() {
Some(path) => match path.extension() {
Some(ext) => ext.to_str(),
None => None,
},
None => None,
}
}
/// Get a reasonable filename for an attachment
pub fn filename(&self) -> &str {
if let Some(transfer_name) = &self.transfer_name {
return transfer_name;
}
if let Some(filename) = &self.filename {
return filename;
}
"Attachment missing name metadata!"
}
/// Get a human readable file size for an attachment
pub fn file_size(&self) -> String {
format_file_size(self.total_bytes)
}
/// Get the total attachment bytes referenced in the table
pub fn get_total_attachment_bytes(
db: &Connection,
context: &QueryContext,
) -> Result<u64, TableError> {
let mut bytes_query = if context.has_filters() {
let mut statement = format!("SELECT SUM(total_bytes) FROM {ATTACHMENT} a");
if context.has_filters() {
statement.push_str(" WHERE ");
if let Some(start) = context.start {
statement.push_str(&format!(
" a.created_date >= {}",
start / TIMESTAMP_FACTOR
));
}
if let Some(end) = context.end {
if context.start.is_some() {
statement.push_str(" AND ");
}
statement
.push_str(&format!(" a.created_date <= {}", end / TIMESTAMP_FACTOR));
}
}
db.prepare(&statement).map_err(TableError::Attachment)?
} else {
db.prepare(&format!("SELECT SUM(total_bytes) FROM {ATTACHMENT}"))
.map_err(TableError::Attachment)?
};
bytes_query
.query_row([], |r| r.get(0))
.map_err(TableError::Attachment)
}
/// Given a platform and database source, resolve the path for the current attachment
///
/// For macOS, `db_path` is unused. For iOS, `db_path` is the path to the root of the backup directory.
///
/// iOS Parsing logic source is from [here](https://github.com/nprezant/iMessageBackup/blob/940d001fb7be557d5d57504eb26b3489e88de26e/imessage_backup_tools.py#L83-L85).
///
/// Use the optional `custom_attachment_root` parameter when the attachments are not stored in the same place as the database expects. The expected location is [`DEFAULT_ATTACHMENT_ROOT`].
/// A custom attachment root like `/custom/path` will overwrite a path like `~/Library/Messages/Attachments/3d/...` to `/custom/path/3d...`
pub fn resolved_attachment_path(
&self,
platform: &Platform,
db_path: &Path,
custom_attachment_root: Option<&str>,
) -> Option<String> {
if let Some(mut path_str) = self.filename.clone() {
// Apply custom attachment path
if let Some(custom_attachment_path) = custom_attachment_root {
path_str = path_str.replace(DEFAULT_ATTACHMENT_ROOT, custom_attachment_path);
}
return match platform {
Platform::macOS => Some(Attachment::gen_macos_attachment(&path_str)),
Platform::iOS => Attachment::gen_ios_attachment(&path_str, db_path),
};
}
None
}
/// Emit diagnostic data for the Attachments table
///
/// This is defined outside of [`Diagnostic`](crate::tables::table::Diagnostic) because it requires additional data.
///
/// Get the number of attachments that are missing from the filesystem
/// or are missing one of the following columns:
///
/// - `ck_server_change_token_blob`
/// - `sr_ck_server_change_token_blob`
///
/// # Example:
///
/// ```
/// use imessage_database::util::{dirs::default_db_path, platform::Platform};
/// use imessage_database::tables::table::{Diagnostic, get_connection};
/// use imessage_database::tables::attachment::Attachment;
///
/// let db_path = default_db_path();
/// let conn = get_connection(&db_path).unwrap();
/// Attachment::run_diagnostic(&conn, &db_path, &Platform::macOS);
/// ```
pub fn run_diagnostic(
db: &Connection,
db_path: &Path,
platform: &Platform,
) -> Result<(), TableError> {
processing();
let mut total_attachments = 0;
let mut null_attachments = 0;
let mut size_on_disk: u64 = 0;
let mut statement_paths = db
.prepare(&format!("SELECT filename FROM {ATTACHMENT}"))
.map_err(TableError::Attachment)?;
let paths = statement_paths
.query_map([], |r| Ok(r.get(0)))
.map_err(TableError::Attachment)?;
let missing_files = paths
.filter_map(Result::ok)
.filter(|path: &Result<String, Error>| {
// Keep track of the number of attachments in the table
total_attachments += 1;
if let Ok(filepath) = path {
match platform {
Platform::macOS => {
let path = Attachment::gen_macos_attachment(filepath);
let file = Path::new(&path);
if let Ok(metadata) = file.metadata() {
size_on_disk += metadata.len();
}
!file.exists()
}
Platform::iOS => {
if let Some(parsed_path) =
Attachment::gen_ios_attachment(filepath, db_path)
{
let file = Path::new(&parsed_path);
if let Ok(metadata) = file.metadata() {
size_on_disk += metadata.len();
}
return !file.exists();
}
// This hits if the attachment path doesn't get generated
true
}
}
} else {
// This hits if there is no path provided for the current attachment
null_attachments += 1;
true
}
})
.count();
let total_bytes =
Attachment::get_total_attachment_bytes(db, &QueryContext::default()).unwrap_or(0);
done_processing();
if total_attachments > 0 {
println!("\rAttachment diagnostic data:");
println!(" Total attachments: {total_attachments}");
println!(
" Data referenced in table: {}",
format_file_size(total_bytes)
);
println!(
" Data present on disk: {}",
format_file_size(size_on_disk)
);
if missing_files > 0 && total_attachments > 0 {
println!(
" Missing files: {missing_files:?} ({:.0}%)",
(missing_files as f64 / total_attachments as f64) * 100f64
);
println!(" No path provided: {null_attachments}");
println!(
" No file located: {}",
missing_files.saturating_sub(null_attachments)
);
}
}
Ok(())
}
/// Generate a macOS path for an attachment
fn gen_macos_attachment(path: &str) -> String {
if path.starts_with('~') {
return path.replacen('~', &home(), 1);
}
path.to_string()
}
/// Generate an iOS path for an attachment
fn gen_ios_attachment(file_path: &str, db_path: &Path) -> Option<String> {
let input = file_path.get(2..)?;
let filename = format!(
"{:x}",
Sha1::digest(format!("MediaDomain-{input}").as_bytes())
);
let directory = filename.get(0..2)?;
Some(format!("{}/{directory}/{filename}", db_path.display()))
}
}
#[cfg(test)]
mod tests {
use crate::{
tables::attachment::{Attachment, MediaType, DEFAULT_ATTACHMENT_ROOT},
util::platform::Platform,
};
use std::path::{Path, PathBuf};
fn sample_attachment() -> Attachment {
Attachment {
rowid: 1,
filename: Some("a/b/c.png".to_string()),
uti: Some("public.png".to_string()),
mime_type: Some("image".to_string()),
transfer_name: Some("c.png".to_string()),
total_bytes: 100,
is_sticker: false,
hide_attachment: 0,
copied_path: None,
}
}
#[test]
fn can_get_path() {
let attachment = sample_attachment();
assert_eq!(attachment.path(), Some(Path::new("a/b/c.png")));
}
#[test]
fn cant_get_path_missing() {
let mut attachment = sample_attachment();
attachment.filename = None;
assert_eq!(attachment.path(), None);
}
#[test]
fn can_get_extension() {
let attachment = sample_attachment();
assert_eq!(attachment.extension(), Some("png"));
}
#[test]
fn cant_get_extension_missing() {
let mut attachment = sample_attachment();
attachment.filename = None;
assert_eq!(attachment.extension(), None);
}
#[test]
fn can_get_mime_type() {
let attachment = sample_attachment();
assert_eq!(attachment.mime_type(), MediaType::Image("image"));
}
#[test]
fn can_get_mime_type_fake() {
let mut attachment = sample_attachment();
attachment.mime_type = Some("bloop".to_string());
assert_eq!(attachment.mime_type(), MediaType::Other("bloop"));
}
#[test]
fn can_get_mime_type_missing() {
let mut attachment = sample_attachment();
attachment.mime_type = None;
assert_eq!(attachment.mime_type(), MediaType::Unknown);
}
#[test]
fn can_get_filename() {
let attachment = sample_attachment();
assert_eq!(attachment.filename(), "c.png");
}
#[test]
fn can_get_filename_no_transfer_name() {
let mut attachment = sample_attachment();
attachment.transfer_name = None;
assert_eq!(attachment.filename(), "a/b/c.png");
}
#[test]
fn can_get_filename_no_filename() {
let mut attachment = sample_attachment();
attachment.filename = None;
assert_eq!(attachment.filename(), "c.png");
}
#[test]
fn can_get_filename_no_meta() {
let mut attachment = sample_attachment();
attachment.transfer_name = None;
attachment.filename = None;
assert_eq!(attachment.filename(), "Attachment missing name metadata!");
}
#[test]
fn can_get_resolved_path_macos() {
let db_path = PathBuf::from("fake_root");
let attachment = sample_attachment();
assert_eq!(
attachment.resolved_attachment_path(&Platform::macOS, &db_path, None),
Some("a/b/c.png".to_string())
);
}
#[test]
fn can_get_resolved_path_macos_custom() {
let db_path = PathBuf::from("fake_root");
let mut attachment = sample_attachment();
// Sample path like `~/Library/Messages/Attachments/0a/10/.../image.jpeg`
attachment.filename = Some(format!("{DEFAULT_ATTACHMENT_ROOT}/a/b/c.png"));
assert_eq!(
attachment.resolved_attachment_path(&Platform::macOS, &db_path, Some("custom/root")),
Some("custom/root/a/b/c.png".to_string())
);
}
#[test]
fn can_get_resolved_path_macos_raw() {
let db_path = PathBuf::from("fake_root");
let mut attachment = sample_attachment();
attachment.filename = Some("~/a/b/c.png".to_string());
assert!(
attachment
.resolved_attachment_path(&Platform::macOS, &db_path, None)
.unwrap()
.len()
> attachment.filename.unwrap().len()
);
}
#[test]
fn can_get_resolved_path_macos_raw_tilde() {
let db_path = PathBuf::from("fake_root");
let mut attachment = sample_attachment();
attachment.filename = Some("~/a/b/c~d.png".to_string());
assert!(attachment
.resolved_attachment_path(&Platform::macOS, &db_path, None)
.unwrap()
.ends_with("c~d.png"));
}
#[test]
fn can_get_resolved_path_ios() {
let db_path = PathBuf::from("fake_root");
let attachment = sample_attachment();
assert_eq!(
attachment.resolved_attachment_path(&Platform::iOS, &db_path, None),
Some("fake_root/41/41746ffc65924078eae42725c979305626f57cca".to_string())
);
}
#[test]
fn can_get_resolved_path_ios_custom() {
let db_path = PathBuf::from("fake_root");
let attachment = sample_attachment();
// iOS Backups store attachments at the same level as the database file, so if the backup
// is intact, the custom root is not relevant
assert_eq!(
attachment.resolved_attachment_path(&Platform::iOS, &db_path, Some("custom/root")),
Some("fake_root/41/41746ffc65924078eae42725c979305626f57cca".to_string())
);
}
#[test]
fn cant_get_missing_resolved_path_macos() {
let db_path = PathBuf::from("fake_root");
let mut attachment = sample_attachment();
attachment.filename = None;
assert_eq!(
attachment.resolved_attachment_path(&Platform::macOS, &db_path, None),
None
);
}
#[test]
fn cant_get_missing_resolved_path_ios() {
let db_path = PathBuf::from("fake_root");
let mut attachment = sample_attachment();
attachment.filename = None;
assert_eq!(
attachment.resolved_attachment_path(&Platform::iOS, &db_path, None),
None
);
}
#[test]
fn can_get_file_size_bytes() {
let attachment = sample_attachment();
assert_eq!(attachment.file_size(), String::from("100.00 B"));
}
#[test]
fn can_get_file_size_kb() {
let mut attachment = sample_attachment();
attachment.total_bytes = 2300;
assert_eq!(attachment.file_size(), String::from("2.25 KB"));
}
#[test]
fn can_get_file_size_mb() {
let mut attachment = sample_attachment();
attachment.total_bytes = 5612000;
assert_eq!(attachment.file_size(), String::from("5.35 MB"));
}
#[test]
fn can_get_file_size_gb() {
let mut attachment: Attachment = sample_attachment();
attachment.total_bytes = 9234712394;
assert_eq!(attachment.file_size(), String::from("8.60 GB"));
}
#[test]
fn can_get_file_size_cap() {
let mut attachment: Attachment = sample_attachment();
attachment.total_bytes = u64::MAX;
assert_eq!(attachment.file_size(), String::from("16777216.00 TB"));
}
}