feat: custom path format

This commit is contained in:
rhunk 2023-12-21 00:03:39 +01:00
parent cc94ea93b2
commit 0d3bffb05b
3 changed files with 33 additions and 19 deletions

View File

@ -212,6 +212,10 @@
"logging": { "logging": {
"name": "Logging", "name": "Logging",
"description": "Shows toasts when media is downloading" "description": "Shows toasts when media is downloading"
},
"custom_path_format": {
"name": "Custom Path Format",
"description": "Specify a custom path format for downloaded media\n\nAvailable variables:\n - %username%\n - %source%\n - %hash%\n - %date_time%"
} }
} }
}, },

View File

@ -48,4 +48,5 @@ class DownloaderConfig : ConfigContainer() {
val logging = multiple("logging", "started", "success", "progress", "failure").apply { val logging = multiple("logging", "started", "success", "progress", "failure").apply {
set(mutableListOf("success", "progress", "failure")) set(mutableListOf("success", "progress", "failure"))
} }
val customPathFormat = string("custom_path_format") { addNotices(FeatureNotice.UNSTABLE) }
} }

View File

@ -44,8 +44,8 @@ fun createNewFilePath(
creationTimestamp: Long? creationTimestamp: Long?
): String { ): String {
val pathFormat by config.downloader.pathFormat val pathFormat by config.downloader.pathFormat
val customPathFormat by config.downloader.customPathFormat
val sanitizedMediaAuthor = mediaAuthor.sanitizeForPath().ifEmpty { hexHash } val sanitizedMediaAuthor = mediaAuthor.sanitizeForPath().ifEmpty { hexHash }
val currentDateTime = SimpleDateFormat("yyyy-MM-dd_HH-mm-ss", Locale.ENGLISH).format(creationTimestamp ?: System.currentTimeMillis()) val currentDateTime = SimpleDateFormat("yyyy-MM-dd_HH-mm-ss", Locale.ENGLISH).format(creationTimestamp ?: System.currentTimeMillis())
val finalPath = StringBuilder() val finalPath = StringBuilder()
@ -58,26 +58,35 @@ fun createNewFilePath(
} }
} }
if (pathFormat.contains("create_author_folder")) { if (customPathFormat.isNotEmpty()) {
finalPath.append(sanitizedMediaAuthor).append("/") finalPath.append(customPathFormat
} .replace("%username%", sanitizedMediaAuthor)
if (pathFormat.contains("create_source_folder")) { .replace("%source%", downloadSource.pathName)
finalPath.append(downloadSource.pathName).append("/") .replace("%hash%", hexHash)
} .replace("%date_time%", currentDateTime)
if (pathFormat.contains("append_hash")) { )
appendFileName(hexHash) } else {
} if (pathFormat.contains("create_author_folder")) {
if (pathFormat.contains("append_source")) { finalPath.append(sanitizedMediaAuthor).append("/")
appendFileName(downloadSource.pathName) }
} if (pathFormat.contains("create_source_folder")) {
if (pathFormat.contains("append_username")) { finalPath.append(downloadSource.pathName).append("/")
appendFileName(sanitizedMediaAuthor) }
} if (pathFormat.contains("append_hash")) {
if (pathFormat.contains("append_date_time")) { appendFileName(hexHash)
appendFileName(currentDateTime) }
if (pathFormat.contains("append_source")) {
appendFileName(downloadSource.pathName)
}
if (pathFormat.contains("append_username")) {
appendFileName(sanitizedMediaAuthor)
}
if (pathFormat.contains("append_date_time")) {
appendFileName(currentDateTime)
}
} }
if (finalPath.isEmpty()) finalPath.append(hexHash) if (finalPath.isEmpty() || finalPath.isBlank()) finalPath.append(hexHash)
return finalPath.toString() return finalPath.toString()
} }