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:
commit
5407c84b2f
1
changelog.d/4767.bugfix
Normal file
1
changelog.d/4767.bugfix
Normal file
@ -0,0 +1 @@
|
|||||||
|
Fixing unable to change change avatar in some scenarios
|
@ -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 {
|
||||||
|
@ -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)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user