Skip to content

Commit 6d9cbe0

Browse files
Merge pull request #59 from wordpress-mobile/fix-race-condition
Fix race condition that resulted in duplicate images when uploading multiple files
2 parents 4cf7b7b + 7c4eaac commit 6d9cbe0

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

Diff for: mediapicker/src/main/java/org/wordpress/android/mediapicker/MediaPickerUtils.kt

+18-18
Original file line numberDiff line numberDiff line change
@@ -113,25 +113,27 @@ class MediaPickerUtils @Inject constructor(
113113
}
114114
}
115115

116-
fun getMediaStoreFilePath(uri: Uri): String? {
116+
private fun getMediaStoreFilePath(uri: Uri): String? {
117117
var path: String? = null
118118
if (VERSION.SDK_INT >= VERSION_CODES.Q) {
119119
try {
120120
val cachedFile = createTempFile()
121-
val parcelFileDescriptor = context.contentResolver.openFile(uri, "r", null)
122-
parcelFileDescriptor?.fileDescriptor?.let { fd ->
123-
val input = FileInputStream(fd)
124-
val byteArray = readBinaryStream(
125-
input,
126-
parcelFileDescriptor.statSize.toInt()
127-
)
128-
if (cachedFile != null) {
129-
val fileSaved = writeFile(cachedFile, byteArray)
130-
if (fileSaved) {
131-
path = cachedFile.absolutePath
121+
context.contentResolver.openFile(uri, "r", null)
122+
.use { parcelFileDescriptor ->
123+
parcelFileDescriptor?.fileDescriptor?.let { fd ->
124+
val input = FileInputStream(fd)
125+
val byteArray = readBinaryStream(
126+
input,
127+
parcelFileDescriptor.statSize.toInt()
128+
)
129+
if (cachedFile != null) {
130+
val fileSaved = writeFile(cachedFile, byteArray)
131+
if (fileSaved) {
132+
path = cachedFile.absolutePath
133+
}
134+
}
132135
}
133136
}
134-
}
135137
} catch (e: IOException) {
136138
log.e(e)
137139
}
@@ -172,14 +174,12 @@ class MediaPickerUtils @Inject constructor(
172174
}
173175

174176
private fun createTempFile(): File? {
175-
var file: File? = null
176-
try {
177-
val tempFileName = "temp-${System.currentTimeMillis()}.jpg"
178-
file = File(context.cacheDir, tempFileName)
177+
return try {
178+
File.createTempFile("temp-${System.currentTimeMillis()}", ".jpg")
179179
} catch (e: RuntimeException) {
180180
log.e(e)
181+
null
181182
}
182-
return file
183183
}
184184

185185
private fun readBinaryStream(

0 commit comments

Comments
 (0)