resolveMetadataFromUploadDescription function

String? resolveMetadataFromUploadDescription(
  1. String uploadDescription,
  2. String key
)

Reads the object metadata key from a Ground Control upload JSON description: the metadata map of a resumable description, or the x-goog-meta- header of a binary one. Returns null if the value is not found or not a non-empty string.

Implementation

String? resolveMetadataFromUploadDescription(
  String uploadDescription,
  String key,
) {
  try {
    final decoded = jsonDecode(uploadDescription);
    if (decoded is! Map<String, dynamic>) {
      return null;
    }
    final metadata = decoded['metadata'];
    final headers = decoded['headers'];
    final value = metadata is Map
        ? metadata[key]
        : headers is Map
        ? headers['x-goog-meta-$key']
        : null;
    if (value is! String) {
      return null;
    }
    final trimmed = value.trim();
    if (trimmed.isEmpty) {
      return null;
    }
    return trimmed;
  } on FormatException {
    return null;
  }
}