renaming fixes

This commit is contained in:
Thibault Deckers 2022-03-24 11:46:04 +09:00
parent 8ee08a881a
commit d1fdac46ca
2 changed files with 26 additions and 11 deletions

View file

@ -142,7 +142,7 @@ abstract class ImageProvider {
var desiredNameWithoutExtension = if (sourceEntry.path != null) { var desiredNameWithoutExtension = if (sourceEntry.path != null) {
val sourceFileName = File(sourceEntry.path).name val sourceFileName = File(sourceEntry.path).name
sourceFileName.replaceFirst(FILE_EXTENSION_PATTERN, "") sourceFileName.substringBeforeLast(".")
} else { } else {
sourceUri.lastPathSegment!! sourceUri.lastPathSegment!!
} }
@ -963,8 +963,6 @@ abstract class ImageProvider {
companion object { companion object {
private val LOG_TAG = LogUtils.createTag<ImageProvider>() private val LOG_TAG = LogUtils.createTag<ImageProvider>()
val FILE_EXTENSION_PATTERN = Regex("[.][^.]+$")
val supportedExportMimeTypes = listOf(MimeTypes.BMP, MimeTypes.JPEG, MimeTypes.PNG, MimeTypes.WEBP) val supportedExportMimeTypes = listOf(MimeTypes.BMP, MimeTypes.JPEG, MimeTypes.PNG, MimeTypes.WEBP)
// used when skipping a move/creation op because the target file already exists // used when skipping a move/creation op because the target file already exists

View file

@ -487,7 +487,7 @@ class MediaStoreImageProvider : ImageProvider() {
return skippedFieldMap return skippedFieldMap
} }
val desiredNameWithoutExtension = desiredName.replaceFirst(FILE_EXTENSION_PATTERN, "") val desiredNameWithoutExtension = desiredName.substringBeforeLast(".")
val targetNameWithoutExtension = resolveTargetFileNameWithoutExtension( val targetNameWithoutExtension = resolveTargetFileNameWithoutExtension(
activity = activity, activity = activity,
dir = targetDir, dir = targetDir,
@ -591,7 +591,7 @@ class MediaStoreImageProvider : ImageProvider() {
) { ) {
for (kv in entriesToNewName) { for (kv in entriesToNewName) {
val entry = kv.key val entry = kv.key
val newFileName = kv.value val desiredName = kv.value
val sourceUri = entry.uri val sourceUri = entry.uri
val sourcePath = entry.path 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 // 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 { try {
val newFields = if (isCancelledOp()) skippedFieldMap else renameSingle( val newFields = if (isCancelledOp()) skippedFieldMap else renameSingle(
activity = activity, activity = activity,
mimeType = mimeType, mimeType = mimeType,
oldMediaUri = sourceUri, oldMediaUri = sourceUri,
oldPath = sourcePath, oldPath = sourcePath,
newFileName = newFileName, desiredName = desiredName,
) )
result["newFields"] = newFields result["newFields"] = newFields
result["success"] = true result["success"] = true
} catch (e: Exception) { } 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) callback.onSuccess(result)
@ -627,10 +627,24 @@ class MediaStoreImageProvider : ImageProvider() {
mimeType: String, mimeType: String,
oldMediaUri: Uri, oldMediaUri: Uri,
oldPath: String, oldPath: String,
newFileName: String, desiredName: String,
): FieldMap { ): FieldMap {
val desiredNameWithoutExtension = desiredName.substringBeforeLast(".")
val oldFile = File(oldPath) 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 { return when {
oldFile == newFile -> skippedFieldMap oldFile == newFile -> skippedFieldMap
StorageUtils.canEditByFile(activity, oldPath) -> renameSingleByFile(activity, mimeType, oldMediaUri, oldPath, newFile) StorageUtils.canEditByFile(activity, oldPath) -> renameSingleByFile(activity, mimeType, oldMediaUri, oldPath, newFile)
@ -682,8 +696,11 @@ class MediaStoreImageProvider : ImageProvider() {
newFile: File newFile: File
): FieldMap { ): FieldMap {
Log.d(LOG_TAG, "rename document at uri=$oldMediaUri path=$oldPath") 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") @Suppress("BlockingMethodInNonBlockingContext")
val renamed = StorageUtils.getDocumentFile(activity, oldPath, oldMediaUri)?.renameTo(newFile.name) ?: false val renamed = df.renameTo(newFile.name)
if (!renamed) { if (!renamed) {
throw Exception("failed to rename document at path=$oldPath") throw Exception("failed to rename document at path=$oldPath")
} }