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,4 +1,4 @@
|
|
|
1
|
-
import { type CloudStorageProviderOptions, type DeepRequired } from '
|
|
1
|
+
import { type CloudStorageProviderOptions, type DeepRequired } from '../../types/main';
|
|
2
2
|
import {
|
|
3
3
|
MimeTypes,
|
|
4
4
|
type GoogleDriveFile,
|
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
type GoogleDriveListOperationQueryParameters,
|
|
7
7
|
type GoogleDriveListOperationResponse,
|
|
8
8
|
} from './types';
|
|
9
|
+
import { localFileSystem } from '../../utils/local-fs';
|
|
9
10
|
|
|
10
11
|
const BASE_URL = 'https://www.googleapis.com/drive/v3';
|
|
11
12
|
const BASE_UPLOAD_URL = 'https://www.googleapis.com/upload/drive/v3';
|
|
@@ -13,19 +14,19 @@ const MULTIPART_BOUNDARY = 'foo_bar_baz';
|
|
|
13
14
|
|
|
14
15
|
export class GoogleDriveHttpError extends Error {
|
|
15
16
|
public status: number;
|
|
17
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
16
18
|
public json: any;
|
|
17
19
|
|
|
18
|
-
constructor(message: string, status: number, json:
|
|
20
|
+
constructor(message: string, status: number, json: unknown) {
|
|
19
21
|
super(message);
|
|
20
22
|
this.status = status;
|
|
21
23
|
this.json = json;
|
|
22
24
|
}
|
|
23
25
|
}
|
|
24
26
|
|
|
25
|
-
// TODO: fetch timeout
|
|
26
27
|
// TODO: properly handle errors
|
|
27
28
|
export default class GoogleDriveApiClient {
|
|
28
|
-
private _fetchTimeout:
|
|
29
|
+
private _fetchTimeout: NodeJS.Timeout | null = null;
|
|
29
30
|
private options: DeepRequired<CloudStorageProviderOptions['googledrive']>;
|
|
30
31
|
|
|
31
32
|
constructor(options: DeepRequired<CloudStorageProviderOptions['googledrive']>) {
|
|
@@ -33,7 +34,7 @@ export default class GoogleDriveApiClient {
|
|
|
33
34
|
}
|
|
34
35
|
|
|
35
36
|
private buildQueryString(query: object): string {
|
|
36
|
-
let
|
|
37
|
+
let result = Object.entries(query)
|
|
37
38
|
.filter(([, value]) => value !== undefined)
|
|
38
39
|
.map(([key, value]) => {
|
|
39
40
|
if (typeof value === 'boolean') return `${encodeURIComponent(key)}=${value ? 'true' : 'false'}`;
|
|
@@ -41,13 +42,13 @@ export default class GoogleDriveApiClient {
|
|
|
41
42
|
})
|
|
42
43
|
.join('&');
|
|
43
44
|
|
|
44
|
-
if (
|
|
45
|
-
|
|
45
|
+
if (result) {
|
|
46
|
+
result = `?${result}`;
|
|
46
47
|
}
|
|
47
|
-
return
|
|
48
|
+
return result;
|
|
48
49
|
}
|
|
49
50
|
|
|
50
|
-
private async request<T extends
|
|
51
|
+
private async request<T extends object | string | void = void>(
|
|
51
52
|
operation: `/${string}`,
|
|
52
53
|
{ queryParameters, baseUrl, ...options }: RequestInit & { queryParameters?: object; baseUrl?: string } = {}
|
|
53
54
|
): Promise<T> {
|
|
@@ -57,7 +58,7 @@ export default class GoogleDriveApiClient {
|
|
|
57
58
|
if (queryParameters) {
|
|
58
59
|
path += this.buildQueryString(queryParameters);
|
|
59
60
|
}
|
|
60
|
-
clearTimeout(this._fetchTimeout);
|
|
61
|
+
if (this._fetchTimeout !== null) clearTimeout(this._fetchTimeout);
|
|
61
62
|
const abortController: AbortController = new AbortController();
|
|
62
63
|
this._fetchTimeout = setTimeout(() => {
|
|
63
64
|
abortController.abort();
|
|
@@ -72,11 +73,20 @@ export default class GoogleDriveApiClient {
|
|
|
72
73
|
});
|
|
73
74
|
if (!response.ok) {
|
|
74
75
|
let errorMessage: string;
|
|
75
|
-
let json:
|
|
76
|
+
let json: unknown = null;
|
|
76
77
|
try {
|
|
77
78
|
json = await response.json();
|
|
78
|
-
errorMessage =
|
|
79
|
-
|
|
79
|
+
errorMessage =
|
|
80
|
+
typeof json === 'object' &&
|
|
81
|
+
json !== null &&
|
|
82
|
+
'error' in json &&
|
|
83
|
+
typeof json.error === 'object' &&
|
|
84
|
+
json.error !== null &&
|
|
85
|
+
'message' in json.error &&
|
|
86
|
+
typeof json.error.message === 'string'
|
|
87
|
+
? json.error.message
|
|
88
|
+
: `Request failed with status ${response.status}`;
|
|
89
|
+
} catch {
|
|
80
90
|
errorMessage = `Request failed with status ${response.status}`;
|
|
81
91
|
}
|
|
82
92
|
throw new GoogleDriveHttpError(errorMessage, response.status, json);
|
|
@@ -93,18 +103,20 @@ export default class GoogleDriveApiClient {
|
|
|
93
103
|
|
|
94
104
|
private buildMultiPartBody(metadata: object, media: { mimeType: string; body: string }): string {
|
|
95
105
|
const body: string[] = [];
|
|
96
|
-
body.push(
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
106
|
+
body.push(
|
|
107
|
+
`--${MULTIPART_BOUNDARY}\r\n`,
|
|
108
|
+
`Content-Type: ${MimeTypes.JSON}; charset=UTF-8\r\n\r\n`,
|
|
109
|
+
`${JSON.stringify(metadata)}\r\n`,
|
|
110
|
+
`--${MULTIPART_BOUNDARY}\r\n`,
|
|
111
|
+
`Content-Type: ${media.mimeType}\r\n\r\n`,
|
|
112
|
+
`${media.body}\r\n`,
|
|
113
|
+
`--${MULTIPART_BOUNDARY}--`
|
|
114
|
+
);
|
|
103
115
|
|
|
104
116
|
return body.join('');
|
|
105
117
|
}
|
|
106
118
|
|
|
107
|
-
public async listFiles(space: GoogleDriveFileSpace): Promise<GoogleDriveFile[]> {
|
|
119
|
+
public async listFiles(space: GoogleDriveFileSpace, query?: string): Promise<GoogleDriveFile[]> {
|
|
108
120
|
const files: GoogleDriveFile[] = [];
|
|
109
121
|
let pageToken: string | undefined;
|
|
110
122
|
const fields = ['id', 'kind', 'mimeType', 'name', 'parents', 'spaces', 'size', 'createdTime', 'modifiedTime'];
|
|
@@ -113,6 +125,7 @@ export default class GoogleDriveApiClient {
|
|
|
113
125
|
fields: `files(${fields.join(',')}),nextPageToken`,
|
|
114
126
|
spaces: space,
|
|
115
127
|
pageToken,
|
|
128
|
+
q: query,
|
|
116
129
|
};
|
|
117
130
|
const response = await this.request<GoogleDriveListOperationResponse>(`/files`, {
|
|
118
131
|
queryParameters,
|
|
@@ -149,20 +162,42 @@ export default class GoogleDriveApiClient {
|
|
|
149
162
|
|
|
150
163
|
public async createFile(
|
|
151
164
|
metadata: { name: string; parents?: string[] },
|
|
152
|
-
media: { mimeType: string; body: string }
|
|
165
|
+
media: { mimeType: string; body: string } | { mimeType: string; localPath: string }
|
|
153
166
|
): Promise<void> {
|
|
154
|
-
|
|
167
|
+
if ('body' in media) {
|
|
168
|
+
const multipartRequestBody = this.buildMultiPartBody(metadata, media);
|
|
169
|
+
|
|
170
|
+
await this.request(`/files`, {
|
|
171
|
+
queryParameters: { uploadType: 'multipart' },
|
|
172
|
+
method: 'POST',
|
|
173
|
+
headers: {
|
|
174
|
+
'Content-Type': `multipart/related; boundary=${MULTIPART_BOUNDARY}`,
|
|
175
|
+
'Content-Length': multipartRequestBody.length.toString(),
|
|
176
|
+
},
|
|
177
|
+
body: multipartRequestBody,
|
|
178
|
+
baseUrl: BASE_UPLOAD_URL,
|
|
179
|
+
});
|
|
180
|
+
} else {
|
|
181
|
+
// First, create an empty file with the required metadata
|
|
182
|
+
const file = await this.request<{ id: string }>(`/files`, {
|
|
183
|
+
method: 'POST',
|
|
184
|
+
headers: {
|
|
185
|
+
'Content-Type': MimeTypes.JSON,
|
|
186
|
+
},
|
|
187
|
+
body: JSON.stringify({ ...metadata, mimeType: media.mimeType }),
|
|
188
|
+
});
|
|
155
189
|
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
'
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
190
|
+
// Then, upload the binary content via the native helper module
|
|
191
|
+
const remoteUri = `${BASE_UPLOAD_URL}/files/${file.id}?uploadType=media`;
|
|
192
|
+
await localFileSystem.uploadFile(media.localPath, remoteUri, {
|
|
193
|
+
method: 'PATCH',
|
|
194
|
+
uploadType: 'binary',
|
|
195
|
+
headers: {
|
|
196
|
+
'Authorization': `Bearer ${this.options.accessToken}`,
|
|
197
|
+
'Content-Type': media.mimeType,
|
|
198
|
+
},
|
|
199
|
+
});
|
|
200
|
+
}
|
|
166
201
|
}
|
|
167
202
|
|
|
168
203
|
public async createDirectory(metadata: { name: string; parents?: string[] }): Promise<void> {
|
|
@@ -175,16 +210,40 @@ export default class GoogleDriveApiClient {
|
|
|
175
210
|
});
|
|
176
211
|
}
|
|
177
212
|
|
|
178
|
-
public async updateFile(
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
213
|
+
public async updateFile(
|
|
214
|
+
fileId: string,
|
|
215
|
+
media: { mimeType: string; body: string } | { mimeType: string; localPath: string }
|
|
216
|
+
): Promise<void> {
|
|
217
|
+
if ('body' in media) {
|
|
218
|
+
await this.request(`/files/${fileId}`, {
|
|
219
|
+
queryParameters: { uploadType: 'media' },
|
|
220
|
+
method: 'PATCH',
|
|
221
|
+
headers: {
|
|
222
|
+
'Content-Type': media.mimeType,
|
|
223
|
+
'Content-Length': media.body.length.toString(),
|
|
224
|
+
},
|
|
225
|
+
body: media.body,
|
|
226
|
+
baseUrl: BASE_UPLOAD_URL,
|
|
227
|
+
});
|
|
228
|
+
} else {
|
|
229
|
+
const remoteUri = `${BASE_UPLOAD_URL}/files/${fileId}?uploadType=media`;
|
|
230
|
+
await localFileSystem.uploadFile(media.localPath, remoteUri, {
|
|
231
|
+
method: 'PATCH',
|
|
232
|
+
uploadType: 'binary',
|
|
233
|
+
headers: {
|
|
234
|
+
'Authorization': `Bearer ${this.options.accessToken}`,
|
|
235
|
+
'Content-Type': media.mimeType,
|
|
236
|
+
},
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
public async downloadFile(fileId: string, localPath: string): Promise<void> {
|
|
242
|
+
const remoteUri = `${BASE_URL}/files/${fileId}?alt=media`;
|
|
243
|
+
await localFileSystem.downloadFile(remoteUri, localPath, {
|
|
182
244
|
headers: {
|
|
183
|
-
|
|
184
|
-
'Content-Length': media.body.length.toString(),
|
|
245
|
+
Authorization: `Bearer ${this.options.accessToken}`,
|
|
185
246
|
},
|
|
186
|
-
body: media.body,
|
|
187
|
-
baseUrl: BASE_UPLOAD_URL,
|
|
188
247
|
});
|
|
189
248
|
}
|
|
190
249
|
}
|