Merge pull request #4783 from vector-im/feature/adm/unable-to-change-avatar

Unable to change avatar due to `NetworkOnMainThread`
This commit is contained in:
Benoit Marty 2021-12-30 12:30:18 +01:00 committed by GitHub
commit 5407c84b2f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 8 deletions

1
changelog.d/4767.bugfix Normal file
View File

@ -0,0 +1 @@
Fixing unable to change change avatar in some scenarios

View File

@ -109,18 +109,23 @@ 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)
} ?: throw FileNotFoundException()
val workingFile = temporaryFileCreator.create()
workingFile.outputStream().use {
inputStream.copyTo(it)
}
return uploadFile(workingFile, filename, mimeType, progressListener).also { return uploadFile(workingFile, filename, mimeType, progressListener).also {
tryOrNull { workingFile.delete() } 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()
workingFile.outputStream().use {
inputStream.copyTo(it)
}
workingFile
}
}
private suspend fun upload(uploadBody: RequestBody, private suspend fun upload(uploadBody: RequestBody,
filename: String?, filename: String?,
progressListener: ProgressRequestBody.Listener?): ContentUploadResponse { progressListener: ProgressRequestBody.Listener?): ContentUploadResponse {

View File

@ -68,7 +68,7 @@ internal class DefaultProfileService @Inject constructor(private val taskExecuto
} }
override suspend fun updateAvatar(userId: String, newAvatarUri: Uri, fileName: String) { override suspend fun updateAvatar(userId: String, newAvatarUri: Uri, fileName: String) {
withContext(coroutineDispatchers.main) { withContext(coroutineDispatchers.io) {
val response = fileUploader.uploadFromUri(newAvatarUri, fileName, MimeTypes.Jpeg) val response = fileUploader.uploadFromUri(newAvatarUri, fileName, MimeTypes.Jpeg)
setAvatarUrlTask.execute(SetAvatarUrlTask.Params(userId = userId, newAvatarUrl = response.contentUri)) setAvatarUrlTask.execute(SetAvatarUrlTask.Params(userId = userId, newAvatarUrl = response.contentUri))
userStore.updateAvatar(userId, response.contentUri) userStore.updateAvatar(userId, response.contentUri)