updateMetadata method

  1. @override
Future<void> updateMetadata(
  1. String key, {
  2. String? contentType,
  3. Map<String, String> metadata = const {},
})
override

Update metadata for an existing object without touching its bytes.

Used after streaming writes where some metadata field (e.g. the original-plaintext size for an encrypted file) is only known once the stream completes.

  • Local: rewrites the .meta.json sidecar.
  • IndexedDB: re-reads the record and writes it back with the updated metadata fields.
  • A remote store: copy-to-itself with new metadata (when it has no in-place metadata update).

Implementation

@override
Future<void> updateMetadata(
  String key, {
  String? contentType,
  Map<String, String> metadata = const {},
}) async {
  checkNotDisposed();
  // Preserve the internal encryption flags. metadata is REPLACED, not merged,
  // so passing only user metadata would strip _metaEncrypted/_metaOriginalSize —
  // leaving readStream, readRange, and head unable to tell the object is
  // encrypted, handing back ciphertext as plaintext.
  final info = await _inner.head(key);
  if (info == null || info.metadata[_metaEncrypted] != 'true') {
    return _inner.updateMetadata(
      key,
      contentType: contentType,
      metadata: metadata,
    );
  }
  final originalSize = info.metadata[_metaOriginalSize];
  return _inner.updateMetadata(
    key,
    contentType: contentType,
    metadata: {
      ...metadata,
      _metaEncrypted: 'true',
      _metaOriginalSize: ?originalSize,
    },
  );
}