@@ -7,19 +7,16 @@ import android.os.StatFs
7
7
import android.provider.MediaStore
8
8
import android.util.Base64
9
9
import com.facebook.react.bridge.*
10
- import com.facebook.react.modules.network.OkHttpClientProvider
11
10
import kotlinx.coroutines.CoroutineScope
12
11
import kotlinx.coroutines.Dispatchers
13
12
import kotlinx.coroutines.launch
14
- import okhttp3.*
15
- import okhttp3.Callback
16
13
import java.io.File
17
14
import java.io.FileOutputStream
18
- import java.io.IOException
19
15
import java.io.InputStream
20
16
import java.security.MessageDigest
21
17
22
- class FileAccessModule (reactContext : ReactApplicationContext ) : ReactContextBaseJavaModule(reactContext) {
18
+ class FileAccessModule (reactContext : ReactApplicationContext ) :
19
+ ReactContextBaseJavaModule (reactContext) {
23
20
private val ioScope = CoroutineScope (Dispatchers .IO )
24
21
25
22
override fun getName (): String {
@@ -214,67 +211,8 @@ class FileAccessModule(reactContext: ReactApplicationContext) : ReactContextBase
214
211
}
215
212
216
213
@ReactMethod
217
- fun fetch (resource : String , init : ReadableMap , promise : Promise ) {
218
- val request = try {
219
- // Request will be saved to a file, no reason to also save in cache.
220
- val builder = Request .Builder ()
221
- .url(resource)
222
- .cacheControl(CacheControl .Builder ().noStore().build())
223
-
224
- if (init .hasKey(" method" )) {
225
- if (init .hasKey(" body" )) {
226
- builder.method(
227
- init .getString(" method" )!! ,
228
- RequestBody .create(null , init .getString(" body" )!! )
229
- )
230
- } else {
231
- builder.method(init .getString(" method" )!! , null )
232
- }
233
- }
234
-
235
- if (init .hasKey(" headers" )) {
236
- for (header in init .getMap(" headers" )!! .entryIterator) {
237
- builder.header(header.key, header.value as String )
238
- }
239
- }
240
-
241
- builder.build()
242
- } catch (e: Throwable ) {
243
- promise.reject(e)
244
- return
245
- }
246
-
247
- // Share client with RN core library.
248
- val call = OkHttpClientProvider .getOkHttpClient().newCall(request)
249
- call.enqueue(object : Callback {
250
- override fun onFailure (call : Call , e : IOException ) {
251
- promise.reject(e)
252
- }
253
-
254
- override fun onResponse (call : Call , response : Response ) {
255
- try {
256
- response.use {
257
- if (init .hasKey(" path" )) {
258
- parsePathToFile(init .getString(" path" )!! )
259
- .outputStream()
260
- .use { response.body()!! .byteStream().copyTo(it) }
261
- }
262
-
263
- val headers = response.headers().names().map { it to response.header(it) }
264
- promise.resolve(Arguments .makeNativeMap(mapOf (
265
- " headers" to Arguments .makeNativeMap(headers.toMap()),
266
- " ok" to response.isSuccessful,
267
- " redirected" to response.isRedirect,
268
- " status" to response.code(),
269
- " statusText" to response.message(),
270
- " url" to response.request().url().toString()
271
- )))
272
- }
273
- } catch (e: Throwable ) {
274
- promise.reject(e)
275
- }
276
- }
277
- })
214
+ fun fetch (requestId : Int , resource : String , init : ReadableMap ) {
215
+ NetworkHandler (reactApplicationContext).fetch(requestId, resource, init )
278
216
}
279
217
280
218
@ReactMethod
@@ -348,7 +286,8 @@ class FileAccessModule(reactContext: ReactApplicationContext) : ReactContextBase
348
286
ioScope.launch {
349
287
try {
350
288
if (! parsePathToFile(source).renameTo(parsePathToFile(target))) {
351
- parsePathToFile(source).also { it.copyTo(parsePathToFile(target), overwrite = true ) }.delete()
289
+ parsePathToFile(source).also { it.copyTo(parsePathToFile(target), overwrite = true ) }
290
+ .delete()
352
291
}
353
292
promise.resolve(null )
354
293
} catch (e: Throwable ) {
@@ -376,13 +315,17 @@ class FileAccessModule(reactContext: ReactApplicationContext) : ReactContextBase
376
315
try {
377
316
val file = parsePathToFile(path)
378
317
if (file.exists()) {
379
- promise.resolve(Arguments .makeNativeMap(mapOf (
380
- " filename" to file.name,
381
- " lastModified" to file.lastModified(),
382
- " path" to file.path,
383
- " size" to file.length(),
384
- " type" to if (file.isDirectory) " directory" else " file" ,
385
- )))
318
+ promise.resolve(
319
+ Arguments .makeNativeMap(
320
+ mapOf (
321
+ " filename" to file.name,
322
+ " lastModified" to file.lastModified(),
323
+ " path" to file.path,
324
+ " size" to file.length(),
325
+ " type" to if (file.isDirectory) " directory" else " file" ,
326
+ )
327
+ )
328
+ )
386
329
} else {
387
330
promise.reject(" ENOENT" , " '$path ' does not exist." )
388
331
}
@@ -432,20 +375,4 @@ class FileAccessModule(reactContext: ReactApplicationContext) : ReactContextBase
432
375
parsePathToFile(path).inputStream()
433
376
}
434
377
}
435
-
436
- /* *
437
- * Return a File object and do some basic sanitization of the passed path.
438
- */
439
- private fun parsePathToFile (path : String ): File {
440
- return if (path.contains(" ://" )) {
441
- try {
442
- val pathUri = Uri .parse(path)
443
- File (pathUri.path!! )
444
- } catch (e: Throwable ) {
445
- File (path)
446
- }
447
- } else {
448
- File (path)
449
- }
450
- }
451
378
}
0 commit comments