mirror of
https://github.com/SchildiChat/SchildiChat-android.git
synced 2025-02-18 04:50:08 +03:00
FIx / interceptors and stream closed
This commit is contained in:
parent
bf4f869524
commit
3b8c61a87e
3 changed files with 21 additions and 40 deletions
|
@ -24,6 +24,7 @@ import okio.BufferedSink
|
||||||
import okio.ForwardingSink
|
import okio.ForwardingSink
|
||||||
import okio.Sink
|
import okio.Sink
|
||||||
import okio.buffer
|
import okio.buffer
|
||||||
|
import org.matrix.android.sdk.api.extensions.tryThis
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
|
|
||||||
internal class ProgressRequestBody(private val delegate: RequestBody,
|
internal class ProgressRequestBody(private val delegate: RequestBody,
|
||||||
|
@ -39,15 +40,9 @@ internal class ProgressRequestBody(private val delegate: RequestBody,
|
||||||
|
|
||||||
override fun isDuplex() = delegate.isDuplex()
|
override fun isDuplex() = delegate.isDuplex()
|
||||||
|
|
||||||
override fun contentLength(): Long {
|
val length = tryThis { delegate.contentLength() } ?: -1
|
||||||
try {
|
|
||||||
return delegate.contentLength()
|
|
||||||
} catch (e: IOException) {
|
|
||||||
e.printStackTrace()
|
|
||||||
}
|
|
||||||
|
|
||||||
return -1
|
override fun contentLength() = length
|
||||||
}
|
|
||||||
|
|
||||||
@Throws(IOException::class)
|
@Throws(IOException::class)
|
||||||
override fun writeTo(sink: BufferedSink) {
|
override fun writeTo(sink: BufferedSink) {
|
||||||
|
|
|
@ -32,6 +32,7 @@ import okhttp3.RequestBody.Companion.toRequestBody
|
||||||
import okio.BufferedSink
|
import okio.BufferedSink
|
||||||
import okio.source
|
import okio.source
|
||||||
import org.greenrobot.eventbus.EventBus
|
import org.greenrobot.eventbus.EventBus
|
||||||
|
import org.matrix.android.sdk.api.extensions.tryThis
|
||||||
import org.matrix.android.sdk.api.session.content.ContentUrlResolver
|
import org.matrix.android.sdk.api.session.content.ContentUrlResolver
|
||||||
import org.matrix.android.sdk.internal.di.Authenticated
|
import org.matrix.android.sdk.internal.di.Authenticated
|
||||||
import org.matrix.android.sdk.internal.network.ProgressRequestBody
|
import org.matrix.android.sdk.internal.network.ProgressRequestBody
|
||||||
|
@ -40,7 +41,7 @@ import org.matrix.android.sdk.internal.network.toFailure
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.io.FileNotFoundException
|
import java.io.FileNotFoundException
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
import java.io.InputStream
|
import java.util.UUID
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
internal class FileUploader @Inject constructor(@Authenticated
|
internal class FileUploader @Inject constructor(@Authenticated
|
||||||
|
@ -57,11 +58,11 @@ internal class FileUploader @Inject constructor(@Authenticated
|
||||||
filename: String?,
|
filename: String?,
|
||||||
mimeType: String?,
|
mimeType: String?,
|
||||||
progressListener: ProgressRequestBody.Listener? = null): ContentUploadResponse {
|
progressListener: ProgressRequestBody.Listener? = null): ContentUploadResponse {
|
||||||
val uploadBody = object : RequestBody() {
|
val uploadBody = object : RequestBody() {
|
||||||
override fun contentLength() = file.length()
|
override fun contentLength() = file.length()
|
||||||
|
|
||||||
// Disable okhttp auto resend for 'large files'
|
// Disable okhttp auto resend for 'large files'
|
||||||
override fun isOneShot() = contentLength() == 0L || contentLength() >= 1_000_000
|
override fun isOneShot() = contentLength() == 0L || contentLength() >= 1_000_000
|
||||||
|
|
||||||
override fun contentType(): MediaType? {
|
override fun contentType(): MediaType? {
|
||||||
return mimeType?.toMediaTypeOrNull()
|
return mimeType?.toMediaTypeOrNull()
|
||||||
|
@ -83,37 +84,22 @@ internal class FileUploader @Inject constructor(@Authenticated
|
||||||
return upload(uploadBody, filename, progressListener)
|
return upload(uploadBody, filename, progressListener)
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun uploadInputStream(inputStream: InputStream,
|
|
||||||
filename: String?,
|
|
||||||
mimeType: String?,
|
|
||||||
progressListener: ProgressRequestBody.Listener? = null): ContentUploadResponse {
|
|
||||||
val length = inputStream.available().toLong()
|
|
||||||
val uploadBody = object : RequestBody() {
|
|
||||||
override fun contentLength() = length
|
|
||||||
|
|
||||||
// Disable okhttp auto resend for 'large files'
|
|
||||||
override fun isOneShot() = contentLength() == 0L || contentLength() >= 1_000_000
|
|
||||||
|
|
||||||
override fun contentType(): MediaType? {
|
|
||||||
return mimeType?.toMediaTypeOrNull()
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun writeTo(sink: BufferedSink) {
|
|
||||||
inputStream.source().use { sink.writeAll(it) }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return upload(uploadBody, filename, progressListener)
|
|
||||||
}
|
|
||||||
|
|
||||||
suspend fun uploadFromUri(uri: Uri,
|
suspend fun uploadFromUri(uri: Uri,
|
||||||
filename: String?,
|
filename: String?,
|
||||||
mimeType: String?,
|
mimeType: String?,
|
||||||
progressListener: ProgressRequestBody.Listener? = null): ContentUploadResponse {
|
progressListener: ProgressRequestBody.Listener? = null): ContentUploadResponse {
|
||||||
val inputStream = withContext(Dispatchers.IO) {
|
val inputStream = withContext(Dispatchers.IO) {
|
||||||
context.contentResolver.openInputStream(uri)
|
context.contentResolver.openInputStream(uri)
|
||||||
} ?: throw FileNotFoundException()
|
} ?: throw FileNotFoundException()
|
||||||
return uploadInputStream(inputStream, filename, mimeType, progressListener)
|
val workingFile = File.createTempFile(UUID.randomUUID().toString(), null, context.cacheDir)
|
||||||
|
workingFile.outputStream().use {
|
||||||
|
inputStream.copyTo(it)
|
||||||
|
}
|
||||||
|
return uploadFile(workingFile, filename, mimeType, progressListener).also {
|
||||||
|
tryThis { workingFile.delete() }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun upload(uploadBody: RequestBody, filename: String?, progressListener: ProgressRequestBody.Listener?): ContentUploadResponse {
|
private suspend fun upload(uploadBody: RequestBody, filename: String?, progressListener: ProgressRequestBody.Listener?): ContentUploadResponse {
|
||||||
val urlBuilder = uploadUrl.toHttpUrlOrNull()?.newBuilder() ?: throw RuntimeException()
|
val urlBuilder = uploadUrl.toHttpUrlOrNull()?.newBuilder() ?: throw RuntimeException()
|
||||||
|
|
||||||
|
|
|
@ -178,7 +178,7 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter
|
||||||
var uploadedFileEncryptedFileInfo: EncryptedFileInfo? = null
|
var uploadedFileEncryptedFileInfo: EncryptedFileInfo? = null
|
||||||
|
|
||||||
return try {
|
return try {
|
||||||
val streamToUpload: InputStream
|
val fileToUplaod: File
|
||||||
|
|
||||||
if (attachment.type == ContentAttachmentData.Type.IMAGE && params.compressBeforeSending) {
|
if (attachment.type == ContentAttachmentData.Type.IMAGE && params.compressBeforeSending) {
|
||||||
// Compressor library works with File instead of Uri for now. Since Scoped Storage doesn't allow us to access files directly, we should
|
// Compressor library works with File instead of Uri for now. Since Scoped Storage doesn't allow us to access files directly, we should
|
||||||
|
@ -200,9 +200,9 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter
|
||||||
options.outHeight,
|
options.outHeight,
|
||||||
fileSize
|
fileSize
|
||||||
)
|
)
|
||||||
streamToUpload = compressedFile.inputStream()
|
fileToUplaod = compressedFile
|
||||||
} else {
|
} else {
|
||||||
streamToUpload = workingFile.inputStream()
|
fileToUplaod = workingFile
|
||||||
}
|
}
|
||||||
|
|
||||||
val contentUploadResponse = if (params.isEncrypted) {
|
val contentUploadResponse = if (params.isEncrypted) {
|
||||||
|
@ -211,7 +211,7 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter
|
||||||
val tmpEncrypted = File.createTempFile(UUID.randomUUID().toString(), null, context.cacheDir)
|
val tmpEncrypted = File.createTempFile(UUID.randomUUID().toString(), null, context.cacheDir)
|
||||||
|
|
||||||
uploadedFileEncryptedFileInfo =
|
uploadedFileEncryptedFileInfo =
|
||||||
MXEncryptedAttachments.encrypt(streamToUpload, attachment.getSafeMimeType(), tmpEncrypted) { read, total ->
|
MXEncryptedAttachments.encrypt(fileToUplaod.inputStream(), attachment.getSafeMimeType(), tmpEncrypted) { read, total ->
|
||||||
notifyTracker(params) {
|
notifyTracker(params) {
|
||||||
contentUploadStateTracker.setEncrypting(it, read.toLong(), total.toLong())
|
contentUploadStateTracker.setEncrypting(it, read.toLong(), total.toLong())
|
||||||
}
|
}
|
||||||
|
@ -228,7 +228,7 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter
|
||||||
} else {
|
} else {
|
||||||
Timber.v("## FileService: Clear file")
|
Timber.v("## FileService: Clear file")
|
||||||
fileUploader
|
fileUploader
|
||||||
.uploadInputStream(streamToUpload, attachment.name, attachment.getSafeMimeType(), progressListener)
|
.uploadFile(fileToUplaod, attachment.name, attachment.getSafeMimeType(), progressListener)
|
||||||
}
|
}
|
||||||
|
|
||||||
Timber.v("## FileService: Update cache storage for ${contentUploadResponse.contentUri}")
|
Timber.v("## FileService: Update cache storage for ${contentUploadResponse.contentUri}")
|
||||||
|
|
Loading…
Add table
Reference in a new issue