feat: force image format

This commit is contained in:
rhunk
2023-08-25 03:14:09 +02:00
parent d0668b67d4
commit b004f92b9c
3 changed files with 29 additions and 1 deletions

View File

@@ -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()))

View File

@@ -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"

View File

@@ -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"))