@nativescript/firebase-storage
Version:
NativeScript Firebase - Storage
520 lines • 16.8 kB
JavaScript
import { FirebaseApp, FirebaseError, firebase } from '@nativescript/firebase-core';
import { TaskEvent, StringFormat } from './common';
import { b64WithoutPrefix, getMIMEforBase64String } from './utils';
export { StringFormat, TaskEvent };
let defaultStorage;
const fb = firebase();
Object.defineProperty(fb, 'storage', {
value: (app) => {
if (!app) {
if (!defaultStorage) {
defaultStorage = new Storage();
}
return defaultStorage;
}
return new Storage(app);
},
writable: false,
});
export class TaskSnapshot {
static fromNative(value) {
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() {
return this.native?.getBytesTransferred?.();
}
get error() {
const error = this.native.getError?.();
if (error) {
return FirebaseError.fromNative(error);
}
return null;
}
get metadata() {
return Metadata.fromNative(this.native.getStorage().getMetadata());
}
get ref() {
return Reference.fromNative(this.native.getStorage());
}
get state() {
return org.nativescript.firebase.storage.FirebaseStorage.StorageReference.getStatus(this.native);
}
get task() {
return Task.fromNative(this.native.getTask());
}
get totalBytes() {
return this.native?.getTotalByteCount?.();
}
}
export class Task {
static fromNative(value) {
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() {
return TaskSnapshot.fromNative(this.native.getSnapshot?.());
}
cancel() {
return this.native?.cancel();
}
on(event, nextOrObserver, error, complete) {
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({
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() {
return this.native?.pause();
}
resume() {
return this.native?.resume();
}
}
export class ListResult {
static fromNative(value) {
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() {
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() {
return this.native.getPageToken?.();
}
get prefixes() {
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 {
constructor() {
this._builder = new com.google.firebase.storage.StorageMetadata.Builder();
}
static fromNative(value) {
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() {
return this.native.getBucket?.();
}
_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();
}
}
get cacheControl() {
return this.native.getCacheControl?.();
}
set cacheControl(value) {
if (!this._builder) {
this._createBuilder();
}
this._builder.setCacheControl?.(value);
}
get contentDisposition() {
return this.native.getContentDisposition?.();
}
set contentDisposition(value) {
if (!this._builder) {
this._createBuilder();
}
this._builder.setContentDisposition?.(value);
}
get contentEncoding() {
return this.native.getContentEncoding?.();
}
set contentEncoding(value) {
if (!this._builder) {
this._createBuilder();
}
this._builder.setContentEncoding?.(value);
}
get contentLanguage() {
return this.native.getContentLanguage?.();
}
set contentLanguage(value) {
if (!this._builder) {
this._createBuilder();
}
this._builder.setContentLanguage?.(value);
}
get contentType() {
return this.native.getContentType?.();
}
set contentType(value) {
if (!this._builder) {
this._createBuilder();
}
this._builder.setContentType?.(value);
}
get customMetadata() {
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() {
return this.native.getPath?.();
}
get generation() {
return this.native.getGeneration?.();
}
get md5hash() {
return this.native?.getMd5Hash?.();
}
get metageneration() {
return this.native.getMetadataGeneration?.();
}
get name() {
return this.native.getName?.();
}
get size() {
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 {
static fromNative(value) {
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() {
return this.native?.getBucket?.();
}
get fullPath() {
return this.native?.getPath?.();
}
get name() {
return this.native?.getName?.();
}
get parent() {
return Reference.fromNative(this.native.getParent?.());
}
get root() {
return Reference.fromNative(this.native.getRoot?.());
}
get storage() {
return Storage.fromNative(this.native.getStorage());
}
child(path) {
return Reference.fromNative(this.native.child(path));
}
delete() {
return new Promise((resolve, reject) => {
org.nativescript.firebase.storage.FirebaseStorage.StorageReference.delete(this.native, new org.nativescript.firebase.storage.FirebaseStorage.Callback({
onError(param0) {
reject(FirebaseError.fromNative(param0));
},
onSuccess(param0) {
resolve();
},
}));
});
}
getDownloadURL() {
return new Promise((resolve, reject) => {
org.nativescript.firebase.storage.FirebaseStorage.StorageReference.getDownloadURL(this.native, new org.nativescript.firebase.storage.FirebaseStorage.Callback({
onError(param0) {
reject(FirebaseError.fromNative(param0));
},
onSuccess(param0) {
resolve(param0.toString());
},
}));
});
}
getMetadata() {
return new Promise((resolve, reject) => {
org.nativescript.firebase.storage.FirebaseStorage.StorageReference.getMetadata(this.native, new org.nativescript.firebase.storage.FirebaseStorage.Callback({
onError(param0) {
reject(FirebaseError.fromNative(param0));
},
onSuccess(param0) {
resolve(Metadata.fromNative(param0));
},
}));
});
}
list(options) {
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({
onError(param0) {
reject(FirebaseError.fromNative(param0));
},
onSuccess(param0) {
resolve(ListResult.fromNative(param0));
},
}));
});
}
listAll() {
return new Promise((resolve, reject) => {
org.nativescript.firebase.storage.FirebaseStorage.StorageReference.listAll(this.native, new org.nativescript.firebase.storage.FirebaseStorage.Callback({
onError(param0) {
reject(FirebaseError.fromNative(param0));
},
onSuccess(param0) {
resolve(ListResult.fromNative(param0));
},
}));
});
}
put(data, metadata) {
if (data instanceof Blob) {
const ab = Blob.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), metadata.native));
}
return Task.fromNative(this.native.putBytes(Array.from(data)));
}
return null;
}
putString(data, format = StringFormat.RAW, metadata) {
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);
}
break;
default:
return Task.fromNative(org.nativescript.firebase.storage.FirebaseStorage.StorageReference.putString(this.native, data, format, metadata?.native || null));
}
}
putFile(path, metadata) {
let 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) {
return new Promise((resolve, reject) => {
org.nativescript.firebase.storage.FirebaseStorage.StorageReference.updateMetadata(this.native, metadata.native, new org.nativescript.firebase.storage.FirebaseStorage.Callback({
onError(param0) {
reject(FirebaseError.fromNative(param0));
},
onSuccess(param0) {
resolve(Metadata.fromNative(param0));
},
}));
});
}
writeToFile(localFilePath) {
return Task.fromNative(this.native.getFile(new java.io.File(localFilePath)));
}
}
export class Storage {
constructor(app) {
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) {
if (storage instanceof com.google.firebase.storage.FirebaseStorage) {
const store = new Storage();
store._native = storage;
return store;
}
return null;
}
useEmulator(host, port) {
this.native.useEmulator(host === 'localhost' || host === '127.0.0.1' ? '10.0.2.2' : host, port);
}
ref(path) {
return Reference.fromNative(this.native.getReference(path || '/'));
}
refFromURL(url) {
return Reference.fromNative(this.native.getReferenceFromUrl(url));
}
get native() {
return this._native;
}
get android() {
return this.native;
}
get app() {
if (!this._app) {
// @ts-ignore
this._app = FirebaseApp.fromNative(this.native.app);
}
return this._app;
}
get maxDownloadRetryTime() {
return this.native?.getMaxDownloadRetryTimeMillis?.() ?? 0 / 1000;
}
set maxDownloadRetryTime(value) {
this.native.setMaxDownloadRetryTimeMillis?.(value * 1000);
}
get maxOperationRetryTime() {
return this.native?.getMaxOperationRetryTimeMillis?.() ?? 0 / 1000;
}
set maxOperationRetryTime(value) {
this.native.setMaxOperationRetryTimeMillis?.(value * 1000);
}
get maxUploadRetryTime() {
return this.native?.getMaxUploadRetryTimeMillis?.() ?? 0 / 1000;
}
set maxUploadRetryTime(value) {
this.native.setMaxUploadRetryTimeMillis(value * 1000);
}
}
//# sourceMappingURL=index.android.js.map