Skip to content

Commit

Permalink
Add setting for download folder
Browse files Browse the repository at this point in the history
Now that yt-dlp has an absolute path, it returns the absolute path for the filename as well, so we no longer have to worry about resolving it
  • Loading branch information
StefanLobbenmeier committed Apr 20, 2024
1 parent a81fd6f commit 57e40e2
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package de.lobbenmeier.stefan.downloadlist.business

import de.lobbenmeier.stefan.common.business.YtDlpJson
import de.lobbenmeier.stefan.updater.business.getPlatform
import io.github.oshai.kotlinlogging.KotlinLogging
import java.io.File
import kotlinx.coroutines.CoroutineScope
Expand Down Expand Up @@ -74,9 +73,7 @@ class DownloadItem(
}
}
downloadProgress.emit(DownloadCompleted)
videoMetadata?.filename?.let {
targetFile.emit(getPlatform().downloadsFolder.resolve(it).toFile())
}
videoMetadata?.filename?.let { targetFile.emit(File(it)) }
} catch (e: Exception) {
e.printStackTrace()
downloadProgress.emit(DownloadFailed(e))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import com.github.pgreze.process.Redirect
import com.github.pgreze.process.process
import de.lobbenmeier.stefan.common.business.getDynamicSemaphoreSingleton
import de.lobbenmeier.stefan.settings.business.Settings
import de.lobbenmeier.stefan.updater.business.getPlatform
import de.lobbenmeier.stefan.updater.model.Binaries
import kotlin.io.path.pathString
import kotlinx.coroutines.sync.withPermit
Expand Down Expand Up @@ -45,8 +44,7 @@ class YtDlp(private val binaries: Binaries, private val settings: Settings) {
ytDlpBinary,
*fullOptions,
stdout = Redirect.Consume { it.collect(consumer) },
stderr = Redirect.Consume { it.collect(consumer) },
directory = getPlatform().downloadsFolder.toFile()
stderr = Redirect.Consume { it.collect(consumer) }
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ fun Settings.toYtDlpConfiguration(): Array<String> =
addArgument("--embed-thumbnail", embedThumbnail)
addArgument("--embed-chapters", embedChapters)

addArgument("--paths", downloadFolder)
addArgument("--output", filenameTemplate)
addArgument("--save", saveMetadataToJsonFile)
addArgument("--write-thumbnail", saveThumbnailToFile)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package de.lobbenmeier.stefan.settings.business

import de.lobbenmeier.stefan.updater.business.getPlatform
import kotlin.io.path.absolutePathString
import kotlinx.serialization.Serializable

@Serializable
Expand Down Expand Up @@ -29,6 +31,7 @@ data class Settings(
val embedChapters: Boolean = false,

// files
val downloadFolder: String = getPlatform().downloadsFolder.absolutePathString(),
val filenameTemplate: String?,
val saveMetadataToJsonFile: Boolean = false,
val saveThumbnailToFile: Boolean = false,
Expand Down
46 changes: 43 additions & 3 deletions src/main/kotlin/de/lobbenmeier/stefan/settings/ui/SettingsUI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,15 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import com.darkrockstudios.libraries.mpfilepicker.DirectoryPicker
import com.darkrockstudios.libraries.mpfilepicker.FilePicker
import compose.icons.FeatherIcons
import compose.icons.feathericons.File
import compose.icons.feathericons.Folder
import de.lobbenmeier.stefan.downloadlist.ui.Menu
import de.lobbenmeier.stefan.settings.business.Settings
import de.lobbenmeier.stefan.updater.business.getPlatform
import kotlin.io.path.absolutePathString

private val textFieldWidth = 350.dp

Expand Down Expand Up @@ -164,6 +167,17 @@ fun SettingsUI(settings: Settings, save: (Settings) -> Unit, cancel: () -> Unit)
}

Section("Files") {
DirectoryInput(
"Download Folder",
mutableSettings.downloadFolder,
onValueChange = {
mutableSettings =
mutableSettings.copy(
downloadFolder =
it ?: getPlatform().downloadsFolder.absolutePathString()
)
}
)
TextInput(
"Filename template",
mutableSettings.filenameTemplate,
Expand Down Expand Up @@ -285,15 +299,41 @@ private fun BooleanInput(description: String, value: Boolean, onValueChange: (Bo

@Composable
private fun FileInput(description: String, value: String?, onValueChange: (String?) -> Unit) {
var directoryPickerOpen by remember { mutableStateOf(false) }
var filePickerOpen by remember { mutableStateOf(false) }

FilePicker(
show = filePickerOpen,
title = description,
initialDirectory = value ?: "${getPlatform().homeFolder}/",
onFileSelected = {
filePickerOpen = false
onValueChange(it?.path)
},
)

TextInput(
description,
value,
onValueChange,
trailingIcon = {
IconButton(onClick = { filePickerOpen = true }) {
Icon(FeatherIcons.File, contentDescription = null)
}
},
)
}

@Composable
private fun DirectoryInput(description: String, value: String?, onValueChange: (String?) -> Unit) {
var directoryPickerOpen by remember { mutableStateOf(false) }

DirectoryPicker(
show = directoryPickerOpen,
title = description,
initialDirectory = value ?: "${getPlatform().homeFolder}/",
onFileSelected = {
directoryPickerOpen = false
onValueChange(it?.path)
onValueChange(it)
},
)

Expand All @@ -303,7 +343,7 @@ private fun FileInput(description: String, value: String?, onValueChange: (Strin
onValueChange,
trailingIcon = {
IconButton(onClick = { directoryPickerOpen = true }) {
Icon(FeatherIcons.File, contentDescription = null)
Icon(FeatherIcons.Folder, contentDescription = null)
}
},
)
Expand Down

0 comments on commit 57e40e2

Please sign in to comment.