Skip to content

Commit 0424dd2

Browse files
committed
feature: Use cache for images from network
1 parent 16b6ee4 commit 0424dd2

File tree

1 file changed

+42
-15
lines changed

1 file changed

+42
-15
lines changed

android/src/main/java/com/reactnativecommunity/imageeditor/ImageEditorModuleImpl.kt

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -333,13 +333,7 @@ class ImageEditorModuleImpl(private val reactContext: ReactApplicationContext) {
333333
} else if (isLocalUri(uri)) {
334334
reactContext.contentResolver.openInputStream(Uri.parse(uri))
335335
} 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)
343337
}
344338
}
345339

@@ -450,6 +444,34 @@ class ImageEditorModuleImpl(private val reactContext: ReactApplicationContext) {
450444
)
451445

452446
// 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+
453475
private fun getResultMap(
454476
resizedImage: File,
455477
image: Bitmap,
@@ -593,14 +615,8 @@ class ImageEditorModuleImpl(private val reactContext: ReactApplicationContext) {
593615
}
594616
}
595617

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-
*/
602618
@Throws(IOException::class)
603-
private fun createTempFile(context: Context, mimeType: String?): File {
619+
private fun getCacheDir(context: Context): File? {
604620
val externalCacheDir = context.externalCacheDir
605621
val internalCacheDir = context.cacheDir
606622
if (externalCacheDir == null && internalCacheDir == null) {
@@ -615,10 +631,21 @@ class ImageEditorModuleImpl(private val reactContext: ReactApplicationContext) {
615631
if (externalCacheDir.freeSpace > internalCacheDir.freeSpace) externalCacheDir
616632
else internalCacheDir
617633
}
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 {
618645
return File.createTempFile(
619646
TEMP_FILE_PREFIX,
620647
getFileExtensionForType(mimeType),
621-
cacheDir
648+
getCacheDir(context)
622649
)
623650
}
624651

0 commit comments

Comments
 (0)