react-native-cloud-storage 0.4.1 → 0.5.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/lib/commonjs/RNCloudStorage.js +12 -2
- package/lib/commonjs/RNCloudStorage.js.map +1 -1
- package/lib/commonjs/createRNCloudStorage.js +5 -5
- package/lib/commonjs/createRNCloudStorage.js.map +1 -1
- package/lib/commonjs/google-drive/index.js +82 -25
- package/lib/commonjs/google-drive/index.js.map +1 -1
- package/lib/commonjs/hooks/useCloudFile.js.map +1 -1
- package/lib/commonjs/index.js +7 -7
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/types/main.js +6 -6
- package/lib/commonjs/types/main.js.map +1 -1
- package/lib/commonjs/types/native.js +15 -13
- package/lib/commonjs/types/native.js.map +1 -1
- package/lib/commonjs/utils/{NativeStorageError.js → CloudStorageError.js} +3 -3
- package/lib/commonjs/utils/CloudStorageError.js.map +1 -0
- package/lib/module/RNCloudStorage.js +12 -2
- package/lib/module/RNCloudStorage.js.map +1 -1
- package/lib/module/createRNCloudStorage.js +6 -6
- package/lib/module/createRNCloudStorage.js.map +1 -1
- package/lib/module/google-drive/index.js +84 -27
- package/lib/module/google-drive/index.js.map +1 -1
- package/lib/module/hooks/useCloudFile.js.map +1 -1
- package/lib/module/index.js +3 -4
- package/lib/module/index.js.map +1 -1
- package/lib/module/types/main.js +4 -4
- package/lib/module/types/main.js.map +1 -1
- package/lib/module/types/native.js +13 -11
- package/lib/module/types/native.js.map +1 -1
- package/lib/module/utils/CloudStorageError.js +9 -0
- package/lib/module/utils/CloudStorageError.js.map +1 -0
- package/lib/typescript/RNCloudStorage.d.ts +15 -8
- package/lib/typescript/RNCloudStorage.d.ts.map +1 -1
- package/lib/typescript/google-drive/index.d.ts +21 -10
- package/lib/typescript/google-drive/index.d.ts.map +1 -1
- package/lib/typescript/hooks/useCloudFile.d.ts +2 -2
- package/lib/typescript/hooks/useCloudFile.d.ts.map +1 -1
- package/lib/typescript/index.d.ts +3 -4
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/types/main.d.ts +2 -2
- package/lib/typescript/types/main.d.ts.map +1 -1
- package/lib/typescript/types/native.d.ts +10 -8
- package/lib/typescript/types/native.d.ts.map +1 -1
- package/lib/typescript/utils/CloudStorageError.d.ts +8 -0
- package/lib/typescript/utils/CloudStorageError.d.ts.map +1 -0
- package/package.json +1 -1
- package/src/RNCloudStorage.ts +18 -8
- package/src/createRNCloudStorage.ts +6 -6
- package/src/google-drive/index.ts +118 -40
- package/src/hooks/useCloudFile.ts +2 -2
- package/src/index.ts +3 -4
- package/src/types/main.ts +2 -2
- package/src/types/native.ts +10 -8
- package/src/utils/CloudStorageError.ts +14 -0
- package/lib/commonjs/utils/NativeStorageError.js.map +0 -1
- package/lib/module/utils/NativeStorageError.js +0 -9
- package/lib/module/utils/NativeStorageError.js.map +0 -1
- package/lib/typescript/utils/NativeStorageError.d.ts +0 -8
- package/lib/typescript/utils/NativeStorageError.d.ts.map +0 -1
- package/src/utils/NativeStorageError.ts +0 -14
package/src/RNCloudStorage.ts
CHANGED
|
@@ -1,11 +1,21 @@
|
|
|
1
1
|
import createRNCloudStorage from './createRNCloudStorage';
|
|
2
2
|
import GoogleDriveApiClient from './google-drive';
|
|
3
|
-
import type {
|
|
3
|
+
import type { CloudStorageFileStat, CloudStorageScope } from './types/main';
|
|
4
|
+
import { Platform } from 'react-native';
|
|
4
5
|
|
|
5
6
|
const nativeInstance = createRNCloudStorage();
|
|
7
|
+
|
|
6
8
|
const RNCloudStorage = {
|
|
7
|
-
|
|
9
|
+
getGoogleDriveAccessToken: () => GoogleDriveApiClient.accessToken,
|
|
8
10
|
setGoogleDriveAccessToken: (accessToken: string) => (GoogleDriveApiClient.accessToken = accessToken),
|
|
11
|
+
setThrowOnFilesWithSameName: (enable: boolean) => (GoogleDriveApiClient.throwOnFilesWithSameName = enable),
|
|
12
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
13
|
+
subscribeToFilesWithSameName:
|
|
14
|
+
Platform.OS === 'ios'
|
|
15
|
+
? // @ts-expect-error - subscriber is undefined; just a mock
|
|
16
|
+
(subscriber: ({ path, fileIds }: { path: string; fileIds: string[] }) => void) => ({ remove: () => {} })
|
|
17
|
+
: (nativeInstance as GoogleDriveApiClient).subscribeToFilesWithSameName.bind(nativeInstance),
|
|
18
|
+
/* eslint-enable @typescript-eslint/no-unused-vars */
|
|
9
19
|
|
|
10
20
|
/**
|
|
11
21
|
* Tests whether or not the cloud storage is available. Always returns true for Google Drive. iCloud may be
|
|
@@ -22,7 +32,7 @@ const RNCloudStorage = {
|
|
|
22
32
|
* @param scope The directory scope the path is in.
|
|
23
33
|
* @returns A promise that resolves to true if the path exists, false otherwise.
|
|
24
34
|
*/
|
|
25
|
-
exists: (path: string, scope:
|
|
35
|
+
exists: (path: string, scope: CloudStorageScope): Promise<boolean> => {
|
|
26
36
|
return nativeInstance.fileExists(path, scope);
|
|
27
37
|
},
|
|
28
38
|
|
|
@@ -33,7 +43,7 @@ const RNCloudStorage = {
|
|
|
33
43
|
* @param scope The directory scope the path is in.
|
|
34
44
|
* @returns A promise that resolves when the file has been written.
|
|
35
45
|
*/
|
|
36
|
-
writeFile: (path: string, data: string, scope:
|
|
46
|
+
writeFile: (path: string, data: string, scope: CloudStorageScope): Promise<void> => {
|
|
37
47
|
return nativeInstance.createFile(path, data, scope, true);
|
|
38
48
|
},
|
|
39
49
|
|
|
@@ -43,7 +53,7 @@ const RNCloudStorage = {
|
|
|
43
53
|
* @param scope The directory scope the path is in.
|
|
44
54
|
* @returns A promise that resolves to the contents of the file.
|
|
45
55
|
*/
|
|
46
|
-
readFile: (path: string, scope:
|
|
56
|
+
readFile: (path: string, scope: CloudStorageScope): Promise<string> => {
|
|
47
57
|
return nativeInstance.readFile(path, scope);
|
|
48
58
|
},
|
|
49
59
|
|
|
@@ -53,7 +63,7 @@ const RNCloudStorage = {
|
|
|
53
63
|
* @param scope The directory scope the path is in.
|
|
54
64
|
* @returns A promise that resolves when the file has been deleted.
|
|
55
65
|
*/
|
|
56
|
-
unlink: (path: string, scope:
|
|
66
|
+
unlink: (path: string, scope: CloudStorageScope): Promise<void> => {
|
|
57
67
|
return nativeInstance.deleteFile(path, scope);
|
|
58
68
|
},
|
|
59
69
|
|
|
@@ -61,9 +71,9 @@ const RNCloudStorage = {
|
|
|
61
71
|
* Gets the size, creation time, and modification time of the file at the given path.
|
|
62
72
|
* @param path The file to stat.
|
|
63
73
|
* @param scope The directory scope the path is in.
|
|
64
|
-
* @returns A promise that resolves to the
|
|
74
|
+
* @returns A promise that resolves to the CloudStorageFileStat object.
|
|
65
75
|
*/
|
|
66
|
-
stat: async (path: string, scope:
|
|
76
|
+
stat: async (path: string, scope: CloudStorageScope): Promise<CloudStorageFileStat> => {
|
|
67
77
|
const native = await nativeInstance.statFile(path, scope);
|
|
68
78
|
|
|
69
79
|
return {
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { NativeModules, Platform } from 'react-native';
|
|
2
2
|
import type NativeRNCloudStorage from './types/native';
|
|
3
3
|
import GoogleDriveApiClient from './google-drive';
|
|
4
|
-
import {
|
|
5
|
-
import
|
|
4
|
+
import { CloudStorageErrorCode } from './types/native';
|
|
5
|
+
import CloudStorageError from './utils/CloudStorageError';
|
|
6
6
|
|
|
7
7
|
const LINKING_ERROR =
|
|
8
8
|
`The package 'react-native-cloud-storage' doesn't seem to be linked. Make sure: \n\n` +
|
|
@@ -10,7 +10,7 @@ const LINKING_ERROR =
|
|
|
10
10
|
'- You rebuilt the app after installing the package\n' +
|
|
11
11
|
'- You are not using Expo Go\n';
|
|
12
12
|
|
|
13
|
-
// proxy NativeModules.CloudStorage to catch any errors thrown by the native module and wrap them in a
|
|
13
|
+
// proxy NativeModules.CloudStorage to catch any errors thrown by the native module and wrap them in a CloudStorageError
|
|
14
14
|
const nativeIosInstance = NativeModules.CloudStorage
|
|
15
15
|
? new Proxy(NativeModules.CloudStorage, {
|
|
16
16
|
get(target: NativeRNCloudStorage, prop: keyof NativeRNCloudStorage) {
|
|
@@ -21,10 +21,10 @@ const nativeIosInstance = NativeModules.CloudStorage
|
|
|
21
21
|
// @ts-expect-error - we can't know the types of the functions and their arguments
|
|
22
22
|
return await originalFunction(...args);
|
|
23
23
|
} catch (error: any) {
|
|
24
|
-
if (error?.code && Object.values(
|
|
25
|
-
throw new
|
|
24
|
+
if (error?.code && Object.values(CloudStorageErrorCode).includes(error.code)) {
|
|
25
|
+
throw new CloudStorageError(error?.message || '', error.code as CloudStorageErrorCode);
|
|
26
26
|
} else {
|
|
27
|
-
throw new
|
|
27
|
+
throw new CloudStorageError('Unknown error', CloudStorageErrorCode.UNKNOWN, error);
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
30
|
};
|
|
@@ -1,26 +1,30 @@
|
|
|
1
|
-
import { GDrive, MimeTypes } from 'react-native-google-drive-api-wrapper-js';
|
|
1
|
+
import { GDrive, HttpError, MimeTypes } from 'react-native-google-drive-api-wrapper-js';
|
|
2
2
|
import type NativeRNCloudStorage from '../types/native';
|
|
3
3
|
import {
|
|
4
|
-
|
|
5
|
-
type
|
|
6
|
-
type
|
|
4
|
+
CloudStorageErrorCode,
|
|
5
|
+
type NativeRNCloudCloudStorageFileStat,
|
|
6
|
+
type NativeRNCloudCloudStorageScope,
|
|
7
7
|
} from '../types/native';
|
|
8
8
|
import type { GoogleDriveDetailedFile, GoogleDriveFile, GoogleDriveListOperationResponse } from './types';
|
|
9
|
-
import
|
|
9
|
+
import CloudStorageError from '../utils/CloudStorageError';
|
|
10
10
|
|
|
11
|
-
class GoogleDriveApiClient implements NativeRNCloudStorage {
|
|
11
|
+
export default class GoogleDriveApiClient implements NativeRNCloudStorage {
|
|
12
12
|
private static drive: GDrive = new GDrive();
|
|
13
|
+
public static throwOnFilesWithSameName = false;
|
|
14
|
+
public filesWithSameNameSubscribers: (({ path, fileIds }: { path: string; fileIds: string[] }) => void)[];
|
|
13
15
|
|
|
14
16
|
constructor() {
|
|
17
|
+
this.filesWithSameNameSubscribers = [];
|
|
15
18
|
GoogleDriveApiClient.drive.fetchTimeout = 3000;
|
|
16
19
|
return new Proxy(this, {
|
|
17
20
|
// before calling any function, check if the access token is set
|
|
18
21
|
get(target: GoogleDriveApiClient, prop: keyof GoogleDriveApiClient) {
|
|
19
|
-
|
|
22
|
+
const allowedFunctions = ['isCloudAvailable', 'subscribeToFilesWithSameName'];
|
|
23
|
+
if (typeof target[prop] === 'function' && !allowedFunctions.includes(prop.toString())) {
|
|
20
24
|
if (!GoogleDriveApiClient.drive.accessToken) {
|
|
21
|
-
throw new
|
|
25
|
+
throw new CloudStorageError(
|
|
22
26
|
`Google Drive access token is not set, cannot call function ${prop.toString()}`,
|
|
23
|
-
|
|
27
|
+
CloudStorageErrorCode.GOOGLE_DRIVE_ACCESS_TOKEN_MISSING
|
|
24
28
|
);
|
|
25
29
|
}
|
|
26
30
|
}
|
|
@@ -31,17 +35,29 @@ class GoogleDriveApiClient implements NativeRNCloudStorage {
|
|
|
31
35
|
}
|
|
32
36
|
|
|
33
37
|
// when setting accessToken, set it on the GDrive instance
|
|
34
|
-
public static set accessToken(accessToken: string) {
|
|
38
|
+
public static set accessToken(accessToken: string | undefined) {
|
|
35
39
|
GoogleDriveApiClient.drive.accessToken = accessToken;
|
|
36
40
|
}
|
|
37
41
|
|
|
38
|
-
public static get accessToken(): string {
|
|
42
|
+
public static get accessToken(): string | undefined {
|
|
39
43
|
return GoogleDriveApiClient.drive.accessToken;
|
|
40
44
|
}
|
|
41
45
|
|
|
46
|
+
public subscribeToFilesWithSameName(subscriber: ({ path, fileIds }: { path: string; fileIds: string[] }) => void): {
|
|
47
|
+
remove: () => void;
|
|
48
|
+
} {
|
|
49
|
+
this.filesWithSameNameSubscribers.push(subscriber);
|
|
50
|
+
|
|
51
|
+
return {
|
|
52
|
+
remove: () => {
|
|
53
|
+
this.filesWithSameNameSubscribers = this.filesWithSameNameSubscribers.filter((s) => s !== subscriber);
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
42
58
|
public isCloudAvailable: () => Promise<boolean> = async () => !!GoogleDriveApiClient.accessToken?.length;
|
|
43
59
|
|
|
44
|
-
private getRootDirectory(scope:
|
|
60
|
+
private getRootDirectory(scope: NativeRNCloudCloudStorageScope): 'drive' | 'appDataFolder' {
|
|
45
61
|
switch (scope) {
|
|
46
62
|
case 'documents':
|
|
47
63
|
return 'drive';
|
|
@@ -80,9 +96,9 @@ class GoogleDriveApiClient implements NativeRNCloudStorage {
|
|
|
80
96
|
}
|
|
81
97
|
|
|
82
98
|
if (!topDirectoryId) {
|
|
83
|
-
throw new
|
|
99
|
+
throw new CloudStorageError(
|
|
84
100
|
`Could not find top directory with name ${directoryTree[0]}`,
|
|
85
|
-
|
|
101
|
+
CloudStorageErrorCode.DIRECTORY_NOT_FOUND
|
|
86
102
|
);
|
|
87
103
|
}
|
|
88
104
|
|
|
@@ -91,15 +107,15 @@ class GoogleDriveApiClient implements NativeRNCloudStorage {
|
|
|
91
107
|
for (let i = 1; i < directoryTree.length; i++) {
|
|
92
108
|
const currentDirectory = files.find((f) => f.id === currentDirectoryId);
|
|
93
109
|
if (!currentDirectory)
|
|
94
|
-
throw new
|
|
110
|
+
throw new CloudStorageError(
|
|
95
111
|
`Could not find directory with id ${currentDirectoryId}`,
|
|
96
|
-
|
|
112
|
+
CloudStorageErrorCode.DIRECTORY_NOT_FOUND
|
|
97
113
|
);
|
|
98
114
|
const nextDirectory = files.find((f) => f.name === directoryTree[i] && f.parents![0] === currentDirectoryId);
|
|
99
115
|
if (!nextDirectory)
|
|
100
|
-
throw new
|
|
116
|
+
throw new CloudStorageError(
|
|
101
117
|
`Could not find directory with name ${directoryTree[i]}`,
|
|
102
|
-
|
|
118
|
+
CloudStorageErrorCode.DIRECTORY_NOT_FOUND
|
|
103
119
|
);
|
|
104
120
|
currentDirectoryId = nextDirectory.id;
|
|
105
121
|
}
|
|
@@ -107,7 +123,7 @@ class GoogleDriveApiClient implements NativeRNCloudStorage {
|
|
|
107
123
|
return currentDirectoryId;
|
|
108
124
|
}
|
|
109
125
|
|
|
110
|
-
private async listFiles(scope:
|
|
126
|
+
private async listFiles(scope: NativeRNCloudCloudStorageScope): Promise<GoogleDriveFile[]> {
|
|
111
127
|
const files: GoogleDriveListOperationResponse = await GoogleDriveApiClient.drive.files.list({
|
|
112
128
|
spaces: [this.getRootDirectory(scope)],
|
|
113
129
|
fields: 'files(id,kind,mimeType,name,parents,spaces)',
|
|
@@ -116,41 +132,105 @@ class GoogleDriveApiClient implements NativeRNCloudStorage {
|
|
|
116
132
|
return files.files;
|
|
117
133
|
}
|
|
118
134
|
|
|
119
|
-
private
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
135
|
+
private checkIfMultipleFilesWithSameName(
|
|
136
|
+
path: string,
|
|
137
|
+
files: GoogleDriveFile[],
|
|
138
|
+
filename: string,
|
|
139
|
+
parentDirectoryId: string | null
|
|
140
|
+
) {
|
|
141
|
+
let possibleFiles: GoogleDriveFile[];
|
|
142
|
+
if (parentDirectoryId) {
|
|
143
|
+
possibleFiles = files.filter((f) => f.name === filename && f.parents![0] === parentDirectoryId);
|
|
128
144
|
} else {
|
|
129
|
-
|
|
145
|
+
possibleFiles = files.filter((f) => f.name === filename && !files.find((f2) => f2.id === f.parents![0]));
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (possibleFiles.length <= 1) return;
|
|
149
|
+
|
|
150
|
+
if (GoogleDriveApiClient.throwOnFilesWithSameName) {
|
|
151
|
+
throw new CloudStorageError(
|
|
152
|
+
`Multiple files with the same name found at path ${path}: ${possibleFiles.map((f) => f.id).join(', ')}`,
|
|
153
|
+
CloudStorageErrorCode.MULTIPLE_FILES_SAME_NAME
|
|
154
|
+
);
|
|
155
|
+
} else {
|
|
156
|
+
this.filesWithSameNameSubscribers.forEach((s) => s({ path, fileIds: possibleFiles.map((f) => f.id) }));
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
private async getFileId(path: string, scope: NativeRNCloudCloudStorageScope): Promise<string> {
|
|
161
|
+
try {
|
|
162
|
+
const files = await this.listFiles(scope);
|
|
163
|
+
const { directories, filename } = this.resolvePathToDirectories(path);
|
|
164
|
+
const parentDirectoryId = this.findParentDirectoryId(files, directories);
|
|
165
|
+
let file: GoogleDriveFile | undefined;
|
|
166
|
+
if (parentDirectoryId === null) {
|
|
167
|
+
this.checkIfMultipleFilesWithSameName(path, files, filename, null);
|
|
168
|
+
/* when the file is supposed to be in the root directory, we need to get the file where the name is the filename
|
|
169
|
+
and the first parent has an id which does not exist in the files array */
|
|
170
|
+
file = files.find((f) => f.name === filename && !files.find((f2) => f2.id === f.parents![0]));
|
|
171
|
+
} else {
|
|
172
|
+
this.checkIfMultipleFilesWithSameName(path, files, filename, parentDirectoryId);
|
|
173
|
+
file = files.find((f) => f.name === filename && f.parents![0] === parentDirectoryId);
|
|
174
|
+
}
|
|
175
|
+
if (!file) throw new CloudStorageError(`File not found`, CloudStorageErrorCode.FILE_NOT_FOUND);
|
|
176
|
+
if (file.mimeType === MimeTypes.FOLDER) {
|
|
177
|
+
throw new CloudStorageError(`Path ${path} is a directory`, CloudStorageErrorCode.FILE_NOT_FOUND);
|
|
178
|
+
}
|
|
179
|
+
return file.id;
|
|
180
|
+
} catch (e: unknown) {
|
|
181
|
+
if (e instanceof HttpError && e.json?.error?.status === 'UNAUTHENTICATED') {
|
|
182
|
+
throw new CloudStorageError(
|
|
183
|
+
`Could not authenticate with Google Drive`,
|
|
184
|
+
CloudStorageErrorCode.AUTHENTICATION_FAILED,
|
|
185
|
+
e.json
|
|
186
|
+
);
|
|
187
|
+
} else {
|
|
188
|
+
if (e instanceof CloudStorageError) throw e;
|
|
189
|
+
throw new CloudStorageError(`Could not get file id for path ${path}`, CloudStorageErrorCode.UNKNOWN, e);
|
|
190
|
+
}
|
|
130
191
|
}
|
|
131
|
-
if (!file) throw new NativeStorageError(`File not found`, NativeStorageErrorCode.FILE_NOT_FOUND);
|
|
132
|
-
return file.id;
|
|
133
192
|
}
|
|
134
193
|
|
|
135
|
-
async fileExists(path: string, scope:
|
|
194
|
+
async fileExists(path: string, scope: NativeRNCloudCloudStorageScope): Promise<boolean> {
|
|
136
195
|
try {
|
|
137
196
|
await this.getFileId(path, scope);
|
|
138
197
|
return true;
|
|
139
198
|
} catch (e: any) {
|
|
140
|
-
if (e instanceof
|
|
199
|
+
if (e instanceof CloudStorageError && e.code === CloudStorageErrorCode.FILE_NOT_FOUND) return false;
|
|
141
200
|
else throw e;
|
|
142
201
|
}
|
|
143
202
|
}
|
|
144
203
|
|
|
145
|
-
async createFile(
|
|
204
|
+
async createFile(
|
|
205
|
+
path: string,
|
|
206
|
+
data: string,
|
|
207
|
+
scope: NativeRNCloudCloudStorageScope,
|
|
208
|
+
overwrite: boolean
|
|
209
|
+
): Promise<void> {
|
|
146
210
|
let fileId: string | undefined;
|
|
147
211
|
if (overwrite) {
|
|
148
212
|
try {
|
|
149
213
|
fileId = await this.getFileId(path, scope);
|
|
150
214
|
} catch (e: any) {
|
|
151
|
-
|
|
215
|
+
if (e instanceof CloudStorageError && e.code === CloudStorageErrorCode.FILE_NOT_FOUND) {
|
|
216
|
+
/* do nothing, simply create the file */
|
|
217
|
+
} else {
|
|
218
|
+
throw e;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
} else {
|
|
222
|
+
try {
|
|
223
|
+
await this.getFileId(path, scope);
|
|
224
|
+
throw new CloudStorageError(`File ${path} already exists`, CloudStorageErrorCode.FILE_ALREADY_EXISTS);
|
|
225
|
+
} catch (e: any) {
|
|
226
|
+
if (e instanceof CloudStorageError && e.code === CloudStorageErrorCode.FILE_NOT_FOUND) {
|
|
227
|
+
/* do nothing, simply create the file */
|
|
228
|
+
} else {
|
|
229
|
+
throw e;
|
|
230
|
+
}
|
|
152
231
|
}
|
|
153
232
|
}
|
|
233
|
+
|
|
154
234
|
const uploader = GoogleDriveApiClient.drive.files.newMultipartUploader().setData(data, MimeTypes.TEXT);
|
|
155
235
|
if (fileId) uploader.setIdOfFileToUpdate(fileId);
|
|
156
236
|
else {
|
|
@@ -169,18 +249,18 @@ class GoogleDriveApiClient implements NativeRNCloudStorage {
|
|
|
169
249
|
await uploader.execute();
|
|
170
250
|
}
|
|
171
251
|
|
|
172
|
-
async readFile(path: string, scope:
|
|
252
|
+
async readFile(path: string, scope: NativeRNCloudCloudStorageScope): Promise<string> {
|
|
173
253
|
const fileId = await this.getFileId(path, scope);
|
|
174
254
|
const content = await GoogleDriveApiClient.drive.files.getText(fileId);
|
|
175
255
|
return content;
|
|
176
256
|
}
|
|
177
257
|
|
|
178
|
-
async deleteFile(path: string, scope:
|
|
258
|
+
async deleteFile(path: string, scope: NativeRNCloudCloudStorageScope): Promise<void> {
|
|
179
259
|
const fileId = await this.getFileId(path, scope);
|
|
180
260
|
await GoogleDriveApiClient.drive.files.delete(fileId);
|
|
181
261
|
}
|
|
182
262
|
|
|
183
|
-
async statFile(path: string, scope:
|
|
263
|
+
async statFile(path: string, scope: NativeRNCloudCloudStorageScope): Promise<NativeRNCloudCloudStorageFileStat> {
|
|
184
264
|
const fileId = await this.getFileId(path, scope);
|
|
185
265
|
const file: GoogleDriveDetailedFile = await GoogleDriveApiClient.drive.files.get(fileId, {
|
|
186
266
|
fields: 'id,kind,mimeType,name,parents,spaces,size,createdTime,modifiedTime',
|
|
@@ -194,5 +274,3 @@ class GoogleDriveApiClient implements NativeRNCloudStorage {
|
|
|
194
274
|
};
|
|
195
275
|
}
|
|
196
276
|
}
|
|
197
|
-
|
|
198
|
-
export default GoogleDriveApiClient;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { CloudStorageScope } from '../types/main';
|
|
2
2
|
import RNCloudStorage from '../RNCloudStorage';
|
|
3
3
|
import { useCallback, useEffect, useState } from 'react';
|
|
4
4
|
|
|
5
|
-
export const useCloudFile = (path: string, scope:
|
|
5
|
+
export const useCloudFile = (path: string, scope: CloudStorageScope) => {
|
|
6
6
|
const [content, setContent] = useState<string | null>(null);
|
|
7
7
|
|
|
8
8
|
const read = useCallback(async () => {
|
package/src/index.ts
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import RNCloudStorage from './RNCloudStorage';
|
|
2
|
-
import {
|
|
2
|
+
import { CloudStorageErrorCode } from './types/native';
|
|
3
3
|
export * from './types/main';
|
|
4
4
|
export * from './hooks/useCloudFile';
|
|
5
5
|
export * from './hooks/useIsCloudAvailable';
|
|
6
|
-
import
|
|
6
|
+
import CloudStorageError from './utils/CloudStorageError';
|
|
7
7
|
|
|
8
|
-
export {
|
|
9
|
-
export { NativeStorageErrorCode };
|
|
8
|
+
export { CloudStorageError, CloudStorageErrorCode };
|
|
10
9
|
export default RNCloudStorage;
|
package/src/types/main.ts
CHANGED
package/src/types/native.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export type
|
|
1
|
+
export type NativeRNCloudCloudStorageScope = 'documents' | 'app_data';
|
|
2
2
|
|
|
3
|
-
export interface
|
|
3
|
+
export interface NativeRNCloudCloudStorageFileStat {
|
|
4
4
|
size: number;
|
|
5
5
|
birthtimeMs: number;
|
|
6
6
|
mtimeMs: number;
|
|
@@ -8,10 +8,12 @@ export interface NativeRNCloudStorageFileStat {
|
|
|
8
8
|
isFile: boolean;
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
export enum
|
|
11
|
+
export enum CloudStorageErrorCode {
|
|
12
12
|
FILE_NOT_FOUND = 'ERR_FILE_NOT_FOUND',
|
|
13
13
|
DIRECTORY_NOT_FOUND = 'ERR_NO_DIRECTORY_FOUND',
|
|
14
14
|
FILE_ALREADY_EXISTS = 'ERR_FILE_EXISTS',
|
|
15
|
+
MULTIPLE_FILES_SAME_NAME = 'ERR_MULTIPLE_FILES_SAME_NAME',
|
|
16
|
+
AUTHENTICATION_FAILED = 'ERR_AUTHENTICATION_FAILED',
|
|
15
17
|
WRITE_ERROR = 'ERR_WRITE_ERROR',
|
|
16
18
|
READ_ERROR = 'ERR_READ_ERROR',
|
|
17
19
|
DELETE_ERROR = 'ERR_DELETE_ERROR',
|
|
@@ -21,10 +23,10 @@ export enum NativeStorageErrorCode {
|
|
|
21
23
|
}
|
|
22
24
|
|
|
23
25
|
export default interface NativeRNCloudStorage {
|
|
24
|
-
fileExists: (path: string, scope:
|
|
25
|
-
createFile: (path: string, data: string, scope:
|
|
26
|
-
readFile: (path: string, scope:
|
|
27
|
-
deleteFile: (path: string, scope:
|
|
28
|
-
statFile: (path: string, scope:
|
|
26
|
+
fileExists: (path: string, scope: NativeRNCloudCloudStorageScope) => Promise<boolean>;
|
|
27
|
+
createFile: (path: string, data: string, scope: NativeRNCloudCloudStorageScope, overwrite: boolean) => Promise<void>;
|
|
28
|
+
readFile: (path: string, scope: NativeRNCloudCloudStorageScope) => Promise<string>;
|
|
29
|
+
deleteFile: (path: string, scope: NativeRNCloudCloudStorageScope) => Promise<void>;
|
|
30
|
+
statFile: (path: string, scope: NativeRNCloudCloudStorageScope) => Promise<NativeRNCloudCloudStorageFileStat>;
|
|
29
31
|
isCloudAvailable: () => Promise<boolean>;
|
|
30
32
|
}
|
|
@@ -0,0 +1,14 @@
|
|
|
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;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"names":["NativeStorageError","Error","constructor","message","code","details","_default","exports","default"],"sourceRoot":"../../../src","sources":["utils/NativeStorageError.ts"],"mappings":";;;;;;AAEA,MAAMA,kBAAkB,SAASC,KAAK,CAAC;EAIrCC,WAAWA,CAACC,OAAe,EAAEC,IAA4B,EAAEC,OAAa,EAAE;IACxE,KAAK,CAACF,OAAO,CAAC;IACd,IAAI,CAACC,IAAI,GAAGA,IAAI;IAChB,IAAI,CAACC,OAAO,GAAGA,OAAO;EACxB;AACF;AAAC,IAAAC,QAAA,GAEcN,kBAAkB;AAAAO,OAAA,CAAAC,OAAA,GAAAF,QAAA"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"names":["NativeStorageError","Error","constructor","message","code","details"],"sourceRoot":"../../../src","sources":["utils/NativeStorageError.ts"],"mappings":"AAEA,MAAMA,kBAAkB,SAASC,KAAK,CAAC;EAIrCC,WAAWA,CAACC,OAAe,EAAEC,IAA4B,EAAEC,OAAa,EAAE;IACxE,KAAK,CAACF,OAAO,CAAC;IACd,IAAI,CAACC,IAAI,GAAGA,IAAI;IAChB,IAAI,CAACC,OAAO,GAAGA,OAAO;EACxB;AACF;AAEA,eAAeL,kBAAkB"}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import type { NativeStorageErrorCode } from '../types/native';
|
|
2
|
-
declare class NativeStorageError extends Error {
|
|
3
|
-
code: NativeStorageErrorCode;
|
|
4
|
-
details?: any;
|
|
5
|
-
constructor(message: string, code: NativeStorageErrorCode, details?: any);
|
|
6
|
-
}
|
|
7
|
-
export default NativeStorageError;
|
|
8
|
-
//# sourceMappingURL=NativeStorageError.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"NativeStorageError.d.ts","sourceRoot":"","sources":["../../../src/utils/NativeStorageError.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,iBAAiB,CAAC;AAE9D,cAAM,kBAAmB,SAAQ,KAAK;IACpC,IAAI,EAAE,sBAAsB,CAAC;IAC7B,OAAO,CAAC,EAAE,GAAG,CAAC;gBAEF,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,sBAAsB,EAAE,OAAO,CAAC,EAAE,GAAG;CAKzE;AAED,eAAe,kBAAkB,CAAC"}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import type { NativeStorageErrorCode } from '../types/native';
|
|
2
|
-
|
|
3
|
-
class NativeStorageError extends Error {
|
|
4
|
-
code: NativeStorageErrorCode;
|
|
5
|
-
details?: any;
|
|
6
|
-
|
|
7
|
-
constructor(message: string, code: NativeStorageErrorCode, details?: any) {
|
|
8
|
-
super(message);
|
|
9
|
-
this.code = code;
|
|
10
|
-
this.details = details;
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export default NativeStorageError;
|