-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathHybridNitroFS.kt
More file actions
executable file
·148 lines (134 loc) · 4.3 KB
/
HybridNitroFS.kt
File metadata and controls
executable file
·148 lines (134 loc) · 4.3 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
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 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("NitroFS", "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("NitroFS", "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("NitroFS", "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("NitroFS", "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("NitroFS", "Error creating directory: ${e.message}")
throw Error(e)
}
}
}
override fun stat(path: String): Promise<NitroFileStat> {
return Promise.async(ioScope) {
nitroFsImpl.stat(path)
}
}
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("NitroFS", "Error uploading file: ${e.message}")
throw Error(e)
}
}
}
override fun downloadFile(
serverUrl: String,
fileName: String,
destinationPath: String,
onProgress: ((Double, Double) -> Unit)?
): Promise<NitroFile> {
return Promise.async(ioScope) {
try {
nitroFsImpl.downloadFile(serverUrl, fileName, destinationPath, onProgress)
NitroFile(
name = fileName,
path = destinationPath,
mimeType = "",
)
} catch (e: Exception) {
Log.e("NitroFS", "Error downloading file: ${e.message}")
throw Error(e)
}
}
}
}