react-native-cloud-storage 1.5.1 → 2.1.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 (107) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +6 -5
  3. package/ios/CloudStorage.swift +52 -262
  4. package/ios/CloudStorage.xcodeproj/project.pbxproj +28 -0
  5. package/ios/CloudStorageEventEmitter.swift +4 -4
  6. package/ios/Utils/CloudKitUtils.swift +112 -0
  7. package/ios/Utils/CloudStorageError.swift +78 -0
  8. package/ios/Utils/FileUtils.swift +132 -0
  9. package/ios/Utils/Promise.swift +58 -0
  10. package/ios/Utils/Types.swift +36 -0
  11. package/lib/commonjs/RNCloudStorage.js +361 -66
  12. package/lib/commonjs/RNCloudStorage.js.map +1 -1
  13. package/lib/commonjs/expo-plugin/types/index.js.map +1 -1
  14. package/lib/commonjs/expo-plugin/withRNCloudStorage.js +2 -3
  15. package/lib/commonjs/expo-plugin/withRNCloudStorage.js.map +1 -1
  16. package/lib/commonjs/expo-plugin/withRNCloudStorageIos.js +4 -7
  17. package/lib/commonjs/expo-plugin/withRNCloudStorageIos.js.map +1 -1
  18. package/lib/commonjs/google-drive/client.js +16 -20
  19. package/lib/commonjs/google-drive/client.js.map +1 -1
  20. package/lib/commonjs/google-drive/index.js +42 -64
  21. package/lib/commonjs/google-drive/index.js.map +1 -1
  22. package/lib/commonjs/google-drive/types.js +1 -2
  23. package/lib/commonjs/google-drive/types.js.map +1 -1
  24. package/lib/commonjs/hooks/useCloudFile.js +14 -17
  25. package/lib/commonjs/hooks/useCloudFile.js.map +1 -1
  26. package/lib/commonjs/hooks/useIsCloudAvailable.js +11 -21
  27. package/lib/commonjs/hooks/useIsCloudAvailable.js.map +1 -1
  28. package/lib/commonjs/index.js +1 -7
  29. package/lib/commonjs/index.js.map +1 -1
  30. package/lib/commonjs/package.json +1 -0
  31. package/lib/commonjs/types/main.js +8 -3
  32. package/lib/commonjs/types/main.js.map +1 -1
  33. package/lib/commonjs/types/native.js +3 -3
  34. package/lib/commonjs/types/native.js.map +1 -1
  35. package/lib/commonjs/utils/CloudStorageError.js +1 -2
  36. package/lib/commonjs/utils/CloudStorageError.js.map +1 -1
  37. package/lib/commonjs/utils/helpers.js +8 -15
  38. package/lib/commonjs/utils/helpers.js.map +1 -1
  39. package/lib/module/RNCloudStorage.js +362 -65
  40. package/lib/module/RNCloudStorage.js.map +1 -1
  41. package/lib/module/expo-plugin/types/index.js +1 -1
  42. package/lib/module/expo-plugin/types/index.js.map +1 -1
  43. package/lib/module/expo-plugin/withRNCloudStorage.js +2 -0
  44. package/lib/module/expo-plugin/withRNCloudStorage.js.map +1 -1
  45. package/lib/module/expo-plugin/withRNCloudStorageIos.js +5 -5
  46. package/lib/module/expo-plugin/withRNCloudStorageIos.js.map +1 -1
  47. package/lib/module/google-drive/client.js +18 -20
  48. package/lib/module/google-drive/client.js.map +1 -1
  49. package/lib/module/google-drive/index.js +41 -62
  50. package/lib/module/google-drive/index.js.map +1 -1
  51. package/lib/module/google-drive/types.js +2 -0
  52. package/lib/module/google-drive/types.js.map +1 -1
  53. package/lib/module/hooks/useCloudFile.js +15 -16
  54. package/lib/module/hooks/useCloudFile.js.map +1 -1
  55. package/lib/module/hooks/useIsCloudAvailable.js +13 -21
  56. package/lib/module/hooks/useIsCloudAvailable.js.map +1 -1
  57. package/lib/module/index.js +2 -5
  58. package/lib/module/index.js.map +1 -1
  59. package/lib/module/package.json +1 -0
  60. package/lib/module/types/main.js +9 -0
  61. package/lib/module/types/main.js.map +1 -1
  62. package/lib/module/types/native.js +4 -1
  63. package/lib/module/types/native.js.map +1 -1
  64. package/lib/module/utils/CloudStorageError.js +2 -0
  65. package/lib/module/utils/CloudStorageError.js.map +1 -1
  66. package/lib/module/utils/helpers.js +8 -13
  67. package/lib/module/utils/helpers.js.map +1 -1
  68. package/lib/typescript/RNCloudStorage.d.ts +159 -39
  69. package/lib/typescript/RNCloudStorage.d.ts.map +1 -1
  70. package/lib/typescript/google-drive/client.d.ts +3 -3
  71. package/lib/typescript/google-drive/client.d.ts.map +1 -1
  72. package/lib/typescript/google-drive/index.d.ts +6 -18
  73. package/lib/typescript/google-drive/index.d.ts.map +1 -1
  74. package/lib/typescript/hooks/useCloudFile.d.ts +4 -7
  75. package/lib/typescript/hooks/useCloudFile.d.ts.map +1 -1
  76. package/lib/typescript/hooks/useIsCloudAvailable.d.ts +3 -2
  77. package/lib/typescript/hooks/useIsCloudAvailable.d.ts.map +1 -1
  78. package/lib/typescript/index.d.ts +0 -4
  79. package/lib/typescript/index.d.ts.map +1 -1
  80. package/lib/typescript/types/main.d.ts +33 -0
  81. package/lib/typescript/types/main.d.ts.map +1 -1
  82. package/lib/typescript/types/native.d.ts +2 -1
  83. package/lib/typescript/types/native.d.ts.map +1 -1
  84. package/lib/typescript/utils/helpers.d.ts +2 -9
  85. package/lib/typescript/utils/helpers.d.ts.map +1 -1
  86. package/package.json +9 -11
  87. package/react-native-cloud-storage.podspec +1 -1
  88. package/src/RNCloudStorage.ts +387 -68
  89. package/src/google-drive/client.ts +8 -7
  90. package/src/google-drive/index.ts +38 -63
  91. package/src/hooks/useCloudFile.ts +13 -16
  92. package/src/hooks/useIsCloudAvailable.ts +12 -25
  93. package/src/index.ts +0 -5
  94. package/src/types/main.ts +38 -0
  95. package/src/types/native.ts +2 -1
  96. package/src/utils/helpers.ts +8 -15
  97. package/ios/CloudStorage.xcodeproj/project.xcworkspace/contents.xcworkspacedata +0 -4
  98. package/ios/CloudStorage.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +0 -8
  99. package/ios/CloudStorage.xcodeproj/project.xcworkspace/xcuserdata/max.xcuserdatad/UserInterfaceState.xcuserstate +0 -0
  100. package/ios/CloudStorage.xcodeproj/xcuserdata/max.xcuserdatad/xcschemes/xcschememanagement.plist +0 -22
  101. package/lib/commonjs/createRNCloudStorage.js +0 -48
  102. package/lib/commonjs/createRNCloudStorage.js.map +0 -1
  103. package/lib/module/createRNCloudStorage.js +0 -41
  104. package/lib/module/createRNCloudStorage.js.map +0 -1
  105. package/lib/typescript/createRNCloudStorage.d.ts +0 -3
  106. package/lib/typescript/createRNCloudStorage.d.ts.map +0 -1
  107. package/src/createRNCloudStorage.ts +0 -53
@@ -0,0 +1,78 @@
1
+ //
2
+ // CloudStorageError.swift
3
+ // CloudStorage
4
+ //
5
+ // Created by Maximilian Krause on 27.09.24.
6
+ // Copyright © 2024 Kuatsu App Agency. All rights reserved.
7
+ //
8
+
9
+ import Foundation
10
+
11
+ enum CloudStorageError: Error {
12
+ case invalidScope(scope: String)
13
+ case fileNotFound(path: String)
14
+ case pathIsDirectory(path: String)
15
+ case pathIsFile(path: String)
16
+ case directoryNotFound(path: String)
17
+ case directoryNotEmpty(path: String)
18
+ case fileAlreadyExists(path: String)
19
+ case authenticationFailed
20
+ case writeError(path: String)
21
+ case readError(path: String)
22
+ case deleteError(path: String)
23
+ case statError(path: String)
24
+ case unknown(message: String = "An unknown error occurred")
25
+ case fileNotDownloadable(path: String)
26
+
27
+ var code: String {
28
+ switch self {
29
+ case .invalidScope: "ERR_INVALID_SCOPE"
30
+ case .fileNotFound: "ERR_FILE_NOT_FOUND"
31
+ case .pathIsDirectory: "ERR_PATH_IS_DIRECTORY"
32
+ case .pathIsFile: "ERR_PATH_IS_FILE"
33
+ case .directoryNotFound: "ERR_DIRECTORY_NOT_FOUND"
34
+ case .directoryNotEmpty: "ERR_DIRECTORY_NOT_EMPTY"
35
+ case .fileAlreadyExists: "ERR_FILE_EXISTS"
36
+ case .authenticationFailed: "ERR_AUTHENTICATION_FAILED"
37
+ case .writeError: "ERR_WRITE_ERROR"
38
+ case .readError: "ERR_READ_ERROR"
39
+ case .deleteError: "ERR_DELETE_ERROR"
40
+ case .statError: "ERR_STAT_ERROR"
41
+ case .unknown: "ERR_UNKNOWN"
42
+ case .fileNotDownloadable: "ERR_FILE_NOT_DOWNLOADABLE"
43
+ }
44
+ }
45
+
46
+ var message: String {
47
+ switch self {
48
+ case let .invalidScope(scope):
49
+ "Invalid scope \(scope) provided"
50
+ case let .fileNotFound(path):
51
+ "File not found at path \(path)"
52
+ case let .pathIsDirectory(path):
53
+ "Path is a directory at path \(path)"
54
+ case let .pathIsFile(path):
55
+ "Path is a file at path \(path)"
56
+ case let .directoryNotFound(path):
57
+ "Directory not found at path \(path)"
58
+ case let .directoryNotEmpty(path):
59
+ "Directory not empty at path \(path)"
60
+ case let .fileAlreadyExists(path):
61
+ "File already exists at path \(path)"
62
+ case .authenticationFailed:
63
+ "Authentication failed"
64
+ case let .writeError(path):
65
+ "Write error for path \(path)"
66
+ case let .readError(path):
67
+ "Read error for path \(path)"
68
+ case let .deleteError(path):
69
+ "Delete error for path \(path)"
70
+ case let .statError(path):
71
+ "Stat error for path \(path)"
72
+ case let .unknown(message):
73
+ message
74
+ case let .fileNotDownloadable(path):
75
+ "File not downloadable at path \(path)"
76
+ }
77
+ }
78
+ }
@@ -0,0 +1,132 @@
1
+ //
2
+ // FileUtils.swift
3
+ // CloudStorage
4
+ //
5
+ // Created by Maximilian Krause on 27.09.24.
6
+ // Copyright © 2024 Kuatsu App Agency. All rights reserved.
7
+ //
8
+
9
+ import Foundation
10
+
11
+ enum FileUtils {
12
+ private static let fileManager = FileManager.default
13
+
14
+ static func checkFileExists(fileUrl: URL) throws -> Bool {
15
+ fileManager.fileExists(atPath: fileUrl.path)
16
+ }
17
+
18
+ /**
19
+ Reads a file and returns its content as a string.
20
+
21
+ - Parameter fileUrl: The URL of the file to read.
22
+ - Returns: The content of the file as a string.
23
+ - Throws: An NSError if the file couldn't be read.
24
+ */
25
+ static func readFile(fileUrl: URL) throws -> String {
26
+ if try !checkFileExists(fileUrl: fileUrl) {
27
+ throw CloudStorageError.fileNotFound(path: fileUrl.path)
28
+ }
29
+
30
+ do {
31
+ let fileContents = try String(contentsOf: fileUrl, encoding: .utf8)
32
+ return fileContents
33
+ } catch {
34
+ throw CloudStorageError.readError(path: fileUrl.path)
35
+ }
36
+ }
37
+
38
+ /**
39
+ Writes a string to a file. If the file already exists, it will be overwritten.
40
+
41
+ - Parameter fileUrl: The URL of the file to write to.
42
+ - Parameter content: The string to write to the file.
43
+ - Throws: An NSError if the file couldn't be written to.
44
+ */
45
+ static func writeFile(fileUrl: URL, content: String) throws {
46
+ do {
47
+ try content.write(to: fileUrl, atomically: true, encoding: .utf8)
48
+ } catch {
49
+ throw CloudStorageError.writeError(path: fileUrl.path)
50
+ }
51
+ }
52
+
53
+ /**
54
+ Creates a directory.
55
+
56
+ - Parameter directoryUrl: The URL of the directory to create.
57
+ - Throws: An NSError if the directory couldn't be created.
58
+ */
59
+ static func createDirectory(directoryUrl: URL) throws {
60
+ do {
61
+ try fileManager.createDirectory(at: directoryUrl, withIntermediateDirectories: true, attributes: nil)
62
+ } catch {
63
+ throw CloudStorageError.writeError(path: directoryUrl.path)
64
+ }
65
+ }
66
+
67
+ /**
68
+ Lists the files in a directory.
69
+
70
+ - Parameter directoryUrl: The URL of the directory to list the files of.
71
+ - Returns: An array of Strings representing the file names.
72
+ - Throws: An NSError if the directory couldn't be read.
73
+ */
74
+ static func listFiles(directoryUrl: URL) throws -> [String] {
75
+ do {
76
+ let fileUrls = try fileManager.contentsOfDirectory(atPath: directoryUrl.path)
77
+ return fileUrls
78
+ } catch {
79
+ throw CloudStorageError.readError(path: directoryUrl.path)
80
+ }
81
+ }
82
+
83
+ /**
84
+ Deletes a file or directory.
85
+
86
+ - Parameter fileUrl: The URL of the file or directory to delete.
87
+ - Throws: An NSError if the file / directory couldn't be deleted.
88
+ */
89
+ static func deleteFileOrDirectory(fileUrl: URL) throws {
90
+ do {
91
+ try fileManager.removeItem(at: fileUrl)
92
+ } catch {
93
+ throw CloudStorageError.deleteError(path: fileUrl.path)
94
+ }
95
+ }
96
+
97
+ /**
98
+ Gets the stats of a file.
99
+
100
+ - Parameter fileUrl: The URL of the file to get the stats of.
101
+ - Returns: The stats of the file.
102
+ - Throws: An NSError if the stats couldn't be retrieved.
103
+ */
104
+ static func statFile(fileUrl: URL) throws -> FileStat {
105
+ if try !checkFileExists(fileUrl: fileUrl) {
106
+ throw CloudStorageError.fileNotFound(path: fileUrl.path)
107
+ }
108
+
109
+ do {
110
+ let attributes = try fileManager.attributesOfItem(atPath: fileUrl.path)
111
+ let size = attributes[FileAttributeKey.size] as! UInt64
112
+ let birthtime = attributes[FileAttributeKey.creationDate] as! Date
113
+ let mtime = attributes[FileAttributeKey.modificationDate] as! Date
114
+ let isDirectory = attributes[FileAttributeKey.type] as! FileAttributeType == FileAttributeType.typeDirectory
115
+ let isFile = attributes[FileAttributeKey.type] as! FileAttributeType == FileAttributeType.typeRegular
116
+
117
+ return FileStat(
118
+ size: size,
119
+ birthtimeMs: birthtime.timeIntervalSince1970 * 1000,
120
+ mtimeMs: mtime.timeIntervalSince1970 * 1000,
121
+ isDirectory: isDirectory,
122
+ isFile: isFile
123
+ )
124
+ } catch {
125
+ throw CloudStorageError.statError(path: fileUrl.path)
126
+ }
127
+ }
128
+
129
+ static func sanitizePath(path: String) -> String {
130
+ path.replacingOccurrences(of: "^/+", with: "", options: .regularExpression)
131
+ }
132
+ }
@@ -0,0 +1,58 @@
1
+ //
2
+ // Promise.swift
3
+ // CloudStorage
4
+ //
5
+ // Created by Maximilian Krause on 27.09.24.
6
+ // Copyright © 2024 Kuatsu App Agency. All rights reserved.
7
+ //
8
+
9
+ import Foundation
10
+
11
+ // MARK: - Promise
12
+
13
+ class Promise {
14
+ private let resolver: RCTPromiseResolveBlock
15
+ private let rejecter: RCTPromiseRejectBlock
16
+
17
+ init(resolver: @escaping RCTPromiseResolveBlock, rejecter: @escaping RCTPromiseRejectBlock) {
18
+ self.resolver = resolver
19
+ self.rejecter = rejecter
20
+ }
21
+
22
+ func reject(error: CloudStorageError, cause: NSError? = nil) {
23
+ rejecter(error.code, error.message, cause)
24
+ }
25
+
26
+ func resolve(_ value: Any? = nil) {
27
+ resolver(value)
28
+ }
29
+ }
30
+
31
+ /**
32
+ * Wrap a code block with automatic promise resolution and rejection handling, simplifying error handling.
33
+ *
34
+ * The `block` should return a value that can be serialized by the React Native bridge (or `nil`).
35
+ * Any error thrown within the `block` should be of type `CloudStorageError`, otherwise it will be treated as
36
+ * `CloudStorageError.unknown`.
37
+ */
38
+ func withPromise(_ promise: Promise, _ block: () throws -> Any?) {
39
+ do {
40
+ let result = try block()
41
+ promise.resolve(result)
42
+ } catch let error as CloudStorageError {
43
+ promise.reject(error: error)
44
+ } catch let error as NSError {
45
+ promise.reject(error: CloudStorageError.unknown(message: error.description), cause: error)
46
+ }
47
+ }
48
+
49
+ /**
50
+ * Wrap a code block with automatic promise resolution and rejection handling, simplifying error handling.
51
+ *
52
+ * The `block` should return a value that can be serialized by the React Native bridge (or `nil`).
53
+ * Any error thrown within the `block` should be of type `CloudStorageError`, otherwise it will be treated as
54
+ * `CloudStorageError.unknown`.
55
+ */
56
+ func withPromise(resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock, _ block: () throws -> Any?) {
57
+ withPromise(Promise(resolver: resolve, rejecter: reject), block)
58
+ }
@@ -0,0 +1,36 @@
1
+ //
2
+ // Types.swift
3
+ // CloudStorage
4
+ //
5
+ // Created by Maximilian Krause on 27.09.24.
6
+ // Copyright © 2024 Kuatsu App Agency. All rights reserved.
7
+ //
8
+
9
+ import Foundation
10
+
11
+ // MARK: - DirectoryScope
12
+
13
+ @frozen
14
+ enum DirectoryScope: String {
15
+ case appData = "app_data"
16
+ case documents
17
+ }
18
+
19
+ // MARK: - FileStat
20
+
21
+ struct FileStat: Encodable {
22
+ let size: UInt64
23
+ let birthtimeMs: Double
24
+ let mtimeMs: Double
25
+ let isDirectory: Bool
26
+ let isFile: Bool
27
+
28
+ func toDictionary() -> [String: Any] {
29
+ guard let data = try? JSONEncoder().encode(self),
30
+ let dictionary = try? JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String: Any]
31
+ else {
32
+ return [:]
33
+ }
34
+ return dictionary
35
+ }
36
+ }