Skip to content

Commit

Permalink
feat: Check whether system supports file command, if not, disable f…
Browse files Browse the repository at this point in the history
…ile type checking and show warning
  • Loading branch information
0ffz committed Oct 7, 2024
1 parent d9eaac0 commit 378830b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
kotlin.code.style=official
group=com.mineinabyss
version=3.1.0-alpha.3
version=3.1.0-alpha.4
idofrontVersion=0.25.6
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.mineinabyss.keepup.downloads.github.GithubConfig
import com.mineinabyss.keepup.downloads.parsing.DownloadParser
import com.mineinabyss.keepup.downloads.parsing.DownloadSource
import com.mineinabyss.keepup.similarfiles.SimilarFileChecker
import com.mineinabyss.keepup.type_checker.FileTypeChecker.SYSTEM_SUPPORTS_FILE
import io.ktor.client.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
Expand All @@ -28,6 +29,7 @@ class KeepupDownloader(
dest: Path,
scope: CoroutineScope,
): ReceiveChannel<DownloadResult> = scope.produce(Dispatchers.IO) {
SYSTEM_SUPPORTS_FILE // check if system supports file command
val similarFileChecker = if (config.ignoreSimilar) SimilarFileChecker(dest) else null
val downloader = DownloadParser(
failAllDownloads = config.failAllDownloads,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class DownloadParser(
val expectedFileType = source.expectedType
if (it !is DownloadResult.HasFiles || expectedFileType == null) return@map it
val type = FileTypeChecker.getType(it.file)
if (type == expectedFileType) it
if (type == null || type == expectedFileType) it
else DownloadResult.Failure(
"Downloaded file type didn't match expected, got $type, expected $expectedFileType",
it.keyInConfig
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
package com.mineinabyss.keepup.type_checker

import com.github.ajalt.mordant.rendering.TextColors
import com.lordcodes.turtle.shellRun
import com.mineinabyss.keepup.helpers.MSG
import com.mineinabyss.keepup.t
import java.nio.file.Path
import kotlin.io.path.absolutePathString

object FileTypeChecker {
fun getType(file: Path): FileType {
val SYSTEM_SUPPORTS_FILE by lazy {
runCatching {
shellRun("command", listOf("-v", "file"))
}.onFailure {
t.println("${MSG.warn} ${TextColors.yellow("System does not support file command, file type checking will be disabled")}")
}.isSuccess
}

fun getType(file: Path): FileType? {
if (!SYSTEM_SUPPORTS_FILE) return null

val result = shellRun("file", listOf("-b", file.absolutePathString()))
return when {
result.startsWith("Java archive data") || result.startsWith("Zip archive data") -> FileType.Archive
Expand Down

0 comments on commit 378830b

Please sign in to comment.