mirror of
				https://github.com/commons-app/apps-android-commons.git
				synced 2025-10-31 14:53:59 +01:00 
			
		
		
		
	Merge pull request #1 from AlexG-2003/improve-file-name-search
Modified findUniqueFileName() in UploadWorker.kt to use a random 3-di…
This commit is contained in:
		
						commit
						7b96dffad2
					
				
					 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
	
	 Jinniu Du
						Jinniu Du