react-native-cloud-storage 2.2.2 → 3.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 +1 -1
- package/android/build.gradle +88 -0
- package/android/gradle.properties +5 -0
- package/android/src/main/AndroidManifest.xml +3 -0
- package/android/src/main/AndroidManifestNew.xml +2 -0
- package/android/src/main/java/com/voicekit/CloudStorageError.kt +86 -0
- package/android/src/main/java/com/voicekit/CloudStorageLocalFileSystemModule.kt +232 -0
- package/android/src/main/java/com/voicekit/CloudStoragePackage.kt +32 -0
- package/android/src/main/java/com/voicekit/FileUtils.kt +41 -0
- package/android/src/main/java/com/voicekit/Types.kt +6 -0
- package/app.plugin.js +1 -1
- package/{lib/commonjs/RNCloudStorage.js → dist/commonjs/cloud-storage.js} +177 -92
- package/dist/commonjs/cloud-storage.js.map +1 -0
- package/dist/commonjs/expo-plugin/index.js +13 -0
- package/dist/commonjs/expo-plugin/index.js.map +1 -0
- package/{lib/commonjs/expo-plugin/withRNCloudStorageIos.js → dist/commonjs/expo-plugin/ios.js} +1 -1
- package/dist/commonjs/expo-plugin/ios.js.map +1 -0
- package/{lib/commonjs/hooks/useCloudFile.js → dist/commonjs/hooks/use-cloud-file.js} +26 -8
- package/dist/commonjs/hooks/use-cloud-file.js.map +1 -0
- package/{lib/commonjs/hooks/useIsCloudAvailable.js → dist/commonjs/hooks/use-is-cloud-available.js} +3 -3
- package/dist/commonjs/hooks/use-is-cloud-available.js.map +1 -0
- package/{lib → dist}/commonjs/index.js +10 -10
- package/dist/commonjs/index.js.map +1 -0
- package/dist/commonjs/specs/NativeCloudStorageCloudKitIOS.js +9 -0
- package/dist/commonjs/specs/NativeCloudStorageCloudKitIOS.js.map +1 -0
- package/dist/commonjs/specs/NativeCloudStorageLocalFileSystem.js +9 -0
- package/dist/commonjs/specs/NativeCloudStorageLocalFileSystem.js.map +1 -0
- package/dist/commonjs/storages/cloudkit.js +12 -0
- package/dist/commonjs/storages/cloudkit.js.map +1 -0
- package/{lib/commonjs → dist/commonjs/storages}/google-drive/client.js +83 -40
- package/dist/commonjs/storages/google-drive/client.js.map +1 -0
- package/dist/commonjs/storages/google-drive/index.js +399 -0
- package/dist/commonjs/storages/google-drive/index.js.map +1 -0
- package/dist/commonjs/storages/google-drive/types.js.map +1 -0
- package/{lib → dist}/commonjs/types/main.js.map +1 -1
- package/dist/commonjs/types/native.js +28 -0
- package/dist/commonjs/types/native.js.map +1 -0
- package/{lib/commonjs/utils/CloudStorageError.js → dist/commonjs/utils/cloud-storage-error.js} +3 -1
- package/dist/commonjs/utils/cloud-storage-error.js.map +1 -0
- package/dist/commonjs/utils/constants.js +25 -0
- package/dist/commonjs/utils/constants.js.map +1 -0
- package/dist/commonjs/utils/local-fs.js +17 -0
- package/dist/commonjs/utils/local-fs.js.map +1 -0
- package/dist/commonjs/utils/native.js +35 -0
- package/dist/commonjs/utils/native.js.map +1 -0
- package/{lib/module/RNCloudStorage.js → dist/module/cloud-storage.js} +178 -93
- package/dist/module/cloud-storage.js.map +1 -0
- package/dist/module/expo-plugin/index.js +8 -0
- package/dist/module/expo-plugin/index.js.map +1 -0
- package/{lib/module/expo-plugin/withRNCloudStorageIos.js → dist/module/expo-plugin/ios.js} +1 -1
- package/dist/module/expo-plugin/ios.js.map +1 -0
- package/{lib/module/hooks/useCloudFile.js → dist/module/hooks/use-cloud-file.js} +25 -7
- package/dist/module/hooks/use-cloud-file.js.map +1 -0
- package/{lib/module/hooks/useIsCloudAvailable.js → dist/module/hooks/use-is-cloud-available.js} +2 -2
- package/dist/module/hooks/use-is-cloud-available.js.map +1 -0
- package/dist/module/index.js +9 -0
- package/dist/module/index.js.map +1 -0
- package/dist/module/specs/NativeCloudStorageCloudKitIOS.js +5 -0
- package/dist/module/specs/NativeCloudStorageCloudKitIOS.js.map +1 -0
- package/dist/module/specs/NativeCloudStorageLocalFileSystem.js +5 -0
- package/dist/module/specs/NativeCloudStorageLocalFileSystem.js.map +1 -0
- package/dist/module/storages/cloudkit.js +7 -0
- package/dist/module/storages/cloudkit.js.map +1 -0
- package/{lib/module → dist/module/storages}/google-drive/client.js +83 -40
- package/dist/module/storages/google-drive/client.js.map +1 -0
- package/dist/module/storages/google-drive/index.js +392 -0
- package/dist/module/storages/google-drive/index.js.map +1 -0
- package/dist/module/storages/google-drive/types.js.map +1 -0
- package/{lib → dist}/module/types/main.js.map +1 -1
- package/dist/module/types/native.js +24 -0
- package/dist/module/types/native.js.map +1 -0
- package/{lib/module/utils/CloudStorageError.js → dist/module/utils/cloud-storage-error.js} +3 -1
- package/dist/module/utils/cloud-storage-error.js.map +1 -0
- package/dist/module/utils/constants.js +21 -0
- package/dist/module/utils/constants.js.map +1 -0
- package/dist/module/utils/local-fs.js +12 -0
- package/dist/module/utils/local-fs.js.map +1 -0
- package/dist/module/utils/native.js +30 -0
- package/dist/module/utils/native.js.map +1 -0
- package/{lib/typescript/RNCloudStorage.d.ts → dist/typescript/cloud-storage.d.ts} +67 -8
- package/dist/typescript/cloud-storage.d.ts.map +1 -0
- package/{lib/typescript/expo-plugin/withRNCloudStorage.d.ts → dist/typescript/expo-plugin/index.d.ts} +1 -1
- package/dist/typescript/expo-plugin/index.d.ts.map +1 -0
- package/{lib/typescript/expo-plugin/withRNCloudStorageIos.d.ts → dist/typescript/expo-plugin/ios.d.ts} +1 -1
- package/dist/typescript/expo-plugin/ios.d.ts.map +1 -0
- package/{lib/typescript/hooks/useCloudFile.d.ts → dist/typescript/hooks/use-cloud-file.d.ts} +22 -4
- package/dist/typescript/hooks/use-cloud-file.d.ts.map +1 -0
- package/{lib/typescript/hooks/useIsCloudAvailable.d.ts → dist/typescript/hooks/use-is-cloud-available.d.ts} +2 -2
- package/dist/typescript/hooks/use-is-cloud-available.d.ts.map +1 -0
- package/dist/typescript/index.d.ts +7 -0
- package/dist/typescript/index.d.ts.map +1 -0
- package/dist/typescript/specs/NativeCloudStorageCloudKitIOS.d.ts +30 -0
- package/dist/typescript/specs/NativeCloudStorageCloudKitIOS.d.ts.map +1 -0
- package/dist/typescript/specs/NativeCloudStorageLocalFileSystem.d.ts +24 -0
- package/dist/typescript/specs/NativeCloudStorageLocalFileSystem.d.ts.map +1 -0
- package/dist/typescript/storages/cloudkit.d.ts +6 -0
- package/dist/typescript/storages/cloudkit.d.ts.map +1 -0
- package/{lib/typescript → dist/typescript/storages}/google-drive/client.d.ts +10 -3
- package/dist/typescript/storages/google-drive/client.d.ts.map +1 -0
- package/dist/typescript/storages/google-drive/index.d.ts +41 -0
- package/dist/typescript/storages/google-drive/index.d.ts.map +1 -0
- package/dist/typescript/storages/google-drive/types.d.ts.map +1 -0
- package/{lib → dist}/typescript/types/main.d.ts +8 -0
- package/dist/typescript/types/main.d.ts.map +1 -0
- package/dist/typescript/types/native.d.ts +27 -0
- package/dist/typescript/types/native.d.ts.map +1 -0
- package/dist/typescript/utils/cloud-storage-error.d.ts +8 -0
- package/dist/typescript/utils/cloud-storage-error.d.ts.map +1 -0
- package/dist/typescript/utils/constants.d.ts +4 -0
- package/dist/typescript/utils/constants.d.ts.map +1 -0
- package/{lib → dist}/typescript/utils/helpers.d.ts.map +1 -1
- package/dist/typescript/utils/local-fs.d.ts +2 -0
- package/dist/typescript/utils/local-fs.d.ts.map +1 -0
- package/dist/typescript/utils/native.d.ts +7 -0
- package/dist/typescript/utils/native.d.ts.map +1 -0
- package/ios/CloudStorage-Bridging-Header.h +0 -1
- package/ios/CloudStorage.xcodeproj/project.pbxproj +12 -6
- package/ios/CloudStorageCloudKit.swift +159 -0
- package/ios/CloudStorageLocalFileSystem.swift +216 -0
- package/ios/RCTCloudStorageCloudKit.mm +209 -0
- package/ios/RCTCloudStorageLocalFileSystem.mm +149 -0
- package/ios/Utils/CloudKitUtils.swift +12 -6
- package/ios/Utils/CloudStorageError.swift +8 -0
- package/ios/Utils/FileUtils.swift +21 -4
- package/ios/Utils/Promise.swift +1 -0
- package/ios/Utils/Types.swift +8 -1
- package/ios/react_native_cloud_storage.h +6 -0
- package/package.json +64 -110
- package/react-native-cloud-storage.podspec +2 -0
- package/src/{RNCloudStorage.ts → cloud-storage.ts} +210 -100
- package/src/expo-plugin/{withRNCloudStorage.ts → index.ts} +2 -2
- package/src/hooks/{useCloudFile.ts → use-cloud-file.ts} +24 -6
- package/src/hooks/{useIsCloudAvailable.ts → use-is-cloud-available.ts} +1 -1
- package/src/index.ts +5 -6
- package/src/specs/NativeCloudStorageCloudKitIOS.ts +33 -0
- package/src/specs/NativeCloudStorageLocalFileSystem.ts +28 -0
- package/src/storages/cloudkit.ts +13 -0
- package/src/{google-drive → storages/google-drive}/client.ts +100 -41
- package/src/storages/google-drive/index.ts +488 -0
- package/src/types/main.ts +9 -0
- package/src/types/native.ts +14 -22
- package/src/utils/cloud-storage-error.ts +15 -0
- package/src/utils/constants.ts +21 -0
- package/src/utils/local-fs.ts +19 -0
- package/src/utils/native.ts +40 -0
- package/ios/CloudStorage.m +0 -22
- package/ios/CloudStorage.swift +0 -103
- package/ios/CloudStorageEventEmitter.m +0 -16
- package/ios/CloudStorageEventEmitter.swift +0 -30
- package/lib/commonjs/RNCloudStorage.js.map +0 -1
- package/lib/commonjs/expo-plugin/withRNCloudStorage.js +0 -13
- package/lib/commonjs/expo-plugin/withRNCloudStorage.js.map +0 -1
- package/lib/commonjs/expo-plugin/withRNCloudStorageIos.js.map +0 -1
- package/lib/commonjs/google-drive/client.js.map +0 -1
- package/lib/commonjs/google-drive/index.js +0 -321
- package/lib/commonjs/google-drive/index.js.map +0 -1
- package/lib/commonjs/google-drive/types.js.map +0 -1
- package/lib/commonjs/hooks/useCloudFile.js.map +0 -1
- package/lib/commonjs/hooks/useIsCloudAvailable.js.map +0 -1
- package/lib/commonjs/index.js.map +0 -1
- package/lib/commonjs/types/native.js +0 -26
- package/lib/commonjs/types/native.js.map +0 -1
- package/lib/commonjs/utils/CloudStorageError.js.map +0 -1
- package/lib/module/RNCloudStorage.js.map +0 -1
- package/lib/module/expo-plugin/withRNCloudStorage.js +0 -8
- package/lib/module/expo-plugin/withRNCloudStorage.js.map +0 -1
- package/lib/module/expo-plugin/withRNCloudStorageIos.js.map +0 -1
- package/lib/module/google-drive/client.js.map +0 -1
- package/lib/module/google-drive/index.js +0 -313
- package/lib/module/google-drive/index.js.map +0 -1
- package/lib/module/google-drive/types.js.map +0 -1
- package/lib/module/hooks/useCloudFile.js.map +0 -1
- package/lib/module/hooks/useIsCloudAvailable.js.map +0 -1
- package/lib/module/index.js +0 -10
- package/lib/module/index.js.map +0 -1
- package/lib/module/types/native.js +0 -22
- package/lib/module/types/native.js.map +0 -1
- package/lib/module/utils/CloudStorageError.js.map +0 -1
- package/lib/typescript/RNCloudStorage.d.ts.map +0 -1
- package/lib/typescript/expo-plugin/withRNCloudStorage.d.ts.map +0 -1
- package/lib/typescript/expo-plugin/withRNCloudStorageIos.d.ts.map +0 -1
- package/lib/typescript/google-drive/client.d.ts.map +0 -1
- package/lib/typescript/google-drive/index.d.ts +0 -34
- package/lib/typescript/google-drive/index.d.ts.map +0 -1
- package/lib/typescript/google-drive/types.d.ts.map +0 -1
- package/lib/typescript/hooks/useCloudFile.d.ts.map +0 -1
- package/lib/typescript/hooks/useIsCloudAvailable.d.ts.map +0 -1
- package/lib/typescript/index.d.ts +0 -8
- package/lib/typescript/index.d.ts.map +0 -1
- package/lib/typescript/types/main.d.ts.map +0 -1
- package/lib/typescript/types/native.d.ts +0 -40
- package/lib/typescript/types/native.d.ts.map +0 -1
- package/lib/typescript/utils/CloudStorageError.d.ts +0 -8
- package/lib/typescript/utils/CloudStorageError.d.ts.map +0 -1
- package/src/google-drive/index.ts +0 -399
- package/src/utils/CloudStorageError.ts +0 -14
- /package/{lib → dist}/commonjs/expo-plugin/types/index.js +0 -0
- /package/{lib → dist}/commonjs/expo-plugin/types/index.js.map +0 -0
- /package/{lib/commonjs → dist/commonjs/storages}/google-drive/types.js +0 -0
- /package/{lib → dist}/commonjs/types/main.js +0 -0
- /package/{lib → dist}/commonjs/utils/helpers.js +0 -0
- /package/{lib → dist}/commonjs/utils/helpers.js.map +0 -0
- /package/{lib → dist}/module/expo-plugin/types/index.js +0 -0
- /package/{lib → dist}/module/expo-plugin/types/index.js.map +0 -0
- /package/{lib → dist}/module/package.json +0 -0
- /package/{lib/module → dist/module/storages}/google-drive/types.js +0 -0
- /package/{lib → dist}/module/types/main.js +0 -0
- /package/{lib → dist}/module/utils/helpers.js +0 -0
- /package/{lib → dist}/module/utils/helpers.js.map +0 -0
- /package/{lib → dist}/typescript/expo-plugin/types/index.d.ts +0 -0
- /package/{lib → dist}/typescript/expo-plugin/types/index.d.ts.map +0 -0
- /package/{lib/typescript → dist/typescript/storages}/google-drive/types.d.ts +0 -0
- /package/{lib → dist}/typescript/utils/helpers.d.ts +0 -0
- /package/src/expo-plugin/{withRNCloudStorageIos.ts → ios.ts} +0 -0
- /package/src/{google-drive → storages/google-drive}/types.ts +0 -0
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import type { CloudStorageErrorCode } from '../types/native';
|
|
2
|
-
declare class CloudStorageError extends Error {
|
|
3
|
-
code: CloudStorageErrorCode;
|
|
4
|
-
details?: any;
|
|
5
|
-
constructor(message: string, code: CloudStorageErrorCode, details?: any);
|
|
6
|
-
}
|
|
7
|
-
export default CloudStorageError;
|
|
8
|
-
//# sourceMappingURL=CloudStorageError.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"CloudStorageError.d.ts","sourceRoot":"","sources":["../../../src/utils/CloudStorageError.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,iBAAiB,CAAC;AAE7D,cAAM,iBAAkB,SAAQ,KAAK;IACnC,IAAI,EAAE,qBAAqB,CAAC;IAC5B,OAAO,CAAC,EAAE,GAAG,CAAC;gBAEF,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,qBAAqB,EAAE,OAAO,CAAC,EAAE,GAAG;CAKxE;AAED,eAAe,iBAAiB,CAAC"}
|
|
@@ -1,399 +0,0 @@
|
|
|
1
|
-
import type NativeproviderService from '../types/native';
|
|
2
|
-
import {
|
|
3
|
-
CloudStorageErrorCode,
|
|
4
|
-
type NativeRNCloudCloudStorageFileStat,
|
|
5
|
-
type NativeRNCloudCloudStorageScope,
|
|
6
|
-
} from '../types/native';
|
|
7
|
-
import CloudStorageError from '../utils/CloudStorageError';
|
|
8
|
-
import { MimeTypes, type GoogleDriveFile, type GoogleDriveFileSpace } from './types';
|
|
9
|
-
import GoogleDriveApiClient, { GoogleDriveHttpError } from './client';
|
|
10
|
-
import { type CloudStorageProviderOptions, type DeepRequired } from '../types/main';
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* A proxy class that wraps the Google Drive API client implementation to match the native iOS interface.
|
|
14
|
-
*/
|
|
15
|
-
export default class GoogleDrive implements NativeproviderService {
|
|
16
|
-
private drive: GoogleDriveApiClient;
|
|
17
|
-
private options: DeepRequired<CloudStorageProviderOptions['googledrive']>;
|
|
18
|
-
|
|
19
|
-
constructor(options: DeepRequired<CloudStorageProviderOptions['googledrive']>) {
|
|
20
|
-
this.options = options;
|
|
21
|
-
this.drive = new GoogleDriveApiClient(options);
|
|
22
|
-
|
|
23
|
-
return new Proxy(this, {
|
|
24
|
-
// before calling any function, check if the access token is set
|
|
25
|
-
get(target: GoogleDrive, prop: keyof GoogleDrive) {
|
|
26
|
-
const allowedFunctions = ['isCloudAvailable'];
|
|
27
|
-
if (typeof target[prop] === 'function' && !allowedFunctions.includes(prop.toString())) {
|
|
28
|
-
const { accessToken } = options;
|
|
29
|
-
if (!accessToken?.length) {
|
|
30
|
-
throw new CloudStorageError(
|
|
31
|
-
`Google Drive access token is not set, cannot call function ${prop.toString()}`,
|
|
32
|
-
CloudStorageErrorCode.ACCESS_TOKEN_MISSING
|
|
33
|
-
);
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
return target[prop];
|
|
38
|
-
},
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
public isCloudAvailable: () => Promise<boolean> = async () => {
|
|
43
|
-
const { accessToken } = this.options;
|
|
44
|
-
return !!accessToken?.length;
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
private getRootDirectory(scope: NativeRNCloudCloudStorageScope): GoogleDriveFileSpace {
|
|
48
|
-
switch (scope) {
|
|
49
|
-
case 'documents':
|
|
50
|
-
return 'drive';
|
|
51
|
-
case 'app_data':
|
|
52
|
-
return 'appDataFolder';
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
private resolvePathToDirectories(path: string): { directories: string[]; filename: string } {
|
|
57
|
-
if (path.startsWith('/')) path = path.slice(1);
|
|
58
|
-
if (path.endsWith('/')) path = path.slice(0, -1);
|
|
59
|
-
const directories = path.split('/');
|
|
60
|
-
const actualFilename = directories.pop() ?? '';
|
|
61
|
-
return { directories, filename: actualFilename };
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
private findParentDirectoryId(files: GoogleDriveFile[], directoryTree: string[]): string | null {
|
|
65
|
-
const possibleTopDirectories = files
|
|
66
|
-
.filter((f) => f.mimeType === MimeTypes.FOLDER)
|
|
67
|
-
.filter((f) => f.name === directoryTree[0]);
|
|
68
|
-
|
|
69
|
-
let topDirectoryId: string | undefined;
|
|
70
|
-
if (possibleTopDirectories.length === 0) return null;
|
|
71
|
-
else if (possibleTopDirectories.length === 1) {
|
|
72
|
-
topDirectoryId = possibleTopDirectories[0]!.id;
|
|
73
|
-
} else {
|
|
74
|
-
/* when multiple directories carry the same name, we need to check every one of them if their parent id exists in
|
|
75
|
-
the files array - if it does not, it means that the directory is a child of the root directory and the one we're
|
|
76
|
-
looking for */
|
|
77
|
-
for (const possibleTopDirectory of possibleTopDirectories) {
|
|
78
|
-
if (!files.find((f) => f.id === possibleTopDirectory!.parents![0] && f.mimeType === MimeTypes.FOLDER)) {
|
|
79
|
-
topDirectoryId = possibleTopDirectory!.id;
|
|
80
|
-
break;
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
if (!topDirectoryId) {
|
|
86
|
-
throw new CloudStorageError(
|
|
87
|
-
`Could not find top directory with name ${directoryTree[0]}`,
|
|
88
|
-
CloudStorageErrorCode.DIRECTORY_NOT_FOUND
|
|
89
|
-
);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
// now, we traverse the directories array and get the id of the last directory from the files array
|
|
93
|
-
let currentDirectoryId = topDirectoryId;
|
|
94
|
-
for (let i = 1; i < directoryTree.length; i++) {
|
|
95
|
-
const currentDirectory = files.find((f) => f.id === currentDirectoryId);
|
|
96
|
-
if (!currentDirectory)
|
|
97
|
-
throw new CloudStorageError(
|
|
98
|
-
`Could not find directory with id ${currentDirectoryId}`,
|
|
99
|
-
CloudStorageErrorCode.DIRECTORY_NOT_FOUND
|
|
100
|
-
);
|
|
101
|
-
const nextDirectory = files.find((f) => f.name === directoryTree[i] && f.parents![0] === currentDirectoryId);
|
|
102
|
-
if (!nextDirectory)
|
|
103
|
-
throw new CloudStorageError(
|
|
104
|
-
`Could not find directory with name ${directoryTree[i]}`,
|
|
105
|
-
CloudStorageErrorCode.DIRECTORY_NOT_FOUND
|
|
106
|
-
);
|
|
107
|
-
currentDirectoryId = nextDirectory.id;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
return currentDirectoryId;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
* Gets the Google Drive ID of the root directory for the given scope.
|
|
115
|
-
* @param scope The scope to get the root directory for.
|
|
116
|
-
* @returns A promise that resolves to the ID of the root directory or null if it could not be found.
|
|
117
|
-
*/
|
|
118
|
-
private async getRootDirectoryId(scope: NativeRNCloudCloudStorageScope): Promise<string | null> {
|
|
119
|
-
const files = await this.drive.listFiles(this.getRootDirectory(scope));
|
|
120
|
-
for (const file of files) {
|
|
121
|
-
if (!files.find((f) => f.id === file.parents![0])) return file.parents![0] ?? null;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
return null;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
private checkIfMultipleFilesWithSameName(
|
|
128
|
-
path: string,
|
|
129
|
-
files: GoogleDriveFile[],
|
|
130
|
-
filename: string,
|
|
131
|
-
parentDirectoryId: string | null
|
|
132
|
-
) {
|
|
133
|
-
const { strictFilenames } = this.options;
|
|
134
|
-
|
|
135
|
-
let possibleFiles: GoogleDriveFile[];
|
|
136
|
-
if (parentDirectoryId) {
|
|
137
|
-
possibleFiles = files.filter((f) => f.name === filename && f.parents![0] === parentDirectoryId);
|
|
138
|
-
} else {
|
|
139
|
-
possibleFiles = files.filter((f) => f.name === filename && !files.find((f2) => f2.id === f.parents![0]));
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
if (possibleFiles.length <= 1) return;
|
|
143
|
-
|
|
144
|
-
if (strictFilenames) {
|
|
145
|
-
throw new CloudStorageError(
|
|
146
|
-
`Multiple files with the same name found at path ${path}: ${possibleFiles.map((f) => f.id).join(', ')}`,
|
|
147
|
-
CloudStorageErrorCode.MULTIPLE_FILES_SAME_NAME
|
|
148
|
-
);
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
private async getFileId(
|
|
153
|
-
path: string,
|
|
154
|
-
scope: NativeRNCloudCloudStorageScope,
|
|
155
|
-
throwIf: 'directory' | 'file' | false = false
|
|
156
|
-
): Promise<string> {
|
|
157
|
-
try {
|
|
158
|
-
const files = await this.drive.listFiles(this.getRootDirectory(scope));
|
|
159
|
-
|
|
160
|
-
if (path === '' || path === '/') {
|
|
161
|
-
const rootDirectoryId = await this.getRootDirectoryId(scope);
|
|
162
|
-
if (!rootDirectoryId)
|
|
163
|
-
throw new CloudStorageError(
|
|
164
|
-
`Root directory in scope ${scope} not found`,
|
|
165
|
-
CloudStorageErrorCode.DIRECTORY_NOT_FOUND
|
|
166
|
-
);
|
|
167
|
-
return rootDirectoryId;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
const { directories, filename } = this.resolvePathToDirectories(path);
|
|
171
|
-
const parentDirectoryId = this.findParentDirectoryId(files, directories);
|
|
172
|
-
let file: GoogleDriveFile | undefined;
|
|
173
|
-
if (parentDirectoryId === null) {
|
|
174
|
-
this.checkIfMultipleFilesWithSameName(path, files, filename, null);
|
|
175
|
-
/* when the file is supposed to be in the root directory, we need to get the file where the name is the filename
|
|
176
|
-
and the first parent has an id which does not exist in the files array */
|
|
177
|
-
file = files.find((f) => f.name === filename && !files.find((f2) => f2.id === f.parents![0]));
|
|
178
|
-
} else {
|
|
179
|
-
this.checkIfMultipleFilesWithSameName(path, files, filename, parentDirectoryId);
|
|
180
|
-
file = files.find((f) => f.name === filename && f.parents![0] === parentDirectoryId);
|
|
181
|
-
}
|
|
182
|
-
if (!file) throw new CloudStorageError(`File not found`, CloudStorageErrorCode.FILE_NOT_FOUND);
|
|
183
|
-
if (file.mimeType === MimeTypes.FOLDER && throwIf === 'directory') {
|
|
184
|
-
throw new CloudStorageError(`Path ${path} is a directory`, CloudStorageErrorCode.PATH_IS_DIRECTORY);
|
|
185
|
-
} else if (file.mimeType !== MimeTypes.FOLDER && throwIf === 'file') {
|
|
186
|
-
throw new CloudStorageError(`Path ${path} is a file`, CloudStorageErrorCode.FILE_NOT_FOUND);
|
|
187
|
-
}
|
|
188
|
-
return file.id;
|
|
189
|
-
} catch (e: unknown) {
|
|
190
|
-
if (e instanceof GoogleDriveHttpError && e.json?.error?.status === 'UNAUTHENTICATED') {
|
|
191
|
-
throw new CloudStorageError(
|
|
192
|
-
`Could not authenticate with Google Drive`,
|
|
193
|
-
CloudStorageErrorCode.AUTHENTICATION_FAILED,
|
|
194
|
-
e.json
|
|
195
|
-
);
|
|
196
|
-
} else {
|
|
197
|
-
if (e instanceof CloudStorageError) throw e;
|
|
198
|
-
throw new CloudStorageError(`Could not get file id for path ${path}`, CloudStorageErrorCode.UNKNOWN, e);
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
async fileExists(path: string, scope: NativeRNCloudCloudStorageScope): Promise<boolean> {
|
|
204
|
-
try {
|
|
205
|
-
await this.getFileId(path, scope);
|
|
206
|
-
return true;
|
|
207
|
-
} catch (e: any) {
|
|
208
|
-
if (e instanceof CloudStorageError && e.code === CloudStorageErrorCode.FILE_NOT_FOUND) return false;
|
|
209
|
-
else throw e;
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
async appendToFile(path: string, data: string, scope: NativeRNCloudCloudStorageScope): Promise<void> {
|
|
214
|
-
let fileId: string | undefined;
|
|
215
|
-
let prevContent = '';
|
|
216
|
-
try {
|
|
217
|
-
fileId = await this.getFileId(path, scope);
|
|
218
|
-
prevContent = await this.drive.getFileText(fileId);
|
|
219
|
-
} catch (e: any) {
|
|
220
|
-
if (e instanceof CloudStorageError && e.code === CloudStorageErrorCode.FILE_NOT_FOUND) {
|
|
221
|
-
/* do nothing, simply create the file */
|
|
222
|
-
} else {
|
|
223
|
-
throw e;
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
if (fileId) {
|
|
228
|
-
await this.drive.updateFile(fileId, {
|
|
229
|
-
body: prevContent + data,
|
|
230
|
-
mimeType: MimeTypes.TEXT,
|
|
231
|
-
});
|
|
232
|
-
} else {
|
|
233
|
-
const files = await this.drive.listFiles(this.getRootDirectory(scope));
|
|
234
|
-
const { directories, filename } = this.resolvePathToDirectories(path);
|
|
235
|
-
const parentDirectoryId = this.findParentDirectoryId(files, directories);
|
|
236
|
-
await this.drive.createFile(
|
|
237
|
-
{
|
|
238
|
-
name: filename,
|
|
239
|
-
parents: parentDirectoryId
|
|
240
|
-
? [parentDirectoryId]
|
|
241
|
-
: scope === 'app_data'
|
|
242
|
-
? [this.getRootDirectory(scope)]
|
|
243
|
-
: undefined,
|
|
244
|
-
},
|
|
245
|
-
{
|
|
246
|
-
body: data,
|
|
247
|
-
mimeType: MimeTypes.TEXT,
|
|
248
|
-
}
|
|
249
|
-
);
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
async createFile(
|
|
254
|
-
path: string,
|
|
255
|
-
data: string,
|
|
256
|
-
scope: NativeRNCloudCloudStorageScope,
|
|
257
|
-
overwrite: boolean
|
|
258
|
-
): Promise<void> {
|
|
259
|
-
let fileId: string | undefined;
|
|
260
|
-
if (overwrite) {
|
|
261
|
-
try {
|
|
262
|
-
fileId = await this.getFileId(path, scope);
|
|
263
|
-
} catch (e: any) {
|
|
264
|
-
if (e instanceof CloudStorageError && e.code === CloudStorageErrorCode.FILE_NOT_FOUND) {
|
|
265
|
-
/* do nothing, simply create the file */
|
|
266
|
-
} else {
|
|
267
|
-
throw e;
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
} else {
|
|
271
|
-
try {
|
|
272
|
-
await this.getFileId(path, scope);
|
|
273
|
-
throw new CloudStorageError(`File ${path} already exists`, CloudStorageErrorCode.FILE_ALREADY_EXISTS);
|
|
274
|
-
} catch (e: any) {
|
|
275
|
-
if (e instanceof CloudStorageError && e.code === CloudStorageErrorCode.FILE_NOT_FOUND) {
|
|
276
|
-
/* do nothing, simply create the file */
|
|
277
|
-
} else {
|
|
278
|
-
throw e;
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
if (fileId) {
|
|
284
|
-
await this.drive.updateFile(fileId, {
|
|
285
|
-
body: data,
|
|
286
|
-
mimeType: MimeTypes.TEXT,
|
|
287
|
-
});
|
|
288
|
-
} else {
|
|
289
|
-
const files = await this.drive.listFiles(this.getRootDirectory(scope));
|
|
290
|
-
const { directories, filename } = this.resolvePathToDirectories(path);
|
|
291
|
-
const parentDirectoryId = this.findParentDirectoryId(files, directories);
|
|
292
|
-
await this.drive.createFile(
|
|
293
|
-
{
|
|
294
|
-
name: filename,
|
|
295
|
-
parents: parentDirectoryId
|
|
296
|
-
? [parentDirectoryId]
|
|
297
|
-
: scope === 'app_data'
|
|
298
|
-
? [this.getRootDirectory(scope)]
|
|
299
|
-
: undefined,
|
|
300
|
-
},
|
|
301
|
-
{
|
|
302
|
-
body: data,
|
|
303
|
-
mimeType: MimeTypes.TEXT,
|
|
304
|
-
}
|
|
305
|
-
);
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
async listFiles(path: string, scope: NativeRNCloudCloudStorageScope): Promise<string[]> {
|
|
310
|
-
const allFiles = await this.drive.listFiles(this.getRootDirectory(scope));
|
|
311
|
-
if (path !== '') {
|
|
312
|
-
const fileId = await this.getFileId(path, scope);
|
|
313
|
-
const files = allFiles.filter((f) => (f.parents ?? [])[0] === fileId);
|
|
314
|
-
|
|
315
|
-
return Array.from(new Set(files.map((f) => f.name)));
|
|
316
|
-
} else {
|
|
317
|
-
const rootDirectoryId = await this.getRootDirectoryId(scope);
|
|
318
|
-
return Array.from(new Set(allFiles.filter((f) => (f.parents ?? [])[0] === rootDirectoryId).map((f) => f.name)));
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
async createDirectory(path: string, scope: NativeRNCloudCloudStorageScope): Promise<void> {
|
|
323
|
-
try {
|
|
324
|
-
await this.getFileId(path, scope);
|
|
325
|
-
throw new CloudStorageError(`File ${path} already exists`, CloudStorageErrorCode.FILE_ALREADY_EXISTS);
|
|
326
|
-
} catch (e: any) {
|
|
327
|
-
if (e instanceof CloudStorageError && e.code === CloudStorageErrorCode.FILE_NOT_FOUND) {
|
|
328
|
-
/* do nothing, simply create the file */
|
|
329
|
-
} else if (e instanceof CloudStorageError && e.code === CloudStorageErrorCode.PATH_IS_DIRECTORY) {
|
|
330
|
-
throw new CloudStorageError(`Directory ${path} already exists`, CloudStorageErrorCode.FILE_ALREADY_EXISTS);
|
|
331
|
-
} else {
|
|
332
|
-
throw e;
|
|
333
|
-
}
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
const files = await this.drive.listFiles(this.getRootDirectory(scope));
|
|
337
|
-
const { directories, filename } = this.resolvePathToDirectories(path);
|
|
338
|
-
const parentDirectoryId = this.findParentDirectoryId(files, directories);
|
|
339
|
-
|
|
340
|
-
await this.drive.createDirectory({
|
|
341
|
-
name: filename,
|
|
342
|
-
parents: parentDirectoryId
|
|
343
|
-
? [parentDirectoryId]
|
|
344
|
-
: scope === 'app_data'
|
|
345
|
-
? [this.getRootDirectory(scope)]
|
|
346
|
-
: undefined,
|
|
347
|
-
});
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
async readFile(path: string, scope: NativeRNCloudCloudStorageScope): Promise<string> {
|
|
351
|
-
const fileId = await this.getFileId(path, scope);
|
|
352
|
-
const content = await this.drive.getFileText(fileId);
|
|
353
|
-
return content;
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
async downloadFile(_path: string, _scope: NativeRNCloudCloudStorageScope): Promise<void> {
|
|
357
|
-
// Downloading files from Google Drive is not necessary / possible, as they need to be downloaded on every read operation via the API anyway
|
|
358
|
-
return;
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
async deleteFile(path: string, scope: NativeRNCloudCloudStorageScope): Promise<void> {
|
|
362
|
-
// if trying to pass a directory, throw an error
|
|
363
|
-
const fileId = await this.getFileId(path, scope, 'directory');
|
|
364
|
-
await this.drive.deleteFile(fileId);
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
async deleteDirectory(path: string, recursive: boolean, scope: NativeRNCloudCloudStorageScope): Promise<void> {
|
|
368
|
-
// if trying to pass a file, throw an error
|
|
369
|
-
const fileId = await this.getFileId(path, scope, 'file');
|
|
370
|
-
|
|
371
|
-
if (!recursive) {
|
|
372
|
-
// check if the directory is empty
|
|
373
|
-
const files = await this.drive.listFiles(this.getRootDirectory(scope));
|
|
374
|
-
const filesInDirectory = files.filter((f) => (f.parents ?? [])[0] === fileId);
|
|
375
|
-
if (filesInDirectory.length > 0) {
|
|
376
|
-
throw new CloudStorageError(
|
|
377
|
-
`Directory ${path} is not empty`,
|
|
378
|
-
CloudStorageErrorCode.DELETE_ERROR,
|
|
379
|
-
filesInDirectory
|
|
380
|
-
);
|
|
381
|
-
}
|
|
382
|
-
}
|
|
383
|
-
|
|
384
|
-
await this.drive.deleteFile(fileId);
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
async statFile(path: string, scope: NativeRNCloudCloudStorageScope): Promise<NativeRNCloudCloudStorageFileStat> {
|
|
388
|
-
const fileId = await this.getFileId(path, scope, false);
|
|
389
|
-
const file = await this.drive.getFile(fileId!);
|
|
390
|
-
|
|
391
|
-
return {
|
|
392
|
-
size: file.size ?? 0,
|
|
393
|
-
birthtimeMs: new Date(file.createdTime!).getTime(),
|
|
394
|
-
mtimeMs: new Date(file.modifiedTime!).getTime(),
|
|
395
|
-
isDirectory: file.mimeType === MimeTypes.FOLDER,
|
|
396
|
-
isFile: file.mimeType !== MimeTypes.FOLDER,
|
|
397
|
-
};
|
|
398
|
-
}
|
|
399
|
-
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import type { CloudStorageErrorCode } from '../types/native';
|
|
2
|
-
|
|
3
|
-
class CloudStorageError extends Error {
|
|
4
|
-
code: CloudStorageErrorCode;
|
|
5
|
-
details?: any;
|
|
6
|
-
|
|
7
|
-
constructor(message: string, code: CloudStorageErrorCode, details?: any) {
|
|
8
|
-
super(message);
|
|
9
|
-
this.code = code;
|
|
10
|
-
this.details = details;
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export default CloudStorageError;
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|