Skip to content

Commit

Permalink
feat(storage): get custom metadata (#667)
Browse files Browse the repository at this point in the history
* add the customMetadata in the getMetadata method

* add changeset

* Update lazy-islands-invite.md

---------

Co-authored-by: Robin Genz <[email protected]>
  • Loading branch information
mamillastre and robingenz authored Jul 3, 2024
1 parent 5ab166a commit 552cbb1
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/lazy-islands-invite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@capacitor-firebase/storage': minor
---

feat: get custom metadata
23 changes: 12 additions & 11 deletions packages/storage/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,17 +299,18 @@ On Android, the cleartext traffic must be allowed. On the Capacitor configuratio

#### GetMetadataResult

| Prop | Type | Description | Since |
| ------------------------ | ------------------- | --------------------------------------------------------------------------------- | ----- |
| **`bucket`** | <code>string</code> | The bucket this file is contained in. | 5.3.0 |
| **`createdAt`** | <code>number</code> | The timestamp at which the file was created in milliseconds since the epoch. | 5.3.0 |
| **`generation`** | <code>string</code> | The object's generation. | 5.3.0 |
| **`md5Hash`** | <code>string</code> | The md5 hash of the file. | 5.3.0 |
| **`metadataGeneration`** | <code>string</code> | The object's metadata generation. | 5.3.0 |
| **`name`** | <code>string</code> | The short name of this file, which is the last component of the full path. | 5.3.0 |
| **`path`** | <code>string</code> | The full path to the file, including the file name. | 5.3.0 |
| **`size`** | <code>number</code> | The size of the file in bytes. | 5.3.0 |
| **`updatedAt`** | <code>number</code> | The timestamp at which the file was last updated in milliseconds since the epoch. | 5.3.0 |
| Prop | Type | Description | Since |
| ------------------------ | --------------------------------------- | --------------------------------------------------------------------------------- | ----- |
| **`bucket`** | <code>string</code> | The bucket this file is contained in. | 5.3.0 |
| **`createdAt`** | <code>number</code> | The timestamp at which the file was created in milliseconds since the epoch. | 5.3.0 |
| **`generation`** | <code>string</code> | The object's generation. | 5.3.0 |
| **`md5Hash`** | <code>string</code> | The md5 hash of the file. | 5.3.0 |
| **`metadataGeneration`** | <code>string</code> | The object's metadata generation. | 5.3.0 |
| **`name`** | <code>string</code> | The short name of this file, which is the last component of the full path. | 5.3.0 |
| **`path`** | <code>string</code> | The full path to the file, including the file name. | 5.3.0 |
| **`size`** | <code>number</code> | The size of the file in bytes. | 5.3.0 |
| **`updatedAt`** | <code>number</code> | The timestamp at which the file was last updated in milliseconds since the epoch. | 5.3.0 |
| **`customMetadata`** | <code>{ [key: string]: string; }</code> | Additional user-defined custom metadata. | 6.1.0 |


#### GetMetadataOptions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.getcapacitor.JSObject;
import com.google.firebase.storage.StorageMetadata;
import io.capawesome.capacitorjs.plugins.firebase.storage.interfaces.Result;
import java.util.Set;

public class GetMetadataResult implements Result {

Expand All @@ -23,6 +24,16 @@ public JSObject toJSObject() {
result.put("path", referenceResult.getPath());
result.put("size", referenceResult.getSizeBytes());
result.put("updatedAt", referenceResult.getUpdatedTimeMillis());

Set<String> customMetadataKeys = referenceResult.getCustomMetadataKeys();
if (!customMetadataKeys.isEmpty()) {
JSObject customMetadata = new JSObject();
for (String key : customMetadataKeys) {
customMetadata.put(key, referenceResult.getCustomMetadata(key));
}
result.put("customMetadata", customMetadata);
}

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ import Capacitor
if let updated = metadata.updated {
result["updatedAt"] = (updated.timeIntervalSince1970 * 1000.0).rounded()
}
if let customMetadata = metadata.customMetadata {
result["customMetadata"] = customMetadata as JSObject
}
return result as AnyObject
}
}
8 changes: 8 additions & 0 deletions packages/storage/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,14 @@ export interface GetMetadataResult {
* @example 1697304435933
*/
updatedAt: number;
/**
* Additional user-defined custom metadata.
*
* @since 6.1.0
*/
customMetadata?: {
[key: string]: string;
};
}

/**
Expand Down
1 change: 1 addition & 0 deletions packages/storage/src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export class FirebaseStorageWeb
path: metadata.fullPath,
size: metadata.size,
updatedAt: new Date(metadata.updated).getTime(),
customMetadata: metadata.customMetadata,
};
if (metadata.md5Hash) {
result.md5Hash = metadata.md5Hash;
Expand Down

0 comments on commit 552cbb1

Please sign in to comment.