2019-07-08 15:07:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2019 New Vector Ltd
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2019-07-08 17:06:17 +00:00
|
|
|
package im.vector.matrix.android.internal.session
|
2019-07-08 15:07:21 +00:00
|
|
|
|
|
|
|
import android.content.Context
|
2019-07-08 17:06:17 +00:00
|
|
|
import android.os.Environment
|
2019-07-08 15:07:21 +00:00
|
|
|
import arrow.core.Try
|
|
|
|
import im.vector.matrix.android.api.MatrixCallback
|
|
|
|
import im.vector.matrix.android.api.auth.data.SessionParams
|
|
|
|
import im.vector.matrix.android.api.session.content.ContentUrlResolver
|
2019-07-08 17:06:17 +00:00
|
|
|
import im.vector.matrix.android.api.session.file.FileService
|
2019-07-08 15:07:21 +00:00
|
|
|
import im.vector.matrix.android.internal.crypto.attachments.ElementToDecrypt
|
|
|
|
import im.vector.matrix.android.internal.crypto.attachments.MXEncryptedAttachments
|
|
|
|
import im.vector.matrix.android.internal.extensions.foldToCallback
|
|
|
|
import im.vector.matrix.android.internal.util.MatrixCoroutineDispatchers
|
|
|
|
import im.vector.matrix.android.internal.util.md5
|
|
|
|
import im.vector.matrix.android.internal.util.writeToFile
|
|
|
|
import kotlinx.coroutines.GlobalScope
|
|
|
|
import kotlinx.coroutines.launch
|
|
|
|
import kotlinx.coroutines.withContext
|
|
|
|
import okhttp3.OkHttpClient
|
|
|
|
import okhttp3.Request
|
|
|
|
import timber.log.Timber
|
|
|
|
import java.io.File
|
|
|
|
import java.io.IOException
|
|
|
|
import javax.inject.Inject
|
|
|
|
|
2019-07-08 17:06:17 +00:00
|
|
|
internal class DefaultFileService @Inject constructor(private val context: Context,
|
|
|
|
private val sessionParams: SessionParams,
|
|
|
|
private val contentUrlResolver: ContentUrlResolver,
|
|
|
|
private val coroutineDispatchers: MatrixCoroutineDispatchers) : FileService {
|
2019-07-08 15:07:21 +00:00
|
|
|
|
|
|
|
val okHttpClient = OkHttpClient()
|
|
|
|
|
2019-07-08 17:06:17 +00:00
|
|
|
/**
|
|
|
|
* Download file in the cache folder, and eventually decrypt it
|
|
|
|
* TODO implement clear file, to delete "MF"
|
|
|
|
*/
|
|
|
|
override fun downloadFile(downloadMode: FileService.DownloadMode,
|
|
|
|
id: String,
|
|
|
|
fileName: String,
|
|
|
|
url: String?,
|
|
|
|
elementToDecrypt: ElementToDecrypt?,
|
|
|
|
callback: MatrixCallback<File>) {
|
2019-07-08 15:07:21 +00:00
|
|
|
GlobalScope.launch(coroutineDispatchers.main) {
|
|
|
|
withContext(coroutineDispatchers.io) {
|
|
|
|
Try {
|
2019-07-08 17:06:17 +00:00
|
|
|
val folder = getFolder(downloadMode, id)
|
2019-07-08 15:07:21 +00:00
|
|
|
|
2019-07-08 17:06:17 +00:00
|
|
|
File(folder, fileName)
|
2019-07-09 09:05:25 +00:00
|
|
|
}.flatMap { destFile ->
|
|
|
|
if (!destFile.exists() || downloadMode == FileService.DownloadMode.TO_EXPORT) {
|
2019-07-08 15:07:21 +00:00
|
|
|
Try {
|
|
|
|
val resolvedUrl = contentUrlResolver.resolveFullSize(url) ?: throw IllegalArgumentException("url is null")
|
|
|
|
|
|
|
|
val request = Request.Builder()
|
|
|
|
.url(resolvedUrl)
|
|
|
|
.build()
|
|
|
|
|
|
|
|
val response = okHttpClient.newCall(request).execute()
|
|
|
|
val inputStream = response.body()?.byteStream()
|
|
|
|
Timber.v("Response size ${response.body()?.contentLength()} - Stream available: ${inputStream?.available()}")
|
2019-07-08 17:06:17 +00:00
|
|
|
if (!response.isSuccessful
|
|
|
|
|| inputStream == null) {
|
2019-07-08 15:07:21 +00:00
|
|
|
throw IOException()
|
|
|
|
}
|
|
|
|
|
2019-07-08 17:06:17 +00:00
|
|
|
if (elementToDecrypt != null) {
|
2019-07-09 09:05:25 +00:00
|
|
|
Timber.v("## decrypt file")
|
2019-07-08 17:06:17 +00:00
|
|
|
MXEncryptedAttachments.decryptAttachment(inputStream, elementToDecrypt) ?: throw IllegalStateException("Decryption error")
|
|
|
|
} else {
|
|
|
|
inputStream
|
|
|
|
}
|
2019-07-08 15:07:21 +00:00
|
|
|
}
|
|
|
|
.map { inputStream ->
|
|
|
|
writeToFile(inputStream, destFile)
|
2019-07-09 09:05:25 +00:00
|
|
|
destFile
|
2019-07-08 15:07:21 +00:00
|
|
|
}
|
2019-07-09 09:05:25 +00:00
|
|
|
} else {
|
|
|
|
Try.just(destFile)
|
2019-07-08 15:07:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.foldToCallback(callback)
|
|
|
|
}
|
|
|
|
}
|
2019-07-08 17:06:17 +00:00
|
|
|
|
|
|
|
private fun getFolder(downloadMode: FileService.DownloadMode, id: String): File {
|
|
|
|
return when (downloadMode) {
|
|
|
|
FileService.DownloadMode.FOR_INTERNAL_USE -> {
|
|
|
|
// Create dir tree (MF stands for Matrix File):
|
|
|
|
// <cache>/MF/<md5(userId)>/<md5(id)>/
|
|
|
|
val tmpFolderRoot = File(context.cacheDir, "MF")
|
|
|
|
val tmpFolderUser = File(tmpFolderRoot, sessionParams.credentials.userId.md5())
|
|
|
|
File(tmpFolderUser, id.md5())
|
|
|
|
}
|
|
|
|
FileService.DownloadMode.TO_EXPORT -> {
|
|
|
|
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.also { folder ->
|
|
|
|
if (!folder.exists()) {
|
|
|
|
folder.mkdirs()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|