From 9fc9088f67ea40d0243f78f3ce882345f324c0cd Mon Sep 17 00:00:00 2001 From: Stefan Lobbenmeier Date: Tue, 28 May 2024 15:20:24 +0200 Subject: [PATCH] Do not make yt-dlp and ffmpeg executable on Windows, (#51) Fixes https://github.com/StefanLobbenmeier/yt-dlp-compose/issues/50 --- .../business/KtorFileDownloadWithProgress.kt | 20 ++++++++++++------- .../stefan/updater/business/Platform.kt | 12 ++++++++--- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/main/kotlin/de/lobbenmeier/stefan/updater/business/KtorFileDownloadWithProgress.kt b/src/main/kotlin/de/lobbenmeier/stefan/updater/business/KtorFileDownloadWithProgress.kt index 73d9e56..6b1c191 100644 --- a/src/main/kotlin/de/lobbenmeier/stefan/updater/business/KtorFileDownloadWithProgress.kt +++ b/src/main/kotlin/de/lobbenmeier/stefan/updater/business/KtorFileDownloadWithProgress.kt @@ -16,8 +16,8 @@ import java.io.InputStream import java.nio.file.Files import java.nio.file.attribute.PosixFilePermissions import java.util.zip.ZipInputStream -import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext private val logger = KotlinLogging.logger {} @@ -33,7 +33,7 @@ suspend fun HttpClient.downloadFile( return targetFile } - return CoroutineScope(Dispatchers.IO).run { + return withContext(Dispatchers.IO) { logger.info { "Starting download to $targetFile from $url" } val downloadFile = @@ -45,11 +45,17 @@ suspend fun HttpClient.downloadFile( } targetFile.parentFile.mkdirs() - - val executable = PosixFilePermissions.fromString("rwxr-xr-x") - val permissions = PosixFilePermissions.asFileAttribute(executable) Files.deleteIfExists(targetFile.toPath()) - Files.createFile(targetFile.toPath(), permissions) + + val platform = getPlatform() + if (platform.needsExecutableBit) { + val executable = PosixFilePermissions.fromString("rwxr-xr-x") + val permissions = PosixFilePermissions.asFileAttribute(executable) + + Files.createFile(targetFile.toPath(), permissions) + } else { + Files.createFile(targetFile.toPath()) + } if (unzipFile) { copyFirstEntryToFile(downloadFile.bodyAsChannel().toInputStream(), targetFile) @@ -60,7 +66,7 @@ suspend fun HttpClient.downloadFile( onProgress(DownloadCompleted) logger.info { "Completed download to $targetFile from $url" } - return targetFile + targetFile } } diff --git a/src/main/kotlin/de/lobbenmeier/stefan/updater/business/Platform.kt b/src/main/kotlin/de/lobbenmeier/stefan/updater/business/Platform.kt index 2507bb9..988cb41 100644 --- a/src/main/kotlin/de/lobbenmeier/stefan/updater/business/Platform.kt +++ b/src/main/kotlin/de/lobbenmeier/stefan/updater/business/Platform.kt @@ -9,6 +9,7 @@ data class Platform( val name: String, val ytDlpName: YtDlpNames, val ffmpegPlatform: FfmpegPlatforms, + val needsExecutableBit: Boolean, ) { val settingsFile = Path.of(Directories.configDir).resolve("settings.json") val binariesFolder = Path.of(Directories.dataDir).resolve("binaries") @@ -46,7 +47,12 @@ fun getPlatform(): Platform { return when { name.contains("Windows") -> { - Platform(displayName, YtDlpNames.windows, FfmpegPlatforms.windows64) + Platform( + displayName, + YtDlpNames.windows, + FfmpegPlatforms.windows64, + needsExecutableBit = false + ) } name.contains("Mac") -> { val ytDlpName = @@ -55,7 +61,7 @@ fun getPlatform(): Platform { } else { YtDlpNames.osx } - Platform(displayName, ytDlpName, FfmpegPlatforms.osx64) + Platform(displayName, ytDlpName, FfmpegPlatforms.osx64, needsExecutableBit = true) } else -> { val ffmpegPlatform = @@ -65,7 +71,7 @@ fun getPlatform(): Platform { else -> FfmpegPlatforms.linux64 } - Platform(displayName, YtDlpNames.python, ffmpegPlatform) + Platform(displayName, YtDlpNames.python, ffmpegPlatform, needsExecutableBit = true) } } }