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.
Files changed (216) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +1 -1
  3. package/android/build.gradle +88 -0
  4. package/android/gradle.properties +5 -0
  5. package/android/src/main/AndroidManifest.xml +3 -0
  6. package/android/src/main/AndroidManifestNew.xml +2 -0
  7. package/android/src/main/java/com/voicekit/CloudStorageError.kt +86 -0
  8. package/android/src/main/java/com/voicekit/CloudStorageLocalFileSystemModule.kt +232 -0
  9. package/android/src/main/java/com/voicekit/CloudStoragePackage.kt +32 -0
  10. package/android/src/main/java/com/voicekit/FileUtils.kt +41 -0
  11. package/android/src/main/java/com/voicekit/Types.kt +6 -0
  12. package/app.plugin.js +1 -1
  13. package/{lib/commonjs/RNCloudStorage.js → dist/commonjs/cloud-storage.js} +177 -92
  14. package/dist/commonjs/cloud-storage.js.map +1 -0
  15. package/dist/commonjs/expo-plugin/index.js +13 -0
  16. package/dist/commonjs/expo-plugin/index.js.map +1 -0
  17. package/{lib/commonjs/expo-plugin/withRNCloudStorageIos.js → dist/commonjs/expo-plugin/ios.js} +1 -1
  18. package/dist/commonjs/expo-plugin/ios.js.map +1 -0
  19. package/{lib/commonjs/hooks/useCloudFile.js → dist/commonjs/hooks/use-cloud-file.js} +26 -8
  20. package/dist/commonjs/hooks/use-cloud-file.js.map +1 -0
  21. package/{lib/commonjs/hooks/useIsCloudAvailable.js → dist/commonjs/hooks/use-is-cloud-available.js} +3 -3
  22. package/dist/commonjs/hooks/use-is-cloud-available.js.map +1 -0
  23. package/{lib → dist}/commonjs/index.js +10 -10
  24. package/dist/commonjs/index.js.map +1 -0
  25. package/dist/commonjs/specs/NativeCloudStorageCloudKitIOS.js +9 -0
  26. package/dist/commonjs/specs/NativeCloudStorageCloudKitIOS.js.map +1 -0
  27. package/dist/commonjs/specs/NativeCloudStorageLocalFileSystem.js +9 -0
  28. package/dist/commonjs/specs/NativeCloudStorageLocalFileSystem.js.map +1 -0
  29. package/dist/commonjs/storages/cloudkit.js +12 -0
  30. package/dist/commonjs/storages/cloudkit.js.map +1 -0
  31. package/{lib/commonjs → dist/commonjs/storages}/google-drive/client.js +83 -40
  32. package/dist/commonjs/storages/google-drive/client.js.map +1 -0
  33. package/dist/commonjs/storages/google-drive/index.js +399 -0
  34. package/dist/commonjs/storages/google-drive/index.js.map +1 -0
  35. package/dist/commonjs/storages/google-drive/types.js.map +1 -0
  36. package/{lib → dist}/commonjs/types/main.js.map +1 -1
  37. package/dist/commonjs/types/native.js +28 -0
  38. package/dist/commonjs/types/native.js.map +1 -0
  39. package/{lib/commonjs/utils/CloudStorageError.js → dist/commonjs/utils/cloud-storage-error.js} +3 -1
  40. package/dist/commonjs/utils/cloud-storage-error.js.map +1 -0
  41. package/dist/commonjs/utils/constants.js +25 -0
  42. package/dist/commonjs/utils/constants.js.map +1 -0
  43. package/dist/commonjs/utils/local-fs.js +17 -0
  44. package/dist/commonjs/utils/local-fs.js.map +1 -0
  45. package/dist/commonjs/utils/native.js +35 -0
  46. package/dist/commonjs/utils/native.js.map +1 -0
  47. package/{lib/module/RNCloudStorage.js → dist/module/cloud-storage.js} +178 -93
  48. package/dist/module/cloud-storage.js.map +1 -0
  49. package/dist/module/expo-plugin/index.js +8 -0
  50. package/dist/module/expo-plugin/index.js.map +1 -0
  51. package/{lib/module/expo-plugin/withRNCloudStorageIos.js → dist/module/expo-plugin/ios.js} +1 -1
  52. package/dist/module/expo-plugin/ios.js.map +1 -0
  53. package/{lib/module/hooks/useCloudFile.js → dist/module/hooks/use-cloud-file.js} +25 -7
  54. package/dist/module/hooks/use-cloud-file.js.map +1 -0
  55. package/{lib/module/hooks/useIsCloudAvailable.js → dist/module/hooks/use-is-cloud-available.js} +2 -2
  56. package/dist/module/hooks/use-is-cloud-available.js.map +1 -0
  57. package/dist/module/index.js +9 -0
  58. package/dist/module/index.js.map +1 -0
  59. package/dist/module/specs/NativeCloudStorageCloudKitIOS.js +5 -0
  60. package/dist/module/specs/NativeCloudStorageCloudKitIOS.js.map +1 -0
  61. package/dist/module/specs/NativeCloudStorageLocalFileSystem.js +5 -0
  62. package/dist/module/specs/NativeCloudStorageLocalFileSystem.js.map +1 -0
  63. package/dist/module/storages/cloudkit.js +7 -0
  64. package/dist/module/storages/cloudkit.js.map +1 -0
  65. package/{lib/module → dist/module/storages}/google-drive/client.js +83 -40
  66. package/dist/module/storages/google-drive/client.js.map +1 -0
  67. package/dist/module/storages/google-drive/index.js +392 -0
  68. package/dist/module/storages/google-drive/index.js.map +1 -0
  69. package/dist/module/storages/google-drive/types.js.map +1 -0
  70. package/{lib → dist}/module/types/main.js.map +1 -1
  71. package/dist/module/types/native.js +24 -0
  72. package/dist/module/types/native.js.map +1 -0
  73. package/{lib/module/utils/CloudStorageError.js → dist/module/utils/cloud-storage-error.js} +3 -1
  74. package/dist/module/utils/cloud-storage-error.js.map +1 -0
  75. package/dist/module/utils/constants.js +21 -0
  76. package/dist/module/utils/constants.js.map +1 -0
  77. package/dist/module/utils/local-fs.js +12 -0
  78. package/dist/module/utils/local-fs.js.map +1 -0
  79. package/dist/module/utils/native.js +30 -0
  80. package/dist/module/utils/native.js.map +1 -0
  81. package/{lib/typescript/RNCloudStorage.d.ts → dist/typescript/cloud-storage.d.ts} +67 -8
  82. package/dist/typescript/cloud-storage.d.ts.map +1 -0
  83. package/{lib/typescript/expo-plugin/withRNCloudStorage.d.ts → dist/typescript/expo-plugin/index.d.ts} +1 -1
  84. package/dist/typescript/expo-plugin/index.d.ts.map +1 -0
  85. package/{lib/typescript/expo-plugin/withRNCloudStorageIos.d.ts → dist/typescript/expo-plugin/ios.d.ts} +1 -1
  86. package/dist/typescript/expo-plugin/ios.d.ts.map +1 -0
  87. package/{lib/typescript/hooks/useCloudFile.d.ts → dist/typescript/hooks/use-cloud-file.d.ts} +22 -4
  88. package/dist/typescript/hooks/use-cloud-file.d.ts.map +1 -0
  89. package/{lib/typescript/hooks/useIsCloudAvailable.d.ts → dist/typescript/hooks/use-is-cloud-available.d.ts} +2 -2
  90. package/dist/typescript/hooks/use-is-cloud-available.d.ts.map +1 -0
  91. package/dist/typescript/index.d.ts +7 -0
  92. package/dist/typescript/index.d.ts.map +1 -0
  93. package/dist/typescript/specs/NativeCloudStorageCloudKitIOS.d.ts +30 -0
  94. package/dist/typescript/specs/NativeCloudStorageCloudKitIOS.d.ts.map +1 -0
  95. package/dist/typescript/specs/NativeCloudStorageLocalFileSystem.d.ts +24 -0
  96. package/dist/typescript/specs/NativeCloudStorageLocalFileSystem.d.ts.map +1 -0
  97. package/dist/typescript/storages/cloudkit.d.ts +6 -0
  98. package/dist/typescript/storages/cloudkit.d.ts.map +1 -0
  99. package/{lib/typescript → dist/typescript/storages}/google-drive/client.d.ts +10 -3
  100. package/dist/typescript/storages/google-drive/client.d.ts.map +1 -0
  101. package/dist/typescript/storages/google-drive/index.d.ts +41 -0
  102. package/dist/typescript/storages/google-drive/index.d.ts.map +1 -0
  103. package/dist/typescript/storages/google-drive/types.d.ts.map +1 -0
  104. package/{lib → dist}/typescript/types/main.d.ts +8 -0
  105. package/dist/typescript/types/main.d.ts.map +1 -0
  106. package/dist/typescript/types/native.d.ts +27 -0
  107. package/dist/typescript/types/native.d.ts.map +1 -0
  108. package/dist/typescript/utils/cloud-storage-error.d.ts +8 -0
  109. package/dist/typescript/utils/cloud-storage-error.d.ts.map +1 -0
  110. package/dist/typescript/utils/constants.d.ts +4 -0
  111. package/dist/typescript/utils/constants.d.ts.map +1 -0
  112. package/{lib → dist}/typescript/utils/helpers.d.ts.map +1 -1
  113. package/dist/typescript/utils/local-fs.d.ts +2 -0
  114. package/dist/typescript/utils/local-fs.d.ts.map +1 -0
  115. package/dist/typescript/utils/native.d.ts +7 -0
  116. package/dist/typescript/utils/native.d.ts.map +1 -0
  117. package/ios/CloudStorage-Bridging-Header.h +0 -1
  118. package/ios/CloudStorage.xcodeproj/project.pbxproj +12 -6
  119. package/ios/CloudStorageCloudKit.swift +159 -0
  120. package/ios/CloudStorageLocalFileSystem.swift +216 -0
  121. package/ios/RCTCloudStorageCloudKit.mm +209 -0
  122. package/ios/RCTCloudStorageLocalFileSystem.mm +149 -0
  123. package/ios/Utils/CloudKitUtils.swift +12 -6
  124. package/ios/Utils/CloudStorageError.swift +8 -0
  125. package/ios/Utils/FileUtils.swift +21 -4
  126. package/ios/Utils/Promise.swift +1 -0
  127. package/ios/Utils/Types.swift +8 -1
  128. package/ios/react_native_cloud_storage.h +6 -0
  129. package/package.json +64 -110
  130. package/react-native-cloud-storage.podspec +2 -0
  131. package/src/{RNCloudStorage.ts → cloud-storage.ts} +210 -100
  132. package/src/expo-plugin/{withRNCloudStorage.ts → index.ts} +2 -2
  133. package/src/hooks/{useCloudFile.ts → use-cloud-file.ts} +24 -6
  134. package/src/hooks/{useIsCloudAvailable.ts → use-is-cloud-available.ts} +1 -1
  135. package/src/index.ts +5 -6
  136. package/src/specs/NativeCloudStorageCloudKitIOS.ts +33 -0
  137. package/src/specs/NativeCloudStorageLocalFileSystem.ts +28 -0
  138. package/src/storages/cloudkit.ts +13 -0
  139. package/src/{google-drive → storages/google-drive}/client.ts +100 -41
  140. package/src/storages/google-drive/index.ts +488 -0
  141. package/src/types/main.ts +9 -0
  142. package/src/types/native.ts +14 -22
  143. package/src/utils/cloud-storage-error.ts +15 -0
  144. package/src/utils/constants.ts +21 -0
  145. package/src/utils/local-fs.ts +19 -0
  146. package/src/utils/native.ts +40 -0
  147. package/ios/CloudStorage.m +0 -22
  148. package/ios/CloudStorage.swift +0 -103
  149. package/ios/CloudStorageEventEmitter.m +0 -16
  150. package/ios/CloudStorageEventEmitter.swift +0 -30
  151. package/lib/commonjs/RNCloudStorage.js.map +0 -1
  152. package/lib/commonjs/expo-plugin/withRNCloudStorage.js +0 -13
  153. package/lib/commonjs/expo-plugin/withRNCloudStorage.js.map +0 -1
  154. package/lib/commonjs/expo-plugin/withRNCloudStorageIos.js.map +0 -1
  155. package/lib/commonjs/google-drive/client.js.map +0 -1
  156. package/lib/commonjs/google-drive/index.js +0 -321
  157. package/lib/commonjs/google-drive/index.js.map +0 -1
  158. package/lib/commonjs/google-drive/types.js.map +0 -1
  159. package/lib/commonjs/hooks/useCloudFile.js.map +0 -1
  160. package/lib/commonjs/hooks/useIsCloudAvailable.js.map +0 -1
  161. package/lib/commonjs/index.js.map +0 -1
  162. package/lib/commonjs/types/native.js +0 -26
  163. package/lib/commonjs/types/native.js.map +0 -1
  164. package/lib/commonjs/utils/CloudStorageError.js.map +0 -1
  165. package/lib/module/RNCloudStorage.js.map +0 -1
  166. package/lib/module/expo-plugin/withRNCloudStorage.js +0 -8
  167. package/lib/module/expo-plugin/withRNCloudStorage.js.map +0 -1
  168. package/lib/module/expo-plugin/withRNCloudStorageIos.js.map +0 -1
  169. package/lib/module/google-drive/client.js.map +0 -1
  170. package/lib/module/google-drive/index.js +0 -313
  171. package/lib/module/google-drive/index.js.map +0 -1
  172. package/lib/module/google-drive/types.js.map +0 -1
  173. package/lib/module/hooks/useCloudFile.js.map +0 -1
  174. package/lib/module/hooks/useIsCloudAvailable.js.map +0 -1
  175. package/lib/module/index.js +0 -10
  176. package/lib/module/index.js.map +0 -1
  177. package/lib/module/types/native.js +0 -22
  178. package/lib/module/types/native.js.map +0 -1
  179. package/lib/module/utils/CloudStorageError.js.map +0 -1
  180. package/lib/typescript/RNCloudStorage.d.ts.map +0 -1
  181. package/lib/typescript/expo-plugin/withRNCloudStorage.d.ts.map +0 -1
  182. package/lib/typescript/expo-plugin/withRNCloudStorageIos.d.ts.map +0 -1
  183. package/lib/typescript/google-drive/client.d.ts.map +0 -1
  184. package/lib/typescript/google-drive/index.d.ts +0 -34
  185. package/lib/typescript/google-drive/index.d.ts.map +0 -1
  186. package/lib/typescript/google-drive/types.d.ts.map +0 -1
  187. package/lib/typescript/hooks/useCloudFile.d.ts.map +0 -1
  188. package/lib/typescript/hooks/useIsCloudAvailable.d.ts.map +0 -1
  189. package/lib/typescript/index.d.ts +0 -8
  190. package/lib/typescript/index.d.ts.map +0 -1
  191. package/lib/typescript/types/main.d.ts.map +0 -1
  192. package/lib/typescript/types/native.d.ts +0 -40
  193. package/lib/typescript/types/native.d.ts.map +0 -1
  194. package/lib/typescript/utils/CloudStorageError.d.ts +0 -8
  195. package/lib/typescript/utils/CloudStorageError.d.ts.map +0 -1
  196. package/src/google-drive/index.ts +0 -399
  197. package/src/utils/CloudStorageError.ts +0 -14
  198. /package/{lib → dist}/commonjs/expo-plugin/types/index.js +0 -0
  199. /package/{lib → dist}/commonjs/expo-plugin/types/index.js.map +0 -0
  200. /package/{lib/commonjs → dist/commonjs/storages}/google-drive/types.js +0 -0
  201. /package/{lib → dist}/commonjs/types/main.js +0 -0
  202. /package/{lib → dist}/commonjs/utils/helpers.js +0 -0
  203. /package/{lib → dist}/commonjs/utils/helpers.js.map +0 -0
  204. /package/{lib → dist}/module/expo-plugin/types/index.js +0 -0
  205. /package/{lib → dist}/module/expo-plugin/types/index.js.map +0 -0
  206. /package/{lib → dist}/module/package.json +0 -0
  207. /package/{lib/module → dist/module/storages}/google-drive/types.js +0 -0
  208. /package/{lib → dist}/module/types/main.js +0 -0
  209. /package/{lib → dist}/module/utils/helpers.js +0 -0
  210. /package/{lib → dist}/module/utils/helpers.js.map +0 -0
  211. /package/{lib → dist}/typescript/expo-plugin/types/index.d.ts +0 -0
  212. /package/{lib → dist}/typescript/expo-plugin/types/index.d.ts.map +0 -0
  213. /package/{lib/typescript → dist/typescript/storages}/google-drive/types.d.ts +0 -0
  214. /package/{lib → dist}/typescript/utils/helpers.d.ts +0 -0
  215. /package/src/expo-plugin/{withRNCloudStorageIos.ts → ios.ts} +0 -0
  216. /package/src/{google-drive → storages/google-drive}/types.ts +0 -0
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _reactNative = require("react-native");
8
+ var _default = exports.default = _reactNative.TurboModuleRegistry.get('CloudStorageCloudKit');
9
+ //# sourceMappingURL=NativeCloudStorageCloudKitIOS.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["_reactNative","require","_default","exports","default","TurboModuleRegistry","get"],"sourceRoot":"../../../src","sources":["specs/NativeCloudStorageCloudKitIOS.ts"],"mappings":";;;;;;AACA,IAAAA,YAAA,GAAAC,OAAA;AAAmD,IAAAC,QAAA,GAAAC,OAAA,CAAAC,OAAA,GA+BpCC,gCAAmB,CAACC,GAAG,CAAO,sBAAsB,CAAC","ignoreList":[]}
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _reactNative = require("react-native");
8
+ var _default = exports.default = _reactNative.TurboModuleRegistry.get('CloudStorageLocalFileSystem');
9
+ //# sourceMappingURL=NativeCloudStorageLocalFileSystem.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["_reactNative","require","_default","exports","default","TurboModuleRegistry","get"],"sourceRoot":"../../../src","sources":["specs/NativeCloudStorageLocalFileSystem.ts"],"mappings":";;;;;;AACA,IAAAA,YAAA,GAAAC,OAAA;AAAmD,IAAAC,QAAA,GAAAC,OAAA,CAAAC,OAAA,GA0BpCC,gCAAmB,CAACC,GAAG,CAAO,6BAA6B,CAAC","ignoreList":[]}
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.NativeCloudKitModule = exports.NativeCloudKit = void 0;
7
+ var _NativeCloudStorageCloudKitIOS = _interopRequireDefault(require("../specs/NativeCloudStorageCloudKitIOS"));
8
+ var _native = require("../utils/native");
9
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
10
+ const NativeCloudKitModule = exports.NativeCloudKitModule = _NativeCloudStorageCloudKitIOS.default;
11
+ const NativeCloudKit = exports.NativeCloudKit = (0, _native.createProxiedNativeModule)(_NativeCloudStorageCloudKitIOS.default);
12
+ //# sourceMappingURL=cloudkit.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["_NativeCloudStorageCloudKitIOS","_interopRequireDefault","require","_native","e","__esModule","default","NativeCloudKitModule","exports","NativeCloudStorageCloudKitIOS","NativeCloudKit","createProxiedNativeModule"],"sourceRoot":"../../../src","sources":["storages/cloudkit.ts"],"mappings":";;;;;;AAAA,IAAAA,8BAAA,GAAAC,sBAAA,CAAAC,OAAA;AAIA,IAAAC,OAAA,GAAAD,OAAA;AAA4D,SAAAD,uBAAAG,CAAA,WAAAA,CAAA,IAAAA,CAAA,CAAAC,UAAA,GAAAD,CAAA,KAAAE,OAAA,EAAAF,CAAA;AAErD,MAAMG,oBAAoB,GAAAC,OAAA,CAAAD,oBAAA,GAAGE,sCAA6B;AAE1D,MAAMC,cAAc,GAAAF,OAAA,CAAAE,cAAA,GAAG,IAAAC,iCAAyB,EACrDF,sCACF,CAAC","ignoreList":[]}
@@ -5,10 +5,13 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = exports.GoogleDriveHttpError = void 0;
7
7
  var _types = require("./types");
8
+ var _localFs = require("../../utils/local-fs");
8
9
  const BASE_URL = 'https://www.googleapis.com/drive/v3';
9
10
  const BASE_UPLOAD_URL = 'https://www.googleapis.com/upload/drive/v3';
10
11
  const MULTIPART_BOUNDARY = 'foo_bar_baz';
11
12
  class GoogleDriveHttpError extends Error {
13
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
14
+
12
15
  constructor(message, status, json) {
13
16
  super(message);
14
17
  this.status = status;
@@ -16,22 +19,22 @@ class GoogleDriveHttpError extends Error {
16
19
  }
17
20
  }
18
21
 
19
- // TODO: fetch timeout
20
22
  // TODO: properly handle errors
21
23
  exports.GoogleDriveHttpError = GoogleDriveHttpError;
22
24
  class GoogleDriveApiClient {
25
+ _fetchTimeout = null;
23
26
  constructor(options) {
24
27
  this.options = options;
25
28
  }
26
29
  buildQueryString(query) {
27
- let res = Object.entries(query).filter(([, value]) => value !== undefined).map(([key, value]) => {
30
+ let result = Object.entries(query).filter(([, value]) => value !== undefined).map(([key, value]) => {
28
31
  if (typeof value === 'boolean') return `${encodeURIComponent(key)}=${value ? 'true' : 'false'}`;
29
32
  return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
30
33
  }).join('&');
31
- if (res) {
32
- res = `?${res}`;
34
+ if (result) {
35
+ result = `?${result}`;
33
36
  }
34
- return res;
37
+ return result;
35
38
  }
36
39
  async request(operation, {
37
40
  queryParameters,
@@ -46,7 +49,7 @@ class GoogleDriveApiClient {
46
49
  if (queryParameters) {
47
50
  path += this.buildQueryString(queryParameters);
48
51
  }
49
- clearTimeout(this._fetchTimeout);
52
+ if (this._fetchTimeout !== null) clearTimeout(this._fetchTimeout);
50
53
  const abortController = new AbortController();
51
54
  this._fetchTimeout = setTimeout(() => {
52
55
  abortController.abort();
@@ -64,8 +67,8 @@ class GoogleDriveApiClient {
64
67
  let json = null;
65
68
  try {
66
69
  json = await response.json();
67
- errorMessage = json.error?.message ?? `Request failed with status ${response.status}`;
68
- } catch (e) {
70
+ errorMessage = typeof json === 'object' && json !== null && 'error' in json && typeof json.error === 'object' && json.error !== null && 'message' in json.error && typeof json.error.message === 'string' ? json.error.message : `Request failed with status ${response.status}`;
71
+ } catch {
69
72
  errorMessage = `Request failed with status ${response.status}`;
70
73
  }
71
74
  throw new GoogleDriveHttpError(errorMessage, response.status, json);
@@ -80,16 +83,10 @@ class GoogleDriveApiClient {
80
83
  }
81
84
  buildMultiPartBody(metadata, media) {
82
85
  const body = [];
83
- body.push(`--${MULTIPART_BOUNDARY}\r\n`);
84
- body.push(`Content-Type: ${_types.MimeTypes.JSON}; charset=UTF-8\r\n\r\n`);
85
- body.push(`${JSON.stringify(metadata)}\r\n`);
86
- body.push(`--${MULTIPART_BOUNDARY}\r\n`);
87
- body.push(`Content-Type: ${media.mimeType}\r\n\r\n`);
88
- body.push(`${media.body}\r\n`);
89
- body.push(`--${MULTIPART_BOUNDARY}--`);
86
+ body.push(`--${MULTIPART_BOUNDARY}\r\n`, `Content-Type: ${_types.MimeTypes.JSON}; charset=UTF-8\r\n\r\n`, `${JSON.stringify(metadata)}\r\n`, `--${MULTIPART_BOUNDARY}\r\n`, `Content-Type: ${media.mimeType}\r\n\r\n`, `${media.body}\r\n`, `--${MULTIPART_BOUNDARY}--`);
90
87
  return body.join('');
91
88
  }
92
- async listFiles(space) {
89
+ async listFiles(space, query) {
93
90
  const files = [];
94
91
  let pageToken;
95
92
  const fields = ['id', 'kind', 'mimeType', 'name', 'parents', 'spaces', 'size', 'createdTime', 'modifiedTime'];
@@ -97,7 +94,8 @@ class GoogleDriveApiClient {
97
94
  const queryParameters = {
98
95
  fields: `files(${fields.join(',')}),nextPageToken`,
99
96
  spaces: space,
100
- pageToken
97
+ pageToken,
98
+ q: query
101
99
  };
102
100
  const response = await this.request(`/files`, {
103
101
  queryParameters
@@ -131,19 +129,44 @@ class GoogleDriveApiClient {
131
129
  });
132
130
  }
133
131
  async createFile(metadata, media) {
134
- const multipartRequestBody = this.buildMultiPartBody(metadata, media);
135
- await this.request(`/files`, {
136
- queryParameters: {
137
- uploadType: 'multipart'
138
- },
139
- method: 'POST',
140
- headers: {
141
- 'Content-Type': `multipart/related; boundary=${MULTIPART_BOUNDARY}`,
142
- 'Content-Length': multipartRequestBody.length.toString()
143
- },
144
- body: multipartRequestBody,
145
- baseUrl: BASE_UPLOAD_URL
146
- });
132
+ if ('body' in media) {
133
+ const multipartRequestBody = this.buildMultiPartBody(metadata, media);
134
+ await this.request(`/files`, {
135
+ queryParameters: {
136
+ uploadType: 'multipart'
137
+ },
138
+ method: 'POST',
139
+ headers: {
140
+ 'Content-Type': `multipart/related; boundary=${MULTIPART_BOUNDARY}`,
141
+ 'Content-Length': multipartRequestBody.length.toString()
142
+ },
143
+ body: multipartRequestBody,
144
+ baseUrl: BASE_UPLOAD_URL
145
+ });
146
+ } else {
147
+ // First, create an empty file with the required metadata
148
+ const file = await this.request(`/files`, {
149
+ method: 'POST',
150
+ headers: {
151
+ 'Content-Type': _types.MimeTypes.JSON
152
+ },
153
+ body: JSON.stringify({
154
+ ...metadata,
155
+ mimeType: media.mimeType
156
+ })
157
+ });
158
+
159
+ // Then, upload the binary content via the native helper module
160
+ const remoteUri = `${BASE_UPLOAD_URL}/files/${file.id}?uploadType=media`;
161
+ await _localFs.localFileSystem.uploadFile(media.localPath, remoteUri, {
162
+ method: 'PATCH',
163
+ uploadType: 'binary',
164
+ headers: {
165
+ 'Authorization': `Bearer ${this.options.accessToken}`,
166
+ 'Content-Type': media.mimeType
167
+ }
168
+ });
169
+ }
147
170
  }
148
171
  async createDirectory(metadata) {
149
172
  await this.request(`/files`, {
@@ -158,17 +181,37 @@ class GoogleDriveApiClient {
158
181
  });
159
182
  }
160
183
  async updateFile(fileId, media) {
161
- await this.request(`/files/${fileId}`, {
162
- queryParameters: {
163
- uploadType: 'media'
164
- },
165
- method: 'PATCH',
184
+ if ('body' in media) {
185
+ await this.request(`/files/${fileId}`, {
186
+ queryParameters: {
187
+ uploadType: 'media'
188
+ },
189
+ method: 'PATCH',
190
+ headers: {
191
+ 'Content-Type': media.mimeType,
192
+ 'Content-Length': media.body.length.toString()
193
+ },
194
+ body: media.body,
195
+ baseUrl: BASE_UPLOAD_URL
196
+ });
197
+ } else {
198
+ const remoteUri = `${BASE_UPLOAD_URL}/files/${fileId}?uploadType=media`;
199
+ await _localFs.localFileSystem.uploadFile(media.localPath, remoteUri, {
200
+ method: 'PATCH',
201
+ uploadType: 'binary',
202
+ headers: {
203
+ 'Authorization': `Bearer ${this.options.accessToken}`,
204
+ 'Content-Type': media.mimeType
205
+ }
206
+ });
207
+ }
208
+ }
209
+ async downloadFile(fileId, localPath) {
210
+ const remoteUri = `${BASE_URL}/files/${fileId}?alt=media`;
211
+ await _localFs.localFileSystem.downloadFile(remoteUri, localPath, {
166
212
  headers: {
167
- 'Content-Type': media.mimeType,
168
- 'Content-Length': media.body.length.toString()
169
- },
170
- body: media.body,
171
- baseUrl: BASE_UPLOAD_URL
213
+ Authorization: `Bearer ${this.options.accessToken}`
214
+ }
172
215
  });
173
216
  }
174
217
  }
@@ -0,0 +1 @@
1
+ {"version":3,"names":["_types","require","_localFs","BASE_URL","BASE_UPLOAD_URL","MULTIPART_BOUNDARY","GoogleDriveHttpError","Error","constructor","message","status","json","exports","GoogleDriveApiClient","_fetchTimeout","options","buildQueryString","query","result","Object","entries","filter","value","undefined","map","key","encodeURIComponent","join","request","operation","queryParameters","baseUrl","timeout","accessToken","path","clearTimeout","abortController","AbortController","setTimeout","abort","response","fetch","headers","Authorization","signal","ok","errorMessage","error","Accept","text","buildMultiPartBody","metadata","media","body","push","MimeTypes","JSON","stringify","mimeType","listFiles","space","files","pageToken","fields","spaces","q","nextPageToken","getFile","fileId","getFileText","alt","deleteFile","method","createFile","multipartRequestBody","uploadType","length","toString","file","remoteUri","id","localFileSystem","uploadFile","localPath","createDirectory","FOLDER","updateFile","downloadFile","default"],"sourceRoot":"../../../../src","sources":["storages/google-drive/client.ts"],"mappings":";;;;;;AACA,IAAAA,MAAA,GAAAC,OAAA;AAOA,IAAAC,QAAA,GAAAD,OAAA;AAEA,MAAME,QAAQ,GAAG,qCAAqC;AACtD,MAAMC,eAAe,GAAG,4CAA4C;AACpE,MAAMC,kBAAkB,GAAG,aAAa;AAEjC,MAAMC,oBAAoB,SAASC,KAAK,CAAC;EAE9C;;EAGAC,WAAWA,CAACC,OAAe,EAAEC,MAAc,EAAEC,IAAa,EAAE;IAC1D,KAAK,CAACF,OAAO,CAAC;IACd,IAAI,CAACC,MAAM,GAAGA,MAAM;IACpB,IAAI,CAACC,IAAI,GAAGA,IAAI;EAClB;AACF;;AAEA;AAAAC,OAAA,CAAAN,oBAAA,GAAAA,oBAAA;AACe,MAAMO,oBAAoB,CAAC;EAChCC,aAAa,GAA0B,IAAI;EAGnDN,WAAWA,CAACO,OAAiE,EAAE;IAC7E,IAAI,CA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9B,gBAAgB,EAAEN,KAAK,CAACC,IAAI,CAACuB,MAAM,CAACC,QAAQ,CAAC;QAC/C,CAAC;QACDxB,IAAI,EAAED,KAAK,CAACC,IAAI;QAChBtB,OAAO,EAAE3B;MACX,CAAC,CAAC;IACJ,CAAC,MAAM;MACL,MAAM2E,SAAS,GAAG,GAAG3E,eAAe,UAAUgE,MAAM,mBAAmB;MACvE,MAAMa,wBAAe,CAACC,UAAU,CAAC9B,KAAK,CAAC+B,SAAS,EAAEJ,SAAS,EAAE;QAC3DP,MAAM,EAAE,OAAO;QACfG,UAAU,EAAE,QAAQ;QACpBjC,OAAO,EAAE;UACP,eAAe,EAAE,UAAU,IAAI,CAAC3B,OAAO,CAACkB,WAAW,EAAE;UACrD,cAAc,EAAEmB,KAAK,CAACM;QACxB;MACF,CAAC,CAAC;IACJ;EACF;EAEA,MAAa6B,YAAYA,CAACnB,MAAc,EAAEe,SAAiB,EAAiB;IAC1E,MAAMJ,SAAS,GAAG,GAAG5E,QAAQ,UAAUiE,MAAM,YAAY;IACzD,MAAMa,wBAAe,CAACM,YAAY,CAACR,SAAS,EAAEI,SAAS,EAAE;MACvDzC,OAAO,EAAE;QACPC,aAAa,EAAE,UAAU,IAAI,CAAC5B,OAAO,CAACkB,WAAW;MACnD;IACF,CAAC,CAAC;EACJ;AACF;AAACrB,OAAA,CAAA4E,OAAA,GAAA3E,oBAAA","ignoreList":[]}
@@ -0,0 +1,399 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _native = require("../../types/native");
8
+ var _cloudStorageError = _interopRequireDefault(require("../../utils/cloud-storage-error"));
9
+ var _types = require("./types");
10
+ var _client = _interopRequireWildcard(require("./client"));
11
+ function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
12
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
13
+ /**
14
+ * A JavaScript-based implementation of the Google Drive API that implements the cloud storage interface.
15
+ */
16
+ class GoogleDrive {
17
+ constructor(options) {
18
+ this.options = options;
19
+ this.drive = new _client.default(options);
20
+ return new Proxy(this, {
21
+ // before calling any function, check if the access token is set
22
+ get(target, property) {
23
+ const allowedFunctions = ['isCloudAvailable'];
24
+ if (typeof target[property] === 'function' && !allowedFunctions.includes(property.toString())) {
25
+ const {
26
+ accessToken
27
+ } = options;
28
+ if (!accessToken?.length) {
29
+ throw new _cloudStorageError.default(`Google Drive access token is not set, cannot call function ${property.toString()}`, _native.NativeCloudStorageErrorCode.ACCESS_TOKEN_MISSING);
30
+ }
31
+ }
32
+ return target[property];
33
+ }
34
+ });
35
+ }
36
+ isCloudAvailable = async () => {
37
+ const {
38
+ accessToken
39
+ } = this.options;
40
+ return !!accessToken?.length;
41
+ };
42
+ getRootDirectory(scope) {
43
+ switch (scope) {
44
+ case 'documents':
45
+ {
46
+ return 'drive';
47
+ }
48
+ case 'documents_legacy':
49
+ {
50
+ return 'drive';
51
+ }
52
+ case 'app_data':
53
+ {
54
+ return 'appDataFolder';
55
+ }
56
+ }
57
+ }
58
+ isRootPath(path) {
59
+ return path === '' || path === '/';
60
+ }
61
+ resolvePathToDirectories(path) {
62
+ if (path.startsWith('/')) path = path.slice(1);
63
+ if (path.endsWith('/')) path = path.slice(0, -1);
64
+ const directories = path.split('/');
65
+ const actualFilename = directories.pop() ?? '';
66
+ return {
67
+ directories,
68
+ filename: actualFilename
69
+ };
70
+ }
71
+ escapeDriveQueryValue(value) {
72
+ return value.replaceAll('\\', String.raw`\\`).replaceAll("'", String.raw`\'`);
73
+ }
74
+ async getQueryRootParentId(scope) {
75
+ if (scope === 'app_data') {
76
+ return this.getRootDirectory(scope);
77
+ }
78
+ return this.getRootDirectoryId(scope);
79
+ }
80
+ async findDirectoryByNameAndParent(name, parentId, scope) {
81
+ const escapedName = this.escapeDriveQueryValue(name);
82
+ const escapedParentId = this.escapeDriveQueryValue(parentId);
83
+ const query = `name = '${escapedName}' and '${escapedParentId}' in parents and mimeType = '${_types.MimeTypes.FOLDER}' and trashed = false`;
84
+ return this.drive.listFiles(this.getRootDirectory(scope), query);
85
+ }
86
+ async findFilesByNameAndParent(name, parentId, scope) {
87
+ const escapedName = this.escapeDriveQueryValue(name);
88
+ const escapedParentId = this.escapeDriveQueryValue(parentId);
89
+ const query = `name = '${escapedName}' and '${escapedParentId}' in parents and trashed = false`;
90
+ return this.drive.listFiles(this.getRootDirectory(scope), query);
91
+ }
92
+ async findFilesByParent(parentId, scope) {
93
+ const escapedParentId = this.escapeDriveQueryValue(parentId);
94
+ const query = `'${escapedParentId}' in parents and trashed = false`;
95
+ return this.drive.listFiles(this.getRootDirectory(scope), query);
96
+ }
97
+ async findParentDirectoryId(directoryTree, scope) {
98
+ let parentDirectoryId = await this.getQueryRootParentId(scope);
99
+ for (const directoryName of directoryTree) {
100
+ const directories = await this.findDirectoryByNameAndParent(directoryName, parentDirectoryId, scope);
101
+ if (directories.length === 0) {
102
+ return null;
103
+ }
104
+ parentDirectoryId = directories[0].id;
105
+ }
106
+ return parentDirectoryId;
107
+ }
108
+
109
+ /**
110
+ * Gets the Google Drive ID of the root directory for the given scope.
111
+ * @param scope The scope to get the root directory for.
112
+ * @returns A promise that resolves to the ID of the root directory.
113
+ */
114
+ async getRootDirectoryId(scope) {
115
+ if (scope !== 'app_data') {
116
+ return 'root';
117
+ }
118
+ const files = await this.drive.listFiles(this.getRootDirectory(scope));
119
+ for (const file of files) {
120
+ const parentId = file.parents?.[0];
121
+ if (parentId && !files.some(candidate => candidate.id === parentId)) {
122
+ return parentId;
123
+ }
124
+ }
125
+ return this.getRootDirectory(scope);
126
+ }
127
+ checkIfMultipleFilesWithSameName(path, files) {
128
+ const {
129
+ strictFilenames
130
+ } = this.options;
131
+ if (files.length <= 1) return;
132
+ if (strictFilenames) {
133
+ throw new _cloudStorageError.default(`Multiple files with the same name found at path ${path}: ${files.map(f => f.id).join(', ')}`, _native.NativeCloudStorageErrorCode.MULTIPLE_FILES_SAME_NAME);
134
+ }
135
+ }
136
+ async getFileId(path, scope, throwIf = false) {
137
+ try {
138
+ if (this.isRootPath(path)) {
139
+ if (throwIf === 'directory') {
140
+ throw new _cloudStorageError.default(`Path ${path} is a directory`, _native.NativeCloudStorageErrorCode.PATH_IS_DIRECTORY);
141
+ }
142
+ const rootDirectoryId = await this.getRootDirectoryId(scope);
143
+ if (scope !== 'app_data') {
144
+ await this.drive.getFile(rootDirectoryId);
145
+ }
146
+ return rootDirectoryId;
147
+ }
148
+ const {
149
+ directories,
150
+ filename
151
+ } = this.resolvePathToDirectories(path);
152
+ const parentDirectoryId = await this.findParentDirectoryId(directories, scope);
153
+ if (parentDirectoryId === null) {
154
+ throw new _cloudStorageError.default(`File not found`, _native.NativeCloudStorageErrorCode.FILE_NOT_FOUND);
155
+ }
156
+ const files = await this.findFilesByNameAndParent(filename, parentDirectoryId, scope);
157
+ this.checkIfMultipleFilesWithSameName(path, files);
158
+ const file = files[0];
159
+ if (!file) throw new _cloudStorageError.default(`File not found`, _native.NativeCloudStorageErrorCode.FILE_NOT_FOUND);
160
+ if (file.mimeType === _types.MimeTypes.FOLDER && throwIf === 'directory') {
161
+ throw new _cloudStorageError.default(`Path ${path} is a directory`, _native.NativeCloudStorageErrorCode.PATH_IS_DIRECTORY);
162
+ } else if (file.mimeType !== _types.MimeTypes.FOLDER && throwIf === 'file') {
163
+ throw new _cloudStorageError.default(`Path ${path} is a file`, _native.NativeCloudStorageErrorCode.FILE_NOT_FOUND);
164
+ }
165
+ return file.id;
166
+ } catch (error) {
167
+ if (error instanceof _client.GoogleDriveHttpError && error.json?.error?.status === 'UNAUTHENTICATED') {
168
+ throw new _cloudStorageError.default(`Could not authenticate with Google Drive`, _native.NativeCloudStorageErrorCode.AUTHENTICATION_FAILED, error.json);
169
+ } else {
170
+ if (error instanceof _cloudStorageError.default) throw error;
171
+ throw new _cloudStorageError.default(`Could not get file id for path ${path}`, _native.NativeCloudStorageErrorCode.UNKNOWN, error);
172
+ }
173
+ }
174
+ }
175
+ async fileExists(path, scope) {
176
+ try {
177
+ await this.getFileId(path, scope);
178
+ return true;
179
+ } catch (error) {
180
+ if (error instanceof _cloudStorageError.default && error.code === _native.NativeCloudStorageErrorCode.FILE_NOT_FOUND) return false;else throw error;
181
+ }
182
+ }
183
+ async appendToFile(path, data, scope) {
184
+ let fileId;
185
+ let previousContent = '';
186
+ try {
187
+ fileId = await this.getFileId(path, scope, 'directory');
188
+ previousContent = await this.drive.getFileText(fileId);
189
+ } catch (error) {
190
+ if (error instanceof _cloudStorageError.default && error.code === _native.NativeCloudStorageErrorCode.FILE_NOT_FOUND) {
191
+ /* do nothing, simply create the file */
192
+ } else {
193
+ throw error;
194
+ }
195
+ }
196
+ if (fileId) {
197
+ await this.drive.updateFile(fileId, {
198
+ body: previousContent + data,
199
+ mimeType: _types.MimeTypes.TEXT
200
+ });
201
+ } else {
202
+ const {
203
+ directories,
204
+ filename
205
+ } = this.resolvePathToDirectories(path);
206
+ const parentDirectoryId = await this.findParentDirectoryId(directories, scope);
207
+ if (parentDirectoryId === null) {
208
+ throw new _cloudStorageError.default(`Directory not found`, _native.NativeCloudStorageErrorCode.DIRECTORY_NOT_FOUND);
209
+ }
210
+ await this.drive.createFile({
211
+ name: filename,
212
+ parents: [parentDirectoryId]
213
+ }, {
214
+ body: data,
215
+ mimeType: _types.MimeTypes.TEXT
216
+ });
217
+ }
218
+ }
219
+ async createFile(path, data, scope, overwrite) {
220
+ let fileId;
221
+ if (overwrite) {
222
+ try {
223
+ fileId = await this.getFileId(path, scope, 'directory');
224
+ } catch (error) {
225
+ if (error instanceof _cloudStorageError.default && error.code === _native.NativeCloudStorageErrorCode.FILE_NOT_FOUND) {
226
+ /* do nothing, simply create the file */
227
+ } else {
228
+ throw error;
229
+ }
230
+ }
231
+ } else {
232
+ try {
233
+ await this.getFileId(path, scope, 'directory');
234
+ throw new _cloudStorageError.default(`File ${path} already exists`, _native.NativeCloudStorageErrorCode.FILE_ALREADY_EXISTS);
235
+ } catch (error) {
236
+ if (error instanceof _cloudStorageError.default && error.code === _native.NativeCloudStorageErrorCode.FILE_NOT_FOUND) {
237
+ /* do nothing, simply create the file */
238
+ } else {
239
+ throw error;
240
+ }
241
+ }
242
+ }
243
+ if (fileId) {
244
+ await this.drive.updateFile(fileId, {
245
+ body: data,
246
+ mimeType: _types.MimeTypes.TEXT
247
+ });
248
+ } else {
249
+ const {
250
+ directories,
251
+ filename
252
+ } = this.resolvePathToDirectories(path);
253
+ const parentDirectoryId = await this.findParentDirectoryId(directories, scope);
254
+ if (parentDirectoryId === null) {
255
+ throw new _cloudStorageError.default(`Directory not found`, _native.NativeCloudStorageErrorCode.DIRECTORY_NOT_FOUND);
256
+ }
257
+ await this.drive.createFile({
258
+ name: filename,
259
+ parents: [parentDirectoryId]
260
+ }, {
261
+ body: data,
262
+ mimeType: _types.MimeTypes.TEXT
263
+ });
264
+ }
265
+ }
266
+ async listFiles(path, scope) {
267
+ const parentDirectoryId = this.isRootPath(path) ? await this.getQueryRootParentId(scope) : await this.getFileId(path, scope);
268
+ const files = await this.findFilesByParent(parentDirectoryId, scope);
269
+ return [...new Set(files.map(f => f.name))];
270
+ }
271
+ async createDirectory(path, scope) {
272
+ if (this.isRootPath(path)) {
273
+ throw new _cloudStorageError.default(`Directory ${path} already exists`, _native.NativeCloudStorageErrorCode.FILE_ALREADY_EXISTS);
274
+ }
275
+ try {
276
+ await this.getFileId(path, scope);
277
+ throw new _cloudStorageError.default(`File ${path} already exists`, _native.NativeCloudStorageErrorCode.FILE_ALREADY_EXISTS);
278
+ } catch (error) {
279
+ if (error instanceof _cloudStorageError.default && error.code === _native.NativeCloudStorageErrorCode.FILE_NOT_FOUND) {
280
+ /* do nothing, simply create the directory */
281
+ } else if (error instanceof _cloudStorageError.default && error.code === _native.NativeCloudStorageErrorCode.PATH_IS_DIRECTORY) {
282
+ throw new _cloudStorageError.default(`Directory ${path} already exists`, _native.NativeCloudStorageErrorCode.FILE_ALREADY_EXISTS);
283
+ } else {
284
+ throw error;
285
+ }
286
+ }
287
+ const {
288
+ directories,
289
+ filename
290
+ } = this.resolvePathToDirectories(path);
291
+ const parentDirectoryId = await this.findParentDirectoryId(directories, scope);
292
+ if (parentDirectoryId === null) {
293
+ throw new _cloudStorageError.default(`Directory not found`, _native.NativeCloudStorageErrorCode.DIRECTORY_NOT_FOUND);
294
+ }
295
+ await this.drive.createDirectory({
296
+ name: filename,
297
+ parents: [parentDirectoryId]
298
+ });
299
+ }
300
+ async readFile(path, scope) {
301
+ const fileId = await this.getFileId(path, scope, 'directory');
302
+ const content = await this.drive.getFileText(fileId);
303
+ return content;
304
+ }
305
+ async triggerSync(_path, _scope) {
306
+ // Triggering file synchronization in Google Drive is not necessary / possible, as they need to be downloaded on every read operation via the API anyway
307
+ return;
308
+ }
309
+ async deleteFile(path, scope) {
310
+ // if trying to pass a directory, throw an error
311
+ const fileId = await this.getFileId(path, scope, 'directory');
312
+ await this.drive.deleteFile(fileId);
313
+ }
314
+ async deleteDirectory(path, recursive, scope) {
315
+ // if trying to pass a file, throw an error
316
+ const fileId = await this.getFileId(path, scope, 'file');
317
+ if (!recursive) {
318
+ // check if the directory is empty
319
+ const filesInDirectory = await this.findFilesByParent(fileId, scope);
320
+ if (filesInDirectory.length > 0) {
321
+ throw new _cloudStorageError.default(`Directory ${path} is not empty`, _native.NativeCloudStorageErrorCode.DELETE_ERROR, filesInDirectory);
322
+ }
323
+ }
324
+ await this.drive.deleteFile(fileId);
325
+ }
326
+ async statFile(path, scope) {
327
+ const fileId = await this.getFileId(path, scope, false);
328
+ const file = await this.drive.getFile(fileId);
329
+ return {
330
+ size: file.size ?? 0,
331
+ birthtimeMs: new Date(file.createdTime).getTime(),
332
+ mtimeMs: new Date(file.modifiedTime).getTime(),
333
+ isDirectory: file.mimeType === _types.MimeTypes.FOLDER,
334
+ isFile: file.mimeType !== _types.MimeTypes.FOLDER
335
+ };
336
+ }
337
+ async downloadFile(remotePath, localPath, scope) {
338
+ const fileId = await this.getFileId(remotePath, scope, 'directory');
339
+ try {
340
+ await this.drive.downloadFile(fileId, localPath);
341
+ } catch (error) {
342
+ if (error instanceof _cloudStorageError.default) throw error;
343
+ throw new _cloudStorageError.default(`Could not download file ${remotePath} to ${localPath}`, _native.NativeCloudStorageErrorCode.UNKNOWN, error);
344
+ }
345
+ }
346
+ async uploadFile(remotePath, localPath, mimeType, scope, overwrite) {
347
+ let fileId;
348
+ if (overwrite) {
349
+ try {
350
+ fileId = await this.getFileId(remotePath, scope, 'directory');
351
+ } catch (error) {
352
+ if (error instanceof _cloudStorageError.default && error.code === _native.NativeCloudStorageErrorCode.FILE_NOT_FOUND) {
353
+ /* File doesn't exist -> we'll create it below */
354
+ } else {
355
+ throw error;
356
+ }
357
+ }
358
+ } else {
359
+ try {
360
+ await this.getFileId(remotePath, scope, 'directory');
361
+ throw new _cloudStorageError.default(`File ${remotePath} already exists`, _native.NativeCloudStorageErrorCode.FILE_ALREADY_EXISTS);
362
+ } catch (error) {
363
+ if (error instanceof _cloudStorageError.default && error.code === _native.NativeCloudStorageErrorCode.FILE_NOT_FOUND) {
364
+ /* not found -> ok, we'll create */
365
+ } else if (error instanceof _cloudStorageError.default) {
366
+ throw error;
367
+ } else {
368
+ throw error;
369
+ }
370
+ }
371
+ }
372
+ if (fileId) {
373
+ // Overwrite existing file
374
+ await this.drive.updateFile(fileId, {
375
+ mimeType,
376
+ localPath
377
+ });
378
+ } else {
379
+ // Need to create a new file first
380
+ const {
381
+ directories,
382
+ filename
383
+ } = this.resolvePathToDirectories(remotePath);
384
+ const parentDirectoryId = await this.findParentDirectoryId(directories, scope);
385
+ if (parentDirectoryId === null) {
386
+ throw new _cloudStorageError.default(`Directory not found`, _native.NativeCloudStorageErrorCode.DIRECTORY_NOT_FOUND);
387
+ }
388
+ await this.drive.createFile({
389
+ name: filename,
390
+ parents: [parentDirectoryId]
391
+ }, {
392
+ mimeType,
393
+ localPath
394
+ });
395
+ }
396
+ }
397
+ }
398
+ exports.default = GoogleDrive;
399
+ //# sourceMappingURL=index.js.map