Browse Source

Use bounded independent Android cache files for each attachment

master
evgeny 3 days ago
parent
commit
add016894b
  1. 27
      tools/chatgui-android/app/src/main/java/com/utun/chat/viewmodel/ChatViewModel.kt
  2. 2
      tools/chatgui-android/libutun_lite/voice_recorder.c

27
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
}

2
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);

Loading…
Cancel
Save