Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Delete tmp files on request failure #582

Merged
merged 1 commit into from
Jun 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import okhttp3.Response
import okhttp3.internal.closeQuietly
import java.io.File
import java.io.FileInputStream
import java.io.IOException
import java.io.InputStream

object ImageResponse {
Expand All @@ -37,12 +38,18 @@ object ImageResponse {
* @param cacheSavePath where to save the cached image. Caller should decide to use perma cache or temp cache (OS temp dir)
* @param fileName what the saved cache file should be named
*/
suspend fun getCachedImageResponse(saveDir: String, fileName: String, fetcher: suspend () -> Response): Pair<InputStream, String> {
suspend fun getCachedImageResponse(
saveDir: String,
fileName: String,
fetcher: suspend () -> Response
): Pair<InputStream, String> {
File(saveDir).mkdirs()

val cachedFile = findFileNameStartingWith(saveDir, fileName)
val filePath = "$saveDir/$fileName"
if (cachedFile != null) {

// in case the cached file is a ".tmp" file something went wrong with the previous download, and it has to be downloaded again
if (cachedFile != null && !cachedFile.endsWith(".tmp")) {
val fileType = cachedFile.substringAfter("$filePath.")
return Pair(
pathToInputStream(cachedFile),
Expand All @@ -52,12 +59,19 @@ object ImageResponse {

val response = fetcher()

if (response.code == 200) {
val (actualSavePath, imageType) = saveImage(filePath, response.body!!.byteStream())
return pathToInputStream(actualSavePath) to imageType
} else {
try {
if (response.code == 200) {
val (actualSavePath, imageType) = saveImage(filePath, response.body.byteStream())
return pathToInputStream(actualSavePath) to imageType
} else {
throw Exception("request error! ${response.code}")
}
} catch (e: IOException) {
// make sure no partial download remains
clearCachedImage(saveDir, fileName)
throw e
} finally {
response.closeQuietly()
throw Exception("request error! ${response.code}")
}
}

Expand Down