office-automation 0.3.2

Windows CLI tool that automates PowerPoint and Excel via COM
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
//! Rewrite OLE/chart link paths in PPTX .rels XML files.
//!
//! This is the critical performance optimization (GOTCHA #22):
//! - Python COM: ~90-100s for 186 links
//! - ZIP relink: ~0.12s for the same
//!
//! Fixes over Python version:
//! - Normalizes slashes in both directions (Python bug #1)
//! - Proper path-based .rels detection (Python bug #7)
//! - Better !sheet!range split handling (Python bug #8)
//! - Input validation (Python bug #9)
//! - UNC path support (Python bug #10)
//! - Per-file error handling with context (Python bug #5)

use std::io::{Read, Write};
use std::path::Path;

use crate::zip_ops::xml_stream::rewrite_xml_attributes;

/// Rewrite OLE and chart link paths in a PPTX file.
///
/// Modifies the PPTX in-place (via temp file + rename) to repoint all
/// external `file:///` references to `new_excel_path`.
///
/// Returns the number of links rewritten.
/// Relink result: (total, ole_count, chart_count)
pub struct RelinkResult {
    pub total: usize,
    pub ole: usize,
    pub charts: usize,
}

pub fn relink_pptx_zip(pptx_path: &Path, new_excel_path: &Path) -> Result<RelinkResult, String> {
    // Validate inputs
    if !pptx_path.exists() {
        return Err(format!("PPTX file not found: {}", pptx_path.display()));
    }
    if !new_excel_path.exists() {
        return Err(format!("Excel file not found: {}", new_excel_path.display()));
    }

    let new_excel_abs = new_excel_path.canonicalize()
        .map_err(|e| format!("Cannot resolve Excel path: {e}"))?;

    let new_file_uri = path_to_file_uri(&new_excel_abs);

    // Read the ZIP, rewrite .rels files, write to temp, then replace
    let pptx_data = std::fs::read(pptx_path)
        .map_err(|e| format!("Failed to read PPTX: {e}"))?;

    let mut reader = zip::ZipArchive::new(std::io::Cursor::new(&pptx_data))
        .map_err(|e| format!("Failed to open PPTX as ZIP: {e}"))?;

    let tmp_path = pptx_path.with_extension("pptx.tmp");
    let tmp_file = std::fs::File::create(&tmp_path)
        .map_err(|e| format!("Failed to create temp file: {e}"))?;
    let mut writer = zip::ZipWriter::new(tmp_file);

    let mut total_rewritten = 0;
    let mut ole_links = 0usize;
    let mut chart_links = 0usize;

    for i in 0..reader.len() {
        let mut entry = reader.by_index(i)
            .map_err(|e| format!("Failed to read ZIP entry {i}: {e}"))?;

        let name = entry.name().to_string();
        let options = zip::write::SimpleFileOptions::default()
            .compression_method(entry.compression());

        if is_rels_file(&name) {
            // Read .rels XML and rewrite links
            let mut xml_data = Vec::new();
            entry.read_to_end(&mut xml_data)
                .map_err(|e| format!("Failed to read {name}: {e}"))?;

            match rewrite_rels_xml(&xml_data, &new_file_uri) {
                Ok((modified_xml, count)) => {
                    if count > 0 {
                        writer.start_file(&name, options)
                            .map_err(|e| format!("Failed to write {name}: {e}"))?;
                        writer.write_all(&modified_xml)
                            .map_err(|e| format!("Failed to write {name}: {e}"))?;
                        total_rewritten += count;

                        // Track OLE vs chart counts for summary
                        if name.contains("slides/_rels/") {
                            ole_links += count;
                        } else if name.contains("charts/_rels/") {
                            chart_links += count;
                        }
                    } else {
                        // No changes — write original
                        writer.start_file(&name, options)
                            .map_err(|e| format!("Failed to write {name}: {e}"))?;
                        writer.write_all(&xml_data)
                            .map_err(|e| format!("Failed to write {name}: {e}"))?;
                    }
                }
                Err(e) => {
                    // Per-file error handling — log and skip, don't fail entire operation
                    eprintln!("Warning: skipping {name}: {e}");
                    writer.start_file(&name, options)
                        .map_err(|e| format!("Failed to write {name}: {e}"))?;
                    writer.write_all(&xml_data)
                        .map_err(|e| format!("Failed to write {name}: {e}"))?;
                }
            }
        } else {
            // Copy non-.rels files unchanged
            writer.raw_copy_file(entry)
                .map_err(|e| format!("Failed to copy {name}: {e}"))?;
        }
    }

    writer.finish().map_err(|e| format!("Failed to finalize ZIP: {e}"))?;

    // Replace original with temp
    std::fs::rename(&tmp_path, pptx_path)
        .map_err(|e| {
            // Try to clean up temp file on failure
            let _ = std::fs::remove_file(&tmp_path);
            format!("Failed to replace PPTX: {e}")
        })?;

    Ok(RelinkResult { total: total_rewritten, ole: ole_links, charts: chart_links })
}

/// Check if a ZIP entry is a .rels file in slides/ or charts/.
///
/// Proper path-based detection instead of fragile substring matching (fixes Python bug #7).
fn is_rels_file(name: &str) -> bool {
    if !name.ends_with(".rels") {
        return false;
    }

    // Normalize to forward slashes for consistent parsing
    let normalized = name.replace('\\', "/");
    let parts: Vec<&str> = normalized.split('/').collect();

    // Expected: ppt/slides/_rels/slide1.xml.rels or ppt/charts/_rels/chart1.xml.rels
    // So we look for: _rels as second-to-last, and slides or charts as third-to-last
    if parts.len() >= 3 {
        let rels_dir = parts[parts.len() - 2];
        let parent_dir = parts[parts.len() - 3];
        return rels_dir == "_rels" && (parent_dir == "slides" || parent_dir == "charts");
    }

    false
}

/// Rewrite external link targets in a .rels XML file.
///
/// Finds all `<Relationship>` elements with `TargetMode="External"` and `Target`
/// starting with `file:///`, then replaces the file path portion while preserving
/// the `!sheet!range` suffix.
fn rewrite_rels_xml(data: &[u8], new_file_uri: &str) -> Result<(Vec<u8>, usize), String> {
    // Normalize the new URI for comparison
    let new_file_uri_normalized = new_file_uri.replace('\\', "/").to_lowercase();

    rewrite_xml_attributes(data, |elem| {
        // Only process Relationship elements
        let local_name = elem.local_name();
        if local_name.as_ref() != b"Relationship" {
            return false;
        }

        let target_mode = elem.try_get_attribute("TargetMode")
            .ok()
            .flatten()
            .map(|a| String::from_utf8_lossy(a.value.as_ref()).to_string())
            .unwrap_or_default();

        if target_mode != "External" {
            return false;
        }

        let target = match elem.try_get_attribute("Target").ok().flatten() {
            Some(a) => String::from_utf8_lossy(a.value.as_ref()).to_string(),
            None => return false,
        };

        // Handle two formats:
        // 1. file:///C:/path/to/file.xlsx  (OLE links in slides/_rels/)
        // 2. C:/path/to/file.xlsx          (chart links in charts/_rels/ — bare path)
        let is_file_uri = target.starts_with("file:///");
        let is_bare_path = !is_file_uri && (target.len() >= 2 && target.as_bytes().get(1) == Some(&b':'));

        if !is_file_uri && !is_bare_path {
            return false;
        }

        // Normalize the existing target for comparison (fixes Python bug #1: slash mismatch)
        let target_normalized = target.replace('\\', "/");

        // Split into file path and !sheet!range suffix
        let (existing_file_part, suffix) = split_file_uri_and_suffix(&target_normalized);
        let existing_file_normalized = existing_file_part.to_lowercase();

        // For bare paths, compare against the bare path version (strip file:/// from new_file_uri)
        let new_bare_path = new_file_uri.strip_prefix("file:///").unwrap_or(new_file_uri);
        let compare_against = if is_bare_path {
            new_bare_path.replace('\\', "/").to_lowercase()
        } else {
            new_file_uri_normalized.clone()
        };

        // Only rewrite if the file part actually differs
        if existing_file_normalized == compare_against {
            return false;
        }

        // Build new target: preserve the original format (file:/// or bare path)
        let new_path = if is_bare_path { new_bare_path } else { new_file_uri };
        let new_target = if suffix.is_empty() {
            new_path.to_string()
        } else {
            format!("{new_path}{suffix}")
        };

        // Rebuild all attributes, replacing Target
        let attrs: Vec<(String, String)> = elem.attributes()
            .filter_map(|a| a.ok())
            .map(|a| {
                let key = String::from_utf8_lossy(a.key.as_ref()).to_string();
                let val = if key == "Target" {
                    new_target.clone()
                } else {
                    String::from_utf8_lossy(a.value.as_ref()).to_string()
                };
                (key, val)
            })
            .collect();

        elem.clear_attributes();
        for (k, v) in attrs {
            elem.push_attribute((k.as_str(), v.as_str()));
        }

        true
    })
}

/// Split a `file:///path!Sheet!Range` URI into the file part and the suffix.
///
/// Handles filenames with `!` by looking for the pattern after the file extension.
/// The suffix always starts with `!` if present.
fn split_file_uri_and_suffix(uri: &str) -> (&str, &str) {
    // Find the last occurrence of a file extension (.xlsx, .xls, .xlsm) followed by !
    let lower = uri.to_lowercase();
    for ext in &[".xlsx!", ".xls!", ".xlsm!", ".xlsb!"] {
        if let Some(pos) = lower.find(ext) {
            let split_at = pos + ext.len() - 1; // Position of the '!' after extension
            return (&uri[..split_at], &uri[split_at..]);
        }
    }

    // Fallback: no known extension found, return entire URI as file part
    (uri, "")
}

/// Convert a Windows path to a file:/// URI.
///
/// Handles both local paths (`C:\...`) and UNC paths (`\\server\...`).
fn path_to_file_uri(path: &Path) -> String {
    let path_str = path.to_string_lossy().replace('\\', "/");

    // Strip \\?\ extended-length prefix first (before UNC check)
    let path_str = path_str.strip_prefix("//?/").unwrap_or(&path_str);

    // UNC paths: \\server\share → file://server/share
    if path_str.starts_with("//") {
        format!("file:{path_str}")
    } else {
        format!("file:///{path_str}")
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // --- is_rels_file tests ---

    #[test]
    fn test_is_rels_slide() {
        assert!(is_rels_file("ppt/slides/_rels/slide1.xml.rels"));
    }

    #[test]
    fn test_is_rels_chart() {
        assert!(is_rels_file("ppt/charts/_rels/chart1.xml.rels"));
    }

    #[test]
    fn test_is_rels_not_rels() {
        assert!(!is_rels_file("ppt/slides/slide1.xml"));
    }

    #[test]
    fn test_is_rels_document_level() {
        // ppt/_rels/presentation.xml.rels should NOT be processed
        assert!(!is_rels_file("ppt/_rels/presentation.xml.rels"));
    }

    #[test]
    fn test_is_rels_top_level() {
        assert!(!is_rels_file("_rels/.rels"));
    }

    #[test]
    fn test_is_rels_arbitrary_path() {
        assert!(!is_rels_file("ppt/myslides/_rels/fake.xml.rels"));
    }

    // --- split_file_uri_and_suffix tests ---

    #[test]
    fn test_split_with_suffix() {
        let (file, suffix) = split_file_uri_and_suffix(
            "file:///C:/Data/report.xlsx!Sheet1!R1C1:R5C5",
        );
        assert_eq!(file, "file:///C:/Data/report.xlsx");
        assert_eq!(suffix, "!Sheet1!R1C1:R5C5");
    }

    #[test]
    fn test_split_no_suffix() {
        let (file, suffix) = split_file_uri_and_suffix("file:///C:/Data/report.xlsx");
        assert_eq!(file, "file:///C:/Data/report.xlsx");
        assert_eq!(suffix, "");
    }

    #[test]
    fn test_split_xls_extension() {
        let (file, suffix) = split_file_uri_and_suffix(
            "file:///C:/Data/old.xls!Sheet1!A1",
        );
        assert_eq!(file, "file:///C:/Data/old.xls");
        assert_eq!(suffix, "!Sheet1!A1");
    }

    #[test]
    fn test_split_bang_in_filename() {
        // Python bug #8: filename with ! should not confuse the parser
        let (file, suffix) = split_file_uri_and_suffix(
            "file:///C:/Data/report!v2.xlsx!Sheet1!A1",
        );
        assert_eq!(file, "file:///C:/Data/report!v2.xlsx");
        assert_eq!(suffix, "!Sheet1!A1");
    }

    // --- path_to_file_uri tests ---

    #[test]
    fn test_local_path_to_uri() {
        let path = Path::new(r"C:\Users\data\report.xlsx");
        let uri = path_to_file_uri(path);
        assert_eq!(uri, "file:///C:/Users/data/report.xlsx");
    }

    #[test]
    fn test_extended_prefix_stripped() {
        let path = Path::new(r"\\?\C:\Users\data\report.xlsx");
        let uri = path_to_file_uri(path);
        assert_eq!(uri, "file:///C:/Users/data/report.xlsx");
    }

    // --- rewrite_rels_xml tests ---

    #[test]
    fn test_rewrite_rels_basic() {
        let xml = br#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
  <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/oleObject" Target="file:///C:/old/data.xlsx!Tables!R1C1:R5C5" TargetMode="External"/>
  <Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="../media/image1.png"/>
</Relationships>"#;

        let (output, count) = rewrite_rels_xml(xml, "file:///C:/new/report.xlsx").unwrap();
        assert_eq!(count, 1);

        let output_str = String::from_utf8(output).unwrap();
        assert!(output_str.contains("file:///C:/new/report.xlsx!Tables!R1C1:R5C5"));
        assert!(output_str.contains("../media/image1.png")); // unchanged
    }

    #[test]
    fn test_rewrite_no_external_links() {
        let xml = br#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
  <Relationship Id="rId1" Type="http://example.com" Target="../media/image1.png"/>
</Relationships>"#;

        let (_, count) = rewrite_rels_xml(xml, "file:///C:/new/report.xlsx").unwrap();
        assert_eq!(count, 0);
    }

    #[test]
    fn test_rewrite_preserves_suffix() {
        let xml = br#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
  <Relationship Id="rId1" Target="file:///C:/old/data.xlsx!Revenue!R1C1:R100C30" TargetMode="External"/>
</Relationships>"#;

        let (output, count) = rewrite_rels_xml(xml, "file:///C:/new/report.xlsx").unwrap();
        assert_eq!(count, 1);
        let output_str = String::from_utf8(output).unwrap();
        assert!(output_str.contains("file:///C:/new/report.xlsx!Revenue!R1C1:R100C30"));
    }

    #[test]
    fn test_rewrite_same_target_no_change() {
        let xml = br#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
  <Relationship Id="rId1" Target="file:///C:/same/data.xlsx!Sheet1!A1" TargetMode="External"/>
</Relationships>"#;

        let (_, count) = rewrite_rels_xml(xml, "file:///C:/same/data.xlsx").unwrap();
        assert_eq!(count, 0); // Same file, no change needed
    }

    #[test]
    fn test_rewrite_normalizes_backslashes() {
        // Python bug #1: existing target has backslashes, new URI has forward slashes
        let xml = br#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
  <Relationship Id="rId1" Target="file:///C:\old\data.xlsx!Sheet1!R1C1" TargetMode="External"/>
</Relationships>"#;

        let (output, count) = rewrite_rels_xml(xml, "file:///C:/new/report.xlsx").unwrap();
        assert_eq!(count, 1);
        let output_str = String::from_utf8(output).unwrap();
        assert!(output_str.contains("file:///C:/new/report.xlsx!Sheet1!R1C1"));
    }

    #[test]
    fn test_rewrite_multiple_links() {
        let xml = br#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
  <Relationship Id="rId1" Target="file:///C:/old/data.xlsx!Sheet1!R1C1" TargetMode="External"/>
  <Relationship Id="rId2" Target="file:///C:/old/data.xlsx!Sheet2!R1C1" TargetMode="External"/>
  <Relationship Id="rId3" Target="../media/image.png"/>
</Relationships>"#;

        let (_, count) = rewrite_rels_xml(xml, "file:///C:/new/report.xlsx").unwrap();
        assert_eq!(count, 2);
    }
}