fix #1946, properly handle overwriting edited files on an SD card

This commit is contained in:
tibbi 2020-10-28 18:47:18 +01:00
parent ba4ba6a338
commit 736416c26c

View file

@ -13,10 +13,12 @@ import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_STORAGE
import com.simplemobiletools.commons.helpers.REAL_FILE_PATH import com.simplemobiletools.commons.helpers.REAL_FILE_PATH
import com.simplemobiletools.commons.helpers.ensureBackgroundThread import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.commons.helpers.isNougatPlus import com.simplemobiletools.commons.helpers.isNougatPlus
import com.simplemobiletools.commons.models.FileDirItem
import com.simplemobiletools.gallery.pro.R import com.simplemobiletools.gallery.pro.R
import com.simplemobiletools.gallery.pro.dialogs.SaveAsDialog import com.simplemobiletools.gallery.pro.dialogs.SaveAsDialog
import com.simplemobiletools.gallery.pro.extensions.config import com.simplemobiletools.gallery.pro.extensions.config
import com.simplemobiletools.gallery.pro.extensions.fixDateTaken import com.simplemobiletools.gallery.pro.extensions.fixDateTaken
import com.simplemobiletools.gallery.pro.extensions.tryDeleteFileDirItem
import ly.img.android.pesdk.PhotoEditorSettingsList import ly.img.android.pesdk.PhotoEditorSettingsList
import ly.img.android.pesdk.assets.filter.basic.FilterPackBasic import ly.img.android.pesdk.assets.filter.basic.FilterPackBasic
import ly.img.android.pesdk.assets.font.basic.FontPackBasic import ly.img.android.pesdk.assets.font.basic.FontPackBasic
@ -134,42 +136,44 @@ class NewEditActivity : SimpleActivity() {
storeOldExif(source) storeOldExif(source)
sourceFileLastModified = File(source).lastModified() sourceFileLastModified = File(source).lastModified()
var inputStream: InputStream? = null handleFileOverwriting(destinationFilePath) {
var outputStream: OutputStream? = null var inputStream: InputStream? = null
try { var outputStream: OutputStream? = null
inputStream = contentResolver.openInputStream(Uri.parse(resultPath))
outputStream = getFileOutputStreamSync(destinationFilePath, destinationFilePath.getMimeType())
inputStream!!.copyTo(outputStream!!)
outputStream.flush()
inputStream.close()
outputStream.close()
try { try {
if (isNougatPlus()) { inputStream = contentResolver.openInputStream(Uri.parse(resultPath))
val newExif = ExifInterface(destinationFilePath) outputStream = getFileOutputStreamSync(destinationFilePath, destinationFilePath.getMimeType())
oldExif?.copyTo(newExif, false) inputStream!!.copyTo(outputStream!!)
outputStream.flush()
inputStream.close()
outputStream.close()
try {
if (isNougatPlus()) {
val newExif = ExifInterface(destinationFilePath)
oldExif?.copyTo(newExif, false)
}
} catch (ignored: Exception) {
} }
} catch (ignored: Exception) {
}
if (config.keepLastModified) { if (config.keepLastModified) {
// add 1 s to the last modified time to properly update the thumbnail // add 1 s to the last modified time to properly update the thumbnail
updateLastModified(destinationFilePath, sourceFileLastModified + 1000) updateLastModified(destinationFilePath, sourceFileLastModified + 1000)
} }
val paths = arrayListOf(destinationFilePath) val paths = arrayListOf(destinationFilePath)
rescanPaths(arrayListOf(destinationFilePath)) { rescanPaths(arrayListOf(destinationFilePath)) {
fixDateTaken(paths, false) fixDateTaken(paths, false)
} }
setResult(Activity.RESULT_OK, intent) setResult(Activity.RESULT_OK, intent)
toast(R.string.file_edited_successfully) toast(R.string.file_edited_successfully)
finish() finish()
} catch (e: Exception) { } catch (e: Exception) {
showErrorToast(e) showErrorToast(e)
} finally { } finally {
inputStream?.close() inputStream?.close()
outputStream?.close() outputStream?.close()
}
} }
} }
} else { } else {
@ -197,6 +201,23 @@ class NewEditActivity : SimpleActivity() {
} }
} }
// in case the user wants to overwrite the original file and it is on an SD card, delete it manually. Else the system just appends (1)
private fun handleFileOverwriting(path: String, callback: () -> Unit) {
if (getDoesFilePathExist(path) && isPathOnSD(path)) {
val fileDirItem = FileDirItem(path, path.getFilenameFromPath())
tryDeleteFileDirItem(fileDirItem, false, true) { success ->
if (success) {
callback()
} else {
toast(R.string.unknown_error_occurred)
finish()
}
}
} else {
callback()
}
}
private fun openEditor(inputImage: Uri) { private fun openEditor(inputImage: Uri) {
val settingsList = createPesdkSettingsList() val settingsList = createPesdkSettingsList()