docbox-processing 0.7.2

Docbox file processing logic
Documentation
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
use crate::common::processing::{test_office_convert_server_container, test_processing_layer};
use bytes::Bytes;
use docbox_database::models::generated_file::GeneratedFileType;
use docbox_processing::{ProcessingError, ProcessingLayerConfig, ProcessingOutput, process_file};
use std::path::Path;

mod common;

/// Test processing a PDF file
#[tokio::test]
async fn test_process_pdf() {
    // Process the file
    let output = process_sample_file("sample.pdf")
        .await
        .expect("pdf should produce processing output");

    assert!(
        !output.encrypted,
        "File was marked as encrypted but should not be"
    );

    assert_eq!(
        output.upload_queue.len(),
        4,
        "PDF file should produce 3 images and 1 text file"
    );

    // Ensure the files match the expectations
    let first = output.upload_queue.first().unwrap();
    assert_eq!(first.mime, mime::IMAGE_JPEG);
    assert!(matches!(first.ty, GeneratedFileType::CoverPage));

    let second = output.upload_queue.get(1).unwrap();
    assert_eq!(second.mime, mime::IMAGE_JPEG);
    assert!(matches!(second.ty, GeneratedFileType::LargeThumbnail));

    let third = output.upload_queue.get(2).unwrap();
    assert_eq!(third.mime, mime::IMAGE_JPEG);
    assert!(matches!(third.ty, GeneratedFileType::SmallThumbnail));

    let forth = output.upload_queue.get(3).unwrap();
    assert_eq!(forth.mime, mime::TEXT_PLAIN);
    assert!(matches!(forth.ty, GeneratedFileType::TextContent));

    // Ensure the text content matches expectation
    let text_content = String::from_utf8_lossy(forth.bytes.as_ref());
    assert_eq!(
        text_content.as_ref().replace("\r\n", "\n"),
        "Sample document\nThis is a second line\n\n\u{c}This is the second page\n\n\u{c}"
    );

    let index_metadata = output
        .index_metadata
        .expect("pdf file should produce index metadata");

    // Ensure page content matches expectation
    let pages = index_metadata.pages.expect("pdf file should produce pages");
    assert_eq!(pages.len(), 3);

    let first_page = pages.first().unwrap();
    assert_eq!(first_page.page, 0);
    assert_eq!(
        first_page.content.replace("\r\n", "\n"),
        "Sample document\nThis is a second line\n\n"
    );

    let second_page = pages.get(1).unwrap();
    assert_eq!(second_page.page, 1);
    assert_eq!(
        second_page.content.replace("\r\n", "\n"),
        "This is the second page\n\n"
    );

    let third_page = pages.get(2).unwrap();
    assert_eq!(third_page.page, 2);
    assert_eq!(third_page.content, "");

    // Ensure no additional files are produced
    assert!(
        output.additional_files.is_empty(),
        "PDF file should not produce additional files"
    );
}

/// Test processing a Word Document (.docx) file
#[tokio::test]
async fn test_process_docx() {
    test_process_document("sample.docx").await;
}

/// Test processing a Rich Text Format (.rtf) file
#[tokio::test]
async fn test_process_rtf() {
    test_process_document("sample.rtf").await;
}

/// Test processing a OpenDocument Text file (.odt) file
#[tokio::test]
async fn test_process_odt() {
    test_process_document("sample.odt").await;
}

/// Test processing a Word Template (.dotx) file
#[tokio::test]
async fn test_process_dotx() {
    test_process_document("sample.dotx").await;
}

/// Test processing a Word 97-2003 Template (.dot) file
#[tokio::test]
async fn test_process_dot() {
    test_process_document("sample.dot").await;
}

/// Test processing a Word 97-2003 Document (.doc) file
#[tokio::test]
async fn test_process_doc() {
    test_process_document("sample.doc").await;
}

/// Test processing a Word Macro-Enabled Template (.dotm) file
#[tokio::test]
async fn test_process_dotm() {
    test_process_document("sample.dotm").await;
}

/// Test processing a encrypted PDF file
#[tokio::test]
async fn test_process_pdf_encrypted() {
    test_process_encrypted("sample_encrypted.pdf").await;
}

/// Test processing a encrypted Word Document (.docx) file
#[tokio::test]
async fn test_process_docx_encrypted() {
    test_process_encrypted("sample_encrypted.docx").await;
}

/// Test processing a encrypted Word 97-2003 Document (.doc) file
#[tokio::test]
async fn test_process_doc_encrypted() {
    test_process_encrypted("sample_encrypted.doc").await;
}

/// Test processing a corrupted Word Document (.docx) file
#[tokio::test]
async fn test_process_docx_corrupted() {
    let container = test_office_convert_server_container().await;
    // Create the processing layer
    let processing_layer =
        test_processing_layer(&container, ProcessingLayerConfig::default()).await;

    // Get the sample file
    let samples_path = Path::new("tests/samples/documents");
    let sample_file = samples_path.join("sample_corrupted.docx");
    let bytes = tokio::fs::read(&sample_file).await.unwrap();
    let bytes = Bytes::from(bytes);
    let mime = mime_guess::from_path(&sample_file).iter().next().unwrap();

    // Process the file
    let output = process_file(&None, &processing_layer, bytes, &mime)
        .await
        .unwrap_err();

    assert!(
        matches!(output, ProcessingError::MalformedFile(_),),
        "corrupted file should produce a malformed document error got {output:?}"
    );
}

/// Test processing a Excel Workbook (.xlsx) file
#[tokio::test]
async fn test_process_xlsx() {
    test_process_workbook("sample.xlsx").await;
}

/// Test processing a Excel Binary Workbook (.xlsb) file
#[tokio::test]
async fn test_process_xlsb() {
    test_process_workbook("sample.xlsb").await;
}

/// Test processing a Excel 97-2003 Workbook (.xls) file
#[tokio::test]
async fn test_process_xls() {
    test_process_workbook("sample.xls").await;
}

/// Test processing a Excel Macro-Enabled Workbook (.xlsm) file
#[tokio::test]
async fn test_process_xlsm() {
    test_process_workbook("sample.xlsm").await;
}

/// Test processing a Excel 97-2003 Template (.xlt) file
#[tokio::test]
async fn test_process_xlt() {
    test_process_workbook("sample.xlt").await;
}

/// Test processing a Excel Macro-Enabled Template (.xltm) file
#[tokio::test]
async fn test_process_xltm() {
    test_process_workbook("sample.xltm").await;
}

/// Test processing a Excel Template (.xltx) file
#[tokio::test]
async fn test_process_xltx() {
    test_process_workbook("sample.xltx").await;
}

/// Test processing a OpenDocument Spreadsheet (.ods) file
#[tokio::test]
async fn test_process_ods() {
    let output = process_sample_file("sample.ods")
        .await
        .expect("office file should produce output");

    assert!(
        !output.encrypted,
        "file was marked as encrypted but should not be"
    );

    assert_eq!(
        output.upload_queue.len(),
        5,
        "office file should produce 1 pdf, 3 images and 1 text file"
    );

    // Ensure the files match the expectations
    let first = output.upload_queue.first().unwrap();
    assert_eq!(first.mime, mime::IMAGE_JPEG);
    assert!(matches!(first.ty, GeneratedFileType::CoverPage));

    let second = output.upload_queue.get(1).unwrap();
    assert_eq!(second.mime, mime::IMAGE_JPEG);
    assert!(matches!(second.ty, GeneratedFileType::LargeThumbnail));

    let third = output.upload_queue.get(2).unwrap();
    assert_eq!(third.mime, mime::IMAGE_JPEG);
    assert!(matches!(third.ty, GeneratedFileType::SmallThumbnail));

    let forth = output.upload_queue.get(3).unwrap();
    assert_eq!(forth.mime, mime::TEXT_PLAIN);
    assert!(matches!(forth.ty, GeneratedFileType::TextContent));

    let fifth = output.upload_queue.get(4).unwrap();
    assert_eq!(fifth.mime, mime::APPLICATION_PDF);
    assert!(matches!(fifth.ty, GeneratedFileType::Pdf));

    // Ensure the text content matches expectation
    let text_content = String::from_utf8_lossy(forth.bytes.as_ref());
    assert_eq!(
        text_content.as_ref().replace("\r\n", "\n"),
        "Sample Sample 1Sample 2\n\n\u{c}"
    );

    let index_metadata = output
        .index_metadata
        .as_ref()
        .expect("office file should produce index metadata");

    // Ensure page content matches expectation
    let pages = index_metadata
        .pages
        .as_ref()
        .expect("office file should produce pages");
    assert_eq!(pages.len(), 2);

    let first_page = pages.first().unwrap();
    assert_eq!(first_page.page, 0);
    assert_eq!(
        first_page.content.replace("\r\n", "\n"),
        "Sample Sample 1Sample 2\n\n"
    );

    let second_page = pages.get(1).unwrap();
    assert_eq!(second_page.page, 1);
    assert_eq!(second_page.content, "");

    // Ensure no additional files are produced
    assert!(
        output.additional_files.is_empty(),
        "office file should not produce additional files"
    );
}

/// Test processing a encrypted Excel Workbook (.xlsx) file
#[tokio::test]
async fn test_process_xlsx_encrypted() {
    test_process_encrypted("sample_encrypted.xlsx").await;
}

/// Test processing a encrypted Excel 97-2003 Workbook (.xls) file
#[tokio::test]
async fn test_process_xls_encrypted() {
    test_process_encrypted("sample_encrypted.xls").await;
}

async fn process_sample_file(sample_file: &str) -> Option<ProcessingOutput> {
    let container = test_office_convert_server_container().await;

    // Create the processing layer
    let processing_layer =
        test_processing_layer(&container, ProcessingLayerConfig::default()).await;

    // Get the sample file
    let samples_path = Path::new("tests/samples/documents");
    let sample_file = samples_path.join(sample_file);
    let bytes = tokio::fs::read(&sample_file).await.unwrap();
    let bytes = Bytes::from(bytes);
    let mime = mime_guess::from_path(&sample_file).iter().next().unwrap();

    // Process the file
    process_file(&None, &processing_layer, bytes, &mime)
        .await
        .unwrap()
}

async fn test_process_encrypted(sample_file: &str) {
    let output = process_sample_file(sample_file)
        .await
        .expect("office file should produce output");

    assert!(
        output.encrypted,
        "File was not marked as encrypted but should be"
    );
    assert!(
        output.upload_queue.is_empty(),
        "Encrypted file should not produce uploads"
    );
    assert!(
        output.index_metadata.is_none(),
        "Encrypted file should not produce index metadata"
    );
    assert!(
        output.additional_files.is_empty(),
        "Encrypted file should not produce additional files"
    );
}

async fn test_process_workbook(sample_file: &str) {
    let output = process_sample_file(sample_file)
        .await
        .expect("office file should produce output");
    validate_workbook_output(&output);
}

async fn test_process_document(sample_file: &str) {
    let output = process_sample_file(sample_file)
        .await
        .expect("office file should produce output");

    validate_document_output(&output);
}

/// Validates the expected output for all office document formats
fn validate_document_output(output: &ProcessingOutput) {
    assert!(
        !output.encrypted,
        "file was marked as encrypted but should not be"
    );

    assert_eq!(
        output.upload_queue.len(),
        5,
        "office file should produce 1 pdf, 3 images and 1 text file"
    );

    // Ensure the files match the expectations
    let first = output.upload_queue.first().unwrap();
    assert_eq!(first.mime, mime::IMAGE_JPEG);
    assert!(matches!(first.ty, GeneratedFileType::CoverPage));

    let second = output.upload_queue.get(1).unwrap();
    assert_eq!(second.mime, mime::IMAGE_JPEG);
    assert!(matches!(second.ty, GeneratedFileType::LargeThumbnail));

    let third = output.upload_queue.get(2).unwrap();
    assert_eq!(third.mime, mime::IMAGE_JPEG);
    assert!(matches!(third.ty, GeneratedFileType::SmallThumbnail));

    let forth = output.upload_queue.get(3).unwrap();
    assert_eq!(forth.mime, mime::TEXT_PLAIN);
    assert!(matches!(forth.ty, GeneratedFileType::TextContent));

    let fifth = output.upload_queue.get(4).unwrap();
    assert_eq!(fifth.mime, mime::APPLICATION_PDF);
    assert!(matches!(fifth.ty, GeneratedFileType::Pdf));

    // Ensure the text content matches expectation
    let text_content = String::from_utf8_lossy(forth.bytes.as_ref());
    assert_eq!(
        text_content.as_ref().replace("\r\n", "\n"),
        "Sample document\nThis is a second line\n\n\u{c}This is the second page\n\n\u{c}"
    );

    let index_metadata = output
        .index_metadata
        .as_ref()
        .expect("office file should produce index metadata");

    // Ensure page content matches expectation
    let pages = index_metadata
        .pages
        .as_ref()
        .expect("office file should produce pages");
    assert_eq!(pages.len(), 3);

    let first_page = pages.first().unwrap();
    assert_eq!(first_page.page, 0);
    assert_eq!(
        first_page.content.replace("\r\n", "\n"),
        "Sample document\nThis is a second line\n\n"
    );

    let second_page = pages.get(1).unwrap();
    assert_eq!(second_page.page, 1);
    assert_eq!(
        second_page.content.replace("\r\n", "\n"),
        "This is the second page\n\n"
    );

    let third_page = pages.get(2).unwrap();
    assert_eq!(third_page.page, 2);
    assert_eq!(third_page.content, "");

    // Ensure no additional files are produced
    assert!(
        output.additional_files.is_empty(),
        "office file should not produce additional files"
    );
}

/// Validates the expected output for all office workbook formats
fn validate_workbook_output(output: &ProcessingOutput) {
    assert!(
        !output.encrypted,
        "file was marked as encrypted but should not be"
    );

    assert_eq!(
        output.upload_queue.len(),
        5,
        "office file should produce 1 pdf, 3 images and 1 text file"
    );

    // Ensure the files match the expectations
    let first = output.upload_queue.first().unwrap();
    assert_eq!(first.mime, mime::IMAGE_JPEG);
    assert!(matches!(first.ty, GeneratedFileType::CoverPage));

    let second = output.upload_queue.get(1).unwrap();
    assert_eq!(second.mime, mime::IMAGE_JPEG);
    assert!(matches!(second.ty, GeneratedFileType::LargeThumbnail));

    let third = output.upload_queue.get(2).unwrap();
    assert_eq!(third.mime, mime::IMAGE_JPEG);
    assert!(matches!(third.ty, GeneratedFileType::SmallThumbnail));

    let forth = output.upload_queue.get(3).unwrap();
    assert_eq!(forth.mime, mime::TEXT_PLAIN);
    assert!(matches!(forth.ty, GeneratedFileType::TextContent));

    let fifth = output.upload_queue.get(4).unwrap();
    assert_eq!(fifth.mime, mime::APPLICATION_PDF);
    assert!(matches!(fifth.ty, GeneratedFileType::Pdf));

    // Ensure the text content matches expectation
    let text_content = String::from_utf8_lossy(forth.bytes.as_ref());
    assert_eq!(
        text_content.as_ref().replace("\r\n", "\n"),
        "Sample\n\nSample 1 Sample 2\n\n\u{c}"
    );

    let index_metadata = output
        .index_metadata
        .as_ref()
        .expect("office file should produce index metadata");

    // Ensure page content matches expectation
    let pages = index_metadata
        .pages
        .as_ref()
        .expect("office file should produce pages");
    assert_eq!(pages.len(), 2);

    let first_page = pages.first().unwrap();
    assert_eq!(first_page.page, 0);
    assert_eq!(
        first_page.content.replace("\r\n", "\n"),
        "Sample\n\nSample 1 Sample 2\n\n"
    );

    let second_page = pages.get(1).unwrap();
    assert_eq!(second_page.page, 1);
    assert_eq!(second_page.content, "");

    // Ensure no additional files are produced
    assert!(
        output.additional_files.is_empty(),
        "office file should not produce additional files"
    );
}