Skip to content

Commit

Permalink
feature: Use cache for images from network
Browse files Browse the repository at this point in the history
  • Loading branch information
retyui committed Mar 8, 2024
1 parent 16b6ee4 commit 0424dd2
Showing 1 changed file with 42 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -333,13 +333,7 @@ class ImageEditorModuleImpl(private val reactContext: ReactApplicationContext) {
} else if (isLocalUri(uri)) {
reactContext.contentResolver.openInputStream(Uri.parse(uri))
} else {
val connection = URL(uri).openConnection()
headers?.forEach { (key, value) ->
if (value is String) {
connection.setRequestProperty(key, value)
}
}
connection.getInputStream()
fetchOrUseCache(uri, headers, reactContext)
}
}

Expand Down Expand Up @@ -450,6 +444,34 @@ class ImageEditorModuleImpl(private val reactContext: ReactApplicationContext) {
)

// Utils
private fun fetchOrUseCache(
uri: String,
headers: HashMap<String, Any?>?,
context: Context
): InputStream? {
val filename = getFileNameFromUrl(uri, headers)
val cacheDir = getCacheDir(context)
val cachedFile = File(cacheDir, filename)
if (!cachedFile.exists()) {
val connection = URL(uri).openConnection()
headers?.forEach { (key, value) ->
if (value is String) {
connection.setRequestProperty(key, value)
}
}
val inputStream = connection.getInputStream()
FileOutputStream(cachedFile).use { outputStream ->
inputStream.copyTo(outputStream)
}
}

return FileInputStream(cachedFile)
}

private fun getFileNameFromUrl(uri: String, headers: HashMap<String, Any?>?): String {
return "$uri${headers?.hashCode() ?: 0}".hashCode().toString()
}

private fun getResultMap(
resizedImage: File,
image: Bitmap,
Expand Down Expand Up @@ -593,14 +615,8 @@ class ImageEditorModuleImpl(private val reactContext: ReactApplicationContext) {
}
}

/**
* Create a temporary file in the cache directory on either internal or external storage,
* whichever is available and has more free space.
*
* @param mimeType the MIME type of the file to create (image/ *)
*/
@Throws(IOException::class)
private fun createTempFile(context: Context, mimeType: String?): File {
private fun getCacheDir(context: Context): File? {
val externalCacheDir = context.externalCacheDir
val internalCacheDir = context.cacheDir
if (externalCacheDir == null && internalCacheDir == null) {
Expand All @@ -615,10 +631,21 @@ class ImageEditorModuleImpl(private val reactContext: ReactApplicationContext) {
if (externalCacheDir.freeSpace > internalCacheDir.freeSpace) externalCacheDir
else internalCacheDir
}
return cacheDir
}

/**
* Create a temporary file in the cache directory on either internal or external storage,
* whichever is available and has more free space.
*
* @param mimeType the MIME type of the file to create (image/ *)
*/
@Throws(IOException::class)
private fun createTempFile(context: Context, mimeType: String?): File {
return File.createTempFile(
TEMP_FILE_PREFIX,
getFileExtensionForType(mimeType),
cacheDir
getCacheDir(context)
)
}

Expand Down

0 comments on commit 0424dd2

Please sign in to comment.