xmpkit 0.1.3

Pure Rust implementation of Adobe XMP Toolkit
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
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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
//! XMP File API
//!
//! This module provides a high-level API for working with XMP metadata in files,
//! similar to the original xmp-toolkit-rs API, but with Wasm compatibility.

use crate::core::error::{XmpError, XmpResult};
use crate::core::metadata::XmpMeta;
use crate::files::handler::{FileHandler, XmpOptions};
use crate::files::registry::default_registry;
use std::io::{Cursor, Read, Seek, Write};

/// High-level API for working with XMP metadata in files
///
/// This struct provides a file-like API similar to the original xmp-toolkit-rs,
/// but works across all platforms including Wasm.
///
/// # Platform Support
///
/// - **Native platforms** (iOS, Android, macOS, Windows): Can use `open_file()`
/// - **Wasm**: Use `from_bytes()` or `from_reader()` with in-memory data
///
/// # File Update Behavior
///
/// When a file is opened with [`XmpOptions::for_update`], changes made via
/// [`XmpFile::put_xmp`] are not written to disk immediately. The file remains open
/// and changes are only written when [`XmpFile::close`] or [`XmpFile::try_close`] is called.
///
/// # Example
///
/// ```rust,no_run
/// use xmpkit::{XmpFile, XmpOptions, XmpMeta, XmpValue};
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let mut file = XmpFile::new();
/// file.open_with("image.jpg", XmpOptions::default().for_update())?;
///
/// if let Some(mut meta) = file.get_xmp().cloned() {
///     meta.set_property(
///         "http://ns.adobe.com/xap/1.0/",
///         "CreatorTool",
///         XmpValue::String("MyApp".to_string()),
///     )?;
///     file.put_xmp(meta);
/// }
///
/// // Changes are written to disk when try_close() is called
/// file.try_close()?;
/// # Ok(())
/// # }
/// ```
pub struct XmpFile {
    meta: Option<XmpMeta>,
    /// Original file path (for native platforms)
    #[cfg(not(target_arch = "wasm32"))]
    file_path: Option<std::path::PathBuf>,
    /// Original file data (for in-memory operations)
    #[allow(dead_code)] // Used in native code paths (open_with, try_close)
    file_data: Option<Vec<u8>>,
    /// Handler used to read/write the file
    #[allow(dead_code)] // Used in native code paths (open_with, try_close)
    handler: Option<crate::files::registry::Handler>,
    /// Open options
    #[allow(dead_code)] // Used in native code paths (open_with, try_close)
    options: XmpOptions,
    /// Whether the file is open
    is_open: bool,
}

impl XmpFile {
    /// Create a new empty XmpFile
    ///
    /// Use `open_file()` or `from_*()` methods to load metadata from a file.
    pub fn new() -> Self {
        Self {
            meta: None,
            #[cfg(not(target_arch = "wasm32"))]
            file_path: None,
            file_data: None,
            handler: None,
            options: XmpOptions::default(),
            is_open: false,
        }
    }

    /// Open a file from a path with options (native platforms only)
    ///
    /// # Platform Support
    ///
    /// - Native platforms (iOS, Android, macOS, Windows)
    /// - Wasm: Not supported (use `from_bytes()` or `from_reader()` instead)
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use xmpkit::{XmpFile, XmpOptions};
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut file = XmpFile::new();
    /// file.open_with("image.jpg", XmpOptions::default().for_update())?;
    /// # Ok(())
    /// # }
    /// ```
    #[cfg(not(target_arch = "wasm32"))]
    pub fn open_with<P: AsRef<std::path::Path>>(
        &mut self,
        path: P,
        options: XmpOptions,
    ) -> XmpResult<()> {
        use std::fs;
        let path = path.as_ref();

        // Check limited_scanning: only scan known file types
        // This check needs to happen before reading the file, so we do it here
        // rather than in from_reader_with_options
        if options.use_packet_scanning && options.limited_scanning {
            let file_ext = path
                .extension()
                .and_then(|ext| ext.to_str())
                .unwrap_or("")
                .to_lowercase();
            // Known file types that need scanning
            const KNOWN_SCANNED_FILES: &[&str] = &["txt", "xml", "html", "htm"];
            if !KNOWN_SCANNED_FILES.contains(&file_ext.as_str()) {
                return Err(XmpError::NotSupported(format!(
                    "File type '{}' not in limited scanning list",
                    file_ext
                )));
            }
        }

        // Read file and use from_reader_with
        let file = fs::File::open(path)?;
        self.file_path = Some(path.to_path_buf());
        self.from_reader_with(file, options)
    }

    /// Scan file content for XMP packet (packet scanning mode)
    ///
    /// This method searches for XMP packets in file content by looking for
    /// the `<?xpacket` marker. Used when packet scanning is requested.
    pub fn scan_for_xmp_packet(file_data: &[u8]) -> XmpResult<Option<XmpMeta>> {
        // Use byte search to find XMP packet (files may contain binary data)
        // Look for "<?xpacket" pattern
        let xpacket_start = b"<?xpacket";
        let mut search_pos = 0;

        while search_pos + xpacket_start.len() <= file_data.len() {
            // Find next occurrence of "<?xpacket"
            let Some(pos) = file_data[search_pos..]
                .windows(xpacket_start.len())
                .position(|window| window == xpacket_start)
            else {
                break;
            };

            let start_pos = search_pos + pos;

            // Find the end of the packet ("<?xpacket end")
            let xpacket_end_marker = b"<?xpacket end";
            let Some(packet_end_offset) = file_data[start_pos..]
                .windows(xpacket_end_marker.len())
                .position(|window| window.starts_with(xpacket_end_marker))
            else {
                search_pos = start_pos + 1;
                continue;
            };

            // Find the actual end: "<?xpacket end=\"w\"?>" or "<?xpacket end=\"r\"?>"
            // Search for "?>" after the end marker (should be close after "end=")
            let end_marker_start = start_pos + packet_end_offset;
            // Look for "?>" after "<?xpacket end" - it should be within a reasonable distance
            // (typically "<?xpacket end=\"w\"?>" or "<?xpacket end=\"r\"?>")
            let Some(close_pos) = file_data[end_marker_start..]
                .iter()
                .enumerate()
                .find(|(_, &b)| b == b'?')
                .and_then(|(q_pos, _)| {
                    if end_marker_start + q_pos + 1 < file_data.len()
                        && file_data[end_marker_start + q_pos + 1] == b'>'
                    {
                        // Verify this is actually the end of <?xpacket end (not just any ?>)
                        // Check that we have "end=" before the ?>
                        let before_close = &file_data[end_marker_start..end_marker_start + q_pos];
                        if before_close.ends_with(b"\"w\"") || before_close.ends_with(b"\"r\"") {
                            Some(q_pos + 2)
                        } else {
                            None
                        }
                    } else {
                        None
                    }
                })
            else {
                search_pos = start_pos + 1;
                continue;
            };

            let packet_end_pos = end_marker_start + close_pos;

            // Extract packet as string (XMP content should be valid UTF-8)
            if let Ok(packet_str) = std::str::from_utf8(&file_data[start_pos..packet_end_pos]) {
                // Try to parse the packet
                match XmpMeta::parse(packet_str) {
                    Ok(meta) => return Ok(Some(meta)),
                    Err(_) => {
                        // If parsing fails, continue searching for another packet
                        search_pos = start_pos + 1;
                        continue;
                    }
                }
            }

            search_pos = start_pos + 1;
        }

        Ok(None)
    }

    /// Open a file from a path (native platforms only)
    ///
    /// # Platform Support
    ///
    /// - Native platforms (iOS, Android, macOS, Windows)
    /// - Wasm: Not supported (use `from_bytes()` or `from_reader()` instead)
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use xmpkit::XmpFile;
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut file = XmpFile::new();
    /// file.open("image.jpg")?;
    /// # Ok(())
    /// # }
    /// ```
    #[cfg(not(target_arch = "wasm32"))]
    pub fn open<P: AsRef<std::path::Path>>(&mut self, path: P) -> XmpResult<()> {
        use std::fs::File;
        let file = File::open(path)?;
        self.from_reader(file)
    }

    /// Open a file from bytes (all platforms, including Wasm)
    ///
    /// This is the recommended method for Wasm environments.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use xmpkit::XmpFile;
    ///
    /// let jpeg_data: &[u8] = /* your JPEG file data */;
    /// let mut file = XmpFile::new();
    /// file.from_bytes(jpeg_data)?;
    /// ```
    pub fn from_bytes(&mut self, data: &[u8]) -> XmpResult<()> {
        self.from_bytes_with(data, XmpOptions::default())
    }

    /// Open a file from bytes with options (all platforms, including Wasm)
    ///
    /// This method allows you to specify opening options, such as forcing packet scanning
    /// or requiring a smart handler.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use xmpkit::{XmpFile, XmpOptions};
    ///
    /// let data: &[u8] = /* your file data */;
    /// let mut file = XmpFile::new();
    /// file.from_bytes_with(data, XmpOptions::default().use_packet_scanning())?;
    /// ```
    pub fn from_bytes_with(&mut self, data: &[u8], options: XmpOptions) -> XmpResult<()> {
        let cursor = Cursor::new(data);
        self.from_reader_with(cursor, options)
    }

    /// Open a file from a reader (all platforms, including Wasm)
    ///
    /// This is the most flexible method, accepting any type that implements
    /// `Read + Seek`. Use this when you have a custom reader or need maximum flexibility.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use std::io::Cursor;
    /// use xmpkit::XmpFile;
    ///
    /// let data: Vec<u8> = /* your JPEG file data */;
    /// let cursor = Cursor::new(data);
    /// let mut file = XmpFile::new();
    /// file.from_reader(cursor)?;
    /// ```
    pub fn from_reader<R: Read + Seek>(&mut self, reader: R) -> XmpResult<()> {
        self.from_reader_with(reader, XmpOptions::default())
    }

    /// Open a file from a reader with options (all platforms, including Wasm)
    ///
    /// This method allows you to specify opening options when reading from a reader.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use std::io::Cursor;
    /// use xmpkit::{XmpFile, XmpOptions};
    ///
    /// let data: Vec<u8> = /* your file data */;
    /// let cursor = Cursor::new(data);
    /// let mut file = XmpFile::new();
    /// file.from_reader_with(cursor, XmpOptions::default().strict())?;
    /// ```
    pub fn from_reader_with<R: Read + Seek>(
        &mut self,
        mut reader: R,
        options: XmpOptions,
    ) -> XmpResult<()> {
        // Reset state before opening (in case of retry)
        self.meta = None;
        #[cfg(not(target_arch = "wasm32"))]
        {
            self.handler = None;
            self.is_open = false;
        }
        self.options = options;
        self.file_data = None;

        // If packet scanning is requested, we need to read the entire file
        // Note: limited_scanning check is done in open_with (for file paths) before calling this
        if options.use_packet_scanning {
            let mut file_data = Vec::new();
            reader.read_to_end(&mut file_data)?;
            self.meta = Self::scan_for_xmp_packet(&file_data)?;
            // Store file_data only if we need it for writing
            if options.for_update {
                self.file_data = Some(file_data);
            }
            #[cfg(not(target_arch = "wasm32"))]
            {
                self.is_open = true;
            }
            return Ok(());
        }

        // Detect handler - this only peeks at file header, no need to read entire file
        let registry = default_registry();
        let handler = registry.find_by_detection(&mut reader)?;

        // Handle use_smart_handler: if set and no handler found, return error
        if options.use_smart_handler {
            let handler = handler.ok_or_else(|| {
                XmpError::NotSupported("No smart file handler available to handle file".to_string())
            })?;

            // For update mode, we need the file data for writing later
            if options.for_update {
                reader.rewind()?;
                let mut file_data = Vec::new();
                reader.read_to_end(&mut file_data)?;
                self.file_data = Some(file_data.clone());
                let mut reader_cursor = Cursor::new(&file_data);
                self.meta = handler.read_xmp(&mut reader_cursor, &options)?;
            } else {
                // Read-only mode: read XMP directly from stream without loading entire file
                reader.rewind()?;
                self.meta = handler.read_xmp(&mut reader, &options)?;
            }

            #[cfg(not(target_arch = "wasm32"))]
            {
                self.handler = Some(handler.clone());
                self.is_open = true;
            }
            return Ok(());
        }

        // Handle strict: if set and no handler found, return error (don't fall back)
        if options.strict {
            let handler = handler.ok_or_else(|| {
                XmpError::NotSupported("No handler available for file format".to_string())
            })?;

            // For update mode, we need the file data for writing later
            if options.for_update {
                reader.rewind()?;
                let mut file_data = Vec::new();
                reader.read_to_end(&mut file_data)?;
                self.file_data = Some(file_data.clone());
                let mut reader_cursor = Cursor::new(&file_data);
                self.meta = handler.read_xmp(&mut reader_cursor, &options)?;
            } else {
                // Read-only mode: read XMP directly from stream without loading entire file
                reader.rewind()?;
                self.meta = handler.read_xmp(&mut reader, &options)?;
            }

            #[cfg(not(target_arch = "wasm32"))]
            {
                self.handler = Some(handler.clone());
                self.is_open = true;
            }
            return Ok(());
        }

        // Normal case: try to find handler, fall back to packet scanning if not found
        if let Some(handler) = handler {
            // For update mode, we need the file data for writing later
            if options.for_update {
                reader.rewind()?;
                let mut file_data = Vec::new();
                reader.read_to_end(&mut file_data)?;
                self.file_data = Some(file_data.clone());
                let mut reader_cursor = Cursor::new(&file_data);
                self.meta = handler.read_xmp(&mut reader_cursor, &options)?;
            } else {
                // Read-only mode: read XMP directly from stream without loading entire file
                reader.rewind()?;
                self.meta = handler.read_xmp(&mut reader, &options)?;
            }

            #[cfg(not(target_arch = "wasm32"))]
            {
                self.handler = Some(handler.clone());
                self.is_open = true;
            }
            Ok(())
        } else {
            // No handler found, need to read entire file for packet scanning fallback
            reader.rewind()?;
            let mut file_data = Vec::new();
            reader.read_to_end(&mut file_data)?;
            self.meta = Self::scan_for_xmp_packet(&file_data)?;
            // Store file_data only if we need it for writing
            if options.for_update {
                self.file_data = Some(file_data);
            }
            #[cfg(not(target_arch = "wasm32"))]
            {
                self.is_open = true;
            }
            Ok(())
        }
    }

    /// Get the XMP metadata
    ///
    /// Returns `None` if no metadata has been loaded or found.
    pub fn get_xmp(&self) -> Option<&XmpMeta> {
        self.meta.as_ref()
    }

    /// Get mutable reference to XMP metadata
    ///
    /// Returns `None` if no metadata has been loaded or found.
    pub fn get_xmp_mut(&mut self) -> Option<&mut XmpMeta> {
        self.meta.as_mut()
    }

    /// Put XMP metadata
    ///
    /// Replaces any existing metadata.
    ///
    /// # Update Behavior
    ///
    /// - If the file was opened with [`XmpOptions::for_update`], changes are
    ///   not written to disk immediately. Call [`XmpFile::close`] or [`XmpFile::try_close`]
    ///   to write changes to disk.
    /// - If the file was opened read-only, this only updates the in-memory metadata.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use xmpkit::{XmpFile, XmpOptions, XmpMeta, XmpValue};
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut file = XmpFile::new();
    /// file.open_with("image.jpg", XmpOptions::default().for_update())?;
    ///
    /// let mut meta = file.get_xmp().cloned().unwrap_or_else(XmpMeta::new);
    /// meta.set_property(
    ///     "http://ns.adobe.com/xap/1.0/",
    ///     "CreatorTool",
    ///     XmpValue::String("MyApp".to_string()),
    /// )?;
    /// file.put_xmp(meta);
    ///
    /// // Write changes to disk
    /// file.try_close()?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn put_xmp(&mut self, meta: XmpMeta) {
        self.meta = Some(meta);
        // Note: Changes are written to disk when close() or try_close() is called
    }

    /// Explicitly closes an opened file.
    ///
    /// Performs any necessary output to the file and closes it. Files that are
    /// opened for update are written to only when closing.
    ///
    /// If the file is opened for read-only access (using
    /// [`XmpOptions::for_read`]), the disk file is closed
    /// immediately after reading the data from it; the `XmpFile`
    /// struct, however, remains in the open state. You must call
    /// [`XmpFile::close`] when finished using it.
    ///
    /// # Platform Support
    ///
    /// - **Native platforms**: Writes changes to disk if opened for update
    /// - **Wasm**: Only cleans up internal state (file writing not supported)
    ///
    /// # Errors
    ///
    /// This method ignores errors for backward compatibility. If you want to
    /// handle errors, use [`XmpFile::try_close`] instead.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use xmpkit::{XmpFile, XmpOptions};
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut file = XmpFile::new();
    /// file.open_with("image.jpg", XmpOptions::default().for_update())?;
    /// // ... modify metadata ...
    /// file.close(); // Ignores errors
    /// # Ok(())
    /// # }
    /// ```
    pub fn close(&mut self) {
        let _ = self.try_close();
        // Ignore error for backward compatibility
    }

    /// Explicitly closes an opened file with error handling.
    ///
    /// Performs any necessary output to the file and closes it. Files that are
    /// opened for update are written to only when closing.
    ///
    /// If the file is opened for read-only access (using
    /// [`XmpOptions::for_read`]), the disk file is closed
    /// immediately after reading the data from it; the `XmpFile`
    /// struct, however, remains in the open state. You must call
    /// [`XmpFile::try_close`] when finished using it.
    ///
    /// # Platform Support
    ///
    /// - **Native platforms**: Writes changes to disk if opened for update
    /// - **Wasm**: Only cleans up internal state (file writing not supported)
    ///
    /// # Errors
    ///
    /// Returns an error if writing the file fails (native platforms only).
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use xmpkit::{XmpFile, XmpOptions};
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut file = XmpFile::new();
    /// file.open_with("image.jpg", XmpOptions::default().for_update())?;
    /// // ... modify metadata ...
    /// file.try_close()?; // Returns error if write fails
    /// # Ok(())
    /// # }
    /// ```
    pub fn try_close(&mut self) -> XmpResult<()> {
        if !self.is_open {
            return Ok(());
        }

        // On native, if opened for update, write changes to disk
        #[cfg(not(target_arch = "wasm32"))]
        {
            if self.options.for_update {
                if let Some(ref path) = self.file_path {
                    if let Some(ref meta) = self.meta {
                        use std::fs::File;
                        use std::io::BufWriter;

                        // If handler is None (e.g., packet scanning mode), detect handler from file data
                        let handler = if let Some(ref h) = self.handler {
                            h.clone()
                        } else {
                            // Use file_data to detect handler (avoid re-opening file)
                            let registry = default_registry();
                            let mut reader =
                                Cursor::new(self.file_data.as_ref().ok_or_else(|| {
                                    XmpError::BadValue(
                                        "File data not available for handler detection. \
                                        This can happen if the file was opened in read-only mode. \
                                        Use XmpOptions::for_update() to enable writing."
                                            .to_string(),
                                    )
                                })?);
                            registry
                                .find_by_detection(&mut reader)?
                                .ok_or_else(|| {
                                    XmpError::NotSupported(
                                        "Unsupported file format for writing".to_string(),
                                    )
                                })?
                                .clone()
                        };

                        // Read original file content first (before creating new file)
                        let file_data = self
                            .file_data
                            .as_ref()
                            .ok_or_else(|| {
                                XmpError::BadValue(
                                    "File data not available for writing. \
                                    This can happen if the file was opened in read-only mode. \
                                    Use XmpOptions::for_update() to enable writing."
                                        .to_string(),
                                )
                            })?
                            .clone();
                        let mut reader = Cursor::new(&file_data);

                        // Write to same file (or create new one)
                        let mut writer = BufWriter::new(File::create(path)?);

                        // Write XMP
                        handler.write_xmp(&mut reader, &mut writer, meta)?;
                        writer.flush()?;
                    }
                }
            }
        }

        // On Wasm, we can't write to files, so just clean up state
        #[cfg(target_arch = "wasm32")]
        {
            // No-op
        }

        self.is_open = false;
        Ok(())
    }

    /// Write XMP metadata to a file path (native platforms only)
    ///
    /// # Platform Support
    ///
    /// - Native platforms (iOS, Android, macOS, Windows)
    /// - Wasm: Not supported (use `write_to_bytes()` or `write_to_writer()` instead)
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use xmpkit::{XmpFile, XmpMeta};
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut file = XmpFile::new();
    /// file.open("image.jpg")?;
    /// // ... modify metadata ...
    /// file.save("output.jpg")?;
    /// # Ok(())
    /// # }
    /// ```
    #[cfg(not(target_arch = "wasm32"))]
    pub fn save<P: AsRef<std::path::Path>>(&self, path: P) -> XmpResult<()> {
        use std::fs::File;
        let file = File::create(path)?;
        self.write_to_writer(file)
    }

    /// Write XMP metadata to bytes (all platforms, including Wasm)
    ///
    /// This is the recommended method for Wasm environments.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use xmpkit::XmpFile;
    ///
    /// let input_data: &[u8] = /* your JPEG file data */;
    /// let mut file = XmpFile::new();
    /// file.from_bytes(input_data)?;
    /// // ... modify metadata ...
    /// let output_data = file.write_to_bytes()?;
    /// ```
    pub fn write_to_bytes(&self) -> XmpResult<Vec<u8>> {
        let mut buffer = Vec::new();
        let cursor = Cursor::new(&mut buffer);
        self.write_to_writer(cursor)?;
        Ok(buffer)
    }

    /// Write XMP metadata to a writer (all platforms, including Wasm)
    ///
    /// This is the most flexible method, accepting any type that implements
    /// `Write + Seek`.
    ///
    /// # Note
    ///
    /// This method requires the original file data to be available. The file data
    /// is only stored when:
    /// - The file was opened with [`XmpOptions::for_update`]
    /// - The file was opened with packet scanning mode
    /// - No handler was found and packet scanning fallback was used
    ///
    /// For read-only operations where a handler was found, the file data is not
    /// stored to save memory. In this case, use [`XmpFile::open_with`] with
    /// [`XmpOptions::for_update`] if you need to write changes.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use std::io::Cursor;
    /// use xmpkit::{XmpFile, XmpOptions};
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut file = XmpFile::new();
    /// // Open with for_update to enable writing
    /// file.open_with("image.jpg", XmpOptions::default().for_update())?;
    /// // ... modify metadata ...
    /// let mut output = Vec::new();
    /// let cursor = Cursor::new(&mut output);
    /// file.write_to_writer(cursor)?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn write_to_writer<W: Write + Seek>(&self, mut writer: W) -> XmpResult<()> {
        // Get XMP metadata
        let meta = self.meta.as_ref().ok_or_else(|| {
            XmpError::BadValue("No XMP metadata available for writing".to_string())
        })?;

        // Get original file data
        let file_data = self.file_data.as_ref().ok_or_else(|| {
            XmpError::BadValue(
                "Original file data not available for writing. \
                To write XMP metadata, open the file with XmpOptions::for_update()."
                    .to_string(),
            )
        })?;

        // Detect handler from file data
        let registry = default_registry();
        let mut reader = Cursor::new(file_data);
        let handler = registry.find_by_detection(&mut reader)?.ok_or_else(|| {
            XmpError::NotSupported("Unsupported file format for writing".to_string())
        })?;

        // Reset reader position
        reader.set_position(0);

        // Write XMP using handler
        handler.write_xmp(&mut reader, &mut writer, meta)?;
        writer.flush()?;

        Ok(())
    }
}

impl Default for XmpFile {
    fn default() -> Self {
        Self::new()
    }
}

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

    #[test]
    fn test_new() {
        let file = XmpFile::new();
        assert!(file.get_xmp().is_none());
    }

    #[test]
    fn test_from_bytes_empty() {
        let mut file = XmpFile::new();
        // Empty data should fail format detection (no handler can handle it)
        let result = file.from_bytes(&[]);
        // Either returns error or Ok but with no XMP (handler not found)
        assert!(result.is_err() || file.get_xmp().is_none());
    }

    #[test]
    fn test_put_and_get_xmp() {
        let mut file = XmpFile::new();
        let meta = XmpMeta::new();
        file.put_xmp(meta);
        assert!(file.get_xmp().is_some());
    }
}