mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 12:23:58 +01:00
Improve Unique File Name Search (#5877)
* 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. * Modified findUniqueFileName() in UploadWorker.kt to use a random 5-digit numeric hash rather than the previous 3-digit alphanumeric hash * Removed unnecessary variable "chars" --------- Co-authored-by: Jinniu Du <127721018+Donutcheese@users.noreply.github.com> Co-authored-by: Zihan Pan <u7726755@anu.edu.au> Co-authored-by: Nicolas Raoul <nicolas.raoul@gmail.com>
This commit is contained in:
parent
634bc3ede1
commit
183e84c098
1 changed files with 22 additions and 24 deletions
|
|
@ -47,6 +47,7 @@ import kotlinx.coroutines.launch
|
|||
import kotlinx.coroutines.withContext
|
||||
import timber.log.Timber
|
||||
import java.util.Date
|
||||
import java.util.Random
|
||||
import java.util.regex.Pattern
|
||||
import javax.inject.Inject
|
||||
|
||||
|
|
@ -548,33 +549,30 @@ class UploadWorker(
|
|||
}
|
||||
|
||||
private fun findUniqueFileName(fileName: String): String {
|
||||
var sequenceFileName: String?
|
||||
var sequenceNumber = 1
|
||||
while (true) {
|
||||
var sequenceFileName: String? = fileName
|
||||
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 = (random.nextInt(90000) + 10000).toString()
|
||||
|
||||
sequenceFileName =
|
||||
if (sequenceNumber == 1) {
|
||||
fileName
|
||||
if (fileName.indexOf('.') == -1) {
|
||||
"$fileName #$randomHash"
|
||||
} else {
|
||||
if (fileName.indexOf('.') == -1) {
|
||||
"$fileName $sequenceNumber"
|
||||
} else {
|
||||
val regex =
|
||||
Pattern.compile("^(.*)(\\..+?)$")
|
||||
val regexMatcher = regex.matcher(fileName)
|
||||
regexMatcher.replaceAll("$1 $sequenceNumber$2")
|
||||
}
|
||||
val regex =
|
||||
Pattern.compile("^(.*)(\\..+?)$")
|
||||
val regexMatcher = regex.matcher(fileName)
|
||||
regexMatcher.replaceAll("$1 #$randomHash")
|
||||
}
|
||||
if (!mediaClient
|
||||
.checkPageExistsUsingTitle(
|
||||
String.format(
|
||||
"File:%s",
|
||||
sequenceFileName,
|
||||
),
|
||||
).blockingGet()
|
||||
) {
|
||||
break
|
||||
}
|
||||
sequenceNumber++
|
||||
}
|
||||
return sequenceFileName!!
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue