Add attachment previews and attachments view
Bump version to 0.2.2 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -345,6 +345,40 @@ internal object AndroidMailService {
|
||||
}
|
||||
}
|
||||
|
||||
fun previewAttachment(account: JSONObject, input: JSONObject): JSONObject =
|
||||
withStore(account) { _, store ->
|
||||
val folder = resolveFolder(store, input.optString("folder", "Inbox"))
|
||||
?: throw IllegalStateException("That mail folder could not be found.")
|
||||
folder.open(Folder.READ_ONLY)
|
||||
try {
|
||||
val message = (folder as? UIDFolder)?.getMessageByUID(input.optLong("uid"))
|
||||
?: throw IllegalStateException("The message is no longer available.")
|
||||
val filename = input.optString("filename")
|
||||
val part = findAttachment(message, filename)
|
||||
?: throw IllegalStateException("No attachment was found in this message.")
|
||||
val mimeType = part.contentType.substringBefore(';').trim().lowercase(Locale.ROOT)
|
||||
val none = JSONObject().put("kind", "none")
|
||||
.put("reason", "No preview is available for this file type. Download it to open it.")
|
||||
if (Regex("image/(png|jpe?g|gif|webp|bmp|svg\\+xml)").matches(mimeType)) {
|
||||
val bytes = part.inputStream.use { it.readBytes() }
|
||||
if (bytes.size > 5 * 1048576) {
|
||||
none.put("reason", "This image is too large to preview. Download it instead.")
|
||||
} else {
|
||||
JSONObject().put("kind", "image").put(
|
||||
"dataUrl", "data:$mimeType;base64," + android.util.Base64.encodeToString(bytes, android.util.Base64.NO_WRAP)
|
||||
)
|
||||
}
|
||||
} else if (mimeType.startsWith("text/") || mimeType == "application/json" || mimeType == "application/xml") {
|
||||
val bytes = part.inputStream.use { it.readBytes() }
|
||||
JSONObject().put("kind", "text")
|
||||
.put("text", String(bytes.copyOf(minOf(bytes.size, 100000)), Charsets.UTF_8))
|
||||
.put("truncated", bytes.size > 100000)
|
||||
} else none
|
||||
} finally {
|
||||
folder.close(false)
|
||||
}
|
||||
}
|
||||
|
||||
fun checkForNewMail(account: JSONObject): Int {
|
||||
flushOutbox(account)
|
||||
return withStore(account) { _, store ->
|
||||
|
||||
Reference in New Issue
Block a user