commit 7b473871e1c1d86e72b9d98eef65d9ea4baadcfa
parent db2ab27467ad8e301c7261632f8b700e63fc8222
Author: rhunk <101876869+rhunk@users.noreply.github.com>
Date:   Mon, 29 May 2023 15:10:03 +0200

refactor: add download options

Diffstat:
Mapp/src/main/assets/lang/en_US.json | 9++++++++-
Mapp/src/main/kotlin/me/rhunk/snapenhance/config/ConfigProperty.kt | 32+++++++++++++++++++-------------
Mapp/src/main/kotlin/me/rhunk/snapenhance/features/impl/downloader/MediaDownloader.kt | 38+++++++++++++++++++++++++++++++++++---
3 files changed, 62 insertions(+), 17 deletions(-)

diff --git a/app/src/main/assets/lang/en_US.json b/app/src/main/assets/lang/en_US.json @@ -29,7 +29,7 @@ "auto_download_stories": "Auto Download Stories", "auto_download_public_stories": "Auto Download Public Stories", "auto_download_spotlight": "Auto Download Spotlight", - "overlay_merge": "Snap Image Overlay Merge", + "download_options": "Download Options", "download_inchat_snaps": "Download Inchat Snaps", "anti_auto_download_button": "Anti Auto Download Button", "disable_metrics": "Disable Metrics", @@ -62,6 +62,13 @@ "option": { "property": { + "download_options": { + "format_user_folder": "Create folder for each user", + "format_hash": "Add a unique hash to the file path", + "format_username": "Add the username to the file path", + "format_date_time": "Add the date and time to the file path", + "merge_overlay": "Merge Snap Image Overlays" + }, "notification_blacklist": { "chat": "Chat", "snap": "Snap", diff --git a/app/src/main/kotlin/me/rhunk/snapenhance/config/ConfigProperty.kt b/app/src/main/kotlin/me/rhunk/snapenhance/config/ConfigProperty.kt @@ -14,14 +14,6 @@ enum class ConfigProperty( val category: ConfigCategory, val valueContainer: ConfigValue<*> ) { - SAVE_FOLDER( - "property.save_folder", "description.save_folder", ConfigCategory.GENERAL, - ConfigStringValue(File( - Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).absolutePath + "/Snapchat", - "SnapEnhance" - ).absolutePath) - ), - PREVENT_READ_RECEIPTS( "property.prevent_read_receipts", "description.prevent_read_receipts", @@ -57,6 +49,13 @@ enum class ConfigProperty( MESSAGE_LOGGER("property.message_logger", "description.message_logger", ConfigCategory.SPYING, ConfigStateValue(false)), UNLIMITED_SNAP_VIEW_TIME("property.unlimited_snap_view_time", "description.unlimited_snap_view_time", ConfigCategory.SPYING, ConfigStateValue(false)), + SAVE_FOLDER( + "property.save_folder", "description.save_folder", ConfigCategory.MEDIA_DOWNLOADER, + ConfigStringValue(File( + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).absolutePath + "/Snapchat", + "SnapEnhance" + ).absolutePath) + ), AUTO_DOWNLOAD_SNAPS( "property.auto_download_snaps", "description.auto_download_snaps", @@ -81,11 +80,18 @@ enum class ConfigProperty( ConfigCategory.MEDIA_DOWNLOADER, ConfigStateValue(false) ), - OVERLAY_MERGE( - "property.overlay_merge", - "description.overlay_merge", - ConfigCategory.MEDIA_DOWNLOADER, - ConfigStateValue(false) + DOWNLOAD_OPTIONS( + "property.download_options", "description.download_options", ConfigCategory.MEDIA_DOWNLOADER, + ConfigStateListValue( + listOf("format_user_folder", "format_hash", "format_date_time", "format_username", "merge_overlay"), + mutableMapOf( + "format_user_folder" to true, + "format_hash" to true, + "format_date_time" to true, + "format_username" to false, + "merge_overlay" to false, + ) + ) ), DOWNLOAD_INCHAT_SNAPS( "property.download_inchat_snaps", diff --git a/app/src/main/kotlin/me/rhunk/snapenhance/features/impl/downloader/MediaDownloader.kt b/app/src/main/kotlin/me/rhunk/snapenhance/features/impl/downloader/MediaDownloader.kt @@ -36,10 +36,13 @@ import java.io.ByteArrayOutputStream import java.io.File import java.io.FileOutputStream import java.io.InputStream +import java.lang.StringBuilder import java.net.HttpURLConnection import java.net.URL import java.nio.file.Paths +import java.text.SimpleDateFormat import java.util.Arrays +import java.util.Locale import java.util.concurrent.atomic.AtomicReference import javax.crypto.Cipher import javax.crypto.CipherInputStream @@ -58,13 +61,42 @@ class MediaDownloader : Feature("MediaDownloader", loadParams = FeatureLoadParam } private fun canMergeOverlay(): Boolean { - if (!context.config.bool(ConfigProperty.OVERLAY_MERGE)) return false + if (context.config.options(ConfigProperty.DOWNLOAD_OPTIONS)["overlay_merge"] == false) return false return isFFmpegPresent } private fun createNewFilePath(hash: Int, author: String, fileType: FileType): String { val hexHash = Integer.toHexString(hash) - return author + "/" + hexHash + "." + fileType.fileExtension + val downloadOptions = context.config.options(ConfigProperty.DOWNLOAD_OPTIONS) + + val currentDateTime = SimpleDateFormat("yyyy-MM-dd_HH-mm-ss", Locale.ENGLISH).format(System.currentTimeMillis()) + + val finalPath = StringBuilder() + + fun appendFileName(string: String) { + if (finalPath.isEmpty() || finalPath.endsWith("/")) { + finalPath.append(string) + } else { + finalPath.append("_").append(string) + } + } + + if (downloadOptions["format_user_folder"] == true) { + finalPath.append(author).append("/") + } + if (downloadOptions["format_hash"] == true) { + appendFileName(hexHash) + } + if (downloadOptions["format_username"] == true) { + appendFileName(author) + } + if (downloadOptions["format_date_time"] == true) { + appendFileName(currentDateTime) + } + + if (finalPath.isEmpty()) finalPath.append(hexHash) + + return finalPath.toString() + "." + fileType.fileExtension } private fun downloadFile(outputFile: File, content: ByteArray): Boolean { @@ -130,7 +162,7 @@ class MediaDownloader : Feature("MediaDownloader", loadParams = FeatureLoadParam } private fun isFileExists(hash: Int, author: String, fileType: FileType): Boolean { - val fileName: String = createNewFilePath(hash, author, fileType) ?: return false + val fileName: String = createNewFilePath(hash, author, fileType) val outputFile: File = createNeededDirectories(File(context.config.string(ConfigProperty.SAVE_FOLDER), fileName)) return outputFile.exists()