making the file upload file creation all happen on the IO dispatcher

This commit is contained in:
Adam Brown 2021-12-20 11:52:01 +00:00
parent 55c0f1fcb3
commit 18b38fe21e
1 changed files with 12 additions and 7 deletions

View File

@ -109,15 +109,20 @@ internal class FileUploader @Inject constructor(
filename: String?, filename: String?,
mimeType: String?, mimeType: String?,
progressListener: ProgressRequestBody.Listener? = null): ContentUploadResponse { progressListener: ProgressRequestBody.Listener? = null): ContentUploadResponse {
val inputStream = withContext(Dispatchers.IO) { val workingFile = context.copyUriToTempFile(uri)
context.contentResolver.openInputStream(uri) return uploadFile(workingFile, filename, mimeType, progressListener).also {
} ?: throw FileNotFoundException() tryOrNull { workingFile.delete() }
}
}
private suspend fun Context.copyUriToTempFile(uri: Uri): File {
return withContext(Dispatchers.IO) {
val inputStream = contentResolver.openInputStream(uri) ?: throw FileNotFoundException()
val workingFile = temporaryFileCreator.create() val workingFile = temporaryFileCreator.create()
workingFile.outputStream().use { workingFile.outputStream().use {
inputStream.copyTo(it) inputStream.copyTo(it)
} }
return uploadFile(workingFile, filename, mimeType, progressListener).also { workingFile
tryOrNull { workingFile.delete() }
} }
} }