mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-31 23:03:54 +01:00
Modified findUniqueFileName() in UploadWorker.kt to use a random 3-digit alphanumeric hash to append to a file name to make it unique. This improves speed over using an incrementing number to append as there are fewer collisions.
This commit is contained in:
parent
c7065e103b
commit
b353e8b1dc
1 changed files with 25 additions and 24 deletions
|
|
@ -44,6 +44,7 @@ import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
import java.util.Date
|
import java.util.Date
|
||||||
|
import java.util.Random
|
||||||
import java.util.regex.Pattern
|
import java.util.regex.Pattern
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
|
@ -532,33 +533,33 @@ class UploadWorker(
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun findUniqueFileName(fileName: String): String {
|
private fun findUniqueFileName(fileName: String): String {
|
||||||
var sequenceFileName: String?
|
var sequenceFileName: String? = fileName
|
||||||
var sequenceNumber = 1
|
val chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
|
||||||
while (true) {
|
val random = Random()
|
||||||
|
|
||||||
|
// Loops until sequenceFileName does not match any existing file names
|
||||||
|
while (mediaClient
|
||||||
|
.checkPageExistsUsingTitle(
|
||||||
|
String.format(
|
||||||
|
"File:%s",
|
||||||
|
sequenceFileName,
|
||||||
|
),
|
||||||
|
).blockingGet()) {
|
||||||
|
|
||||||
|
// Generate a random 5-character alphanumeric string
|
||||||
|
val randomHash = (1..3)
|
||||||
|
.map { chars[random.nextInt(chars.length)] }
|
||||||
|
.joinToString("")
|
||||||
|
|
||||||
sequenceFileName =
|
sequenceFileName =
|
||||||
if (sequenceNumber == 1) {
|
if (fileName.indexOf('.') == -1) {
|
||||||
fileName
|
"$fileName #$randomHash"
|
||||||
} else {
|
} else {
|
||||||
if (fileName.indexOf('.') == -1) {
|
val regex =
|
||||||
"$fileName $sequenceNumber"
|
Pattern.compile("^(.*)(\\..+?)$")
|
||||||
} else {
|
val regexMatcher = regex.matcher(fileName)
|
||||||
val regex =
|
regexMatcher.replaceAll("$1 #$randomHash")
|
||||||
Pattern.compile("^(.*)(\\..+?)$")
|
|
||||||
val regexMatcher = regex.matcher(fileName)
|
|
||||||
regexMatcher.replaceAll("$1 $sequenceNumber$2")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (!mediaClient
|
|
||||||
.checkPageExistsUsingTitle(
|
|
||||||
String.format(
|
|
||||||
"File:%s",
|
|
||||||
sequenceFileName,
|
|
||||||
),
|
|
||||||
).blockingGet()
|
|
||||||
) {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
sequenceNumber++
|
|
||||||
}
|
}
|
||||||
return sequenceFileName!!
|
return sequenceFileName!!
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue