Skip to content

Commit 157952b

Browse files
committed
download to temp file first
1 parent 352ff63 commit 157952b

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

app/src/main/java/com/geode/launcher/updater/ReleaseManager.kt

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import okio.buffer
2222
import okio.source
2323
import java.io.File
2424
import java.io.InterruptedIOException
25+
import kotlin.io.path.deleteIfExists
2526

2627
private const val TAG_LATEST = "latest"
2728
private const val TAG_BETA = "prerelease"
@@ -181,6 +182,10 @@ class ReleaseManager private constructor(
181182
// set an initial download size
182183
_uiState.value = ReleaseManagerState.InDownload(0, releaseAsset.size)
183184

185+
val outputFile = getTempFile()
186+
// clone the file instance as renameTo may move the original file
187+
val tempFilePath = outputFile.path
188+
184189
try {
185190
val fileStream = DownloadUtils.downloadStream(
186191
httpClient,
@@ -189,23 +194,30 @@ class ReleaseManager private constructor(
189194
_uiState.value = ReleaseManagerState.InDownload(progress, outOf)
190195
}
191196

197+
outputFile.parentFile?.mkdirs()
198+
192199
val geodeFile = getGeodeOutputPath()
193200

201+
DownloadUtils.extractFileFromZipStream(
202+
fileStream,
203+
outputFile.outputStream(),
204+
geodeFile.name
205+
)
206+
194207
// work around a permission issue from adb push
195208
if (geodeFile.exists()) {
196209
geodeFile.delete()
197210
}
198211

199-
geodeFile.parentFile?.mkdirs()
200-
201-
DownloadUtils.extractFileFromZipStream(
202-
fileStream,
203-
geodeFile.outputStream(),
204-
geodeFile.name
205-
)
212+
outputFile.renameTo(geodeFile)
206213
} catch (e: Exception) {
207214
sendError(e)
208215
return
216+
} finally {
217+
val tempFileClone = File(tempFilePath)
218+
try {
219+
if (tempFileClone.exists()) tempFileClone.delete()
220+
} catch (_: Exception) { }
209221
}
210222

211223
downloadLauncherUpdateIfNecessary()
@@ -325,6 +337,15 @@ class ReleaseManager private constructor(
325337
return File(geodeDirectory, geodeName)
326338
}
327339

340+
private fun getTempFile(): File {
341+
val geodeName = LaunchUtils.geodeFilename
342+
val geodeDirectory = LaunchUtils.getBaseDirectory(applicationContext)
343+
344+
val tempFile = File.createTempFile(geodeName, null, geodeDirectory)
345+
346+
return tempFile
347+
}
348+
328349
/**
329350
* Cancels the current update job.
330351
*/

0 commit comments

Comments
 (0)