fix resizeToStr

This commit is contained in:
IC Rainbow
2024-10-31 13:15:31 +02:00
parent a787447a2d
commit b7c77651bc
9 changed files with 18 additions and 16 deletions
+1 -1
View File
@@ -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
+1 -1
View File
@@ -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);
@@ -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
}
@@ -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);
@@ -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);
@@ -35,7 +35,7 @@ external fun chatWriteFile(ctrl: ChatCtrl, path: String, buffer: ByteBuffer): St
external fun chatReadFile(path: String, key: String, nonce: String): Array<Any>
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
@@ -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")
@@ -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
}
+2 -2
View File
@@ -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