diff --git a/apps/ios/SimpleXChat/SimpleX.h b/apps/ios/SimpleXChat/SimpleX.h index 7a492c8c4e..8d9e25e02d 100644 --- a/apps/ios/SimpleXChat/SimpleX.h +++ b/apps/ios/SimpleXChat/SimpleX.h @@ -34,6 +34,9 @@ extern char *chat_resize_image_to_str_size(const char *path, int 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); +// chat_write_image returns null-terminated string with JSON of WriteFileResult +extern char *chat_write_image(chat_ctrl ctl, long maxSize, char *path, char *data, int len); + // chat_read_file returns a buffer with: // result status (1 byte), then if // status == 0 (success): buffer length (uint32, 4 bytes), buffer of specified length. 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 72d8b0e3a1..0c373e327c 100644 --- a/apps/multiplatform/common/src/commonMain/cpp/android/simplex-api.c +++ b/apps/multiplatform/common/src/commonMain/cpp/android/simplex-api.c @@ -68,6 +68,7 @@ extern char *chat_password_hash(const char *pwd, const char *salt); extern char *chat_valid_name(const char *name); extern int chat_json_length(const char *str); extern char *chat_write_file(chat_ctrl ctrl, const char *path, char *ptr, int length); +extern char *chat_write_image(chat_ctrl ctl, long maxSize, char *path, char *data, int len); 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); @@ -183,6 +184,16 @@ Java_chat_simplex_common_platform_CoreKt_chatWriteFile(JNIEnv *env, jclass clazz return res; } +JNIEXPORT jstring JNICALL +Java_chat_simplex_common_platform_CoreKt_chatWriteImage(JNIEnv *env, jclass clazz, jlong controller, jlong maxSize, jstring path, jobject buffer) { + const char *_path = encode_to_utf8_chars(env, path); + jbyte *buff = (jbyte *) (*env)->GetDirectBufferAddress(env, buffer); + jlong capacity = (*env)->GetDirectBufferCapacity(env, buffer); + jstring res = decode_to_utf8_string(env, chat_write_image((void*)controller, maxSize, _path, buff, capacity)); + (*env)->ReleaseStringUTFChars(env, path, _path); + return res; +} + JNIEXPORT jobjectArray JNICALL Java_chat_simplex_common_platform_CoreKt_chatReadFile(JNIEnv *env, jclass clazz, jstring path, jstring key, jstring nonce) { const char *_path = (*env)->GetStringUTFChars(env, path, JNI_FALSE); 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 0febec3d2e..d36ba58c0d 100644 --- a/apps/multiplatform/common/src/commonMain/cpp/desktop/simplex-api.c +++ b/apps/multiplatform/common/src/commonMain/cpp/desktop/simplex-api.c @@ -41,6 +41,7 @@ extern char *chat_password_hash(const char *pwd, const char *salt); extern char *chat_valid_name(const char *name); extern int chat_json_length(const char *str); extern char *chat_write_file(chat_ctrl ctrl, const char *path, char *ptr, int length); +extern char *chat_write_image(chat_ctrl ctrl, long max_size, const char *path, char *ptr, int length); 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); @@ -193,6 +194,16 @@ Java_chat_simplex_common_platform_CoreKt_chatWriteFile(JNIEnv *env, jclass clazz return res; } +JNIEXPORT jstring JNICALL +Java_chat_simplex_common_platform_CoreKt_chatWriteImage(JNIEnv *env, jclass clazz, jlong controller, jlong maxSize, jstring path, jobject buffer) { + const char *_path = encode_to_utf8_chars(env, path); + jbyte *buff = (jbyte *) (*env)->GetDirectBufferAddress(env, buffer); + jlong capacity = (*env)->GetDirectBufferCapacity(env, buffer); + jstring res = decode_to_utf8_string(env, chat_write_image((void*)controller, maxSize, _path, buff, capacity)); + (*env)->ReleaseStringUTFChars(env, path, _path); + return res; +} + JNIEXPORT jobjectArray JNICALL Java_chat_simplex_common_platform_CoreKt_chatReadFile(JNIEnv *env, jclass clazz, jstring path, jstring key, jstring nonce) { const char *_path = encode_to_utf8_chars(env, path); 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 236cf63345..1d6ca8e922 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 @@ -25,10 +25,12 @@ 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 { ImageIO.read(ByteArrayInputStream(Base64.getMimeDecoder().decode(imageString))).toComposeImageBitmap() - } catch (e: IOException) { - Log.e(TAG, "base64ToBitmap error: $e") + } catch (e: Exception) { // ByteArrayInputStream returns null + // } catch (e: IOException) { + Log.e(TAG, "base64ToBitmap error: $e for $base64ImageString") errorBitmap() } } diff --git a/flake.nix b/flake.nix index 45b4d2b0a7..1217812e55 100644 --- a/flake.nix +++ b/flake.nix @@ -387,6 +387,7 @@ "chat_valid_name" "chat_json_length" "chat_write_file" + "chat_write_image" "chat_resize_image_to_str_size" ]; postInstall = '' @@ -491,6 +492,7 @@ "chat_valid_name" "chat_json_length" "chat_write_file" + "chat_write_image" "chat_resize_image_to_str_size" ]; postInstall = '' diff --git a/libsimplex.dll.def b/libsimplex.dll.def index 75d89498ef..db510917d5 100644 --- a/libsimplex.dll.def +++ b/libsimplex.dll.def @@ -16,6 +16,7 @@ EXPORTS chat_encrypt_media chat_decrypt_media chat_write_file + chat_write_image chat_read_file chat_encrypt_file chat_decrypt_file