Skip to content

Commit

Permalink
Merge pull request #1441 from Infomaniak/fix-sync-with-cloudstorage
Browse files Browse the repository at this point in the history
fix: Syncing a file from an external app via cloud storage doesn't work
  • Loading branch information
tevincent authored Oct 1, 2024
2 parents 4bc9a40 + 06c88ed commit f4fc68e
Showing 1 changed file with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -258,17 +258,28 @@ class CloudStorageProvider : DocumentsProvider() {
SentryLog.d(TAG, "openDocument(), id=$documentId, mode=$mode, signalIsCancelled: ${signal?.isCanceled}")
val context = context ?: return null

fun getRemoteFile(localFile: File?, fileId: Int, driveId: Int): File? {
val okHttpClient = runBlocking { AccountUtils.getHttpClient(getUserId(documentId).toInt()) }
return ApiRepository.getFileDetails(localFile ?: File(id = fileId, driveId = driveId), okHttpClient).data
}

val isWrite = mode.indexOf('w') != -1
val accessMode = ParcelFileDescriptor.parseMode(mode)
val fileId = getFileIdFromDocumentId(documentId)
val userDrive = createUserDrive(documentId)
val file = FileController.getFileById(fileId, userDrive)
val localFile = FileController.getFileById(fileId, userDrive)

return file?.let {
val updatedFile = runCatching {
getRemoteFile(localFile, fileId, userDrive.driveId)
}.getOrElse {
localFile
}

return updatedFile?.let {
if (isWrite) {
writeDataFile(context, file, userDrive, accessMode)
writeDataFile(context, updatedFile, userDrive, accessMode)
} else {
getDataFile(context, file, userDrive, accessMode)
getDataFile(context, updatedFile, userDrive, accessMode)
}
}
}
Expand Down Expand Up @@ -552,6 +563,7 @@ class CloudStorageProvider : DocumentsProvider() {

private fun writeDataFile(context: Context, file: File, userDrive: UserDrive, accessMode: Int): ParcelFileDescriptor? {
val tempFile = createTempFile(file.parentId, file.name)
val cacheFile = file.getCacheFile(context, userDrive)
val handler = Handler(context.mainLooper)

return ParcelFileDescriptor.open(tempFile, accessMode, handler) { exception: IOException? ->
Expand All @@ -568,6 +580,7 @@ class CloudStorageProvider : DocumentsProvider() {
userId = userDrive.userId,
).store()
context.syncImmediately()
cacheFile.delete() // Delete old cache
}
} else {
exception.printStackTrace()
Expand Down

0 comments on commit f4fc68e

Please sign in to comment.