react-native-cloud-store
Version:
A react-native module for cloud operation
230 lines (225 loc) • 8.52 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.copy = copy;
exports.createDir = createDir;
exports.defaultICloudContainerPath = void 0;
exports.download = download;
exports.exist = exist;
exports.getDefaultICloudContainerPath = getDefaultICloudContainerPath;
exports.getICloudURL = getICloudURL;
exports.isICloudAvailable = isICloudAvailable;
exports.moveDir = moveDir;
exports.onICloudDocumentsFinishGathering = onICloudDocumentsFinishGathering;
exports.onICloudDocumentsGathering = onICloudDocumentsGathering;
exports.onICloudDocumentsStartGathering = onICloudDocumentsStartGathering;
exports.onICloudDocumentsUpdateGathering = onICloudDocumentsUpdateGathering;
exports.onICloudIdentityDidChange = onICloudIdentityDidChange;
exports.readDir = readDir;
exports.readFile = readFile;
exports.registerGlobalDownloadEvent = registerGlobalDownloadEvent;
exports.registerGlobalUploadEvent = registerGlobalUploadEvent;
exports.stat = stat;
exports.unlink = unlink;
exports.upload = upload;
exports.writeFile = writeFile;
var _reactNative = require("react-native");
var _module = _interopRequireWildcard(require("./module"));
var _path = require("./path");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function getConstants() {
// TODO: android not implement getConstants method, so here just return an empty object
return _reactNative.Platform.OS === 'ios' ? _module.default.getConstants() : {};
}
const defaultICloudContainerPath = getConstants().defaultICloudContainerPath;
exports.defaultICloudContainerPath = defaultICloudContainerPath;
async function getDefaultICloudContainerPath() {
return _reactNative.Platform.OS === 'ios' ? _module.default.getDefaultICloudContainerPath() : undefined;
}
// https://developer.apple.com/documentation/foundation/filemanager/1411653-url
async function getICloudURL(containerIdentifier) {
return _module.default.getICloudURL(containerIdentifier);
}
async function isICloudAvailable() {
return _module.default.isICloudAvailable();
}
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 _module.default.writeFile(path, content, {
...options,
id: canProgress ? uploadId.toString() : undefined
});
}
async function readFile(path) {
return _module.default.readFile(path);
}
// if returned filename format is .[file-full-name-with-ext].icloud, means this file is not yet downloaded to local device
async function readDir(path) {
return _module.default.readDir(path);
}
async function createDir(path) {
return _module.default.createDir(path);
}
async function moveDir(pathFrom, pathTo) {
return _module.default.moveDir(pathFrom, pathTo);
}
async function copy(pathFrom, pathTo, options) {
return _module.default.copy(pathFrom, pathTo, {
...options
});
}
async function unlink(path) {
return _module.default.unlink(path);
}
async function exist(path) {
return _module.default.exist(path);
}
async function stat(path) {
return _module.default.stat(path);
}
let calledGlobalUploadEvent = false;
let uploadId = 0;
const uploadId2CallbackDataMap = {};
let calledGlobalDownloadEvent = false;
let downloadId = 0;
const downloadId2CallbackDataMap = {};
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 _module.default.upload(u(localPath), path, {
id: uploadId.toString()
});
}
async function download(path, options) {
downloadId++;
const fileInfo = await _module.default.stat(path);
if (fileInfo.downloadStatus === "NSURLUbiquitousItemDownloadingStatusCurrent") {
options === null || options === void 0 ? void 0 : options.onProgress({
progress: 100
});
return Promise.resolve();
}
const pathWithoutDot = _path.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 _module.default.download(pathWithoutDot, {
id: downloadId.toString()
});
}
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;
}
};
}
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;
}
};
}
function onICloudIdentityDidChange(fn) {
return _module.eventEmitter.addListener('onICloudIdentityDidChange', fn);
}
function onICloudDocumentsStartGathering(fn) {
return _module.eventEmitter.addListener('onICloudDocumentsStartGathering', nativeData => {
fn(nativeData);
});
}
function onICloudDocumentsGathering(fn) {
return _module.eventEmitter.addListener('onICloudDocumentsGathering', fn);
}
function onICloudDocumentsFinishGathering(fn) {
return _module.eventEmitter.addListener('onICloudDocumentsFinishGathering', fn);
}
function onICloudDocumentsUpdateGathering(fn) {
return _module.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