From fc4d52b57ad41713c52d475586a5b83b36da85e6 Mon Sep 17 00:00:00 2001 From: Abdelilah El Aissaoui Date: Fri, 4 Feb 2022 01:34:22 +0100 Subject: [PATCH] Temporary files are now deleted when exiting the app --- src/main/kotlin/app/TempFilesManager.kt | 20 +++++++++++++++++++ src/main/kotlin/app/git/RawFileManager.kt | 9 ++++++--- src/main/kotlin/app/{ => ui}/SideMenuUtils.kt | 0 3 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 src/main/kotlin/app/TempFilesManager.kt rename src/main/kotlin/app/{ => ui}/SideMenuUtils.kt (100%) diff --git a/src/main/kotlin/app/TempFilesManager.kt b/src/main/kotlin/app/TempFilesManager.kt new file mode 100644 index 0000000..c2e6edf --- /dev/null +++ b/src/main/kotlin/app/TempFilesManager.kt @@ -0,0 +1,20 @@ +package app + +import app.di.TabScope +import javax.inject.Inject +import kotlin.io.path.createTempDirectory +import kotlin.io.path.deleteIfExists + +@TabScope +class TempFilesManager @Inject constructor() { + val tempDir by lazy { + val tempDirPath = createTempDirectory("gitnuro_") + tempDirPath.toFile().deleteOnExit() + + tempDirPath + } + + fun removeTempDir() { + tempDir.deleteIfExists() + } +} \ No newline at end of file diff --git a/src/main/kotlin/app/git/RawFileManager.kt b/src/main/kotlin/app/git/RawFileManager.kt index 0f6ff95..ba5e07b 100644 --- a/src/main/kotlin/app/git/RawFileManager.kt +++ b/src/main/kotlin/app/git/RawFileManager.kt @@ -1,5 +1,6 @@ package app.git +import app.TempFilesManager import dagger.assisted.Assisted import dagger.assisted.AssistedInject import org.eclipse.jgit.diff.ContentSource @@ -14,14 +15,14 @@ import org.eclipse.jgit.util.LfsFactory import java.io.FileOutputStream import java.nio.file.Path import kotlin.io.path.absolutePathString -import kotlin.io.path.createTempDirectory import kotlin.io.path.createTempFile private const val DEFAULT_BINARY_FILE_THRESHOLD = PackConfig.DEFAULT_BIG_FILE_THRESHOLD class RawFileManager @AssistedInject constructor( - @Assisted private val repository: Repository + @Assisted private val repository: Repository, + private val tempFilesManager: TempFilesManager, ) : AutoCloseable { private var reader: ObjectReader = repository.newObjectReader() private var source: ContentSource.Pair @@ -75,8 +76,10 @@ class RawFileManager @AssistedInject constructor( ): EntryContent.ImageBinary { println("Data's size is ${ldr.size}") - val tempDir = createTempDirectory("gitnuro${repository.directory.absolutePath.replace("/", "_")}") + val tempDir = tempFilesManager.tempDir + val tempFile = createTempFile(tempDir, prefix = "${entry.newPath.replace("/", "_")}_${side.name}") + tempFile.toFile().deleteOnExit() println("Temp file generated: ${tempFile.absolutePathString()}") val out = FileOutputStream(tempFile.toFile()) diff --git a/src/main/kotlin/app/SideMenuUtils.kt b/src/main/kotlin/app/ui/SideMenuUtils.kt similarity index 100% rename from src/main/kotlin/app/SideMenuUtils.kt rename to src/main/kotlin/app/ui/SideMenuUtils.kt