-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathNitroFSImpl.kt
More file actions
131 lines (112 loc) · 3.73 KB
/
NitroFSImpl.kt
File metadata and controls
131 lines (112 loc) · 3.73 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
package com.nitrofs
import android.content.Context
import android.os.Environment
import android.util.Log
import com.facebook.react.bridge.ReactApplicationContext
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 java.io.File
import java.io.FileOutputStream
import java.nio.charset.Charset
class NitroFSImpl(val context: ReactApplicationContext) {
private val nitroFileUploader: NitroFileUploader = NitroFileUploader()
private val fileDownloader: FileDownloader = FileDownloader()
fun exists(path: String): Boolean {
val dir = File(path)
return dir.exists()
}
fun unlink(path: String): Boolean {
val file = File(path)
return file.deleteRecursively()
}
fun writeFile(
path: String,
data: String,
encoding: NitroFileEncoding
) {
val file = File(path)
file.writeText(data, charset = getFileEncoding(encoding))
}
fun readFile(
path: String,
encoding: NitroFileEncoding
): String {
val file = File(path)
return file.readText(charset = getFileEncoding(encoding))
}
fun copyFile(
srcPath: String,
destPath: String
) {
val file = File(srcPath)
val dest = File(destPath)
file.copyTo(dest)
}
fun mkdir(path: String): Boolean {
val file = File(path)
return file.mkdirs()
}
fun stat(path: String): NitroFileStat {
val file = File(path)
val stat = NitroFileStat(
size = file.length().toDouble(),
isFile = file.isFile,
isDirectory = file.isDirectory,
ctime = file.lastModified().toDouble(),
mtime = file.lastModified().toDouble()
)
return stat
}
fun getDocumentDir(): String {
return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS)?.absolutePath ?: ""
}
fun getCacheDir(): String {
return context.cacheDir.absolutePath
}
fun getDownloadDir(): String {
return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS)?.absolutePath ?: ""
}
suspend fun uploadFile(file: NitroFile,
uploadOptions: NitroUploadOptions,
onProgress: ((Double, Double) -> Unit)?
) {
val nitroFile = File(file.path)
nitroFileUploader.handleUpload(nitroFile, uploadOptions, onProgress)
}
suspend fun downloadFile(
serverUrl: String,
destinationPath: String,
onProgress: ((Double, Double) -> Unit)?
): NitroFile {
val file = fileDownloader.downloadFile(
serverUrl,
destinationPath,
onProgress
)
if (file != null) {
return file
} else {
throw RuntimeException("Failed to download file from: $serverUrl")
}
}
fun getFileEncoding(encoding: NitroFileEncoding): Charset {
return when(encoding){
NitroFileEncoding.UTF8 -> Charsets.UTF_8
NitroFileEncoding.ASCII -> Charsets.US_ASCII
}
}
// used to generate a large file for testing purposes
fun generateLargeFile(context: Context, fileName: String = "large_dummy_file.txt", sizeInMB: Int = 100): File {
val file = File(context.cacheDir, fileName)
val buffer = ByteArray(1024 * 1024)
FileOutputStream(file).use { output ->
repeat(sizeInMB) {
output.write(buffer)
}
}
Log.d("LargeFile", "Generated file: ${file.absolutePath}, Size: ${file.length()} bytes")
return file
}
}