UNPKG

@wu_rong_tai/react-native-cos-xml

Version:
139 lines (112 loc) 3.56 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _reactNative = require("react-native"); function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } const { CosXmlReactNative } = _reactNative.NativeModules; let cosModule = CosXmlReactNative; class COSTransferManagerService { constructor() { _defineProperty(this, "callbacks", void 0); _defineProperty(this, "requests", void 0); _defineProperty(this, "emitter", void 0); _defineProperty(this, "initialized", false); this.callbacks = new Map(); this.requests = new Map(); this.emitter = new _reactNative.NativeEventEmitter(CosXmlReactNative); this.emitter.addListener('COSTransferProgressUpdate', event => { let requestId = event.requestId; if (this.callbacks.has(requestId)) { this.callbacks.get(requestId)(event.processedBytes, event.targetBytes); } }); } initWithPlainSecret(configurations, credential) { if (!this.initialized) { this.initialized = true; cosModule.initWithPlainSecret(configurations, credential); } else { console.log('COS Service has been inited before.'); } } initWithSessionCredentialCallback(configurations, callback) { if (!this.initialized) { this.initialized = true; cosModule.initWithSessionCredentialCallback(configurations); this.emitter.addListener('COSUpdateSessionCredential', async () => { const credential = await callback(); cosModule.updateSessionCredential(credential); }); } else { console.log('COS Service has been inited before.'); } } async upload(request, progressListener) { let uniqueId = request.requestId; if (!uniqueId) { uniqueId = this.uniqueRequestId(); request.requestId = uniqueId; } if (progressListener) { this.callbacks.set(uniqueId, progressListener); } this.requests.set(uniqueId, request); try { const info = await cosModule.putObject(request); this.removeRequestIdRecord(uniqueId); return Promise.resolve(info); } catch (err) { this.removeRequestIdRecord(uniqueId); return Promise.reject(err); } } async pause(request) { let uniqueId = null; this.requests.forEach((value, key) => { if (value === request) { uniqueId = key; } }); if (uniqueId) { try { await cosModule.pauseUpload(uniqueId); this.removeRequestIdRecord(uniqueId); } catch (error) { console.log(error); } } else { console.log('Request not found'); } } async download(request, progressListener) { let uniqueId = this.uniqueRequestId(); request.requestId = uniqueId; if (progressListener) { this.callbacks.set(uniqueId, progressListener); } try { const filePath = await cosModule.getObject(request); return filePath; } catch (err) { throw err; } finally { this.removeRequestIdRecord(uniqueId); } } removeRequestIdRecord(uniqueId) { if (uniqueId) { this.callbacks.delete(uniqueId); this.requests.delete(uniqueId); } } uniqueRequestId() { return '_' + Math.random().toString(36).substr(2, 9); } } var _default = new COSTransferManagerService(); exports.default = _default; //# sourceMappingURL=index.js.map