-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathHybridNitroFS.kt
More file actions
executable file
·212 lines (186 loc) · 5.91 KB
/
HybridNitroFS.kt
File metadata and controls
executable file
·212 lines (186 loc) · 5.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package com.nitrofs
import android.util.Log
import com.margelo.nitro.NitroModules
import com.margelo.nitro.core.Promise
import com.margelo.nitro.nitrofs.HybridNitroFSSpec
import com.margelo.nitro.nitrofs.NitroFile
import com.margelo.nitro.nitrofs.NitroFileEncoding
import com.margelo.nitro.nitrofs.NitroFileStat
import com.margelo.nitro.nitrofs.NitroUploadOptions
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
class HybridNitroFS: HybridNitroFSSpec() {
val context = NitroModules.applicationContext ?: error("React Native context not found")
val nitroFsImpl = NitroFSImpl(context)
val ioScope = CoroutineScope(Dispatchers.IO)
override val BUNDLE_DIR: String
get() = ""
override val DOCUMENT_DIR: String
get() = nitroFsImpl.getDocumentDir()
override val CACHE_DIR: String
get() = nitroFsImpl.getCacheDir()
override val DOWNLOAD_DIR: String
get() = nitroFsImpl.getDownloadDir()
override val PICTURES_DIR: String
get() = nitroFsImpl.getPicturesDir()
override val MOVIES_DIR: String
get() = nitroFsImpl.getMoviesDir()
override val DCIM_DIR: String
get() = nitroFsImpl.getDCIMDir()
override val MUSIC_DIR: String
get() = nitroFsImpl.getMusicDir()
override fun exists(path: String): Promise<Boolean> {
return Promise.async {
nitroFsImpl.exists(path)
}
}
override fun writeFile(
path: String,
data: String,
encoding: NitroFileEncoding
): Promise<Unit> {
return Promise.async(ioScope) {
try {
nitroFsImpl.writeFile(path, data, encoding)
} catch (e: Exception) {
Log.e(TAG, "Error writing file: ${e.message}")
throw Error(e)
}
}
}
override fun readFile(
path: String,
encoding: NitroFileEncoding
): Promise<String> {
return Promise.async(ioScope) {
try {
nitroFsImpl.readFile(path, encoding)
} catch (e: Exception) {
Log.e(TAG, "Error reading file: ${e.message}")
throw Error(e)
}
}
}
override fun copyFile(
srcPath: String,
destPath: String
): Promise<Unit> {
return Promise.async(ioScope) {
try {
nitroFsImpl.copyFile(srcPath, destPath)
} catch (e: Exception) {
Log.e(TAG, "Error copying file: ${e.message}")
throw Error(e)
}
}
}
override fun copy(
srcPath: String,
destPath: String
): Promise<Unit> {
return copyFile(srcPath, destPath)
}
override fun unlink(path: String): Promise<Boolean> {
return Promise.async(ioScope) {
try {
nitroFsImpl.unlink(path)
} catch (e: Exception) {
Log.e(TAG, "Error unlinking file: ${e.message}")
throw Error(e)
}
}
}
override fun mkdir(path: String): Promise<Boolean> {
return Promise.async(ioScope) {
try {
nitroFsImpl.mkdir(path)
} catch (e: Exception) {
Log.e(TAG, "Error creating directory: ${e.message}")
throw Error(e)
}
}
}
override fun stat(path: String): Promise<NitroFileStat> {
return Promise.async(ioScope) {
nitroFsImpl.stat(path)
}
}
override fun readdir(path: String): Promise<Array<NitroFile>> {
return Promise.async(ioScope) {
try {
nitroFsImpl.readdir(path)
} catch (e: Exception) {
Log.e(TAG, "Error while calling readdir(...): ${e.message}")
throw Error(e)
}
}
}
override fun rename(
oldPath: String,
newPath: String
): Promise<Unit> {
return Promise.async(ioScope) {
try {
nitroFsImpl.rename(oldPath, newPath)
} catch (e: Exception) {
Log.e(TAG, "Error while calling rename(...): ${e.message}")
throw Error(e)
}
}
}
override fun dirname(path: String): String {
try {
return nitroFsImpl.dirname(path)
} catch (e: Exception) {
Log.e(TAG, "Error while calling dirname(...): ${e.message}")
throw Error(e)
}
}
override fun basename(path: String): String {
try {
return nitroFsImpl.basename(path)
} catch (e: Exception) {
Log.e(TAG, "Error while calling basename(...): ${e.message}")
throw Error(e)
}
}
override fun extname(path: String): String {
try {
return nitroFsImpl.extname(path)
} catch (e: Exception) {
Log.e(TAG, "Error while calling extname(...): ${e.message}")
throw Error(e)
}
}
override fun uploadFile(
file: NitroFile,
uploadOptions: NitroUploadOptions,
onProgress: ((Double, Double) -> Unit)?
): Promise<Unit> {
return Promise.async(ioScope) {
try {
nitroFsImpl.uploadFile(file, uploadOptions, onProgress)
} catch (e: Exception) {
Log.e(TAG, "Error uploading file: ${e.message}")
throw Error(e)
}
}
}
override fun downloadFile(
serverUrl: String,
destinationPath: String,
onProgress: ((Double, Double) -> Unit)?
): Promise<NitroFile> {
return Promise.async(ioScope) {
try {
nitroFsImpl.downloadFile(serverUrl, destinationPath, onProgress)
} catch (e: Exception) {
Log.e(TAG, "Error downloading file: ${e.message}")
throw Error(e)
}
}
}
companion object {
const val TAG = "NitroFS"
}
}