Browse Source

Fix: do not delete the source file when copying failed

cooperku_kdanmobile 5 years ago
parent
commit
eee0749ac1

+ 8 - 0
src/main/java/com/kdanmobile/reader/copyfile/CopyFileViewModel.kt

@@ -11,6 +11,7 @@ import io.reactivex.android.schedulers.AndroidSchedulers
 import io.reactivex.disposables.Disposable
 import io.reactivex.disposables.Disposable
 import io.reactivex.schedulers.Schedulers
 import io.reactivex.schedulers.Schedulers
 import java.io.File
 import java.io.File
+import java.lang.Exception
 
 
 class CopyFileViewModel(applicationContext: Context,
 class CopyFileViewModel(applicationContext: Context,
                         private val info: CopyFileInfo,
                         private val info: CopyFileInfo,
@@ -28,6 +29,7 @@ class CopyFileViewModel(applicationContext: Context,
     private val mCopyProgressLiveData = MutableLiveData<Int>().apply { value = 0 }
     private val mCopyProgressLiveData = MutableLiveData<Int>().apply { value = 0 }
 
 
     private var copyFileDisposable: Disposable? = null
     private var copyFileDisposable: Disposable? = null
+    private var srcFilePath: String? = null
     private var dstFilePath: String? = null
     private var dstFilePath: String? = null
     private var hasCompleteCopy = false
     private var hasCompleteCopy = false
 
 
@@ -46,6 +48,7 @@ class CopyFileViewModel(applicationContext: Context,
             } else {
             } else {
                 null
                 null
             }
             }
+            srcFilePath = source?.getFilePath()
             val fullFileSize = Math.max(1, source?.getFileSize() ?: 1)
             val fullFileSize = Math.max(1, source?.getFileSize() ?: 1)
             val task = CopyFileTask()
             val task = CopyFileTask()
             task.setCopyMode(CopyFileTask.Mode.CREATE_NEW_FILE)
             task.setCopyMode(CopyFileTask.Mode.CREATE_NEW_FILE)
@@ -84,6 +87,11 @@ class CopyFileViewModel(applicationContext: Context,
 
 
     private fun deleteDstFile() {
     private fun deleteDstFile() {
         dstFilePath?.apply {
         dstFilePath?.apply {
+            //  do not delete the destination file if it is also the source file
+            try {
+                if (!srcFilePath.isNullOrEmpty() && File(srcFilePath).canonicalPath == File(dstFilePath).canonicalPath) return
+            } catch (e: Exception) {}
+
             val file = File(this)
             val file = File(this)
             if (file.exists()) {
             if (file.exists()) {
                 file.delete()
                 file.delete()