Fixed encrypted file export to external app
This commit is contained in:
parent
66f86c278e
commit
12dcb10485
4 changed files with 48 additions and 5 deletions
|
@ -199,7 +199,8 @@ class ChatMessageContentData(
|
||||||
downloadLabel.value = spannable
|
downloadLabel.value = spannable
|
||||||
|
|
||||||
if (content.isFile || (content.isFileTransfer && chatMessage.isOutgoing)) {
|
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()
|
downloadable.value = content.filePath.orEmpty().isEmpty()
|
||||||
|
|
||||||
if (path.isNotEmpty()) {
|
if (path.isNotEmpty()) {
|
||||||
|
|
|
@ -298,7 +298,7 @@ class DetailChatRoomFragment : MasterFragment<ChatRoomDetailFragmentBinding, Cha
|
||||||
else -> {
|
else -> {
|
||||||
if (content.isFileEncrypted) {
|
if (content.isFileEncrypted) {
|
||||||
Log.w("[Chat Message] File is encrypted and can't be opened in one of our viewers...")
|
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)) {
|
} else if (!FileUtils.openFileInThirdPartyApp(requireActivity(), path)) {
|
||||||
showDialogToSuggestOpeningFileAsText()
|
showDialogToSuggestOpeningFileAsText()
|
||||||
}
|
}
|
||||||
|
@ -675,7 +675,7 @@ class DetailChatRoomFragment : MasterFragment<ChatRoomDetailFragmentBinding, Cha
|
||||||
dialog.show()
|
dialog.show()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun showDialogForUserConsentBeforeExportingFileInThirdPartyApp(path: String) {
|
private fun showDialogForUserConsentBeforeExportingFileInThirdPartyApp(content: Content) {
|
||||||
val dialogViewModel = DialogViewModel(
|
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_message),
|
||||||
getString(R.string.chat_message_cant_open_file_in_app_dialog_title)
|
getString(R.string.chat_message_cant_open_file_in_app_dialog_title)
|
||||||
|
@ -684,9 +684,17 @@ class DetailChatRoomFragment : MasterFragment<ChatRoomDetailFragmentBinding, Cha
|
||||||
|
|
||||||
dialogViewModel.showDeleteButton({
|
dialogViewModel.showDeleteButton({
|
||||||
dialog.dismiss()
|
dialog.dismiss()
|
||||||
if (!FileUtils.openFileInThirdPartyApp(requireActivity(), path)) {
|
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()
|
showDialogToSuggestOpeningFileAsText()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}, getString(R.string.chat_message_cant_open_file_in_app_dialog_export_button))
|
}, getString(R.string.chat_message_cant_open_file_in_app_dialog_export_button))
|
||||||
|
|
||||||
dialogViewModel.showOkButton({
|
dialogViewModel.showOkButton({
|
||||||
|
|
|
@ -118,6 +118,24 @@ class FileUtils {
|
||||||
return returnPath
|
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 {
|
fun getFileStoragePath(fileName: String): File {
|
||||||
val path = getFileStorageDir(isExtensionImage(fileName))
|
val path = getFileStorageDir(isExtensionImage(fileName))
|
||||||
var file = File(path, fileName)
|
var file = File(path, fileName)
|
||||||
|
@ -251,6 +269,21 @@ class FileUtils {
|
||||||
return false
|
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 {
|
private fun createFile(file: String): File {
|
||||||
var fileName = file
|
var fileName = file
|
||||||
|
|
||||||
|
|
|
@ -4,4 +4,5 @@
|
||||||
<external-files-path name="pictures" path="Pictures/" />
|
<external-files-path name="pictures" path="Pictures/" />
|
||||||
<external-files-path name="downloads" path="Download/" />
|
<external-files-path name="downloads" path="Download/" />
|
||||||
<external-files-path name="files" path="." />
|
<external-files-path name="files" path="." />
|
||||||
|
<cache-path name="cache" path="."/>
|
||||||
</paths>
|
</paths>
|
Loading…
Reference in a new issue