Skip to content

Commit

Permalink
Cleanup old releases of binaries after downloading them (#134)
Browse files Browse the repository at this point in the history
Fixes #85
  • Loading branch information
StefanLobbenmeier authored Jan 18, 2025
1 parent 0af5c41 commit 744ffdd
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import de.lobbenmeier.stefan.settings.business.BinariesSettings
import de.lobbenmeier.stefan.settings.business.FfmpegLocation
import de.lobbenmeier.stefan.settings.business.YtDlpLocation
import de.lobbenmeier.stefan.updater.business.ffmpeg.FfmpegReleaseDownloader
import de.lobbenmeier.stefan.updater.business.ffmpeg.getFfmpegChannelFolders
import de.lobbenmeier.stefan.updater.model.Binaries
import de.lobbenmeier.stefan.updater.model.BinariesProgress
import de.lobbenmeier.stefan.updater.model.RemoteBinaryProgress
import io.github.oshai.kotlinlogging.KotlinLogging
import java.io.File
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
Expand All @@ -23,6 +25,7 @@ class BinariesUpdater(private val binariesSettings: BinariesSettings) {

val progress: SnapshotStateList<BinariesProgress> = mutableStateListOf()
val binaries = MutableStateFlow<Binaries?>(null)
val logger = KotlinLogging.logger {}

init {
downloadOrFindBinaries()
Expand Down Expand Up @@ -75,6 +78,8 @@ class BinariesUpdater(private val binariesSettings: BinariesSettings) {
val ffmpeg = ffmpegFuture.await()

binaries.value = Binaries(ytDlp.toPath(), ffmpeg.toPath())

cleanupOldReleases(ytDlp, ffmpeg)
}
}

Expand Down Expand Up @@ -108,4 +113,42 @@ class BinariesUpdater(private val binariesSettings: BinariesSettings) {
): suspend (UpdateDownloadProgress) -> Unit {
return { updateProcess.progress.emit(it) }
}

private fun cleanupOldReleases(ytDlp: File, ffmpeg: File) {
val ytDlpChannelFolders = getYtDlpChannelFolders(platform.binariesFolder)

deleteBinariesExceptFor(ytDlpChannelFolders, ytDlp)

val ffmpegChannelFolders = getFfmpegChannelFolders(platform.binariesFolder)
deleteBinariesExceptFor(ffmpegChannelFolders, ytDlp)
}

private fun deleteBinariesExceptFor(channelFolders: List<File>, except: File) {
for (channelFolder in channelFolders) {
if (!channelFolder.exists()) {
logger.info { "Channel $channelFolder did not exist yet, nothign to clean up" }
continue
}

val binaryFolders = channelFolder.listFiles()
val numberOfBinaries = binaryFolders?.size ?: 0
if (binaryFolders == null || numberOfBinaries <= 1) {
logger.info {
"There are $numberOfBinaries of in $channelFolder, leaving that channel untouched"
}
continue
}

binaryFolders
.filterNot { except.absolutePath.startsWith(it.absolutePath) }
.forEach {
val deleteSuccess = it.deleteRecursively()
if (!deleteSuccess) {
logger.warn { "Failed to delete $it" }
} else {
logger.info { "Deleted version that was no longer needed: $it" }
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@ fun createYtDlpDownloader(
}
return GithubReleaseDownloader("yt-dlp", repo, downloadDirectory)
}

fun getYtDlpChannelFolders(downloadDirectory: Path) =
listOf("yt-dlp", "yt-dlp-nightly-builds", "yt-dlp-master-builds").map {
downloadDirectory.resolve("yt-dlp").resolve(it).toFile()
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,6 @@ class FfmpegReleaseDownloader(
return releaseResponse.body()
}
}

fun getFfmpegChannelFolders(downloadDirectory: Path) =
listOf(downloadDirectory.resolve("ffbinaries").toFile())

0 comments on commit 744ffdd

Please sign in to comment.