Skip to content

Commit 82a4aa0

Browse files
Merge pull request #17270 from nextcloud/fix/permission-denial
fix(document-storage-provider): permission denial
2 parents d47c77e + fa8fbe2 commit 82a4aa0

2 files changed

Lines changed: 401 additions & 296 deletions

File tree

app/src/main/java/com/owncloud/android/operations/DownloadFileOperation.kt

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ class DownloadFileOperation(
120120
if (cancellationRequested.get()) return RemoteOperationResult(OperationCancelledException())
121121
}
122122

123+
if (file.isFolder) {
124+
Log_OC.e(TAG, "Cannot download a folder as a file: ${file.remotePath}")
125+
return RemoteOperationResult(RemoteOperationResult.ResultCode.CANNOT_CREATE_FILE)
126+
}
127+
123128
if (!FileStorageUtils.isValidExtFilename(file.fileName)) {
124129
mainThreadHandler.post { context.get()?.showToast(R.string.download_download_invalid_local_file_name) }
125130
return RemoteOperationResult(RemoteOperationResult.ResultCode.INVALID_CHARACTER_IN_NAME)
@@ -213,11 +218,21 @@ class DownloadFileOperation(
213218
)
214219
}
215220

221+
private fun ensureParentDirectory(target: File) {
222+
val parent = target.parentFile ?: return
223+
224+
if (parent.exists() && !parent.isDirectory && !parent.delete()) {
225+
Log_OC.e(TAG, "Unable to delete file blocking parent folder ${parent.absolutePath}")
226+
}
227+
228+
if (!parent.exists() && !parent.mkdirs()) {
229+
Log_OC.e(TAG, "Unable to create parent folder ${parent.absolutePath}")
230+
}
231+
}
232+
216233
private fun handleFileMove(tmpFile: File, currentResult: RemoteOperationResult<Unit>): RemoteOperationResult<Unit> {
217234
val newFile = File(savePath)
218-
newFile.parentFile?.takeIf { !it.exists() }?.also {
219-
if (!it.mkdirs()) Log_OC.e(TAG, "Unable to create parent folder ${it.absolutePath}")
220-
}
235+
ensureParentDirectory(newFile)
221236

222237
val tempFilePath = tmpFile.toPath()
223238
val newFilePath = newFile.toPath()

0 commit comments

Comments
 (0)