From d1fdac46ca12046ba5e529dd5bf86940342ab20a Mon Sep 17 00:00:00 2001 From: Thibault Deckers Date: Thu, 24 Mar 2022 11:46:04 +0900 Subject: [PATCH] renaming fixes --- .../aves/model/provider/ImageProvider.kt | 4 +-- .../model/provider/MediaStoreImageProvider.kt | 33 ++++++++++++++----- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/android/app/src/main/kotlin/deckers/thibault/aves/model/provider/ImageProvider.kt b/android/app/src/main/kotlin/deckers/thibault/aves/model/provider/ImageProvider.kt index fcb3247b9..3484999f7 100644 --- a/android/app/src/main/kotlin/deckers/thibault/aves/model/provider/ImageProvider.kt +++ b/android/app/src/main/kotlin/deckers/thibault/aves/model/provider/ImageProvider.kt @@ -142,7 +142,7 @@ abstract class ImageProvider { var desiredNameWithoutExtension = if (sourceEntry.path != null) { val sourceFileName = File(sourceEntry.path).name - sourceFileName.replaceFirst(FILE_EXTENSION_PATTERN, "") + sourceFileName.substringBeforeLast(".") } else { sourceUri.lastPathSegment!! } @@ -963,8 +963,6 @@ abstract class ImageProvider { companion object { private val LOG_TAG = LogUtils.createTag() - val FILE_EXTENSION_PATTERN = Regex("[.][^.]+$") - val supportedExportMimeTypes = listOf(MimeTypes.BMP, MimeTypes.JPEG, MimeTypes.PNG, MimeTypes.WEBP) // used when skipping a move/creation op because the target file already exists diff --git a/android/app/src/main/kotlin/deckers/thibault/aves/model/provider/MediaStoreImageProvider.kt b/android/app/src/main/kotlin/deckers/thibault/aves/model/provider/MediaStoreImageProvider.kt index 0d5f1f643..f6eeb0242 100644 --- a/android/app/src/main/kotlin/deckers/thibault/aves/model/provider/MediaStoreImageProvider.kt +++ b/android/app/src/main/kotlin/deckers/thibault/aves/model/provider/MediaStoreImageProvider.kt @@ -487,7 +487,7 @@ class MediaStoreImageProvider : ImageProvider() { return skippedFieldMap } - val desiredNameWithoutExtension = desiredName.replaceFirst(FILE_EXTENSION_PATTERN, "") + val desiredNameWithoutExtension = desiredName.substringBeforeLast(".") val targetNameWithoutExtension = resolveTargetFileNameWithoutExtension( activity = activity, dir = targetDir, @@ -591,7 +591,7 @@ class MediaStoreImageProvider : ImageProvider() { ) { for (kv in entriesToNewName) { val entry = kv.key - val newFileName = kv.value + val desiredName = kv.value val sourceUri = entry.uri val sourcePath = entry.path @@ -603,19 +603,19 @@ class MediaStoreImageProvider : ImageProvider() { ) // prevent naming with a `.` prefix as it would hide the file and remove it from the Media Store - if (sourcePath != null && !newFileName.startsWith('.')) { + if (sourcePath != null && !desiredName.startsWith('.')) { try { val newFields = if (isCancelledOp()) skippedFieldMap else renameSingle( activity = activity, mimeType = mimeType, oldMediaUri = sourceUri, oldPath = sourcePath, - newFileName = newFileName, + desiredName = desiredName, ) result["newFields"] = newFields result["success"] = true } catch (e: Exception) { - Log.w(LOG_TAG, "failed to rename to newFileName=$newFileName entry with sourcePath=$sourcePath", e) + Log.w(LOG_TAG, "failed to rename to newFileName=$desiredName entry with sourcePath=$sourcePath", e) } } callback.onSuccess(result) @@ -627,10 +627,24 @@ class MediaStoreImageProvider : ImageProvider() { mimeType: String, oldMediaUri: Uri, oldPath: String, - newFileName: String, + desiredName: String, ): FieldMap { + val desiredNameWithoutExtension = desiredName.substringBeforeLast(".") + val oldFile = File(oldPath) - val newFile = File(oldFile.parent, newFileName) + if (oldFile.nameWithoutExtension == desiredNameWithoutExtension) return skippedFieldMap + + val dir = oldFile.parent ?: return skippedFieldMap + val targetNameWithoutExtension = resolveTargetFileNameWithoutExtension( + activity = activity, + dir = dir, + desiredNameWithoutExtension = desiredNameWithoutExtension, + mimeType = mimeType, + conflictStrategy = NameConflictStrategy.RENAME, + ) ?: return skippedFieldMap + val targetFileName = "$targetNameWithoutExtension${extensionFor(mimeType)}" + + val newFile = File(dir, targetFileName) return when { oldFile == newFile -> skippedFieldMap StorageUtils.canEditByFile(activity, oldPath) -> renameSingleByFile(activity, mimeType, oldMediaUri, oldPath, newFile) @@ -682,8 +696,11 @@ class MediaStoreImageProvider : ImageProvider() { newFile: File ): FieldMap { Log.d(LOG_TAG, "rename document at uri=$oldMediaUri path=$oldPath") + val df = StorageUtils.getDocumentFile(activity, oldPath, oldMediaUri) + df ?: throw Exception("failed to get document at path=$oldPath") + @Suppress("BlockingMethodInNonBlockingContext") - val renamed = StorageUtils.getDocumentFile(activity, oldPath, oldMediaUri)?.renameTo(newFile.name) ?: false + val renamed = df.renameTo(newFile.name) if (!renamed) { throw Exception("failed to rename document at path=$oldPath") }