fix moving OTG files in the recycle bin

This commit is contained in:
tibbi 2019-11-04 23:36:08 +01:00
parent 556a775848
commit 04ecb6e0b4
2 changed files with 49 additions and 14 deletions

View file

@ -945,7 +945,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
private fun deleteConfirmed() { private fun deleteConfirmed() {
val path = getCurrentMedia().getOrNull(mPos)?.path ?: return val path = getCurrentMedia().getOrNull(mPos)?.path ?: return
if (File(path).isDirectory || !path.isMediaFile()) { if (getIsPathDirectory(path) || !path.isMediaFile()) {
return return
} }

View file

@ -228,13 +228,47 @@ fun BaseSimpleActivity.tryDeleteFileDirItem(fileDirItem: FileDirItem, allowDelet
fun BaseSimpleActivity.movePathsInRecycleBin(paths: ArrayList<String>, mediumDao: MediumDao = galleryDB.MediumDao(), callback: ((wasSuccess: Boolean) -> Unit)?) { fun BaseSimpleActivity.movePathsInRecycleBin(paths: ArrayList<String>, mediumDao: MediumDao = galleryDB.MediumDao(), callback: ((wasSuccess: Boolean) -> Unit)?) {
ensureBackgroundThread { ensureBackgroundThread {
var pathsCnt = paths.size var pathsCnt = paths.size
paths.forEach { val OTGPath = config.OTGPath
val file = File(it)
val internalFile = File(recycleBinPath, it) for (source in paths) {
if (OTGPath.isNotEmpty() && source.startsWith(OTGPath)) {
var inputStream: InputStream? = null
var out: OutputStream? = null
try {
val destination = "$recycleBinPath/$source"
val fileDocument = getSomeDocumentFile(source)
inputStream = applicationContext.contentResolver.openInputStream(fileDocument?.uri)
out = getFileOutputStreamSync(destination, source.getMimeType())
var copiedSize = 0L
val buffer = ByteArray(DEFAULT_BUFFER_SIZE)
var bytes = inputStream.read(buffer)
while (bytes >= 0) {
out!!.write(buffer, 0, bytes)
copiedSize += bytes
bytes = inputStream.read(buffer)
}
out?.flush()
if (fileDocument?.getItemSize(false) == copiedSize && getDoesFilePathExist(destination)) {
mediumDao.updateDeleted("$RECYCLE_BIN$source", System.currentTimeMillis(), source)
pathsCnt--
}
} catch (e: Exception) {
showErrorToast(e)
return@ensureBackgroundThread
} finally {
inputStream?.close()
out?.close()
}
} else {
val file = File(source)
val internalFile = File(recycleBinPath, source)
val lastModified = file.lastModified() val lastModified = file.lastModified()
try { try {
if (file.copyRecursively(internalFile, true)) { if (file.copyRecursively(internalFile, true)) {
mediumDao.updateDeleted("$RECYCLE_BIN$it", System.currentTimeMillis(), it) mediumDao.updateDeleted("$RECYCLE_BIN$source", System.currentTimeMillis(), source)
pathsCnt-- pathsCnt--
if (config.keepLastModified) { if (config.keepLastModified) {
@ -243,7 +277,8 @@ fun BaseSimpleActivity.movePathsInRecycleBin(paths: ArrayList<String>, mediumDao
} }
} catch (e: Exception) { } catch (e: Exception) {
showErrorToast(e) showErrorToast(e)
return@forEach return@ensureBackgroundThread
}
} }
} }
callback?.invoke(pathsCnt == 0) callback?.invoke(pathsCnt == 0)