From 732e703d885835a9430bab46fea65d3e90ce1c30 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 19 Nov 2016 22:59:44 +0100 Subject: [PATCH] do not lowercase deleted path urls + some safety checks --- .../gallery/activities/MainActivity.kt | 6 +++++- .../gallery/activities/MediaActivity.kt | 13 +++++++++---- .../gallery/activities/ViewPagerActivity.kt | 2 +- .../gallery/adapters/DirectoryAdapter.kt | 2 +- .../gallery/adapters/MediaAdapter.kt | 2 +- .../gallery/asynctasks/GetDirectoriesAsynctask.kt | 2 +- .../gallery/asynctasks/GetMediaAsynctask.kt | 2 +- 7 files changed, 19 insertions(+), 10 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt index 1430d9166..2ca7086d4 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt @@ -204,7 +204,11 @@ class MainActivity : SimpleActivity(), GetDirectoriesAsynctask.GetDirectoriesLis private fun deleteItem(file: File) { if (needsStupidWritePermissions(file.absolutePath)) { if (!isShowingPermDialog(file)) { - getFileDocument(file.absolutePath, mConfig.treeUri).delete() + val document = getFileDocument(file.absolutePath, mConfig.treeUri) + + // double check we have the uri to the proper file path, not some parent folder + if (document.uri.toString().endsWith(file.absolutePath.getFilenameFromPath())) + document.delete() } } else { file.delete() diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt index dd7d1953c..370fa695a 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt @@ -231,12 +231,17 @@ class MediaActivity : SimpleActivity(), View.OnTouchListener, MediaAdapter.Media for (delPath in mToBeDeleted) { val file = File(delPath) if (file.exists() && file.isPhotoVideo()) { - if (needsStupidWritePermissions(delPath)) { + if (needsStupidWritePermissions(file.absolutePath)) { if (isShowingPermDialog(file)) return - if (getFileDocument(delPath, mConfig.treeUri).delete()) { - wereFilesDeleted = true + val document = getFileDocument(file.absolutePath, mConfig.treeUri) + + // double check we have the uri to the proper file path, not some parent folder + if (document.uri.toString().endsWith(file.absolutePath.getFilenameFromPath())) { + if (document.delete()) { + wereFilesDeleted = true + } } } else { if (file.delete()) @@ -251,8 +256,8 @@ class MediaActivity : SimpleActivity(), View.OnTouchListener, MediaAdapter.Media finish() } } - mToBeDeleted.clear() } + mToBeDeleted.clear() } private val undoDeletion = View.OnClickListener { diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt index 6df397d32..11015f699 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt @@ -343,7 +343,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View private fun deleteDirectoryIfEmpty() { val file = File(mDirectory) - if (file.isDirectory && file.listFiles().size == 0) { + if (file.isDirectory && file.listFiles().isEmpty()) { file.delete() } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt index ec4ef62e1..37b3451d3 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt @@ -187,7 +187,7 @@ class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList(selections.size) - selections.forEach { paths.add(dirs[it].path.toLowerCase()) } + selections.forEach { paths.add(dirs[it].path) } listener?.prepareForDeleting(paths) } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/MediaAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/MediaAdapter.kt index d0b7d4953..db68485f5 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/MediaAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/MediaAdapter.kt @@ -137,7 +137,7 @@ class MediaAdapter(val activity: SimpleActivity, val media: MutableList, private fun prepareForDeleting() { val selections = multiSelector.selectedPositions val paths = ArrayList(selections.size) - selections.forEach { paths.add(media[it].path.toLowerCase()) } + selections.forEach { paths.add(media[it].path) } listener?.prepareForDeleting(paths) } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt index 6f3b5fda2..a22c56687 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt @@ -65,7 +65,7 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va val newImageCnt = directory.mediaCnt + 1 directory.mediaCnt = newImageCnt directory.addSize(file.length()) - } else if (!mToBeDeleted.contains(parentDir.toLowerCase())) { + } else if (!mToBeDeleted.contains(parentDir)) { var dirName = context.getHumanizedFilename(parentDir) if (mConfig.getIsFolderHidden(parentDir)) { dirName += " ${context.resources.getString(R.string.hidden)}" diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetMediaAsynctask.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetMediaAsynctask.kt index 6636ef664..01e9f6336 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetMediaAsynctask.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetMediaAsynctask.kt @@ -51,7 +51,7 @@ class GetMediaAsynctask(val context: Context, val mPath: String, val isPickVideo do { val curPath = cursor.getString(pathIndex) ?: continue - if (curPath.matches(pattern.toRegex()) && !mToBeDeleted.contains(curPath.toLowerCase())) { + if (curPath.matches(pattern.toRegex()) && !mToBeDeleted.contains(curPath)) { val file = File(curPath) if (file.exists()) { val dateIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATE_MODIFIED)