This repository was archived by the owner on Apr 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 431
Expand file tree
/
Copy pathstorage.ios.ts
More file actions
246 lines (203 loc) · 7.11 KB
/
storage.ios.ts
File metadata and controls
246 lines (203 loc) · 7.11 KB
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
import { firebase } from "../firebase-common";
import { DeleteFileOptions, DownloadFileOptions, GetDownloadUrlOptions, ListOptions, Reference, UploadFileOptions, UploadFileResult } from "./storage";
import { ListResult as ListResultBase } from "./storage-common";
function getReference(nativeReference: FIRStorageReference, listOptions: ListOptions): Reference {
return {
ios: nativeReference,
bucket: nativeReference.bucket,
name: nativeReference.name,
fullPath: nativeReference.fullPath,
listAll: (): Promise<ListResult> => listAll({ remoteFullPath: nativeReference.fullPath, bucket: listOptions.bucket })
};
}
function getReferences(nativeReferences: NSArray<FIRStorageReference>, listOptions: ListOptions): Array<Reference> {
const references: Array<Reference> = [];
for (let i = 0, l = nativeReferences.count; i < l; i++) {
const ref = nativeReferences.objectAtIndex(i);
references.push(getReference(ref, listOptions));
}
return references;
}
class ListResult extends ListResultBase {
ios: FIRStorageListResult;
constructor(private listResult: FIRStorageListResult, private listOptions: ListOptions) {
super(getReferences(listResult.items, listOptions), getReferences(listResult.prefixes, listOptions), listResult.pageToken);
this.ios = listResult;
// don't expose these
delete this.listResult;
delete this.listOptions;
}
}
function getStorageRef(reject, arg): FIRStorageReference {
if (typeof (FIRStorage) === "undefined") {
reject("Uncomment Storage in the plugin's Podfile first");
return undefined;
}
if (!arg.remoteFullPath) {
reject("remoteFullPath is mandatory");
return undefined;
}
if (arg.bucket) {
return FIRStorage.storage().referenceForURL(arg.bucket);
} else if (firebase.storageBucket) {
return firebase.storageBucket;
} else {
return FIRStorage.storage().reference();
}
}
export function uploadFile(arg: UploadFileOptions): Promise<any> {
return new Promise((resolve, reject) => {
try {
const onCompletion = (metadata: FIRStorageMetadata, error: NSError) => {
if (error) {
reject(error);
} else {
resolve({
name: metadata.name,
// url: metadata.downloadURL() ? metadata.downloadURL().absoluteString : null,
contentType: metadata.contentType,
created: metadata.timeCreated,
updated: metadata.updated,
bucket: metadata.bucket,
size: metadata.size
});
}
};
const storageRef: FIRStorageReference = getStorageRef(reject, arg);
if (!storageRef) {
return;
}
const fIRStorageReference = storageRef.child(arg.remoteFullPath);
let fIRStorageUploadTask = null;
if (arg.localFile) {
if (typeof (arg.localFile) !== "object") {
reject("localFile argument must be a File object; use file-system module to create one");
return;
}
// using 'putFile' (not 'putData') so Firebase can infer the mime-type
fIRStorageUploadTask = fIRStorageReference.putFileMetadataCompletion(NSURL.fileURLWithPath(arg.localFile.path), null, onCompletion);
} else if (arg.localFullPath) {
fIRStorageUploadTask = fIRStorageReference.putFileMetadataCompletion(NSURL.fileURLWithPath(arg.localFullPath), null, onCompletion);
} else {
reject("One of localFile or localFullPath is required");
return;
}
if (fIRStorageUploadTask !== null) {
// Add a progress observer to an upload task
fIRStorageUploadTask.observeStatusHandler(FIRStorageTaskStatus.Progress, snapshot => {
if (!snapshot.error && typeof (arg.onProgress) === "function") {
arg.onProgress({
fractionCompleted: snapshot.progress.fractionCompleted,
percentageCompleted: Math.round(snapshot.progress.fractionCompleted * 100)
});
}
});
}
} catch (ex) {
console.log("Error in firebase.uploadFile: " + ex);
reject(ex);
}
});
}
export function downloadFile(arg: DownloadFileOptions): Promise<any> {
return new Promise((resolve, reject) => {
try {
const onCompletion = (url, error) => {
if (error) {
reject(error);
} else {
resolve(url.absoluteString);
}
};
const storageRef = getStorageRef(reject, arg);
if (!storageRef) {
return;
}
const fIRStorageReference = storageRef.child(arg.remoteFullPath);
let localFilePath;
if (arg.localFile) {
if (typeof (arg.localFile) !== "object") {
reject("localFile argument must be a File object; use file-system module to create one");
return;
}
localFilePath = arg.localFile.path;
} else if (arg.localFullPath) {
localFilePath = arg.localFullPath;
} else {
reject("One of localFile or localFullPath is required");
return;
}
// Create local filesystem URL
const localFileUrl = NSURL.fileURLWithPath(localFilePath);
fIRStorageReference.writeToFileCompletion(localFileUrl, onCompletion);
} catch (ex) {
console.log("Error in firebase.downloadFile: " + ex);
reject(ex);
}
});
}
export function getDownloadUrl(arg: GetDownloadUrlOptions): Promise<any> {
return new Promise((resolve, reject) => {
try {
const onCompletion = (url, error) => {
if (error) {
reject(error);
} else {
resolve(url.absoluteString);
}
};
const storageRef = getStorageRef(reject, arg);
if (!storageRef) {
return;
}
const fIRStorageReference = storageRef.child(arg.remoteFullPath);
fIRStorageReference.downloadURLWithCompletion(onCompletion);
} catch (ex) {
console.log("Error in firebase.getDownloadUrl: " + ex);
reject(ex);
}
});
}
export function deleteFile(arg: DeleteFileOptions): Promise<any> {
return new Promise<any>((resolve, reject) => {
try {
const onCompletion = error => {
if (error) {
reject(error);
} else {
resolve();
}
};
const storageRef = getStorageRef(reject, arg);
if (!storageRef) {
return;
}
const fIRStorageFileRef = storageRef.child(arg.remoteFullPath);
fIRStorageFileRef.deleteWithCompletion(onCompletion);
} catch (ex) {
console.log("Error in firebase.deleteFile: " + ex);
reject(ex);
}
});
}
export function listAll(listOptions: ListOptions): Promise<ListResult> {
return new Promise<ListResult>((resolve, reject) => {
try {
const storageRef = getStorageRef(reject, listOptions);
if (!storageRef) {
return;
}
const fIRStorageReference = storageRef.child(listOptions.remoteFullPath);
fIRStorageReference.listAllWithCompletion((result: FIRStorageListResult, error: NSError) => {
if (error) {
reject(error);
} else {
resolve(new ListResult(result, listOptions));
}
});
} catch (ex) {
console.log("Error in firebase.listAll: " + ex);
reject(ex);
}
});
}