UNPKG

@triniwiz/nativescript-downloader

Version:

Download Manager for NativeScript

220 lines 8.9 kB
import { DownloaderBase, StatusCode, generateId } from './common'; import { Utils, path as nsPath, knownFolders } from '@nativescript/core'; export class Downloader extends DownloaderBase { constructor() { super(); if (!Downloader._fetch) { Downloader._setFetcher(); } } static _setFetcher() { if (!this._downloadRequests) { Downloader._downloadRequests = new Map(); } if (!this._downloads) { Downloader._downloads = new Map(); } if (!this._downloadsData) { Downloader._downloadsData = new Map(); } Downloader._okHttpClient = new okhttp3.OkHttpClient.Builder().readTimeout(Downloader._timeout, java.util.concurrent.TimeUnit.SECONDS).writeTimeout(Downloader._timeout, java.util.concurrent.TimeUnit.SECONDS).build(); const config = new com.tonyodev.fetch2.FetchConfiguration.Builder(Utils.android.getApplicationContext()); config.setHttpDownloader(new com.tonyodev.fetch2okhttp.OkHttpDownloader(Downloader._okHttpClient)); config.setDownloadConcurrentLimit(10); config.enableAutoStart(false); Downloader._fetch = com.tonyodev.fetch2.Fetch.Impl.getInstance(config.build()); Downloader._fetch.addListener(new com.tonyodev.fetch2.FetchListener({ onAdded(param0) { }, onQueued(param0, param1) { }, onWaitingNetwork(param0) { }, onCompleted(param0) { const request = param0.getRequest(); const id = Downloader._downloadRequests.get(request.getId()); if (Downloader._downloads.has(id)) { const data = Downloader._downloadsData.get(id); const resolve = data.resolve; Downloader._downloadsData.set(id, Object.assign({}, data, { status: StatusCode.COMPLETED, })); if (resolve) { resolve({ status: StatusCode.COMPLETED, path: request.getFileUri()?.toString(), }); } } }, onError(param0, error, param2) { const id = Downloader._downloadRequests.get(param0.getRequest().getId()); if (Downloader._downloads.has(id)) { const data = Downloader._downloadsData.get(id); Downloader._downloadsData.set(id, Object.assign({}, data, { status: StatusCode.ERROR, })); const reject = data.reject; if (reject) { reject({ status: StatusCode.ERROR, message: error.getHttpResponse?.().getErrorResponse?.(), native: error, }); } } }, onDownloadBlockUpdated(param0, param1, param2) { }, onStarted(param0, param1, param2) { }, onProgress(param0, param1, param2) { const id = Downloader._downloadRequests.get(param0.getRequest().getId()); if (Downloader._downloads.has(id)) { const data = Downloader._downloadsData.get(id); const callback = data.callback; if (data.status !== StatusCode.DOWNLOADING) { Downloader._downloadsData.set(id, Object.assign({}, data, { status: StatusCode.DOWNLOADING, })); } if (callback && typeof callback === 'function') { const progress = param0.getProgress(); callback({ value: progress, speed: param2, currentSize: param0.getDownloaded(), totalSize: param0.getTotal(), }); } } }, onPaused(param0) { const requestId = Downloader._downloadRequests.get(param0.getRequest().getId()); if (Downloader._downloads.has(requestId)) { const data = Downloader._downloadsData.get(requestId); Downloader._downloadsData.set(requestId, Object.assign({}, data, { status: StatusCode.PAUSED, })); } }, onResumed(param0) { }, onCancelled(param0) { }, onRemoved(param0) { }, onDeleted(param0) { }, })); } static setTimeout(timeout) { Downloader._timeout = timeout; Downloader._setFetcher(); } createDownload(options) { if (options && !options.url) throw new Error('Url missing'); const taskId = generateId(); let url; let query; if (options.query) { if (typeof options.query === 'object') { const keysArray = Object.keys(options.query); query = ''; for (const key of keysArray) { query += key + '=' + options.query[key] + '&'; } } else if (typeof options.query === 'string') { query = options.query; } url = encodeURI(options.url + query); } else { url = options.url; } let path = ''; if (options.path && options.fileName) { path = nsPath.join(options.path, options.fileName); } else if (!options.path && options.fileName) { path = nsPath.join(knownFolders.temp().path, options.fileName); } else if (options.path && !options.fileName) { path = nsPath.join(options.path, `${generateId()}`); } else { path = nsPath.join(knownFolders.temp().path, `${generateId()}`); } const request = new com.tonyodev.fetch2.Request(url, path); request.setNetworkType(com.tonyodev.fetch2.NetworkType.ALL); request.setDownloadOnEnqueue(false); if (options.headers) { const keysArray = Object.keys(options.headers); for (const key of keysArray) { request.addHeader(key, options.headers[key]); } } Downloader._fetch.enqueue(request, null, null); const requestId = request.getId(); Downloader._downloads.set(taskId, requestId); Downloader._downloadRequests.set(requestId, taskId); Downloader._downloadsData.set(taskId, { status: StatusCode.PENDING, }); return taskId; } getStatus(id) { if (id && Downloader._downloads.has(id)) { const data = Downloader._downloadsData.get(id); return data.status; } return StatusCode.PENDING; } start(id, progress) { return new Promise((resolve, reject) => { if (id) { const data = Downloader._downloadsData.get(id); Downloader._downloadsData.set(id, Object.assign({}, data, { reject: reject, resolve: resolve, callback: progress, })); if (Downloader._downloads.has(id)) { const requestId = Downloader._downloads.get(id); if (requestId) { Downloader._fetch.resume(requestId); } } } }); } resume(id) { if (id) { if (Downloader._downloads.has(id)) { const requestId = Downloader._downloads.get(id); Downloader._fetch.resume(requestId); } } } cancel(id) { if (id) { if (Downloader._downloads.has(id)) { const requestId = Downloader._downloads.get(id); Downloader._fetch.cancel(requestId); Downloader._downloads.delete(id); Downloader._downloadsData.delete(id); Downloader._downloadRequests.delete(requestId); } } } pause(id) { if (id) { if (Downloader._downloads.has(id)) { const requestId = Downloader._downloads.get(id); Downloader._fetch.pause(requestId); } } } getPath(id) { if (id && Downloader._downloadsData.has(id)) { const download = Downloader._downloadsData.get(id); return download.path; } return null; } } Downloader._timeout = 60; //# sourceMappingURL=index.android.js.map