forked from NativeScript/firebase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.android.ts
641 lines (543 loc) · 15.6 KB
/
index.android.ts
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
import { FirebaseApp, FirebaseError, firebase } from '@nativescript/firebase-core';
import { TaskEvent, StringFormat } from './common';
import { IMetadata, IListResult, IReference, IStorage, ITask, ITaskSnapshot, TaskSnapshotObserver, ListOptions } from '.';
import { b64WithoutPrefix, getMIMEforBase64String } from './utils';
export { TaskSnapshotObserver, StringFormat, TaskEvent };
let defaultStorage: Storage;
const fb = firebase();
Object.defineProperty(fb, 'storage', {
value: (app?: FirebaseApp) => {
if (!app) {
if (!defaultStorage) {
defaultStorage = new Storage();
}
return defaultStorage;
}
return new Storage(app);
},
writable: false,
});
export class TaskSnapshot implements ITaskSnapshot {
_native: com.google.firebase.storage.FileDownloadTask.TaskSnapshot | com.google.firebase.storage.UploadTask.TaskSnapshot;
static fromNative(value: com.google.firebase.storage.FileDownloadTask.TaskSnapshot | com.google.firebase.storage.UploadTask.TaskSnapshot) {
if (value instanceof com.google.firebase.storage.FileDownloadTask.TaskSnapshot || value instanceof com.google.firebase.storage.UploadTask.TaskSnapshot) {
const ss = new TaskSnapshot();
ss._native = value;
return ss;
}
return null;
}
get native() {
return this._native;
}
get android() {
return this.native;
}
get bytesTransferred(): number {
return this.native?.getBytesTransferred?.();
}
get error(): FirebaseError {
const error = this.native.getError?.();
if (error) {
return FirebaseError.fromNative(error);
}
return null;
}
get metadata(): Metadata {
return Metadata.fromNative(this.native.getStorage().getMetadata());
}
get ref(): Reference {
return Reference.fromNative(this.native.getStorage());
}
get state() {
return org.nativescript.firebase.storage.FirebaseStorage.StorageReference.getStatus(this.native) as any;
}
get task(): Task {
return Task.fromNative(this.native.getTask() as any);
}
get totalBytes(): number {
return this.native?.getTotalByteCount?.();
}
}
export class Task implements ITask {
_native: com.google.firebase.storage.FileDownloadTask | com.google.firebase.storage.UploadTask;
static fromNative(value: com.google.firebase.storage.FileDownloadTask | com.google.firebase.storage.UploadTask) {
if (value instanceof com.google.firebase.storage.FileDownloadTask || value instanceof com.google.firebase.storage.UploadTask) {
const task = new Task();
task._native = value;
return task;
}
return null;
}
get native() {
return this._native;
}
get android() {
return this.native;
}
get snapshot(): TaskSnapshot {
return TaskSnapshot.fromNative(this.native.getSnapshot?.());
}
cancel(): boolean {
return this.native?.cancel();
}
on(event: TaskEvent, nextOrObserver?: TaskSnapshotObserver | ((a: TaskSnapshot) => any), error?: (a: FirebaseError) => any, complete?: () => void) {
if (event !== TaskEvent.STATE_CHANGED) {
return;
}
let hasNextOrObserver = false;
if (nextOrObserver) {
if (typeof nextOrObserver === 'function') {
hasNextOrObserver = true;
} else if (typeof nextOrObserver === 'object') {
hasNextOrObserver = true;
}
}
org.nativescript.firebase.storage.FirebaseStorage.StorageTask.on(
this.native,
hasNextOrObserver,
!!error,
!!complete,
new org.nativescript.firebase.storage.FirebaseStorage.StorageTask.Callback<any>({
onNextOrObserver(param0) {
if (hasNextOrObserver) {
const task = TaskSnapshot.fromNative(param0);
if (typeof nextOrObserver === 'function') {
nextOrObserver(task);
} else {
nextOrObserver?.next(task);
}
}
},
onComplete() {
if (hasNextOrObserver) {
if (typeof nextOrObserver === 'object') {
nextOrObserver?.complete?.();
}
}
complete?.();
},
onError(param0) {
if (nextOrObserver) {
if (typeof nextOrObserver === 'object') {
nextOrObserver?.error?.(FirebaseError.fromNative(param0));
}
}
error?.(FirebaseError.fromNative(param0));
},
})
);
}
pause(): boolean {
return this.native?.pause();
}
resume() {
return this.native?.resume();
}
}
export class ListResult implements IListResult {
_native: com.google.firebase.storage.ListResult;
static fromNative(value: com.google.firebase.storage.ListResult) {
if (value instanceof com.google.firebase.storage.ListResult) {
const list = new ListResult();
list._native = value;
return list;
}
return null;
}
get native() {
return this._native;
}
get android() {
return this.native;
}
get items(): Reference[] {
const items = this.native.getItems?.();
const count = items.size();
const result = [];
for (let i = 0; i < count; i++) {
result.push(Reference.fromNative(items.get(i)));
}
return result;
}
get nextPageToken(): string {
return this.native.getPageToken?.();
}
get prefixes(): Reference[] {
const items = this.native.getPrefixes?.();
const count = items.size();
const result = [];
for (let i = 0; i < count; i++) {
result.push(Reference.fromNative(items.get(i)));
}
return result;
}
}
export class Metadata implements IMetadata {
_native: com.google.firebase.storage.StorageMetadata;
_builder: com.google.firebase.storage.StorageMetadata.Builder;
constructor() {
this._builder = new com.google.firebase.storage.StorageMetadata.Builder();
}
static fromNative(value: com.google.firebase.storage.StorageMetadata) {
if (value instanceof com.google.firebase.storage.StorageMetadata) {
const meta = new Metadata();
meta._native = value;
meta._builder = null;
return meta;
}
return null;
}
get native() {
if (this._builder) {
this._native = this._builder.build();
}
return this._native;
}
get android() {
return this.native;
}
get bucket(): string {
return this.native.getBucket?.();
}
get cacheControl(): string {
return this.native.getCacheControl?.();
}
private _createBuilder() {
if (this._native) {
this._builder = new com.google.firebase.storage.StorageMetadata.Builder(this._native);
} else {
this._builder = new com.google.firebase.storage.StorageMetadata.Builder();
}
}
set cacheControl(value) {
if (!this._builder) {
this._createBuilder();
}
this._builder.setCacheControl?.(value);
}
get contentDisposition(): string {
return this.native.getContentDisposition?.();
}
set contentDisposition(value) {
if (!this._builder) {
this._createBuilder();
}
this._builder.setContentDisposition?.(value);
}
get contentEncoding(): string {
return this.native.getContentEncoding?.();
}
set contentEncoding(value) {
if (!this._builder) {
this._createBuilder();
}
this._builder.setContentEncoding?.(value);
}
get contentLanguage(): string {
return this.native.getContentLanguage?.();
}
set contentLanguage(value) {
if (!this._builder) {
this._createBuilder();
}
this._builder.setContentLanguage?.(value);
}
get contentType(): string {
return this.native.getContentType?.();
}
set contentType(value) {
if (!this._builder) {
this._createBuilder();
}
this._builder.setContentType?.(value);
}
get customMetadata(): { [key: string]: string } {
const meta = org.nativescript.firebase.storage.FirebaseStorage.StorageMetadata.getCustomMetadata(this.native);
let result = {};
try {
result = JSON.parse(meta);
} catch (e) {}
return result;
}
set customMetadata(value) {
if (!this._builder) {
this._createBuilder();
}
try {
org.nativescript.firebase.storage.FirebaseStorage.StorageMetadata.setCustomMetadata(this._builder, JSON.stringify(value));
} catch (e) {}
}
get fullPath(): string {
return this.native.getPath?.();
}
get generation(): string {
return this.native.getGeneration?.();
}
get md5hash(): string {
return this.native?.getMd5Hash?.();
}
get metageneration(): string {
return this.native.getMetadataGeneration?.();
}
get name(): string {
return this.native.getName?.();
}
get size(): number {
return this.native.getSizeBytes?.();
}
get timeCreated() {
const time = this.native.getCreationTimeMillis?.();
if (time) {
return new Date(time);
}
return undefined;
}
get updated() {
const time = this.native.getUpdatedTimeMillis?.();
if (time) {
return new Date(time);
}
return undefined;
}
}
export class Reference implements IReference {
_native: com.google.firebase.storage.StorageReference;
static fromNative(value: com.google.firebase.storage.StorageReference) {
if (value instanceof com.google.firebase.storage.StorageReference) {
const ref = new Reference();
ref._native = value;
return ref;
}
return null;
}
get native() {
return this._native;
}
get android() {
return this.native;
}
get bucket(): string {
return this.native?.getBucket?.();
}
get fullPath(): string {
return this.native?.getPath?.();
}
get name(): string {
return this.native?.getName?.();
}
get parent(): Reference {
return Reference.fromNative(this.native.getParent?.());
}
get root(): Reference {
return Reference.fromNative(this.native.getRoot?.());
}
get storage() {
return Storage.fromNative(this.native.getStorage());
}
child(path: string): Reference {
return Reference.fromNative(this.native.child(path));
}
delete(): Promise<void> {
return new Promise((resolve, reject) => {
org.nativescript.firebase.storage.FirebaseStorage.StorageReference.delete(
this.native,
new org.nativescript.firebase.storage.FirebaseStorage.Callback<java.lang.Void>({
onError(param0) {
reject(FirebaseError.fromNative(param0));
},
onSuccess(param0) {
resolve();
},
})
);
});
}
getDownloadURL(): Promise<string> {
return new Promise((resolve, reject) => {
org.nativescript.firebase.storage.FirebaseStorage.StorageReference.getDownloadURL(
this.native,
new org.nativescript.firebase.storage.FirebaseStorage.Callback<android.net.Uri>({
onError(param0) {
reject(FirebaseError.fromNative(param0));
},
onSuccess(param0) {
resolve(param0.toString());
},
})
);
});
}
getMetadata(): Promise<Metadata> {
return new Promise((resolve, reject) => {
org.nativescript.firebase.storage.FirebaseStorage.StorageReference.getMetadata(
this.native,
new org.nativescript.firebase.storage.FirebaseStorage.Callback<com.google.firebase.storage.StorageMetadata>({
onError(param0) {
reject(FirebaseError.fromNative(param0));
},
onSuccess(param0) {
resolve(Metadata.fromNative(param0));
},
})
);
});
}
list(options?: ListOptions): Promise<ListResult> {
return new Promise((resolve, reject) => {
org.nativescript.firebase.storage.FirebaseStorage.StorageReference.list(
this.native,
options?.maxResults ?? 1000,
options.pageToken || null,
new org.nativescript.firebase.storage.FirebaseStorage.Callback<com.google.firebase.storage.ListResult>({
onError(param0) {
reject(FirebaseError.fromNative(param0));
},
onSuccess(param0) {
resolve(ListResult.fromNative(param0));
},
})
);
});
}
listAll(): Promise<ListResult> {
return new Promise((resolve, reject) => {
org.nativescript.firebase.storage.FirebaseStorage.StorageReference.listAll(
this.native,
new org.nativescript.firebase.storage.FirebaseStorage.Callback<com.google.firebase.storage.ListResult>({
onError(param0) {
reject(FirebaseError.fromNative(param0));
},
onSuccess(param0) {
resolve(ListResult.fromNative(param0));
},
})
);
});
}
put(data: Blob | Uint8Array | ArrayBuffer, metadata?: Metadata): Task {
if (data instanceof Blob) {
const ab = (Blob as any).InternalAccessor.getBuffer(data).buffer.slice(0);
if (metadata) {
return Task.fromNative(this.native.putBytes(Array.from(ab), metadata.native));
}
return Task.fromNative(this.native.putBytes(Array.from(ab)));
} else if (data instanceof Uint8Array || data instanceof ArrayBuffer) {
if (metadata) {
return Task.fromNative(this.native.putBytes(Array.from(data as any), metadata.native));
}
return Task.fromNative(this.native.putBytes(Array.from(data as any)));
}
return null;
}
putString(data: string, format: StringFormat = StringFormat.RAW, metadata?: Metadata): Task {
switch (format) {
case StringFormat.DATA_URL:
const base64 = b64WithoutPrefix(data);
const mime = getMIMEforBase64String(data);
const meta = metadata || new Metadata();
if (!metadata.contentType) {
meta.contentType = mime;
}
org.nativescript.firebase.storage.FirebaseStorage.StorageReference.putString(this.native, base64, format, meta.native);
default:
return Task.fromNative(org.nativescript.firebase.storage.FirebaseStorage.StorageReference.putString(this.native, data, format, metadata?.native || null));
}
}
putFile(path: string, metadata?: Metadata): Task {
let task: Task = null;
if (typeof path === 'string' && path.startsWith('/')) {
path = `file://${path}`;
}
try {
if (metadata) {
task = Task.fromNative(this.native.putFile(android.net.Uri.parse(path), metadata.native));
} else {
task = Task.fromNative(this.native.putFile(android.net.Uri.parse(path)));
}
} catch (e) {}
return task;
}
updateMetadata(metadata: Metadata): Promise<Metadata> {
return new Promise((resolve, reject) => {
org.nativescript.firebase.storage.FirebaseStorage.StorageReference.updateMetadata(
this.native,
metadata.native,
new org.nativescript.firebase.storage.FirebaseStorage.Callback<com.google.firebase.storage.StorageMetadata>({
onError(param0) {
reject(FirebaseError.fromNative(param0));
},
onSuccess(param0) {
resolve(Metadata.fromNative(param0));
},
})
);
});
}
writeToFile(localFilePath: string): Task {
return Task.fromNative(this.native.getFile(new java.io.File(localFilePath)));
}
}
export class Storage implements IStorage {
_native: com.google.firebase.storage.FirebaseStorage;
_app: FirebaseApp;
constructor(app?: FirebaseApp) {
if (app?.native) {
this._native = com.google.firebase.storage.FirebaseStorage.getInstance(app.native);
} else {
if (defaultStorage) {
return defaultStorage;
}
defaultStorage = this;
this._native = com.google.firebase.storage.FirebaseStorage.getInstance();
}
}
static fromNative(storage: com.google.firebase.storage.FirebaseStorage) {
if (storage instanceof com.google.firebase.storage.FirebaseStorage) {
const store = new Storage();
store._native = storage;
return store;
}
return null;
}
useEmulator(host: string, port: number) {
this.native.useEmulator(host === 'localhost' || host === '127.0.0.1' ? '10.0.2.2' : host, port);
}
ref(path?: string): Reference {
return Reference.fromNative(this.native.getReference(path || '/'));
}
refFromURL(url: string): Reference {
return Reference.fromNative(this.native.getReferenceFromUrl(url));
}
get native() {
return this._native;
}
get android() {
return this.native;
}
get app(): FirebaseApp {
if (!this._app) {
// @ts-ignore
this._app = FirebaseApp.fromNative(this.native.app);
}
return this._app;
}
get maxDownloadRetryTime(): number {
return this.native?.getMaxDownloadRetryTimeMillis?.() ?? 0 / 1000;
}
set maxDownloadRetryTime(value) {
this.native.setMaxDownloadRetryTimeMillis?.(value * 1000);
}
get maxOperationRetryTime(): number {
return this.native?.getMaxOperationRetryTimeMillis?.() ?? 0 / 1000;
}
set maxOperationRetryTime(value) {
this.native.setMaxOperationRetryTimeMillis?.(value * 1000);
}
get maxUploadRetryTime(): number {
return this.native?.getMaxUploadRetryTimeMillis?.() ?? 0 / 1000;
}
set maxUploadRetryTime(value) {
this.native.setMaxUploadRetryTimeMillis(value * 1000);
}
}