Use file service in glide loader (avoid re-dl after send)

This commit is contained in:
Valere 2020-08-07 16:50:57 +02:00
parent caf0ac1c9f
commit de53166193

View file

@ -33,6 +33,8 @@ import timber.log.Timber
import java.io.File import java.io.File
import java.io.IOException import java.io.IOException
import java.io.InputStream import java.io.InputStream
import java.lang.Exception
import java.lang.IllegalArgumentException
class VectorGlideModelLoaderFactory(private val activeSessionHolder: ActiveSessionHolder) class VectorGlideModelLoaderFactory(private val activeSessionHolder: ActiveSessionHolder)
: ModelLoaderFactory<ImageContentRenderer.Data, InputStream> { : ModelLoaderFactory<ImageContentRenderer.Data, InputStream> {
@ -89,7 +91,7 @@ class VectorGlideDataFetcher(private val activeSessionHolder: ActiveSessionHolde
stream?.close() // interrupts decode if any stream?.close() // interrupts decode if any
stream = null stream = null
} catch (ignore: Throwable) { } catch (ignore: Throwable) {
Timber.e(ignore) Timber.e("Failed to close stream ${ignore.localizedMessage}")
} finally { } finally {
stream = null stream = null
} }
@ -103,26 +105,48 @@ class VectorGlideDataFetcher(private val activeSessionHolder: ActiveSessionHolde
callback.onDataReady(initialFile.inputStream()) callback.onDataReady(initialFile.inputStream())
return return
} }
val contentUrlResolver = activeSessionHolder.getActiveSession().contentUrlResolver() // val contentUrlResolver = activeSessionHolder.getActiveSession().contentUrlResolver()
val url = contentUrlResolver.resolveFullSize(data.url)
?: return
val request = Request.Builder() val fileService = activeSessionHolder.getSafeActiveSession()?.fileService() ?: return Unit.also {
.url(url) callback.onLoadFailed(IllegalArgumentException("No File service"))
.build() }
// Use the file vector service, will avoid flickering and redownload after upload
fileService.downloadFile(
downloadMode = FileService.DownloadMode.FOR_INTERNAL_USE,
mimeType = data.mimeType,
id = data.eventId,
url = data.url,
fileName = data.filename,
elementToDecrypt = data.elementToDecrypt,
callback = object: MatrixCallback<File> {
override fun onSuccess(data: File) {
callback.onDataReady(data.inputStream())
}
val response = client.newCall(request).execute() override fun onFailure(failure: Throwable) {
val inputStream = response.body?.byteStream() callback.onLoadFailed(failure as? Exception ?: IOException(failure.localizedMessage))
Timber.v("Response size ${response.body?.contentLength()} - Stream available: ${inputStream?.available()}") }
if (!response.isSuccessful) { }
callback.onLoadFailed(IOException("Unexpected code $response")) )
return // val url = contentUrlResolver.resolveFullSize(data.url)
} // ?: return
stream = if (data.elementToDecrypt != null && data.elementToDecrypt.k.isNotBlank()) { //
Matrix.decryptStream(inputStream, data.elementToDecrypt) // val request = Request.Builder()
} else { // .url(url)
inputStream // .build()
} //
callback.onDataReady(stream) // val response = client.newCall(request).execute()
// val inputStream = response.body?.byteStream()
// Timber.v("Response size ${response.body?.contentLength()} - Stream available: ${inputStream?.available()}")
// if (!response.isSuccessful) {
// callback.onLoadFailed(IOException("Unexpected code $response"))
// return
// }
// stream = if (data.elementToDecrypt != null && data.elementToDecrypt.k.isNotBlank()) {
// Matrix.decryptStream(inputStream, data.elementToDecrypt)
// } else {
// inputStream
// }
// callback.onDataReady(stream)
} }
} }