commit 0d3bffb05bc14af3dab21a05ad2795876e34dfc5
parent cc94ea93b27398a0ef17b1d6e259f525a2d796ae
Author: rhunk <101876869+rhunk@users.noreply.github.com>
Date: Thu, 21 Dec 2023 00:03:39 +0100
feat: custom path format
Diffstat:
3 files changed, 33 insertions(+), 19 deletions(-)
diff --git a/common/src/main/assets/lang/en_US.json b/common/src/main/assets/lang/en_US.json
@@ -212,6 +212,10 @@
"logging": {
"name": "Logging",
"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%"
}
}
},
diff --git a/common/src/main/kotlin/me/rhunk/snapenhance/common/config/impl/DownloaderConfig.kt b/common/src/main/kotlin/me/rhunk/snapenhance/common/config/impl/DownloaderConfig.kt
@@ -48,4 +48,5 @@ class DownloaderConfig : ConfigContainer() {
val logging = multiple("logging", "started", "success", "progress", "failure").apply {
set(mutableListOf("success", "progress", "failure"))
}
+ val customPathFormat = string("custom_path_format") { addNotices(FeatureNotice.UNSTABLE) }
}
\ No newline at end of file
diff --git a/common/src/main/kotlin/me/rhunk/snapenhance/common/data/download/DownloadRequest.kt b/common/src/main/kotlin/me/rhunk/snapenhance/common/data/download/DownloadRequest.kt
@@ -44,8 +44,8 @@ fun createNewFilePath(
creationTimestamp: Long?
): String {
val pathFormat by config.downloader.pathFormat
+ val customPathFormat by config.downloader.customPathFormat
val sanitizedMediaAuthor = mediaAuthor.sanitizeForPath().ifEmpty { hexHash }
-
val currentDateTime = SimpleDateFormat("yyyy-MM-dd_HH-mm-ss", Locale.ENGLISH).format(creationTimestamp ?: System.currentTimeMillis())
val finalPath = StringBuilder()
@@ -58,26 +58,35 @@ fun createNewFilePath(
}
}
- if (pathFormat.contains("create_author_folder")) {
- finalPath.append(sanitizedMediaAuthor).append("/")
- }
- if (pathFormat.contains("create_source_folder")) {
- finalPath.append(downloadSource.pathName).append("/")
- }
- if (pathFormat.contains("append_hash")) {
- appendFileName(hexHash)
- }
- if (pathFormat.contains("append_source")) {
- appendFileName(downloadSource.pathName)
- }
- if (pathFormat.contains("append_username")) {
- appendFileName(sanitizedMediaAuthor)
- }
- if (pathFormat.contains("append_date_time")) {
- appendFileName(currentDateTime)
+ if (customPathFormat.isNotEmpty()) {
+ finalPath.append(customPathFormat
+ .replace("%username%", sanitizedMediaAuthor)
+ .replace("%source%", downloadSource.pathName)
+ .replace("%hash%", hexHash)
+ .replace("%date_time%", currentDateTime)
+ )
+ } else {
+ if (pathFormat.contains("create_author_folder")) {
+ finalPath.append(sanitizedMediaAuthor).append("/")
+ }
+ if (pathFormat.contains("create_source_folder")) {
+ finalPath.append(downloadSource.pathName).append("/")
+ }
+ if (pathFormat.contains("append_hash")) {
+ appendFileName(hexHash)
+ }
+ 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()
}
\ No newline at end of file