UNPKG

react-native-cloud-store

Version:
200 lines (196 loc) 6.28 kB
import { Platform } from 'react-native'; import CloudStore, { eventEmitter } from './module'; import { PathUtils } from './path'; function getConstants() { // TODO: android not implement getConstants method, so here just return an empty object return Platform.OS === 'ios' ? CloudStore.getConstants() : {}; } export const defaultICloudContainerPath = getConstants().defaultICloudContainerPath; export async function getDefaultICloudContainerPath() { return Platform.OS === 'ios' ? CloudStore.getDefaultICloudContainerPath() : undefined; } // https://developer.apple.com/documentation/foundation/filemanager/1411653-url export async function getICloudURL(containerIdentifier) { return CloudStore.getICloudURL(containerIdentifier); } export async function isICloudAvailable() { return CloudStore.isICloudAvailable(); } export async function writeFile(path, content, options) { let canProgress = false; if (options !== null && options !== void 0 && options.onProgress) { if (!calledGlobalUploadEvent) { console.error(`You didn't call registerGlobalUploadEvent(), onProgress will not be triggered `); } else { uploadId++; uploadId2CallbackDataMap[uploadId] = { path: path, callback: options.onProgress }; canProgress = true; } } return CloudStore.writeFile(path, content, { ...options, id: canProgress ? uploadId.toString() : undefined }); } export async function readFile(path) { return CloudStore.readFile(path); } // if returned filename format is .[file-full-name-with-ext].icloud, means this file is not yet downloaded to local device export async function readDir(path) { return CloudStore.readDir(path); } export async function createDir(path) { return CloudStore.createDir(path); } export async function moveDir(pathFrom, pathTo) { return CloudStore.moveDir(pathFrom, pathTo); } export async function copy(pathFrom, pathTo, options) { return CloudStore.copy(pathFrom, pathTo, { ...options }); } export async function unlink(path) { return CloudStore.unlink(path); } export async function exist(path) { return CloudStore.exist(path); } export async function stat(path) { return CloudStore.stat(path); } let calledGlobalUploadEvent = false; let uploadId = 0; const uploadId2CallbackDataMap = {}; let calledGlobalDownloadEvent = false; let downloadId = 0; const downloadId2CallbackDataMap = {}; export async function upload(localPath, path, options) { uploadId++; if (options !== null && options !== void 0 && options.onProgress) { if (!calledGlobalUploadEvent) { console.error(`You didn't call registerGlobalUploadEvent(), onProgress will not be triggered `); } uploadId2CallbackDataMap[uploadId] = { path: path, callback: options.onProgress }; } return CloudStore.upload(u(localPath), path, { id: uploadId.toString() }); } export async function download(path, options) { downloadId++; const fileInfo = await CloudStore.stat(path); if (fileInfo.downloadStatus === "NSURLUbiquitousItemDownloadingStatusCurrent") { options === null || options === void 0 ? void 0 : options.onProgress({ progress: 100 }); return Promise.resolve(); } const pathWithoutDot = PathUtils.iCloudRemoveDotExt(path); if (options !== null && options !== void 0 && options.onProgress) { if (!calledGlobalDownloadEvent) { console.error(`You didn't call registerGlobalDownloadEvent(), onProgress will not be triggered `); } downloadId2CallbackDataMap[downloadId] = { path: pathWithoutDot, callback: options.onProgress }; } return CloudStore.download(pathWithoutDot, { id: downloadId.toString() }); } export function registerGlobalUploadEvent() { if (calledGlobalUploadEvent) { return; } const subscription = onICloudDocumentsUpdateGathering(data => { const callbackData = uploadId2CallbackDataMap[data.id]; if (!callbackData) return; const { path, callback } = callbackData; const uploadTarget = data.detail.find(i => i.type === "upload" && i.path === path); if (uploadTarget) { const progress = uploadTarget.progress ?? 0; if (progress === 100) { delete uploadId2CallbackDataMap[uploadId]; } callback({ progress: progress }); } }); calledGlobalUploadEvent = true; // TODO: directly return the unsubscribe function in next version return { remove() { subscription.remove(); calledGlobalUploadEvent = false; } }; } export function registerGlobalDownloadEvent() { if (calledGlobalDownloadEvent) { return; } calledGlobalDownloadEvent = true; const subscription = onICloudDocumentsUpdateGathering(data => { const callbackData = downloadId2CallbackDataMap[data.id]; if (!callbackData) return; const { path, callback } = callbackData; const downloadTarget = data.detail.find(i => i.type === "download" && i.path === path); if (downloadTarget) { const progress = downloadTarget.progress ?? 0; if (progress === 100) { delete downloadId2CallbackDataMap[uploadId]; } callback({ progress: progress }); } }); calledGlobalDownloadEvent = true; // TODO: directly return the unsubscribe function in next version return { remove() { subscription.remove(); calledGlobalDownloadEvent = false; } }; } export function onICloudIdentityDidChange(fn) { return eventEmitter.addListener('onICloudIdentityDidChange', fn); } export function onICloudDocumentsStartGathering(fn) { return eventEmitter.addListener('onICloudDocumentsStartGathering', nativeData => { fn(nativeData); }); } export function onICloudDocumentsGathering(fn) { return eventEmitter.addListener('onICloudDocumentsGathering', fn); } export function onICloudDocumentsFinishGathering(fn) { return eventEmitter.addListener('onICloudDocumentsFinishGathering', fn); } export function onICloudDocumentsUpdateGathering(fn) { return eventEmitter.addListener('onICloudDocumentsUpdateGathering', fn); } function u(path) { let prefix = "file://"; if (path.startsWith(prefix)) { path = path.slice(prefix.length); } return path; } //# sourceMappingURL=icloud.js.map