react-native-cloud-storage 1.5.1 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +6 -5
- package/ios/CloudStorage.swift +52 -262
- package/ios/CloudStorage.xcodeproj/project.pbxproj +28 -0
- package/ios/CloudStorageEventEmitter.swift +4 -4
- package/ios/Utils/CloudKitUtils.swift +112 -0
- package/ios/Utils/CloudStorageError.swift +78 -0
- package/ios/Utils/FileUtils.swift +132 -0
- package/ios/Utils/Promise.swift +58 -0
- package/ios/Utils/Types.swift +36 -0
- package/lib/commonjs/RNCloudStorage.js +361 -66
- package/lib/commonjs/RNCloudStorage.js.map +1 -1
- package/lib/commonjs/expo-plugin/types/index.js.map +1 -1
- package/lib/commonjs/expo-plugin/withRNCloudStorage.js +2 -3
- package/lib/commonjs/expo-plugin/withRNCloudStorage.js.map +1 -1
- package/lib/commonjs/expo-plugin/withRNCloudStorageIos.js +4 -7
- package/lib/commonjs/expo-plugin/withRNCloudStorageIos.js.map +1 -1
- package/lib/commonjs/google-drive/client.js +16 -20
- package/lib/commonjs/google-drive/client.js.map +1 -1
- package/lib/commonjs/google-drive/index.js +42 -64
- package/lib/commonjs/google-drive/index.js.map +1 -1
- package/lib/commonjs/google-drive/types.js +1 -2
- package/lib/commonjs/google-drive/types.js.map +1 -1
- package/lib/commonjs/hooks/useCloudFile.js +14 -17
- package/lib/commonjs/hooks/useCloudFile.js.map +1 -1
- package/lib/commonjs/hooks/useIsCloudAvailable.js +11 -21
- package/lib/commonjs/hooks/useIsCloudAvailable.js.map +1 -1
- package/lib/commonjs/index.js +1 -7
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/package.json +1 -0
- package/lib/commonjs/types/main.js +8 -3
- package/lib/commonjs/types/main.js.map +1 -1
- package/lib/commonjs/types/native.js +3 -3
- package/lib/commonjs/types/native.js.map +1 -1
- package/lib/commonjs/utils/CloudStorageError.js +1 -2
- package/lib/commonjs/utils/CloudStorageError.js.map +1 -1
- package/lib/commonjs/utils/helpers.js +8 -15
- package/lib/commonjs/utils/helpers.js.map +1 -1
- package/lib/module/RNCloudStorage.js +362 -65
- package/lib/module/RNCloudStorage.js.map +1 -1
- package/lib/module/expo-plugin/types/index.js +1 -1
- package/lib/module/expo-plugin/types/index.js.map +1 -1
- package/lib/module/expo-plugin/withRNCloudStorage.js +2 -0
- package/lib/module/expo-plugin/withRNCloudStorage.js.map +1 -1
- package/lib/module/expo-plugin/withRNCloudStorageIos.js +5 -5
- package/lib/module/expo-plugin/withRNCloudStorageIos.js.map +1 -1
- package/lib/module/google-drive/client.js +18 -20
- package/lib/module/google-drive/client.js.map +1 -1
- package/lib/module/google-drive/index.js +41 -62
- package/lib/module/google-drive/index.js.map +1 -1
- package/lib/module/google-drive/types.js +2 -0
- package/lib/module/google-drive/types.js.map +1 -1
- package/lib/module/hooks/useCloudFile.js +15 -16
- package/lib/module/hooks/useCloudFile.js.map +1 -1
- package/lib/module/hooks/useIsCloudAvailable.js +13 -21
- package/lib/module/hooks/useIsCloudAvailable.js.map +1 -1
- package/lib/module/index.js +2 -5
- package/lib/module/index.js.map +1 -1
- package/lib/module/package.json +1 -0
- package/lib/module/types/main.js +9 -0
- package/lib/module/types/main.js.map +1 -1
- package/lib/module/types/native.js +4 -1
- package/lib/module/types/native.js.map +1 -1
- package/lib/module/utils/CloudStorageError.js +2 -0
- package/lib/module/utils/CloudStorageError.js.map +1 -1
- package/lib/module/utils/helpers.js +8 -13
- package/lib/module/utils/helpers.js.map +1 -1
- package/lib/typescript/RNCloudStorage.d.ts +159 -39
- package/lib/typescript/RNCloudStorage.d.ts.map +1 -1
- package/lib/typescript/google-drive/client.d.ts +3 -3
- package/lib/typescript/google-drive/client.d.ts.map +1 -1
- package/lib/typescript/google-drive/index.d.ts +6 -18
- package/lib/typescript/google-drive/index.d.ts.map +1 -1
- package/lib/typescript/hooks/useCloudFile.d.ts +4 -7
- package/lib/typescript/hooks/useCloudFile.d.ts.map +1 -1
- package/lib/typescript/hooks/useIsCloudAvailable.d.ts +3 -2
- package/lib/typescript/hooks/useIsCloudAvailable.d.ts.map +1 -1
- package/lib/typescript/index.d.ts +0 -4
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/types/main.d.ts +33 -0
- package/lib/typescript/types/main.d.ts.map +1 -1
- package/lib/typescript/types/native.d.ts +2 -1
- package/lib/typescript/types/native.d.ts.map +1 -1
- package/lib/typescript/utils/helpers.d.ts +2 -9
- package/lib/typescript/utils/helpers.d.ts.map +1 -1
- package/package.json +9 -11
- package/src/RNCloudStorage.ts +387 -68
- package/src/google-drive/client.ts +8 -7
- package/src/google-drive/index.ts +38 -63
- package/src/hooks/useCloudFile.ts +13 -16
- package/src/hooks/useIsCloudAvailable.ts +12 -25
- package/src/index.ts +0 -5
- package/src/types/main.ts +38 -0
- package/src/types/native.ts +2 -1
- package/src/utils/helpers.ts +8 -15
- package/lib/commonjs/createRNCloudStorage.js +0 -48
- package/lib/commonjs/createRNCloudStorage.js.map +0 -1
- package/lib/module/createRNCloudStorage.js +0 -41
- package/lib/module/createRNCloudStorage.js.map +0 -1
- package/lib/typescript/createRNCloudStorage.d.ts +0 -3
- package/lib/typescript/createRNCloudStorage.d.ts.map +0 -1
- package/src/createRNCloudStorage.ts +0 -53
package/lib/commonjs/index.js
CHANGED
|
@@ -26,7 +26,6 @@ Object.defineProperty(exports, "CloudStorageErrorCode", {
|
|
|
26
26
|
return _native.CloudStorageErrorCode;
|
|
27
27
|
}
|
|
28
28
|
});
|
|
29
|
-
exports.default = void 0;
|
|
30
29
|
var _RNCloudStorage = _interopRequireDefault(require("./RNCloudStorage"));
|
|
31
30
|
var _native = require("./types/native");
|
|
32
31
|
var _main = require("./types/main");
|
|
@@ -66,10 +65,5 @@ Object.keys(_useIsCloudAvailable).forEach(function (key) {
|
|
|
66
65
|
});
|
|
67
66
|
});
|
|
68
67
|
var _CloudStorageError = _interopRequireDefault(require("./utils/CloudStorageError"));
|
|
69
|
-
function _interopRequireDefault(
|
|
70
|
-
/**
|
|
71
|
-
* @deprecated Use the named export `CloudStorage` instead.
|
|
72
|
-
*/
|
|
73
|
-
var _default = _RNCloudStorage.default;
|
|
74
|
-
exports.default = _default;
|
|
68
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
75
69
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["_RNCloudStorage","_interopRequireDefault","require","_native","_main","Object","keys","forEach","key","prototype","hasOwnProperty","call","_exportNames","exports","defineProperty","enumerable","get","_useCloudFile","_useIsCloudAvailable","_CloudStorageError","
|
|
1
|
+
{"version":3,"names":["_RNCloudStorage","_interopRequireDefault","require","_native","_main","Object","keys","forEach","key","prototype","hasOwnProperty","call","_exportNames","exports","defineProperty","enumerable","get","_useCloudFile","_useIsCloudAvailable","_CloudStorageError","e","__esModule","default"],"sourceRoot":"../../src","sources":["index.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,IAAAA,eAAA,GAAAC,sBAAA,CAAAC,OAAA;AACA,IAAAC,OAAA,GAAAD,OAAA;AACA,IAAAE,KAAA,GAAAF,OAAA;AAAAG,MAAA,CAAAC,IAAA,CAAAF,KAAA,EAAAG,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAH,MAAA,CAAAI,SAAA,CAAAC,cAAA,CAAAC,IAAA,CAAAC,YAAA,EAAAJ,GAAA;EAAA,IAAAA,GAAA,IAAAK,OAAA,IAAAA,OAAA,CAAAL,GAAA,MAAAJ,KAAA,CAAAI,GAAA;EAAAH,MAAA,CAAAS,cAAA,CAAAD,OAAA,EAAAL,GAAA;IAAAO,UAAA;IAAAC,GAAA,WAAAA,CAAA;MAAA,OAAAZ,KAAA,CAAAI,GAAA;IAAA;EAAA;AAAA;AACA,IAAAS,aAAA,GAAAf,OAAA;AAAAG,MAAA,CAAAC,IAAA,CAAAW,aAAA,EAAAV,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAH,MAAA,CAAAI,SAAA,CAAAC,cAAA,CAAAC,IAAA,CAAAC,YAAA,EAAAJ,GAAA;EAAA,IAAAA,GAAA,IAAAK,OAAA,IAAAA,OAAA,CAAAL,GAAA,MAAAS,aAAA,CAAAT,GAAA;EAAAH,MAAA,CAAAS,cAAA,CAAAD,OAAA,EAAAL,GAAA;IAAAO,UAAA;IAAAC,GAAA,WAAAA,CAAA;MAAA,OAAAC,aAAA,CAAAT,GAAA;IAAA;EAAA;AAAA;AACA,IAAAU,oBAAA,GAAAhB,OAAA;AAAAG,MAAA,CAAAC,IAAA,CAAAY,oBAAA,EAAAX,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAH,MAAA,CAAAI,SAAA,CAAAC,cAAA,CAAAC,IAAA,CAAAC,YAAA,EAAAJ,GAAA;EAAA,IAAAA,GAAA,IAAAK,OAAA,IAAAA,OAAA,CAAAL,GAAA,MAAAU,oBAAA,CAAAV,GAAA;EAAAH,MAAA,CAAAS,cAAA,CAAAD,OAAA,EAAAL,GAAA;IAAAO,UAAA;IAAAC,GAAA,WAAAA,CAAA;MAAA,OAAAE,oBAAA,CAAAV,GAAA;IAAA;EAAA;AAAA;AACA,IAAAW,kBAAA,GAAAlB,sBAAA,CAAAC,OAAA;AAA0D,SAAAD,uBAAAmB,CAAA,WAAAA,CAAA,IAAAA,CAAA,CAAAC,UAAA,GAAAD,CAAA,KAAAE,OAAA,EAAAF,CAAA","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"commonjs"}
|
|
@@ -3,11 +3,16 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.CloudStorageScope = void 0;
|
|
7
|
-
|
|
6
|
+
exports.CloudStorageScope = exports.CloudStorageProvider = void 0;
|
|
7
|
+
/* Custom utility type to make properties required, but still allow null if defined */
|
|
8
|
+
let CloudStorageScope = exports.CloudStorageScope = /*#__PURE__*/function (CloudStorageScope) {
|
|
8
9
|
CloudStorageScope["Documents"] = "documents";
|
|
9
10
|
CloudStorageScope["AppData"] = "app_data";
|
|
10
11
|
return CloudStorageScope;
|
|
11
12
|
}({});
|
|
12
|
-
exports.
|
|
13
|
+
let CloudStorageProvider = exports.CloudStorageProvider = /*#__PURE__*/function (CloudStorageProvider) {
|
|
14
|
+
CloudStorageProvider["ICloud"] = "icloud";
|
|
15
|
+
CloudStorageProvider["GoogleDrive"] = "googledrive";
|
|
16
|
+
return CloudStorageProvider;
|
|
17
|
+
}({});
|
|
13
18
|
//# sourceMappingURL=main.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["CloudStorageScope","exports"],"sourceRoot":"../../../src","sources":["types/main.ts"],"mappings":";;;;;;
|
|
1
|
+
{"version":3,"names":["CloudStorageScope","exports","CloudStorageProvider"],"sourceRoot":"../../../src","sources":["types/main.ts"],"mappings":";;;;;;AAAA;AAAA,IAKYA,iBAAiB,GAAAC,OAAA,CAAAD,iBAAA,0BAAjBA,iBAAiB;EAAjBA,iBAAiB;EAAjBA,iBAAiB;EAAA,OAAjBA,iBAAiB;AAAA;AAAA,IAejBE,oBAAoB,GAAAD,OAAA,CAAAC,oBAAA,0BAApBA,oBAAoB;EAApBA,oBAAoB;EAApBA,oBAAoB;EAAA,OAApBA,oBAAoB;AAAA","ignoreList":[]}
|
|
@@ -4,7 +4,8 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.CloudStorageErrorCode = void 0;
|
|
7
|
-
let CloudStorageErrorCode = /*#__PURE__*/function (CloudStorageErrorCode) {
|
|
7
|
+
let CloudStorageErrorCode = exports.CloudStorageErrorCode = /*#__PURE__*/function (CloudStorageErrorCode) {
|
|
8
|
+
CloudStorageErrorCode["INVALID_SCOPE"] = "ERR_INVALID_SCOPE";
|
|
8
9
|
CloudStorageErrorCode["FILE_NOT_FOUND"] = "ERR_FILE_NOT_FOUND";
|
|
9
10
|
CloudStorageErrorCode["PATH_IS_FILE"] = "ERR_PATH_IS_FILE";
|
|
10
11
|
CloudStorageErrorCode["PATH_IS_DIRECTORY"] = "ERR_PATH_IS_DIRECTORY";
|
|
@@ -19,8 +20,7 @@ let CloudStorageErrorCode = /*#__PURE__*/function (CloudStorageErrorCode) {
|
|
|
19
20
|
CloudStorageErrorCode["STAT_ERROR"] = "ERR_STAT_ERROR";
|
|
20
21
|
CloudStorageErrorCode["UNKNOWN"] = "ERR_UNKNOWN";
|
|
21
22
|
CloudStorageErrorCode["FILE_NOT_DOWNLOADABLE"] = "ERR_FILE_NOT_DOWNLOADABLE";
|
|
22
|
-
CloudStorageErrorCode["
|
|
23
|
+
CloudStorageErrorCode["ACCESS_TOKEN_MISSING"] = "ERR_ACCESS_TOKEN_MISSING";
|
|
23
24
|
return CloudStorageErrorCode;
|
|
24
25
|
}({});
|
|
25
|
-
exports.CloudStorageErrorCode = CloudStorageErrorCode;
|
|
26
26
|
//# sourceMappingURL=native.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["CloudStorageErrorCode","exports"],"sourceRoot":"../../../src","sources":["types/native.ts"],"mappings":";;;;;;IAUYA,qBAAqB,0BAArBA,qBAAqB;EAArBA,qBAAqB;EAArBA,qBAAqB;EAArBA,qBAAqB;EAArBA,qBAAqB;EAArBA,qBAAqB;EAArBA,qBAAqB;EAArBA,qBAAqB;EAArBA,qBAAqB;EAArBA,qBAAqB;EAArBA,qBAAqB;EAArBA,qBAAqB;EAArBA,qBAAqB;EAArBA,qBAAqB;EAArBA,qBAAqB;EAArBA,qBAAqB;EAAA,OAArBA,qBAAqB;AAAA
|
|
1
|
+
{"version":3,"names":["CloudStorageErrorCode","exports"],"sourceRoot":"../../../src","sources":["types/native.ts"],"mappings":";;;;;;IAUYA,qBAAqB,GAAAC,OAAA,CAAAD,qBAAA,0BAArBA,qBAAqB;EAArBA,qBAAqB;EAArBA,qBAAqB;EAArBA,qBAAqB;EAArBA,qBAAqB;EAArBA,qBAAqB;EAArBA,qBAAqB;EAArBA,qBAAqB;EAArBA,qBAAqB;EAArBA,qBAAqB;EAArBA,qBAAqB;EAArBA,qBAAqB;EAArBA,qBAAqB;EAArBA,qBAAqB;EAArBA,qBAAqB;EAArBA,qBAAqB;EAArBA,qBAAqB;EAAA,OAArBA,qBAAqB;AAAA","ignoreList":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["CloudStorageError","Error","constructor","message","code","details","_default","exports","default"],"sourceRoot":"../../../src","sources":["utils/CloudStorageError.ts"],"mappings":";;;;;;AAEA,MAAMA,iBAAiB,SAASC,KAAK,CAAC;EAIpCC,WAAWA,CAACC,OAAe,EAAEC,IAA2B,EAAEC,OAAa,EAAE;IACvE,KAAK,CAACF,OAAO,CAAC;IACd,IAAI,CAACC,IAAI,GAAGA,IAAI;IAChB,IAAI,CAACC,OAAO,GAAGA,OAAO;EACxB;AACF;AAAC,IAAAC,QAAA,
|
|
1
|
+
{"version":3,"names":["CloudStorageError","Error","constructor","message","code","details","_default","exports","default"],"sourceRoot":"../../../src","sources":["utils/CloudStorageError.ts"],"mappings":";;;;;;AAEA,MAAMA,iBAAiB,SAASC,KAAK,CAAC;EAIpCC,WAAWA,CAACC,OAAe,EAAEC,IAA2B,EAAEC,OAAa,EAAE;IACvE,KAAK,CAACF,OAAO,CAAC;IACd,IAAI,CAACC,IAAI,GAAGA,IAAI;IAChB,IAAI,CAACC,OAAO,GAAGA,OAAO;EACxB;AACF;AAAC,IAAAC,QAAA,GAAAC,OAAA,CAAAC,OAAA,GAEcR,iBAAiB","ignoreList":[]}
|
|
@@ -3,21 +3,14 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
* @returns The path with a leading slash, if it didn't have one already.
|
|
13
|
-
* @private
|
|
14
|
-
*/
|
|
15
|
-
const verifyLeadingSlash = path => {
|
|
16
|
-
if (!path.startsWith('/')) {
|
|
17
|
-
console.warn(`[react-native-cloud-storage] Path "${path}" did not start with a leading slash. This is deprecated and will be an error in the future.`);
|
|
18
|
-
return `/${path}`;
|
|
6
|
+
exports.isProviderSupported = void 0;
|
|
7
|
+
var _main = require("../types/main");
|
|
8
|
+
var _reactNative = require("react-native");
|
|
9
|
+
const isProviderSupported = provider => {
|
|
10
|
+
if (_reactNative.Platform.OS !== 'ios' && provider === _main.CloudStorageProvider.ICloud) {
|
|
11
|
+
return false;
|
|
19
12
|
}
|
|
20
|
-
return
|
|
13
|
+
return true;
|
|
21
14
|
};
|
|
22
|
-
exports.
|
|
15
|
+
exports.isProviderSupported = isProviderSupported;
|
|
23
16
|
//# sourceMappingURL=helpers.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["
|
|
1
|
+
{"version":3,"names":["_main","require","_reactNative","isProviderSupported","provider","Platform","OS","CloudStorageProvider","ICloud","exports"],"sourceRoot":"../../../src","sources":["utils/helpers.ts"],"mappings":";;;;;;AAAA,IAAAA,KAAA,GAAAC,OAAA;AACA,IAAAC,YAAA,GAAAD,OAAA;AAEO,MAAME,mBAAmB,GAAIC,QAA8B,IAAc;EAC9E,IAAIC,qBAAQ,CAACC,EAAE,KAAK,KAAK,IAAIF,QAAQ,KAAKG,0BAAoB,CAACC,MAAM,EAAE;IACrE,OAAO,KAAK;EACd;EAEA,OAAO,IAAI;AACb,CAAC;AAACC,OAAA,CAAAN,mBAAA,GAAAA,mBAAA","ignoreList":[]}
|
|
@@ -1,125 +1,285 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { CloudStorageProvider, CloudStorageScope } from './types/main';
|
|
4
|
+
import { isProviderSupported } from './utils/helpers';
|
|
5
|
+
import { NativeEventEmitter, NativeModules, Platform } from 'react-native';
|
|
6
|
+
import CloudStorageError from './utils/CloudStorageError';
|
|
7
|
+
import { CloudStorageErrorCode } from './types/native';
|
|
3
8
|
import GoogleDrive from './google-drive';
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
9
|
+
const LINKING_ERROR = `The package 'react-native-cloud-storage' doesn't seem to be linked. Make sure: \n\n` + Platform.select({
|
|
10
|
+
ios: "- You have run 'pod install'\n",
|
|
11
|
+
default: ''
|
|
12
|
+
}) + '- You rebuilt the app after installing the package\n' + '- You are not using Expo Go\n';
|
|
13
|
+
|
|
14
|
+
// proxy NativeModules.CloudStorage to catch any errors thrown by the native module and wrap them in a CloudStorageError
|
|
15
|
+
const nativeIosInstance = NativeModules.CloudStorage ? new Proxy(NativeModules.CloudStorage, {
|
|
16
|
+
get(target, prop) {
|
|
17
|
+
const originalFunction = target[prop];
|
|
18
|
+
if (typeof originalFunction === 'function') {
|
|
19
|
+
return async (...args) => {
|
|
20
|
+
try {
|
|
21
|
+
// @ts-expect-error - we can't know the types of the functions and their arguments
|
|
22
|
+
return await originalFunction(...args);
|
|
23
|
+
} catch (error) {
|
|
24
|
+
if (error?.code && Object.values(CloudStorageErrorCode).includes(error.code)) {
|
|
25
|
+
throw new CloudStorageError(error?.message || '', error.code);
|
|
26
|
+
} else {
|
|
27
|
+
throw new CloudStorageError('Unknown error', CloudStorageErrorCode.UNKNOWN, error);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
return originalFunction;
|
|
33
|
+
}
|
|
34
|
+
}) : null;
|
|
35
|
+
const defaultProviderOptions = {
|
|
36
|
+
[CloudStorageProvider.ICloud]: {
|
|
37
|
+
scope: CloudStorageScope.AppData
|
|
38
|
+
},
|
|
39
|
+
[CloudStorageProvider.GoogleDrive]: {
|
|
40
|
+
scope: CloudStorageScope.AppData,
|
|
41
|
+
accessToken: null,
|
|
42
|
+
strictFilenames: false,
|
|
43
|
+
timeout: 3000
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
export default class RNCloudStorage {
|
|
47
|
+
cloudAvailabilityListeners = [];
|
|
48
|
+
|
|
49
|
+
//#region Constructor and configuration
|
|
50
|
+
/**
|
|
51
|
+
* Creates a new RNCloudStorage instance for the given provider.
|
|
52
|
+
* @param provider The provider to create the instance for. Defaults to the default provider for the current platform.
|
|
53
|
+
*/
|
|
54
|
+
constructor(provider, options) {
|
|
55
|
+
if (provider && !isProviderSupported(provider)) {
|
|
56
|
+
throw new Error(`Provider ${provider} is not supported on the current platform.`);
|
|
57
|
+
}
|
|
58
|
+
this.provider = {
|
|
59
|
+
provider: provider ?? RNCloudStorage.getDefaultProvider(),
|
|
60
|
+
options: defaultProviderOptions[provider ?? RNCloudStorage.getDefaultProvider()]
|
|
61
|
+
};
|
|
62
|
+
this.setProvider(provider ?? RNCloudStorage.getDefaultProvider());
|
|
63
|
+
if (options) {
|
|
64
|
+
this.setProviderOptions(options);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
get nativeInstance() {
|
|
68
|
+
switch (this.provider.provider) {
|
|
69
|
+
case CloudStorageProvider.ICloud:
|
|
70
|
+
return nativeIosInstance ?? new Proxy({}, {
|
|
71
|
+
get() {
|
|
72
|
+
throw new Error(LINKING_ERROR);
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
default:
|
|
76
|
+
return new GoogleDrive(this.provider.options);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Gets the default CloudStorageProvider for the current platform.
|
|
82
|
+
* @returns The default CloudStorageProvider.
|
|
83
|
+
*/
|
|
84
|
+
static getDefaultProvider() {
|
|
85
|
+
switch (Platform.OS) {
|
|
86
|
+
case 'ios':
|
|
87
|
+
return CloudStorageProvider.ICloud;
|
|
88
|
+
default:
|
|
89
|
+
return CloudStorageProvider.GoogleDrive;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Gets the list of supported CloudStorageProviders on the current platform.
|
|
95
|
+
* @returns An array of supported CloudStorageProviders.
|
|
96
|
+
*/
|
|
97
|
+
static getSupportedProviders() {
|
|
98
|
+
return Object.values(CloudStorageProvider).filter(isProviderSupported);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Gets the current CloudStorageProvider.
|
|
103
|
+
* @returns The current CloudStorageProvider.
|
|
104
|
+
*/
|
|
105
|
+
getProvider() {
|
|
106
|
+
return this.provider.provider;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Sets the current CloudStorageProvider.
|
|
111
|
+
* @param provider The provider to set.
|
|
112
|
+
*/
|
|
113
|
+
setProvider(provider) {
|
|
114
|
+
if (!isProviderSupported(provider)) {
|
|
115
|
+
throw new Error(`Provider ${provider} is not supported on the current platform.`);
|
|
116
|
+
}
|
|
117
|
+
this.provider = {
|
|
118
|
+
provider,
|
|
119
|
+
options: defaultProviderOptions[provider]
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
// Emit an event to notify useIsCloudAvailable() hook consumers of the new cloud availability status
|
|
123
|
+
this.nativeInstance.isCloudAvailable().then(available => {
|
|
124
|
+
this.cloudAvailabilityListeners.forEach(listener => {
|
|
125
|
+
listener(available);
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
if (provider === CloudStorageProvider.ICloud) {
|
|
129
|
+
// Listen to native cloud availability change events
|
|
130
|
+
const eventEmitter = new NativeEventEmitter(NativeModules.CloudStorageEventEmitter);
|
|
131
|
+
eventEmitter.addListener('RNCloudStorage.cloud.availability-changed', event => {
|
|
132
|
+
this.cloudAvailabilityListeners.forEach(listener => {
|
|
133
|
+
listener(event.available);
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Gets the current options for the current provider.
|
|
141
|
+
* @returns The current options for the current provider.
|
|
142
|
+
*/
|
|
143
|
+
getProviderOptions() {
|
|
144
|
+
return this.provider.options;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Sets the options for the current provider.
|
|
149
|
+
* @param options The options to set for the provider.
|
|
150
|
+
*/
|
|
151
|
+
setProviderOptions(options) {
|
|
152
|
+
const newOptions = Object.fromEntries(Object.entries(options).filter(([_, v]) => v !== undefined));
|
|
153
|
+
this.provider.options = {
|
|
154
|
+
...this.provider.options,
|
|
155
|
+
...newOptions
|
|
156
|
+
};
|
|
157
|
+
if (this.provider.provider === CloudStorageProvider.GoogleDrive && 'accessToken' in newOptions) {
|
|
158
|
+
// Emit an event to notify useIsCloudAvailable() hook consumers of the new cloud availability status
|
|
159
|
+
this.cloudAvailabilityListeners.forEach(listener => {
|
|
160
|
+
listener(!!newOptions.accessToken?.length);
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
subscribeToCloudAvailability(listener) {
|
|
165
|
+
this.cloudAvailabilityListeners.push(listener);
|
|
166
|
+
}
|
|
167
|
+
unsubscribeFromCloudAvailability(listener) {
|
|
168
|
+
this.cloudAvailabilityListeners = this.cloudAvailabilityListeners.filter(l => l !== listener);
|
|
169
|
+
}
|
|
170
|
+
//#endregion
|
|
22
171
|
|
|
172
|
+
//#region File system operations
|
|
23
173
|
/**
|
|
24
174
|
* Tests whether or not the cloud storage is available. Always returns true for Google Drive. iCloud may be
|
|
25
175
|
* unavailable right after app launch or if the user is not logged in.
|
|
26
176
|
* @returns A promise that resolves to true if the cloud storage is available, false otherwise.
|
|
27
177
|
*/
|
|
28
|
-
isCloudAvailable
|
|
29
|
-
return nativeInstance.isCloudAvailable();
|
|
30
|
-
}
|
|
178
|
+
isCloudAvailable() {
|
|
179
|
+
return this.nativeInstance.isCloudAvailable();
|
|
180
|
+
}
|
|
181
|
+
|
|
31
182
|
/**
|
|
32
183
|
* Appends the data to the file at the given path, creating the file if it doesn't exist.
|
|
33
184
|
* @param path The file to append to.
|
|
34
185
|
* @param data The data to append.
|
|
35
|
-
* @param scope The directory scope the path is in. Defaults to the set
|
|
186
|
+
* @param scope The directory scope the path is in. Defaults to the default scope set for the current provider.
|
|
36
187
|
* @returns A promise that resolves when the data has been appended.
|
|
37
188
|
*/
|
|
38
|
-
appendFile
|
|
39
|
-
return nativeInstance.appendToFile(
|
|
40
|
-
}
|
|
189
|
+
appendFile(path, data, scope) {
|
|
190
|
+
return this.nativeInstance.appendToFile(path, data, scope ?? this.provider.options.scope);
|
|
191
|
+
}
|
|
192
|
+
|
|
41
193
|
/**
|
|
42
194
|
* Tests whether or not the file at the given path exists.
|
|
43
195
|
* @param path The path to test.
|
|
44
|
-
* @param scope The directory scope the path is in. Defaults to
|
|
196
|
+
* @param scope The directory scope the path is in. Defaults to set default scope set for the current provider.
|
|
45
197
|
* @returns A promise that resolves to true if the path exists, false otherwise.
|
|
46
198
|
*/
|
|
47
|
-
exists
|
|
48
|
-
return nativeInstance.fileExists(
|
|
49
|
-
}
|
|
199
|
+
exists(path, scope) {
|
|
200
|
+
return this.nativeInstance.fileExists(path, scope ?? this.provider.options.scope);
|
|
201
|
+
}
|
|
202
|
+
|
|
50
203
|
/**
|
|
51
204
|
* Writes to the file at the given path, creating it if it doesn't exist or overwriting it if it does.
|
|
52
205
|
* @param path The file to write to.
|
|
53
206
|
* @param data The data to write.
|
|
54
|
-
* @param scope The directory scope the path is in. Defaults to
|
|
207
|
+
* @param scope The directory scope the path is in. Defaults to set default scope set for the current provider.
|
|
55
208
|
* @returns A promise that resolves when the file has been written.
|
|
56
209
|
*/
|
|
57
|
-
writeFile
|
|
58
|
-
return nativeInstance.createFile(
|
|
59
|
-
}
|
|
210
|
+
writeFile(path, data, scope) {
|
|
211
|
+
return this.nativeInstance.createFile(path, data, scope ?? this.provider.options.scope, true);
|
|
212
|
+
}
|
|
213
|
+
|
|
60
214
|
/**
|
|
61
215
|
* Creates a new directory at the given path.
|
|
62
216
|
* @param path The directory to create.
|
|
63
|
-
* @param scope The directory scope the path is in. Defaults to
|
|
217
|
+
* @param scope The directory scope the path is in. Defaults to set default scope set for the current provider.
|
|
64
218
|
* @returns A promise that resolves when the directory has been created.
|
|
65
219
|
*/
|
|
66
|
-
mkdir
|
|
67
|
-
return nativeInstance.createDirectory(
|
|
68
|
-
}
|
|
220
|
+
mkdir(path, scope) {
|
|
221
|
+
return this.nativeInstance.createDirectory(path, scope ?? this.provider.options.scope);
|
|
222
|
+
}
|
|
223
|
+
|
|
69
224
|
/**
|
|
70
225
|
* Lists the contents of the directory at the given path.
|
|
71
226
|
* @param path The directory to list.
|
|
72
|
-
* @param scope The directory scope the path is in. Defaults to
|
|
227
|
+
* @param scope The directory scope the path is in. Defaults to set default scope set for the current provider.
|
|
73
228
|
* @returns A promise that resolves to an array of file names, excluding '.' and '..'.
|
|
74
229
|
*/
|
|
75
|
-
readdir
|
|
76
|
-
return nativeInstance.listFiles(
|
|
77
|
-
}
|
|
230
|
+
readdir(path, scope) {
|
|
231
|
+
return this.nativeInstance.listFiles(path, scope ?? this.provider.options.scope);
|
|
232
|
+
}
|
|
233
|
+
|
|
78
234
|
/**
|
|
79
235
|
* Reads the contents of the file at the given path.
|
|
80
236
|
* @param path The file to read.
|
|
81
|
-
* @param scope The directory scope the path is in. Defaults to
|
|
237
|
+
* @param scope The directory scope the path is in. Defaults to set default scope set for the current provider.
|
|
82
238
|
* @returns A promise that resolves to the contents of the file.
|
|
83
239
|
*/
|
|
84
|
-
readFile
|
|
85
|
-
return nativeInstance.readFile(
|
|
86
|
-
}
|
|
240
|
+
readFile(path, scope) {
|
|
241
|
+
return this.nativeInstance.readFile(path, scope ?? this.provider.options.scope);
|
|
242
|
+
}
|
|
243
|
+
|
|
87
244
|
/**
|
|
88
|
-
* Downloads the file at the given path
|
|
245
|
+
* Downloads the file at the given path. Does not have any effect on Google Drive.
|
|
89
246
|
* @param path The file to trigger the download for.
|
|
90
|
-
* @param scope The directory scope the path is in. Defaults to
|
|
247
|
+
* @param scope The directory scope the path is in. Defaults to set default scope set for the current provider.
|
|
91
248
|
* @returns A promise that resolves once the download has been triggered.
|
|
92
249
|
*/
|
|
93
|
-
downloadFile
|
|
94
|
-
return nativeInstance.downloadFile(
|
|
95
|
-
}
|
|
250
|
+
downloadFile(path, scope) {
|
|
251
|
+
return this.nativeInstance.downloadFile(path, scope ?? this.provider.options.scope);
|
|
252
|
+
}
|
|
253
|
+
|
|
96
254
|
/**
|
|
97
255
|
* Deletes the file at the given path.
|
|
98
256
|
* @param path The file to delete.
|
|
99
|
-
* @param scope The directory scope the path is in. Defaults to
|
|
257
|
+
* @param scope The directory scope the path is in. Defaults to set default scope set for the current provider.
|
|
100
258
|
* @returns A promise that resolves when the file has been deleted.
|
|
101
259
|
*/
|
|
102
|
-
unlink
|
|
103
|
-
return nativeInstance.deleteFile(
|
|
104
|
-
}
|
|
260
|
+
unlink(path, scope) {
|
|
261
|
+
return this.nativeInstance.deleteFile(path, scope ?? this.provider.options.scope);
|
|
262
|
+
}
|
|
263
|
+
|
|
105
264
|
/**
|
|
106
265
|
* Deletes the directory at the given path.
|
|
107
266
|
* @param path The directory to delete.
|
|
108
267
|
* @param options Options for the delete operation. Defaults to { recursive: false }.
|
|
109
|
-
* @param scope The directory scope the path is in. Defaults to
|
|
268
|
+
* @param scope The directory scope the path is in. Defaults to set default scope set for the current provider.
|
|
110
269
|
* @returns A promise that resolves when the directory has been deleted.
|
|
111
270
|
*/
|
|
112
|
-
rmdir
|
|
113
|
-
return nativeInstance.deleteDirectory(
|
|
114
|
-
}
|
|
271
|
+
rmdir(path, options, scope) {
|
|
272
|
+
return this.nativeInstance.deleteDirectory(path, options?.recursive ?? false, scope ?? this.provider.options.scope);
|
|
273
|
+
}
|
|
274
|
+
|
|
115
275
|
/**
|
|
116
276
|
* Gets the size, creation time, and modification time of the file at the given path.
|
|
117
277
|
* @param path The file to stat.
|
|
118
|
-
* @param scope The directory scope the path is in. Defaults to
|
|
278
|
+
* @param scope The directory scope the path is in. Defaults to set default scope set for the current provider.
|
|
119
279
|
* @returns A promise that resolves to the CloudStorageFileStat object.
|
|
120
280
|
*/
|
|
121
|
-
|
|
122
|
-
const native = await nativeInstance.statFile(
|
|
281
|
+
async stat(path, scope) {
|
|
282
|
+
const native = await this.nativeInstance.statFile(path, scope ?? this.provider.options.scope);
|
|
123
283
|
return {
|
|
124
284
|
...native,
|
|
125
285
|
birthtime: new Date(native.birthtimeMs),
|
|
@@ -128,6 +288,143 @@ const RNCloudStorage = {
|
|
|
128
288
|
isFile: () => native.isFile
|
|
129
289
|
};
|
|
130
290
|
}
|
|
131
|
-
|
|
132
|
-
|
|
291
|
+
//#endregion
|
|
292
|
+
|
|
293
|
+
//#region Static methods for default static instance
|
|
294
|
+
static getDefaultInstance() {
|
|
295
|
+
if (!RNCloudStorage.defaultInstance) {
|
|
296
|
+
RNCloudStorage.defaultInstance = new RNCloudStorage();
|
|
297
|
+
}
|
|
298
|
+
return RNCloudStorage.defaultInstance;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* Gets the current options for the provider of the default static instance.
|
|
303
|
+
* @returns The current options for the provider of the default static instance.
|
|
304
|
+
*/
|
|
305
|
+
static getProviderOptions() {
|
|
306
|
+
return RNCloudStorage.getDefaultInstance().getProviderOptions();
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* Sets the options for the provider of the default static instance.
|
|
311
|
+
* @param options The options to set for the provider of the default static instance.
|
|
312
|
+
*/
|
|
313
|
+
static setProviderOptions(options) {
|
|
314
|
+
RNCloudStorage.getDefaultInstance().setProviderOptions(options);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* Tests whether or not the file at the given path exists in the provider of the default static instance.
|
|
319
|
+
* @param path The path to test.
|
|
320
|
+
* @param scope The directory scope the path is in. Defaults to set default scope set for the current provider.
|
|
321
|
+
* @returns A promise that resolves to true if the path exists, false otherwise.
|
|
322
|
+
*/
|
|
323
|
+
static exists(path, scope) {
|
|
324
|
+
return RNCloudStorage.getDefaultInstance().exists(path, scope);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* Tests whether or not the cloud storage is available for the provider of the default static instance. Always returns true for Google Drive. iCloud may be
|
|
329
|
+
* unavailable right after app launch or if the user is not logged in.
|
|
330
|
+
* @returns A promise that resolves to true if the cloud storage is available, false otherwise.
|
|
331
|
+
*/
|
|
332
|
+
static isCloudAvailable() {
|
|
333
|
+
return RNCloudStorage.getDefaultInstance().isCloudAvailable();
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* Appends the data to the file at the given path in the provider of the default static instance, creating the file if it doesn't exist.
|
|
338
|
+
* @param path The file to append to.
|
|
339
|
+
* @param data The data to append.
|
|
340
|
+
* @param scope The directory scope the path is in. Defaults to the default scope set for the default static instance.
|
|
341
|
+
* @returns A promise that resolves when the data has been appended.
|
|
342
|
+
*/
|
|
343
|
+
static appendFile(path, data, scope) {
|
|
344
|
+
return RNCloudStorage.getDefaultInstance().appendFile(path, data, scope);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* Writes to the file at the given path in the provider of the default static instance, creating it if it doesn't exist or overwriting it if it does.
|
|
349
|
+
* @param path The file to write to.
|
|
350
|
+
* @param data The data to write.
|
|
351
|
+
* @param scope The directory scope the path is in. Defaults to the default scope set for the default static instance.
|
|
352
|
+
* @returns A promise that resolves when the file has been written.
|
|
353
|
+
*/
|
|
354
|
+
static writeFile(path, data, scope) {
|
|
355
|
+
return RNCloudStorage.getDefaultInstance().writeFile(path, data, scope);
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/**
|
|
359
|
+
* Creates a new directory at the given path in the provider of the default static instance.
|
|
360
|
+
* @param path The directory to create.
|
|
361
|
+
* @param scope The directory scope the path is in. Defaults to the default scope set for the default static instance.
|
|
362
|
+
* @returns A promise that resolves when the directory has been created.
|
|
363
|
+
*/
|
|
364
|
+
static mkdir(path, scope) {
|
|
365
|
+
return RNCloudStorage.getDefaultInstance().mkdir(path, scope);
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/**
|
|
369
|
+
* Lists the contents of the directory at the given path in the provider of the default static instance.
|
|
370
|
+
* @param path The directory to list.
|
|
371
|
+
* @param scope The directory scope the path is in. Defaults to the default scope set for the default static instance.
|
|
372
|
+
* @returns A promise that resolves to an array of file names, excluding '.' and '..'.
|
|
373
|
+
*/
|
|
374
|
+
static readdir(path, scope) {
|
|
375
|
+
return RNCloudStorage.getDefaultInstance().readdir(path, scope);
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
/**
|
|
379
|
+
* Reads the contents of the file at the given path in the provider of the default static instance.
|
|
380
|
+
* @param path The file to read.
|
|
381
|
+
* @param scope The directory scope the path is in. Defaults to the default scope set for the default static instance.
|
|
382
|
+
* @returns A promise that resolves to the contents of the file.
|
|
383
|
+
*/
|
|
384
|
+
static readFile(path, scope) {
|
|
385
|
+
return RNCloudStorage.getDefaultInstance().readFile(path, scope);
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/**
|
|
389
|
+
* Downloads the file at the given path in the provider of the default static instance. Does not have any effect on Google Drive.
|
|
390
|
+
* @param path The file to trigger the download for.
|
|
391
|
+
* @param scope The directory scope the path is in. Defaults to the default scope set for the default static instance.
|
|
392
|
+
* @returns A promise that resolves once the download has been triggered.
|
|
393
|
+
*/
|
|
394
|
+
static downloadFile(path, scope) {
|
|
395
|
+
return RNCloudStorage.getDefaultInstance().downloadFile(path, scope);
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* Deletes the file at the given path in the provider of the default static instance.
|
|
400
|
+
* @param path The file to delete.
|
|
401
|
+
* @param scope The directory scope the path is in. Defaults to the default scope set for the default static instance.
|
|
402
|
+
* @returns A promise that resolves when the file has been deleted.
|
|
403
|
+
*/
|
|
404
|
+
static unlink(path, scope) {
|
|
405
|
+
return RNCloudStorage.getDefaultInstance().unlink(path, scope);
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/**
|
|
409
|
+
* Deletes the directory at the given path in the provider of the default static instance.
|
|
410
|
+
* @param path The directory to delete.
|
|
411
|
+
* @param options Options for the delete operation. Defaults to { recursive: false }.
|
|
412
|
+
* @param scope The directory scope the path is in. Defaults to the default scope set for the default static instance.
|
|
413
|
+
* @returns A promise that resolves when the directory has been deleted.
|
|
414
|
+
*/
|
|
415
|
+
static rmdir(path, options, scope) {
|
|
416
|
+
return RNCloudStorage.getDefaultInstance().rmdir(path, options, scope);
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/**
|
|
420
|
+
* Gets the size, creation time, and modification time of the file at the given path in the provider of the default static instance.
|
|
421
|
+
* @param path The file to stat.
|
|
422
|
+
* @param scope The directory scope the path is in. Defaults to the default scope set for the default static instance.
|
|
423
|
+
* @returns A promise that resolves to the CloudStorageFileStat object.
|
|
424
|
+
*/
|
|
425
|
+
static stat(path, scope) {
|
|
426
|
+
return RNCloudStorage.getDefaultInstance().stat(path, scope);
|
|
427
|
+
}
|
|
428
|
+
//#endregion
|
|
429
|
+
}
|
|
133
430
|
//# sourceMappingURL=RNCloudStorage.js.map
|