#295 fix crash when opening a file using third party app and resuming

Crash or that the file is no longer available only occurs under certain circumstances
This commit is contained in:
Julian Raufelder 2021-04-14 14:02:03 +02:00
parent 7f93b019c4
commit 99408e53fd
No known key found for this signature in database
GPG Key ID: 17EE71F6634E381D
2 changed files with 16 additions and 14 deletions

View File

@ -850,8 +850,7 @@ class BrowseFilesPresenter @Inject constructor( //
private fun createNewDocumentUri(parentUri: Uri, fileName: String): Uri { private fun createNewDocumentUri(parentUri: Uri, fileName: String): Uri {
val mimeType = mimeTypes.fromFilename(fileName) // val mimeType = mimeTypes.fromFilename(fileName) //
.orElse(MimeType.APPLICATION_OCTET_STREAM) .orElse(MimeType.APPLICATION_OCTET_STREAM)
val newDocumentUri: Uri? return try {
newDocumentUri = try {
DocumentsContract.createDocument( // DocumentsContract.createDocument( //
context().contentResolver, // context().contentResolver, //
parentUri, // parentUri, //
@ -859,11 +858,7 @@ class BrowseFilesPresenter @Inject constructor( //
fileName) fileName)
} catch (e: FileNotFoundException) { } catch (e: FileNotFoundException) {
throw NoSuchCloudFileException(fileName) throw NoSuchCloudFileException(fileName)
} } ?: throw IllegalFileNameException()
if (newDocumentUri == null) {
throw IllegalFileNameException()
}
return newDocumentUri
} }
@Callback @Callback
@ -1093,7 +1088,7 @@ class BrowseFilesPresenter @Inject constructor( //
fun openFileFinished() { fun openFileFinished() {
try { try {
// necessary see https://gitlab.skymatic.de/cryptomator/android/-/issues/569 // necessary see https://community.cryptomator.org/t/android-tabelle-nach-upload-unlesbar/6550
Thread.sleep(500) Thread.sleep(500)
} catch (e: InterruptedException) { } catch (e: InterruptedException) {
Timber.tag("BrowseFilesPresenter").e(e, "Failed to sleep after resuming editing, necessary for google office apps") Timber.tag("BrowseFilesPresenter").e(e, "Failed to sleep after resuming editing, necessary for google office apps")
@ -1107,6 +1102,7 @@ class BrowseFilesPresenter @Inject constructor( //
context().revokeUriPermission(uriToOpenedFile, Intent.FLAG_GRANT_WRITE_URI_PERMISSION or Intent.FLAG_GRANT_READ_URI_PERMISSION) context().revokeUriPermission(uriToOpenedFile, Intent.FLAG_GRANT_WRITE_URI_PERMISSION or Intent.FLAG_GRANT_READ_URI_PERMISSION)
uriToOpenedFile?.let { uriToOpenedFile?.let {
try {
val hashAfterEdit = calculateDigestFromUri(it) val hashAfterEdit = calculateDigestFromUri(it)
if (hashAfterEdit.isPresent && openedCloudFileMd5.isPresent // if (hashAfterEdit.isPresent && openedCloudFileMd5.isPresent //
&& Arrays.equals(hashAfterEdit.get(), openedCloudFileMd5.get())) { && Arrays.equals(hashAfterEdit.get(), openedCloudFileMd5.get())) {
@ -1114,6 +1110,10 @@ class BrowseFilesPresenter @Inject constructor( //
} else { } else {
uploadChangedFile() uploadChangedFile()
} }
} catch (e: FileNotFoundException) {
Timber.tag("BrowseFilesPresenter").e(e, "Failed to read back changes, file isn't present anymore")
Toast.makeText(context(), R.string.error_file_not_found_after_opening_using_3party, Toast.LENGTH_LONG).show()
}
} }
} }
@ -1159,6 +1159,7 @@ class BrowseFilesPresenter @Inject constructor( //
openWritableFileNotification.ifPresent { obj: OpenWritableFileNotification -> obj.hide() } openWritableFileNotification.ifPresent { obj: OpenWritableFileNotification -> obj.hide() }
} }
@Throws(FileNotFoundException::class)
private fun calculateDigestFromUri(uri: Uri): Optional<ByteArray> { private fun calculateDigestFromUri(uri: Uri): Optional<ByteArray> {
val digest = MessageDigest.getInstance("MD5") val digest = MessageDigest.getInstance("MD5")
DigestInputStream(context().contentResolver.openInputStream(uri), digest).use { dis -> DigestInputStream(context().contentResolver.openInputStream(uri), digest).use { dis ->

View File

@ -32,6 +32,7 @@
<string name="error_failed_to_decrypt_webdav_password">Failed to decrypt WebDAV password, please re add in settings</string> <string name="error_failed_to_decrypt_webdav_password">Failed to decrypt WebDAV password, please re add in settings</string>
<string name="error_play_services_not_available">Play Services not installed</string> <string name="error_play_services_not_available">Play Services not installed</string>
<string name="error_biometric_auth_aborted">Biometric authentication aborted</string> <string name="error_biometric_auth_aborted">Biometric authentication aborted</string>
<string name="error_file_not_found_after_opening_using_3party">Local file isn\'t present anymore after switching back to Cryptomator. Possible changes cannot be propagated back to the cloud.</string>
<!-- # clouds --> <!-- # clouds -->