Skip to content

Commit

Permalink
add open folder to file utils
Browse files Browse the repository at this point in the history
  • Loading branch information
amir1376 committed Oct 12, 2024
1 parent a093ab9 commit ebb2ad3
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import java.io.File
interface FileUtils {
fun openFile(file: File): Boolean
fun openFolderOfFile(file: File): Boolean
fun openFolder(folder: File): Boolean
fun canWriteInThisFolder(folder: String): Boolean

companion object : FileUtils by getPlatformFileUtil()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,21 @@ import java.io.FileNotFoundException

abstract class FileUtilsBase : FileUtils {
override fun openFile(file: File): Boolean {
if (!file.exists()) {
throw FileNotFoundException("$file not found")
}
return openFileInternal(file)
return openFileInternal(
file = preparedFile(file)
)
}

override fun openFolderOfFile(file: File): Boolean {
val file = file.canonicalFile.absoluteFile
return openFolderOfFileInternal(file)
return openFolderOfFileInternal(
file = preparedFile(file)
)
}

override fun openFolder(folder: File): Boolean {
return openFolderInternal(
folder = preparedFile(folder)
)
}

override fun canWriteInThisFolder(folder: String): Boolean {
Expand All @@ -34,6 +40,15 @@ abstract class FileUtilsBase : FileUtils {
return false
}

private fun preparedFile(file: File): File {
val file = file.canonicalFile.absoluteFile
if (!file.exists()) {
throw FileNotFoundException("$file not found")
}
return file
}

protected abstract fun openFileInternal(file: File): Boolean
protected abstract fun openFolderOfFileInternal(file: File): Boolean
protected abstract fun openFolderInternal(folder: File): Boolean
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,12 @@ internal class JVMFileUtils : FileUtilsBase() {
}
return false
}

override fun openFolderInternal(folder: File): Boolean {
kotlin.runCatching {
Desktop.getDesktop().open(folder)
return true
}
return false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@ internal class LinuxFileUtils : FileUtilsBase() {
return xdgOpenResult
}

override fun openFolderInternal(folder: File): Boolean {
return execAndWait(arrayOf("xdg-open", folder.parent))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ internal class MacOsFileUtils : FileUtilsBase() {
override fun openFolderOfFileInternal(file: File): Boolean {
return execAndWait(arrayOf("open", "-R", file.path))
}

override fun openFolderInternal(folder: File): Boolean {
return execAndWait(arrayOf("open", folder.path))
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ internal class WindowsFileUtils : FileUtilsBase() {
override fun openFolderOfFileInternal(file: File): Boolean {
return execAndWait(arrayOf("cmd", "/c", "explorer.exe", "/select,", file.path))
}

override fun openFolderInternal(folder: File): Boolean {
return execAndWait(arrayOf("cmd", "/c", "explorer.exe", folder.path))
}
}

0 comments on commit ebb2ad3

Please sign in to comment.