Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit 3430b6d

Browse files
Pass custom metadata to "uploadFile()" function. #1478
1 parent d718ba6 commit 3430b6d

File tree

6 files changed

+122
-12
lines changed

6 files changed

+122
-12
lines changed

demo/app/main-view-model.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { Observable } from "tns-core-modules/data/observable";
66
import * as fs from "tns-core-modules/file-system";
77
import { isAndroid, isIOS } from "tns-core-modules/platform";
88
import { alert, prompt } from "tns-core-modules/ui/dialogs";
9+
import { UploadMetadata } from "../../src/storage/storage";
910
import { MessagingViewModel } from "./messaging-view-model";
1011

1112
const firebaseWebApi = require("nativescript-plugin-firebase/app");
@@ -314,7 +315,16 @@ export class HelloWorldModel extends Observable {
314315
const storageRef = firebaseWebApi.storage().ref();
315316
const childRef = storageRef.child("uploads/images/telerik-logo-uploaded.png");
316317

317-
childRef.put(fs.File.fromPath(logoPath)).then(
318+
const metadata: UploadMetadata = {
319+
contentType: "demo/test",
320+
contentLanguage: "fr",
321+
customMetadata: {
322+
"foo": "bar",
323+
"foo2": "bar2"
324+
}
325+
};
326+
327+
childRef.put(fs.File.fromPath(logoPath), metadata).then(
318328
uploadedFile => {
319329
console.log("Uploaded! " + JSON.stringify(uploadedFile));
320330
this.set("storageFeedback", "Uploaded!");
@@ -1639,13 +1649,23 @@ export class HelloWorldModel extends Observable {
16391649
const appPath = fs.knownFolders.currentApp().path;
16401650
const logoPath = appPath + "/images/telerik-logo.png";
16411651

1652+
const metadata: UploadMetadata = {
1653+
contentType: "demo/test2",
1654+
contentLanguage: "de",
1655+
customMetadata: {
1656+
"first": "first!",
1657+
"second": "second!"
1658+
}
1659+
};
1660+
16421661
firebaseStorage.uploadFile({
16431662
remoteFullPath: 'uploads/images/telerik-logo-uploaded.png',
16441663
localFile: fs.File.fromPath(logoPath), // use this (a file-system module File object)
16451664
// localFullPath: logoPath, // or this, a full file path
16461665
onProgress: status => {
16471666
console.log("Uploaded fraction: " + status.fractionCompleted + " (" + status.percentageCompleted + "%)");
1648-
}
1667+
},
1668+
metadata
16491669
}).then(
16501670
uploadedFile => {
16511671
alert({

docs/STORAGE.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ You can either pass in a full local path to a file, or (as a convenience) use th
5959
// determine the path to a file in the app/res folder
6060
var logoPath = appPath + "/res/telerik-logo.png";
6161

62+
var metadata = {
63+
contentType: "demo/test",
64+
contentLanguage: "fr",
65+
customMetadata: {
66+
"foo": "bar",
67+
"foo2": "bar2"
68+
}
69+
};
70+
6271
// now upload the file with either of the options below:
6372
firebase.storage.uploadFile({
6473
// optional, can be omitted since 6.5.0, and also be passed during init() as 'storageBucket' param so we can cache it (find it in the Firebase console)
@@ -73,7 +82,8 @@ You can either pass in a full local path to a file, or (as a convenience) use th
7382
onProgress: function(status) {
7483
console.log("Uploaded fraction: " + status.fractionCompleted);
7584
console.log("Percentage complete: " + status.percentageCompleted);
76-
}
85+
},
86+
metadata
7787
}).then(
7888
function (uploadedFile) {
7989
console.log("File uploaded: " + JSON.stringify(uploadedFile));
@@ -100,7 +110,15 @@ You can either pass in a full local path to a file, or (as a convenience) use th
100110
const storageRef = firebaseWebApi.storage().ref();
101111
const childRef = storageRef.child("uploads/images/telerik-logo-uploaded.png");
102112

103-
childRef.put(fs.File.fromPath(logoPath)).then(
113+
const metadata = {
114+
contentType: "demo/test",
115+
contentLanguage: "fr",
116+
customMetadata: {
117+
"foo": "bar",
118+
"foo2": "bar2"
119+
}
120+
};
121+
childRef.put(fs.File.fromPath(logoPath), metadata).then(
104122
uploadedFile => console.log("Uploaded! " + JSON.stringify(uploadedFile)),
105123
error => console.log("firebase.doWebUploadFile error: " + error)
106124
);

src/app/storage/index.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { File } from "tns-core-modules/file-system"
22
import * as firebaseStorage from "../../storage/storage";
3-
import { ListResult, UploadFileResult } from "../../storage/storage";
3+
import { ListResult, UploadFileResult, UploadMetadata } from "../../storage/storage";
44

55
export module storage {
66

@@ -9,6 +9,10 @@ export module storage {
99
totalBytes: number;
1010
}
1111

12+
export interface Metadata {
13+
string: string;
14+
}
15+
1216
export class Reference {
1317

1418
private path: string;
@@ -43,19 +47,26 @@ export module storage {
4347
});
4448
}
4549

50+
getMetadata(): Promise<string> {
51+
return firebaseStorage.getDownloadUrl({
52+
remoteFullPath: this.path
53+
});
54+
}
55+
4656
listAll(): Promise<ListResult> {
4757
return firebaseStorage.listAll({
4858
remoteFullPath: this.path
4959
});
5060
}
5161

52-
public put(data: File | string /* path */, metadata?: any /* ignored */): Promise<UploadTaskSnapshot> {
62+
public put(data: File | string /* path */, metadata?: UploadMetadata): Promise<UploadTaskSnapshot> {
5363
return new Promise((resolve, reject) => {
5464
firebaseStorage.uploadFile({
5565
localFile: data instanceof File ? data : undefined,
5666
localFullPath: !(data instanceof File) ? data : undefined,
5767
remoteFullPath: this.path,
58-
onProgress: progress => console.log(`Upload progress: ${progress.percentageCompleted}% completed`)
68+
onProgress: progress => console.log(`Upload progress: ${progress.percentageCompleted}% completed`),
69+
metadata
5970
}).then((result: UploadFileResult) => {
6071
this.getDownloadURL()
6172
.then(url => {

src/storage/storage.android.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,32 @@ export function uploadFile(arg: UploadFileOptions): Promise<UploadFileResult> {
101101
}
102102
});
103103

104+
let metadata: com.google.firebase.storage.StorageMetadata = null;
105+
if (arg.metadata) {
106+
const metadataBuilder = new com.google.firebase.storage.StorageMetadata.Builder();
107+
if (arg.metadata.cacheControl) {
108+
metadataBuilder.setCacheControl(arg.metadata.cacheControl);
109+
}
110+
if (arg.metadata.contentDisposition) {
111+
metadataBuilder.setContentDisposition(arg.metadata.contentDisposition);
112+
}
113+
if (arg.metadata.contentEncoding) {
114+
metadataBuilder.setContentEncoding(arg.metadata.contentEncoding);
115+
}
116+
if (arg.metadata.contentLanguage) {
117+
metadataBuilder.setContentLanguage(arg.metadata.contentLanguage);
118+
}
119+
if (arg.metadata.contentType) {
120+
metadataBuilder.setContentType(arg.metadata.contentType);
121+
}
122+
if (arg.metadata.customMetadata) {
123+
for (let p in arg.metadata.customMetadata) {
124+
metadataBuilder.setCustomMetadata(p, arg.metadata.customMetadata[p]);
125+
}
126+
}
127+
metadata = metadataBuilder.build();
128+
}
129+
104130
if (arg.localFile) {
105131
if (typeof (arg.localFile) !== "object") {
106132
reject("localFile argument must be a File object; use file-system module to create one");
@@ -109,7 +135,7 @@ export function uploadFile(arg: UploadFileOptions): Promise<UploadFileResult> {
109135

110136
// using 'putFile' (not 'putBytes') so Firebase can infer the mimetype
111137
const localFileUrl = android.net.Uri.fromFile(new java.io.File(arg.localFile.path));
112-
storageReference.putFile(localFileUrl)
138+
storageReference.putFile(localFileUrl, metadata)
113139
.addOnFailureListener(onFailureListener)
114140
.addOnSuccessListener(onSuccessListener)
115141
.addOnProgressListener(onProgressListener);
@@ -137,7 +163,7 @@ export function uploadFile(arg: UploadFileOptions): Promise<UploadFileResult> {
137163
}
138164

139165
const localFileUrl = android.net.Uri.fromFile(new java.io.File(arg.localFullPath));
140-
storageReference.putFile(localFileUrl)
166+
storageReference.putFile(localFileUrl, metadata)
141167
.addOnFailureListener(onFailureListener)
142168
.addOnSuccessListener(onSuccessListener)
143169
.addOnProgressListener(onProgressListener);

src/storage/storage.d.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,19 @@ export interface ProgressStatus {
33
percentageCompleted: Number;
44
}
55

6+
interface SettableMetadata {
7+
cacheControl?: string | null;
8+
contentDisposition?: string | null;
9+
contentEncoding?: string | null;
10+
contentLanguage?: string | null;
11+
contentType?: string | null;
12+
customMetadata?: { [key: string]: string; };
13+
}
14+
15+
interface UploadMetadata extends SettableMetadata {
16+
// md5Hash?: string | null;
17+
}
18+
619
/**
720
* Use either the 'localFile' or 'localFullPath' param to upload a file.
821
*/
@@ -37,7 +50,12 @@ export interface UploadFileOptions {
3750
* console.log("Percentage complete: " + status.percentageCompleted);
3851
* }
3952
*/
40-
onProgress: (data: ProgressStatus) => void;
53+
onProgress?: (data: ProgressStatus) => void;
54+
55+
/**
56+
*
57+
*/
58+
metadata?: UploadMetadata;
4159
}
4260

4361
export interface UploadFileResult {

src/storage/storage.ios.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,34 @@ export function uploadFile(arg: UploadFileOptions): Promise<UploadFileResult> {
8181
const fIRStorageReference = storageRef.child(arg.remoteFullPath);
8282
let fIRStorageUploadTask = null;
8383

84+
let metadata: FIRStorageMetadata = null;
85+
if (arg.metadata) {
86+
metadata = FIRStorageMetadata.new();
87+
metadata.cacheControl = arg.metadata.cacheControl;
88+
metadata.contentDisposition = arg.metadata.contentDisposition;
89+
metadata.contentEncoding = arg.metadata.contentEncoding;
90+
metadata.contentLanguage = arg.metadata.contentLanguage;
91+
metadata.contentType = arg.metadata.contentType;
92+
if (arg.metadata.customMetadata) {
93+
const customMetadata = NSMutableDictionary.new();
94+
for (let p in arg.metadata.customMetadata) {
95+
customMetadata.setObjectForKey(arg.metadata.customMetadata[p], p);
96+
}
97+
metadata.customMetadata = <any>customMetadata;
98+
}
99+
}
100+
84101
if (arg.localFile) {
85102
if (typeof (arg.localFile) !== "object") {
86103
reject("localFile argument must be a File object; use file-system module to create one");
87104
return;
88105
}
89106

90107
// using 'putFile' (not 'putData') so Firebase can infer the mime-type
91-
fIRStorageUploadTask = fIRStorageReference.putFileMetadataCompletion(NSURL.fileURLWithPath(arg.localFile.path), null, onCompletion);
108+
fIRStorageUploadTask = fIRStorageReference.putFileMetadataCompletion(NSURL.fileURLWithPath(arg.localFile.path), metadata, onCompletion);
92109

93110
} else if (arg.localFullPath) {
94-
fIRStorageUploadTask = fIRStorageReference.putFileMetadataCompletion(NSURL.fileURLWithPath(arg.localFullPath), null, onCompletion);
111+
fIRStorageUploadTask = fIRStorageReference.putFileMetadataCompletion(NSURL.fileURLWithPath(arg.localFullPath), metadata, onCompletion);
95112

96113
} else {
97114
reject("One of localFile or localFullPath is required");

0 commit comments

Comments
 (0)