commit b004f92b9cd9751fa0f22403b8b07ae426cd58cd
parent d0668b67d4c64b2b70d5e9ae9c88af8161abe8e9
Author: rhunk <101876869+rhunk@users.noreply.github.com>
Date:   Fri, 25 Aug 2023 03:14:09 +0200

feat: force image format

Diffstat:
Mapp/src/main/kotlin/me/rhunk/snapenhance/download/DownloadProcessor.kt | 23++++++++++++++++++++++-
Mcore/src/main/assets/lang/en_US.json | 4++++
Mcore/src/main/kotlin/me/rhunk/snapenhance/core/config/impl/DownloaderConfig.kt | 3+++
3 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/app/src/main/kotlin/me/rhunk/snapenhance/download/DownloadProcessor.kt b/app/src/main/kotlin/me/rhunk/snapenhance/download/DownloadProcessor.kt @@ -2,6 +2,8 @@ package me.rhunk.snapenhance.download import android.annotation.SuppressLint import android.content.Intent +import android.graphics.Bitmap +import android.graphics.BitmapFactory import android.net.Uri import android.widget.Toast import androidx.documentfile.provider.DocumentFile @@ -121,12 +123,31 @@ class DownloadProcessor ( if (coroutineContext.job.isCancelled) return runCatching { - val fileType = FileType.fromFile(inputFile) + var fileType = FileType.fromFile(inputFile) + if (fileType == FileType.UNKNOWN) { callbackOnFailure(translation.format("failed_gallery_toast", "error" to "Unknown media type"), null) return } + if (fileType.isImage) { + remoteSideContext.config.root.downloader.forceImageFormat.getNullable()?.let { format -> + val bitmap = BitmapFactory.decodeFile(inputFile.absolutePath) ?: throw Exception("Failed to decode bitmap") + @Suppress("DEPRECATION") val compressFormat = when (format) { + "png" -> Bitmap.CompressFormat.PNG + "jpg" -> Bitmap.CompressFormat.JPEG + "webp" -> Bitmap.CompressFormat.WEBP + else -> throw Exception("Invalid image format") + } + + val outputStream = inputFile.outputStream() + bitmap.compress(compressFormat, 100, outputStream) + outputStream.close() + + fileType = FileType.fromFile(inputFile) + } + } + val fileName = downloadObject.metadata.outputPath.substringAfterLast("/") + "." + fileType.fileExtension val outputFolder = DocumentFile.fromTreeUri(remoteSideContext.androidContext, Uri.parse(remoteSideContext.config.root.downloader.saveFolder.get())) diff --git a/core/src/main/assets/lang/en_US.json b/core/src/main/assets/lang/en_US.json @@ -123,6 +123,10 @@ "name": "Merge Overlays", "description": "Combines the text and the media of a Snap into a single file" }, + "force_image_format": { + "name": "Force Image Format", + "description": "Forces images to be saved as a specific format" + }, "chat_download_context_menu": { "name": "Chat Download Context Menu", "description": "Allows to download messages from a conversation by long pressing them" diff --git a/core/src/main/kotlin/me/rhunk/snapenhance/core/config/impl/DownloaderConfig.kt b/core/src/main/kotlin/me/rhunk/snapenhance/core/config/impl/DownloaderConfig.kt @@ -22,6 +22,9 @@ class DownloaderConfig : ConfigContainer() { ).apply { set(mutableListOf("append_hash", "append_date_time", "append_type", "append_username")) } val allowDuplicate = boolean("allow_duplicate") val mergeOverlays = boolean("merge_overlays") { addNotices(FeatureNotice.MAY_CAUSE_CRASHES) } + val forceImageFormat = unique("force_image_format", "jpg", "png", "webp") { + addFlags(ConfigFlag.NO_TRANSLATE) + } val chatDownloadContextMenu = boolean("chat_download_context_menu") val logging = multiple("logging", "started", "success", "progress", "failure").apply { set(mutableListOf("started", "success"))