diff --git a/apps/ios/SimpleXChat/ImageUtils.swift b/apps/ios/SimpleXChat/ImageUtils.swift index e0bcce2e94..bb25cbf48f 100644 --- a/apps/ios/SimpleXChat/ImageUtils.swift +++ b/apps/ios/SimpleXChat/ImageUtils.swift @@ -107,7 +107,7 @@ public func resizeImageToStrSizeSync(_ image: UIImage, maxDataSize: Int64) -> St guard let d = image.pngData() else { return nil } if let _ = saveFile(d, tmpFile, encrypted: false) { defer { removeFile(tmpFile) } - let ptr = chat_resize_image_to_str_size(tmpFile, Int32(maxDataSize)) + let ptr = chat_resize_image_to_str_size(tmpFile, maxDataSize) if let ptr = ptr { let str = fromCString(ptr) let dataSize = str.count diff --git a/apps/ios/SimpleXChat/SimpleX.h b/apps/ios/SimpleXChat/SimpleX.h index 8d9e25e02d..37759e7b56 100644 --- a/apps/ios/SimpleXChat/SimpleX.h +++ b/apps/ios/SimpleXChat/SimpleX.h @@ -29,7 +29,7 @@ extern char *chat_valid_name(char *name); extern int chat_json_length(char *str); extern char *chat_encrypt_media(chat_ctrl ctl, char *key, char *frame, int len); extern char *chat_decrypt_media(char *key, char *frame, int len); -extern char *chat_resize_image_to_str_size(const char *path, int maxSize); +extern char *chat_resize_image_to_str_size(const char *path, long maxSize); // chat_write_file returns null-terminated string with JSON of WriteFileResult extern char *chat_write_file(chat_ctrl ctl, char *path, char *data, int len); diff --git a/apps/multiplatform/common/src/androidMain/kotlin/chat/simplex/common/platform/Images.android.kt b/apps/multiplatform/common/src/androidMain/kotlin/chat/simplex/common/platform/Images.android.kt index f3a89dd664..e09aa42982 100644 --- a/apps/multiplatform/common/src/androidMain/kotlin/chat/simplex/common/platform/Images.android.kt +++ b/apps/multiplatform/common/src/androidMain/kotlin/chat/simplex/common/platform/Images.android.kt @@ -27,6 +27,7 @@ actual fun base64ToBitmap(base64ImageString: String): ImageBitmap { val imageString = base64ImageString .removePrefix("data:image/png;base64,") .removePrefix("data:image/jpg;base64,") + .removePrefix("data:image/jpeg;base64,") return try { val imageBytes = Base64.decode(imageString, Base64.NO_WRAP) BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.size).asImageBitmap() @@ -43,7 +44,7 @@ actual fun resizeImageToStrSize(image: ImageBitmap, maxDataSize: Long): String { compressImageData(image, true).writeTo(output) output.flush() output.close() - var str = chatResizeImageToStrSize(tmpFileName, maxDataSize.toInt()) + var str = chatResizeImageToStrSize(tmpFileName, maxDataSize) removeFile(tmpFileName) return str } diff --git a/apps/multiplatform/common/src/commonMain/cpp/android/simplex-api.c b/apps/multiplatform/common/src/commonMain/cpp/android/simplex-api.c index 0c373e327c..ebda8c81a1 100644 --- a/apps/multiplatform/common/src/commonMain/cpp/android/simplex-api.c +++ b/apps/multiplatform/common/src/commonMain/cpp/android/simplex-api.c @@ -72,7 +72,7 @@ extern char *chat_write_image(chat_ctrl ctl, long maxSize, char *path, char *dat extern char *chat_read_file(const char *path, const char *key, const char *nonce); extern char *chat_encrypt_file(chat_ctrl ctrl, const char *from_path, const char *to_path); extern char *chat_decrypt_file(const char *from_path, const char *key, const char *nonce, const char *to_path); -extern char *chat_resize_image_to_str_size(const char *from_path, int max_size); +extern char *chat_resize_image_to_str_size(const char *from_path, long max_size); JNIEXPORT jobjectArray JNICALL Java_chat_simplex_common_platform_CoreKt_chatMigrateInit(JNIEnv *env, __unused jclass clazz, jstring dbPath, jstring dbKey, jstring confirm) { @@ -258,7 +258,7 @@ Java_chat_simplex_common_platform_CoreKt_chatDecryptFile(JNIEnv *env, jclass cla } JNIEXPORT jstring JNICALL -Java_chat_simplex_common_platform_CoreKt_chatResizeImageToStrSize(JNIEnv *env, jclass clazz, jstring from_path, jint max_size) { +Java_chat_simplex_common_platform_CoreKt_chatResizeImageToStrSize(JNIEnv *env, jclass clazz, jstring from_path, jlong max_size) { const char *_from_path = encode_to_utf8_chars(env, from_path); jstring res = decode_to_utf8_string(env, chat_resize_image_to_str_size(_from_path, max_size)); (*env)->ReleaseStringUTFChars(env, from_path, _from_path); diff --git a/apps/multiplatform/common/src/commonMain/cpp/desktop/simplex-api.c b/apps/multiplatform/common/src/commonMain/cpp/desktop/simplex-api.c index d36ba58c0d..c2bfb60e99 100644 --- a/apps/multiplatform/common/src/commonMain/cpp/desktop/simplex-api.c +++ b/apps/multiplatform/common/src/commonMain/cpp/desktop/simplex-api.c @@ -45,7 +45,7 @@ extern char *chat_write_image(chat_ctrl ctrl, long max_size, const char *path, c extern char *chat_read_file(const char *path, const char *key, const char *nonce); extern char *chat_encrypt_file(chat_ctrl ctrl, const char *from_path, const char *to_path); extern char *chat_decrypt_file(const char *from_path, const char *key, const char *nonce, const char *to_path); -extern char *chat_resize_image_to_str_size(const char *from_path, int max_size); +extern char *chat_resize_image_to_str_size(const char *from_path, long max_size); // As a reference: https://stackoverflow.com/a/60002045 jstring decode_to_utf8_string(JNIEnv *env, char *string) { @@ -268,7 +268,7 @@ Java_chat_simplex_common_platform_CoreKt_chatDecryptFile(JNIEnv *env, jclass cla } JNIEXPORT jstring JNICALL -Java_chat_simplex_common_platform_CoreKt_chatResizeImageToStrSize(JNIEnv *env, jclass clazz, jstring from_path, jint max_size) { +Java_chat_simplex_common_platform_CoreKt_chatResizeImageToStrSize(JNIEnv *env, jclass clazz, jstring from_path, jlong max_size) { const char *_from_path = encode_to_utf8_chars(env, from_path); jstring res = decode_to_utf8_string(env, chat_resize_image_to_str_size(_from_path, max_size)); (*env)->ReleaseStringUTFChars(env, from_path, _from_path); diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/platform/Core.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/platform/Core.kt index 1cf9172595..dc2dccf3ea 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/platform/Core.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/platform/Core.kt @@ -35,7 +35,7 @@ external fun chatWriteFile(ctrl: ChatCtrl, path: String, buffer: ByteBuffer): St external fun chatReadFile(path: String, key: String, nonce: String): Array external fun chatEncryptFile(ctrl: ChatCtrl, fromPath: String, toPath: String): String external fun chatDecryptFile(fromPath: String, key: String, nonce: String, toPath: String): String -external fun chatResizeImageToStrSize(fromPath: String, maxSize: Int): String +external fun chatResizeImageToStrSize(fromPath: String, maxSize: Long): String val chatModel: ChatModel get() = chatController.chatModel diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/helpers/Utils.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/helpers/Utils.kt index 39611361e3..76a06ed4a3 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/helpers/Utils.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/helpers/Utils.kt @@ -365,7 +365,10 @@ fun formatBytes(bytes: Long): String { } fun removeFile(fileName: String): Boolean { - val file = File(getAppFilePath(fileName)) + return removeFile(File(getAppFilePath(fileName))) +} + +fun removeFile(file: File): Boolean { val fileDeleted = file.delete() if (!fileDeleted) { Log.e(TAG, "Util.kt removeFile error") diff --git a/apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/platform/Images.desktop.kt b/apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/platform/Images.desktop.kt index 1d6ca8e922..7f1884e4f0 100644 --- a/apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/platform/Images.desktop.kt +++ b/apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/platform/Images.desktop.kt @@ -29,10 +29,8 @@ actual fun base64ToBitmap(base64ImageString: String): ImageBitmap { return try { ImageIO.read(ByteArrayInputStream(Base64.getMimeDecoder().decode(imageString))).toComposeImageBitmap() } catch (e: Exception) { // ByteArrayInputStream returns null - // } catch (e: IOException) { - Log.e(TAG, "base64ToBitmap error: $e for $base64ImageString") + Log.e(TAG, "base64ToBitmap error: $e for \"$base64ImageString\"") errorBitmap() - } } actual fun resizeImageToStrSize(image: ImageBitmap, maxDataSize: Long): String { @@ -42,8 +40,8 @@ actual fun resizeImageToStrSize(image: ImageBitmap, maxDataSize: Long): String { compressImageData(image, true).writeTo(output) output.flush() output.close() - var str = chatResizeImageToStrSize(tmpFileName, maxDataSize.toInt()) - removeFile(tmpFileName) + var str = chatResizeImageToStrSize(tmpFile.absolutePath, maxDataSize) + removeFile(tmpFile) return str } diff --git a/src/Simplex/Chat/Mobile.hs b/src/Simplex/Chat/Mobile.hs index d5cd7e9039..55d08b9e75 100644 --- a/src/Simplex/Chat/Mobile.hs +++ b/src/Simplex/Chat/Mobile.hs @@ -113,7 +113,7 @@ foreign export ccall "chat_encrypt_file" cChatEncryptFile :: StablePtr ChatContr foreign export ccall "chat_decrypt_file" cChatDecryptFile :: CString -> CString -> CString -> CString -> IO CString -foreign export ccall "chat_resize_image_to_str_size" cChatResizeImageToStrSize :: CString -> CInt -> IO CString +foreign export ccall "chat_resize_image_to_str_size" cChatResizeImageToStrSize :: CString -> CLong -> IO CString -- | check / migrate database and initialize chat controller on success cChatMigrateInit :: CString -> CString -> CString -> Ptr (StablePtr ChatController) -> IO CJSONString @@ -205,7 +205,7 @@ cChatJsonLength s = fromIntegral . subtract 2 . LB.length . J.encode . safeDecod -- Returns data-uri/base64 encoded image as 0-terminated string. -- Empty result string means operation failure. -- The caller must free the result ptr. -cChatResizeImageToStrSize :: CString -> CInt -> IO CString +cChatResizeImageToStrSize :: CString -> CLong -> IO CString cChatResizeImageToStrSize fp' maxSize = do fp <- peekCString fp' res <- runExceptT $ do