diff --git a/tools/chatgui-android/app/src/main/java/com/utun/chat/viewmodel/ChatViewModel.kt b/tools/chatgui-android/app/src/main/java/com/utun/chat/viewmodel/ChatViewModel.kt index eabc8dd8..da1d52c3 100644 --- a/tools/chatgui-android/app/src/main/java/com/utun/chat/viewmodel/ChatViewModel.kt +++ b/tools/chatgui-android/app/src/main/java/com/utun/chat/viewmodel/ChatViewModel.kt @@ -1130,16 +1130,29 @@ class ChatViewModel : ViewModel() { } fun copyUriToCache(context: Context, uri: Uri, fileName: String): String? { + var cacheFile: File? = null try { - val cacheDir = File(context.cacheDir, "attachments") - cacheDir.mkdirs() - val cacheFile = File(cacheDir, fileName) + val name = fileName.substringAfterLast('/').substringAfterLast('\\') + require(name.isNotBlank() && name != "." && name != "..") { "Неверное имя файла" } + val cacheDir = File(context.cacheDir, "attachments/${java.util.UUID.randomUUID()}") + check(cacheDir.mkdirs()) { "Не удалось создать каталог вложения" } + val outputFile = File(cacheDir, name) + cacheFile = outputFile context.contentResolver.openInputStream(uri)?.use { input -> - FileOutputStream(cacheFile).use { output -> input.copyTo(output) } - } ?: return null - LogManager.addLog("INFO", "VM", "copied attachment to cache: ${cacheFile.absolutePath}") - return cacheFile.absolutePath + FileOutputStream(outputFile).use { output -> + val buffer = ByteArray(65536); var size = 0L + while (true) { + val n = input.read(buffer) + if (n < 0) break + size += n; check(size <= 1024L * 1024 * 1024) { "Файл больше 1 ГиБ" } + output.write(buffer, 0, n) + } + } + } ?: throw IllegalStateException("Не удалось открыть вложение") + LogManager.addLog("INFO", "VM", "copied attachment to cache: ${outputFile.absolutePath}") + return outputFile.absolutePath } catch (e: Exception) { + cacheFile?.delete(); cacheFile?.parentFile?.delete() LogManager.addLog("ERROR", "VM", "copyUriToCache failed: ${e.message}") return null } diff --git a/tools/chatgui-android/libutun_lite/voice_recorder.c b/tools/chatgui-android/libutun_lite/voice_recorder.c index 2c46257a..90258914 100644 --- a/tools/chatgui-android/libutun_lite/voice_recorder.c +++ b/tools/chatgui-android/libutun_lite/voice_recorder.c @@ -273,7 +273,7 @@ void voice_recorder_set_preset(int preset) { if (g_rec) { pthread_mutex_lock(&g_rec->mtx); g_rec->preset = preset; - DEBUG_INFO(DEBUG_CATEGORY_GENERAL, "voice_recorder_set_preset: %d (%d kbps)", preset, s_bitrates[preset] / 1000); + DEBUG_INFO(DEBUG_CATEGORY_GENERAL, "voice_recorder_set_preset: %d", preset); pthread_mutex_unlock(&g_rec->mtx); } pthread_mutex_unlock(&g_init_mtx);