@@ -333,13 +333,7 @@ class ImageEditorModuleImpl(private val reactContext: ReactApplicationContext) {
333
333
} else if (isLocalUri(uri)) {
334
334
reactContext.contentResolver.openInputStream(Uri .parse(uri))
335
335
} else {
336
- val connection = URL (uri).openConnection()
337
- headers?.forEach { (key, value) ->
338
- if (value is String ) {
339
- connection.setRequestProperty(key, value)
340
- }
341
- }
342
- connection.getInputStream()
336
+ fetchOrUseCache(uri, headers, reactContext)
343
337
}
344
338
}
345
339
@@ -450,6 +444,34 @@ class ImageEditorModuleImpl(private val reactContext: ReactApplicationContext) {
450
444
)
451
445
452
446
// Utils
447
+ private fun fetchOrUseCache (
448
+ uri : String ,
449
+ headers : HashMap <String , Any ?>? ,
450
+ context : Context
451
+ ): InputStream ? {
452
+ val filename = getFileNameFromUrl(uri, headers)
453
+ val cacheDir = getCacheDir(context)
454
+ val cachedFile = File (cacheDir, filename)
455
+ if (! cachedFile.exists()) {
456
+ val connection = URL (uri).openConnection()
457
+ headers?.forEach { (key, value) ->
458
+ if (value is String ) {
459
+ connection.setRequestProperty(key, value)
460
+ }
461
+ }
462
+ val inputStream = connection.getInputStream()
463
+ FileOutputStream (cachedFile).use { outputStream ->
464
+ inputStream.copyTo(outputStream)
465
+ }
466
+ }
467
+
468
+ return FileInputStream (cachedFile)
469
+ }
470
+
471
+ private fun getFileNameFromUrl (uri : String , headers : HashMap <String , Any ?>? ): String {
472
+ return " $uri${headers?.hashCode() ? : 0 } " .hashCode().toString()
473
+ }
474
+
453
475
private fun getResultMap (
454
476
resizedImage : File ,
455
477
image : Bitmap ,
@@ -593,14 +615,8 @@ class ImageEditorModuleImpl(private val reactContext: ReactApplicationContext) {
593
615
}
594
616
}
595
617
596
- /* *
597
- * Create a temporary file in the cache directory on either internal or external storage,
598
- * whichever is available and has more free space.
599
- *
600
- * @param mimeType the MIME type of the file to create (image/ *)
601
- */
602
618
@Throws(IOException ::class )
603
- private fun createTempFile (context : Context , mimeType : String? ): File {
619
+ private fun getCacheDir (context : Context ): File ? {
604
620
val externalCacheDir = context.externalCacheDir
605
621
val internalCacheDir = context.cacheDir
606
622
if (externalCacheDir == null && internalCacheDir == null ) {
@@ -615,10 +631,21 @@ class ImageEditorModuleImpl(private val reactContext: ReactApplicationContext) {
615
631
if (externalCacheDir.freeSpace > internalCacheDir.freeSpace) externalCacheDir
616
632
else internalCacheDir
617
633
}
634
+ return cacheDir
635
+ }
636
+
637
+ /* *
638
+ * Create a temporary file in the cache directory on either internal or external storage,
639
+ * whichever is available and has more free space.
640
+ *
641
+ * @param mimeType the MIME type of the file to create (image/ *)
642
+ */
643
+ @Throws(IOException ::class )
644
+ private fun createTempFile (context : Context , mimeType : String? ): File {
618
645
return File .createTempFile(
619
646
TEMP_FILE_PREFIX ,
620
647
getFileExtensionForType(mimeType),
621
- cacheDir
648
+ getCacheDir(context)
622
649
)
623
650
}
624
651
0 commit comments