Fixed encrypted file export to external app

This commit is contained in:
Sylvain Berfini 2021-07-13 15:10:28 +02:00
parent 66f86c278e
commit 12dcb10485
4 changed files with 48 additions and 5 deletions

View file

@ -199,7 +199,8 @@ class ChatMessageContentData(
downloadLabel.value = spannable
if (content.isFile || (content.isFileTransfer && chatMessage.isOutgoing)) {
val path = if (content.isFileEncrypted) content.plainFilePath else content.filePath ?: ""
Log.i("[Content] Is content encrypted ? $isFileEncrypted")
val path = if (isFileEncrypted) content.plainFilePath else content.filePath ?: ""
downloadable.value = content.filePath.orEmpty().isEmpty()
if (path.isNotEmpty()) {

View file

@ -298,7 +298,7 @@ class DetailChatRoomFragment : MasterFragment<ChatRoomDetailFragmentBinding, Cha
else -> {
if (content.isFileEncrypted) {
Log.w("[Chat Message] File is encrypted and can't be opened in one of our viewers...")
showDialogForUserConsentBeforeExportingFileInThirdPartyApp(path)
showDialogForUserConsentBeforeExportingFileInThirdPartyApp(content)
} else if (!FileUtils.openFileInThirdPartyApp(requireActivity(), path)) {
showDialogToSuggestOpeningFileAsText()
}
@ -675,7 +675,7 @@ class DetailChatRoomFragment : MasterFragment<ChatRoomDetailFragmentBinding, Cha
dialog.show()
}
private fun showDialogForUserConsentBeforeExportingFileInThirdPartyApp(path: String) {
private fun showDialogForUserConsentBeforeExportingFileInThirdPartyApp(content: Content) {
val dialogViewModel = DialogViewModel(
getString(R.string.chat_message_cant_open_file_in_app_dialog_message),
getString(R.string.chat_message_cant_open_file_in_app_dialog_title)
@ -684,8 +684,16 @@ class DetailChatRoomFragment : MasterFragment<ChatRoomDetailFragmentBinding, Cha
dialogViewModel.showDeleteButton({
dialog.dismiss()
if (!FileUtils.openFileInThirdPartyApp(requireActivity(), path)) {
showDialogToSuggestOpeningFileAsText()
lifecycleScope.launch {
val plainFilePath = content.plainFilePath
Log.i("[Cht Room] Making a copy of [$plainFilePath] to the cache directory before exporting it")
val cacheCopyPath = FileUtils.copyFileToCache(plainFilePath)
if (cacheCopyPath != null) {
Log.i("[Cht Room] Cache copy has been made: $cacheCopyPath")
if (!FileUtils.openFileInThirdPartyApp(requireActivity(), cacheCopyPath)) {
showDialogToSuggestOpeningFileAsText()
}
}
}
}, getString(R.string.chat_message_cant_open_file_in_app_dialog_export_button))

View file

@ -118,6 +118,24 @@ class FileUtils {
return returnPath
}
private fun getFileStorageCacheDir(fileName: String): File {
val path = coreContext.context.cacheDir
Log.i("[File Utils] Cache directory is: $path")
val realFileName = if (fileName.endsWith(VFS_PLAIN_FILE_EXTENSION)) {
fileName.substring(0, fileName.length - VFS_PLAIN_FILE_EXTENSION.length)
} else fileName
var file = File(path, realFileName)
var prefix = 1
while (file.exists()) {
file = File(path, prefix.toString() + "_" + realFileName)
Log.w("[File Utils] File with that name already exists, renamed to ${file.name}")
prefix += 1
}
return file
}
fun getFileStoragePath(fileName: String): File {
val path = getFileStorageDir(isExtensionImage(fileName))
var file = File(path, fileName)
@ -251,6 +269,21 @@ class FileUtils {
return false
}
suspend fun copyFileToCache(plainFilePath: String): String? {
val cacheFile = getFileStorageCacheDir(getNameFromFilePath(plainFilePath))
try {
withContext(Dispatchers.IO) {
FileOutputStream(cacheFile).use { out ->
copyFileTo(plainFilePath, out)
}
}
return cacheFile.absolutePath
} catch (e: IOException) {
Log.e("[File Utils] copyFileToCache exception: $e")
}
return null
}
private fun createFile(file: String): File {
var fileName = file

View file

@ -4,4 +4,5 @@
<external-files-path name="pictures" path="Pictures/" />
<external-files-path name="downloads" path="Download/" />
<external-files-path name="files" path="." />
<cache-path name="cache" path="."/>
</paths>