From 80a42d0a554c6157de01cc1aa4b788cb3f1c1133 Mon Sep 17 00:00:00 2001 From: yostyle Date: Mon, 25 Oct 2021 17:23:13 +0200 Subject: [PATCH 1/7] Add content scanner APIs --- .../content/ElementContentUrlResolver.kt | 112 +++++++++++++ .../session/contentscanning/ContentScanApi.kt | 35 ++++ .../contentscanning/ContentScannerModule.kt | 74 +++++++++ .../ContentScanningApiProvider.kt | 15 ++ .../DefaultContentScannerService.kt | 150 ++++++++++++++++++ .../contentscanning/ScanEncryptorUtils.kt | 53 +++++++ .../data/ContentScanningStore.kt | 30 ++++ .../db/ContentScanResultEntity.kt | 45 ++++++ .../db/ContentScannerEntityQueries.kt | 31 ++++ .../db/ContentScannerInfoEntity.kt | 17 ++ .../db/ContentScannerRealmModule.kt | 19 +++ .../db/RealmContentScannerStore.kt | 132 +++++++++++++++ .../contentscanning/model/DownloadBody.kt | 30 ++++ .../contentscanning/model/ScanResponse.kt | 23 +++ .../model/ServerPublicKeyResponse.kt | 16 ++ .../tasks/DownloadEncryptedTask.kt | 41 +++++ .../tasks/GetServerPublicKeyTask.kt | 29 ++++ .../tasks/ScanEncryptedTask.kt | 57 +++++++ .../contentscanning/tasks/ScanMediaTask.kt | 65 ++++++++ .../android/sdk/api/failure/Extensions.kt | 18 +++ .../matrix/android/sdk/api/session/Session.kt | 6 + .../api/session/content/ContentUrlResolver.kt | 16 ++ .../contentscanning/ContentScanError.kt | 39 +++++ .../contentscanning/ContentScannerService.kt | 35 ++++ .../DisabledContentScannerService.kt | 69 ++++++++ .../api/session/contentscanning/ScanState.kt | 20 +++ .../internal/auth/login/DefaultLoginWizard.kt | 3 +- .../android/sdk/internal/di/DbQualifiers.kt | 4 + .../sdk/internal/network/NetworkConstants.kt | 3 + .../sdk/internal/session/DefaultSession.kt | 4 + .../sdk/internal/session/SessionComponent.kt | 2 + .../content/DefaultContentUrlResolver.kt | 11 +- 32 files changed, 1202 insertions(+), 2 deletions(-) create mode 100644 matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/content/ElementContentUrlResolver.kt create mode 100644 matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/ContentScanApi.kt create mode 100644 matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/ContentScannerModule.kt create mode 100644 matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/ContentScanningApiProvider.kt create mode 100644 matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/DefaultContentScannerService.kt create mode 100644 matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/ScanEncryptorUtils.kt create mode 100644 matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/data/ContentScanningStore.kt create mode 100644 matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/db/ContentScanResultEntity.kt create mode 100644 matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/db/ContentScannerEntityQueries.kt create mode 100644 matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/db/ContentScannerInfoEntity.kt create mode 100644 matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/db/ContentScannerRealmModule.kt create mode 100644 matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/db/RealmContentScannerStore.kt create mode 100644 matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/model/DownloadBody.kt create mode 100644 matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/model/ScanResponse.kt create mode 100644 matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/model/ServerPublicKeyResponse.kt create mode 100644 matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/tasks/DownloadEncryptedTask.kt create mode 100644 matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/tasks/GetServerPublicKeyTask.kt create mode 100644 matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/tasks/ScanEncryptedTask.kt create mode 100644 matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/tasks/ScanMediaTask.kt create mode 100644 matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/contentscanning/ContentScanError.kt create mode 100644 matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/contentscanning/ContentScannerService.kt create mode 100644 matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/contentscanning/DisabledContentScannerService.kt create mode 100644 matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/contentscanning/ScanState.kt diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/content/ElementContentUrlResolver.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/content/ElementContentUrlResolver.kt new file mode 100644 index 0000000000..e2f0ac2499 --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/content/ElementContentUrlResolver.kt @@ -0,0 +1,112 @@ +/* + * Copyright 2020 The Matrix.org Foundation C.I.C. + * + * 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. + */ + +package im.vector.matrix.android.sdk.internal.session.content + +import im.vector.matrix.android.sdk.internal.session.contentscanning.ScanEncryptorUtils +import im.vector.matrix.android.sdk.internal.session.contentscanning.model.toJson +import org.matrix.android.sdk.api.MatrixUrls +import org.matrix.android.sdk.api.MatrixUrls.isMxcUrl +import org.matrix.android.sdk.api.auth.data.HomeServerConnectionConfig +import org.matrix.android.sdk.api.session.content.ContentUrlResolver +import org.matrix.android.sdk.api.session.contentscanning.ContentScannerService +import org.matrix.android.sdk.internal.crypto.attachments.ElementToDecrypt +import org.matrix.android.sdk.internal.network.NetworkConstants +import org.matrix.android.sdk.internal.util.ensureTrailingSlash +import javax.inject.Inject + +internal class ElementContentUrlResolver @Inject constructor( + homeServerConnectionConfig: HomeServerConnectionConfig, + private val scannerService: ContentScannerService +) : ContentUrlResolver { + + private val baseUrl = homeServerConnectionConfig.homeServerUriBase.toString().ensureTrailingSlash() + + override val uploadUrl = baseUrl + NetworkConstants.URI_API_MEDIA_PREFIX_PATH_R0 + "upload" + + override fun resolveForDownload(contentUrl: String?, elementToDecrypt: ElementToDecrypt?): ContentUrlResolver.ResolvedMethod? { + return if (scannerService.isScannerEnabled() && elementToDecrypt != null) { + val baseUrl = scannerService.getContentScannerServer() + val sep = if (baseUrl?.endsWith("/") == true) "" else "/" + + val url = baseUrl + sep + NetworkConstants.URI_API_PREFIX_PATH_MEDIA_PROXY_UNSTABLE + "download_encrypted" + + ContentUrlResolver.ResolvedMethod.POST( + url = url, + jsonBody = ScanEncryptorUtils + .getDownloadBodyAndEncryptIfNeeded(scannerService.serverPublicKey, contentUrl ?: "", elementToDecrypt) + .toJson() + ) + } else { + resolveFullSize(contentUrl)?.let { ContentUrlResolver.ResolvedMethod.GET(it) } + } + } + + override fun resolveFullSize(contentUrl: String?): String? { + return contentUrl + // do not allow non-mxc content URLs + ?.takeIf { it.isMxcUrl() } + ?.let { + resolve( + contentUrl = it, + toThumbnail = false + ) + } + } + + override fun resolveThumbnail(contentUrl: String?, width: Int, height: Int, method: ContentUrlResolver.ThumbnailMethod): String? { + return contentUrl + // do not allow non-mxc content URLs + ?.takeIf { it.isMxcUrl() } + ?.let { + resolve( + contentUrl = it, + toThumbnail = true, + params = "?width=$width&height=$height&method=${method.value}" + ) + } + } + + private fun resolve(contentUrl: String, + toThumbnail: Boolean, + params: String = ""): String? { + var serverAndMediaId = contentUrl.removePrefix(MatrixUrls.MATRIX_CONTENT_URI_SCHEME) + + val apiPath = if (scannerService.isScannerEnabled()) { + NetworkConstants.URI_API_PREFIX_PATH_MEDIA_PROXY_UNSTABLE + } else { + NetworkConstants.URI_API_MEDIA_PREFIX_PATH_R0 + } + val prefix = if (toThumbnail) { + apiPath + "thumbnail/" + } else { + apiPath + "download/" + } + val fragmentOffset = serverAndMediaId.indexOf("#") + var fragment = "" + if (fragmentOffset >= 0) { + fragment = serverAndMediaId.substring(fragmentOffset) + serverAndMediaId = serverAndMediaId.substring(0, fragmentOffset) + } + + val resolvedUrl = if (scannerService.isScannerEnabled()) { + scannerService.getContentScannerServer()!!.ensureTrailingSlash() + } else { + baseUrl + } + return resolvedUrl + prefix + serverAndMediaId + params + fragment + } +} diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/ContentScanApi.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/ContentScanApi.kt new file mode 100644 index 0000000000..44745e4c50 --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/ContentScanApi.kt @@ -0,0 +1,35 @@ +/* + * Copyright 2020 New Vector Ltd - All Rights Reserved + * Unauthorized copying of this file, via any medium is strictly prohibited + * Proprietary and confidential + */ + +package im.vector.matrix.android.sdk.internal.session.contentscanning + +import im.vector.matrix.android.sdk.internal.session.contentscanning.model.DownloadBody +import im.vector.matrix.android.sdk.internal.session.contentscanning.model.ScanResponse +import im.vector.matrix.android.sdk.internal.session.contentscanning.model.ServerPublicKeyResponse +import okhttp3.ResponseBody +import org.matrix.android.sdk.internal.network.NetworkConstants +import retrofit2.http.Body +import retrofit2.http.GET +import retrofit2.http.POST +import retrofit2.http.Path + +/** + * https://github.com/matrix-org/matrix-content-scanner + */ +internal interface ContentScanApi { + + @POST(NetworkConstants.URI_API_PREFIX_PATH_MEDIA_PROXY_UNSTABLE + "download_encrypted") + suspend fun downloadEncrypted(@Body info: DownloadBody): ResponseBody + + @POST(NetworkConstants.URI_API_PREFIX_PATH_MEDIA_PROXY_UNSTABLE + "scan_encrypted") + fun scanFile(@Body info: DownloadBody): ScanResponse + + @GET(NetworkConstants.URI_API_PREFIX_PATH_MEDIA_PROXY_UNSTABLE + "public_key") + fun getServerPublicKey(): ServerPublicKeyResponse + + @GET(NetworkConstants.URI_API_PREFIX_PATH_MEDIA_PROXY_UNSTABLE + "scan/{domain}/{mediaId}") + fun scanMedia(@Path(value = "domain") domain: String, @Path(value = "mediaId") mediaId: String): ScanResponse +} diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/ContentScannerModule.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/ContentScannerModule.kt new file mode 100644 index 0000000000..02a0c49455 --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/ContentScannerModule.kt @@ -0,0 +1,74 @@ +/* + * Copyright 2020 New Vector Ltd - All Rights Reserved + * Unauthorized copying of this file, via any medium is strictly prohibited + * Proprietary and confidential + */ + +package im.vector.matrix.android.sdk.internal.session.contentscanning + +import dagger.Binds +import dagger.Module +import dagger.Provides +import im.vector.matrix.android.sdk.internal.session.contentscanning.data.ContentScanningStore +import im.vector.matrix.android.sdk.internal.session.contentscanning.db.ContentScannerRealmModule +import im.vector.matrix.android.sdk.internal.session.contentscanning.db.RealmContentScannerStore +import im.vector.matrix.android.sdk.internal.session.contentscanning.tasks.DefaultDownloadEncryptedTask +import im.vector.matrix.android.sdk.internal.session.contentscanning.tasks.DefaultGetServerPublicKeyTask +import im.vector.matrix.android.sdk.internal.session.contentscanning.tasks.DefaultScanEncryptedTask +import im.vector.matrix.android.sdk.internal.session.contentscanning.tasks.DefaultScanMediaTask +import im.vector.matrix.android.sdk.internal.session.contentscanning.tasks.DownloadEncryptedTask +import im.vector.matrix.android.sdk.internal.session.contentscanning.tasks.GetServerPublicKeyTask +import im.vector.matrix.android.sdk.internal.session.contentscanning.tasks.ScanEncryptedTask +import im.vector.matrix.android.sdk.internal.session.contentscanning.tasks.ScanMediaTask +import io.realm.RealmConfiguration +import org.matrix.android.sdk.api.session.contentscanning.ContentScannerService +import org.matrix.android.sdk.internal.database.RealmKeysUtils +import org.matrix.android.sdk.internal.di.ContentScannerDatabase +import org.matrix.android.sdk.internal.di.SessionFilesDirectory +import org.matrix.android.sdk.internal.di.UserMd5 +import org.matrix.android.sdk.internal.session.SessionModule +import org.matrix.android.sdk.internal.session.SessionScope +import java.io.File + +@Module +internal abstract class ContentScannerModule { + @Module + companion object { + + @JvmStatic + @Provides + @ContentScannerDatabase + @SessionScope + fun providesContentScannerRealmConfiguration(realmKeysUtils: RealmKeysUtils, + @SessionFilesDirectory directory: File, + @UserMd5 userMd5: String): RealmConfiguration { + return RealmConfiguration.Builder() + .directory(directory) + .name("matrix-sdk-content-scanning.realm") + .apply { + realmKeysUtils.configureEncryption(this, SessionModule.getKeyAlias(userMd5)) + } + .allowWritesOnUiThread(true) + .modules(ContentScannerRealmModule()) + .build() + } + } + + @Binds + abstract fun bindContentScannerService(service: DefaultContentScannerService): ContentScannerService + + @Binds + abstract fun bindContentScannerStore(store: RealmContentScannerStore): ContentScanningStore + + @Binds + abstract fun bindDownloadEncryptedTask(task: DefaultDownloadEncryptedTask): DownloadEncryptedTask + + @Binds + abstract fun bindGetServerPublicKeyTask(task: DefaultGetServerPublicKeyTask): GetServerPublicKeyTask + + @Binds + abstract fun bindScanMediaTask(task: DefaultScanMediaTask): ScanMediaTask + + @Binds + abstract fun bindScanEncryptedTask(task: DefaultScanEncryptedTask): ScanEncryptedTask +} diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/ContentScanningApiProvider.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/ContentScanningApiProvider.kt new file mode 100644 index 0000000000..44330ce2d0 --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/ContentScanningApiProvider.kt @@ -0,0 +1,15 @@ +/* + * Copyright 2020 New Vector Ltd - All Rights Reserved + * Unauthorized copying of this file, via any medium is strictly prohibited + * Proprietary and confidential + */ + +package im.vector.matrix.android.sdk.internal.session.contentscanning + +import org.matrix.android.sdk.internal.session.SessionScope +import javax.inject.Inject + +@SessionScope +internal class ContentScanningApiProvider @Inject constructor() { + var contentScannerApi: ContentScanApi? = null +} diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/DefaultContentScannerService.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/DefaultContentScannerService.kt new file mode 100644 index 0000000000..e632e8873a --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/DefaultContentScannerService.kt @@ -0,0 +1,150 @@ +/* + * Copyright 2020 New Vector Ltd - All Rights Reserved + * Unauthorized copying of this file, via any medium is strictly prohibited + * Proprietary and confidential + */ + +package im.vector.matrix.android.sdk.internal.session.contentscanning + +import androidx.lifecycle.LiveData +import dagger.Lazy +import im.vector.matrix.android.sdk.internal.session.contentscanning.data.ContentScanningStore +import im.vector.matrix.android.sdk.internal.session.contentscanning.tasks.GetServerPublicKeyTask +import im.vector.matrix.android.sdk.internal.session.contentscanning.tasks.ScanEncryptedTask +import im.vector.matrix.android.sdk.internal.session.contentscanning.tasks.ScanMediaTask +import kotlinx.coroutines.launch +import okhttp3.OkHttpClient +import org.matrix.android.sdk.api.MatrixCallback +import org.matrix.android.sdk.api.NoOpMatrixCallback +import org.matrix.android.sdk.api.session.contentscanning.ContentScannerService +import org.matrix.android.sdk.api.session.contentscanning.ScanState +import org.matrix.android.sdk.api.session.contentscanning.ScanStatusInfo +import org.matrix.android.sdk.api.util.Optional +import org.matrix.android.sdk.internal.crypto.attachments.ElementToDecrypt +import org.matrix.android.sdk.internal.di.Unauthenticated +import org.matrix.android.sdk.internal.network.RetrofitFactory +import org.matrix.android.sdk.internal.session.SessionScope +import org.matrix.android.sdk.internal.task.TaskExecutor +import org.matrix.android.sdk.internal.task.launchToCallback +import org.matrix.android.sdk.internal.util.MatrixCoroutineDispatchers +import org.matrix.android.sdk.internal.util.awaitCallback +import timber.log.Timber +import javax.inject.Inject + +@SessionScope +internal class DefaultContentScannerService @Inject constructor( + private val retrofitFactory: RetrofitFactory, + @Unauthenticated + private val okHttpClient: Lazy, + private val contentScanningApiProvider: ContentScanningApiProvider, + private val contentScanningStore: ContentScanningStore, +// private val sessionParams: SessionParams, + private val getServerPublicKeyTask: GetServerPublicKeyTask, + private val scanEncryptedTask: ScanEncryptedTask, + private val scanMediaTask: ScanMediaTask, + private val taskExecutor: TaskExecutor, + private val coroutineDispatchers: MatrixCoroutineDispatchers +) : ContentScannerService { + + // Cache public key in memory + override var serverPublicKey: String? = null + private set + + override fun getContentScannerServer(): String? { + return contentScanningStore.getScannerUrl() + } + + override fun getServerPublicKey(forceDownload: Boolean, callback: MatrixCallback) { + val api = contentScanningApiProvider.contentScannerApi ?: return Unit.also { + callback.onFailure(IllegalArgumentException("No content scanner defined")) + } + + if (!forceDownload && serverPublicKey != null) { + callback.onSuccess(serverPublicKey) + return + } + taskExecutor.executorScope.launchToCallback(coroutineDispatchers.io, callback) { + getServerPublicKeyTask.execute(GetServerPublicKeyTask.Params(api)).also { + serverPublicKey = it + } + } + } + + override fun getScanResultForAttachment(mxcUrl: String, fileInfo: ElementToDecrypt, callback: MatrixCallback) { + taskExecutor.executorScope.launchToCallback(coroutineDispatchers.io, callback) { + val serverPublicKey = serverPublicKey ?: awaitCallback { + getServerPublicKey(false, it) + } + + val result = scanEncryptedTask.execute(ScanEncryptedTask.Params( + mxcUrl = mxcUrl, + publicServerKey = serverPublicKey, + encryptedInfo = fileInfo + )) + + ScanStatusInfo( + state = if (result.clean) ScanState.TRUSTED else ScanState.INFECTED, + humanReadableMessage = result.info, + scanDateTimestamp = System.currentTimeMillis() + ) + } + } + + override fun getScanResultForAttachment(mxcUrl: String, callback: MatrixCallback) { + taskExecutor.executorScope.launchToCallback(coroutineDispatchers.io, callback) { + val result = scanMediaTask.execute(ScanMediaTask.Params(mxcUrl)) + + ScanStatusInfo( + state = if (result.clean) ScanState.TRUSTED else ScanState.INFECTED, + humanReadableMessage = result.info, + scanDateTimestamp = System.currentTimeMillis() + ) + } + } + + override fun setScannerUrl(url: String?) = contentScanningStore.setScannerUrl(url).also { + if (url == null) { + contentScanningApiProvider.contentScannerApi = null + serverPublicKey = null + } else { + val api = retrofitFactory + .create(okHttpClient, url) + .create(ContentScanApi::class.java) + contentScanningApiProvider.contentScannerApi = api + + taskExecutor.executorScope.launch(coroutineDispatchers.io) { + try { + awaitCallback { + getServerPublicKey(true, it) + } + } catch (failure: Throwable) { + Timber.e("Failed to get public server api") + } + } + } + } + + override fun enableScanner(enabled: Boolean) = contentScanningStore.enableScanning(enabled) + + override fun isScannerEnabled(): Boolean = contentScanningStore.isScanEnabled() + + override fun getCachedScanResultForFile(mxcUrl: String): ScanStatusInfo? { + return contentScanningStore.getScanResult(mxcUrl) + } + + override fun getLiveStatusForFile(mxcUrl: String, fetchIfNeeded: Boolean): LiveData> { + val data = contentScanningStore.getLiveScanResult(mxcUrl) + if (fetchIfNeeded && !contentScanningStore.isScanResultKnownOrInProgress(mxcUrl, getContentScannerServer())) { + getScanResultForAttachment(mxcUrl, NoOpMatrixCallback()) + } + return data + } + + override fun getLiveStatusForEncryptedFile(mxcUrl: String, fileInfo: ElementToDecrypt, fetchIfNeeded: Boolean): LiveData> { + val data = contentScanningStore.getLiveScanResult(mxcUrl) + if (fetchIfNeeded && !contentScanningStore.isScanResultKnownOrInProgress(mxcUrl, getContentScannerServer())) { + getScanResultForAttachment(mxcUrl, fileInfo, NoOpMatrixCallback()) + } + return data + } +} diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/ScanEncryptorUtils.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/ScanEncryptorUtils.kt new file mode 100644 index 0000000000..7d9f833954 --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/ScanEncryptorUtils.kt @@ -0,0 +1,53 @@ +/* + * Copyright 2020 New Vector Ltd - All Rights Reserved + * Unauthorized copying of this file, via any medium is strictly prohibited + * Proprietary and confidential + */ + +package im.vector.matrix.android.sdk.internal.session.contentscanning + +import org.matrix.android.sdk.internal.crypto.attachments.ElementToDecrypt +import org.matrix.android.sdk.internal.crypto.model.rest.EncryptedFileInfo +import org.matrix.android.sdk.internal.crypto.model.rest.EncryptedFileKey +import org.matrix.android.sdk.internal.crypto.tools.withOlmEncryption +import im.vector.matrix.android.sdk.internal.session.contentscanning.model.DownloadBody +import im.vector.matrix.android.sdk.internal.session.contentscanning.model.EncryptedBody +import im.vector.matrix.android.sdk.internal.session.contentscanning.model.toCanonicalJson + +object ScanEncryptorUtils { + + fun getDownloadBodyAndEncryptIfNeeded(publicServerKey: String?, mxcUrl: String, elementToDecrypt: ElementToDecrypt): DownloadBody { + // TODO, upstream refactoring changed the object model here... + // it's bad we have to recreate and use hardcoded values + val encryptedInfo = EncryptedFileInfo( + url = mxcUrl, + iv = elementToDecrypt.iv, + hashes = mapOf("sha256" to elementToDecrypt.sha256), + key = EncryptedFileKey( + k = elementToDecrypt.k, + alg = "A256CTR", + keyOps = listOf("encrypt", "decrypt"), + kty = "oct", + ext = true + ), + v = "v2" + ) + return if (publicServerKey != null) { + // We should encrypt + withOlmEncryption { olm -> + olm.setRecipientKey(publicServerKey) + + val olmResult = olm.encrypt(DownloadBody(encryptedInfo).toCanonicalJson()) + DownloadBody( + encryptedBody = EncryptedBody( + cipherText = olmResult.mCipherText, + ephemeral = olmResult.mEphemeralKey, + mac = olmResult.mMac + ) + ) + } + } else { + DownloadBody(encryptedInfo) + } + } +} diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/data/ContentScanningStore.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/data/ContentScanningStore.kt new file mode 100644 index 0000000000..74bd3e6db3 --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/data/ContentScanningStore.kt @@ -0,0 +1,30 @@ +/* + * Copyright 2020 New Vector Ltd - All Rights Reserved + * Unauthorized copying of this file, via any medium is strictly prohibited + * Proprietary and confidential + */ + +package im.vector.matrix.android.sdk.internal.session.contentscanning.data + +import androidx.lifecycle.LiveData +import org.matrix.android.sdk.api.session.contentscanning.ScanState +import org.matrix.android.sdk.api.session.contentscanning.ScanStatusInfo +import org.matrix.android.sdk.api.util.Optional + +internal interface ContentScanningStore { + + fun getScannerUrl(): String? + + fun setScannerUrl(url: String?) + + fun enableScanning(enabled: Boolean) + + fun isScanEnabled(): Boolean + + fun getScanResult(mxcUrl: String): ScanStatusInfo? + fun getLiveScanResult(mxcUrl: String): LiveData> + fun isScanResultKnownOrInProgress(mxcUrl: String, scannerUrl: String?): Boolean + + fun updateStateForContent(mxcUrl: String, state: ScanState, scannerUrl: String?) + fun updateScanResultForContent(mxcUrl: String, scannerUrl: String?, state: ScanState, humanReadable: String) +} diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/db/ContentScanResultEntity.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/db/ContentScanResultEntity.kt new file mode 100644 index 0000000000..6765461fb1 --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/db/ContentScanResultEntity.kt @@ -0,0 +1,45 @@ +/* + * Copyright 2020 New Vector Ltd - All Rights Reserved + * Unauthorized copying of this file, via any medium is strictly prohibited + * Proprietary and confidential + */ + +package im.vector.matrix.android.sdk.internal.session.contentscanning.db + +import io.realm.RealmObject +import io.realm.annotations.Index +import org.matrix.android.sdk.api.extensions.tryOrNull +import org.matrix.android.sdk.api.session.contentscanning.ScanState +import org.matrix.android.sdk.api.session.contentscanning.ScanStatusInfo + +internal open class ContentScanResultEntity( + @Index + var mediaUrl: String? = null, + var scanStatusString: String? = null, + var humanReadableMessage: String? = null, + var scanDateTimestamp: Long? = null, + var scannerUrl: String? = null +) : RealmObject() { + + var scanResult: ScanState + get() { + return scanStatusString + ?.let { + tryOrNull { ScanState.valueOf(it) } + } + ?: ScanState.UNKNOWN + } + set(result) { + scanStatusString = result.name + } + + fun toModel() : ScanStatusInfo { + return ScanStatusInfo( + state = this.scanResult, + humanReadableMessage = humanReadableMessage, + scanDateTimestamp = scanDateTimestamp + ) + } + + companion object +} diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/db/ContentScannerEntityQueries.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/db/ContentScannerEntityQueries.kt new file mode 100644 index 0000000000..5056f1d1ba --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/db/ContentScannerEntityQueries.kt @@ -0,0 +1,31 @@ +/* + * Copyright 2020 New Vector Ltd - All Rights Reserved + * Unauthorized copying of this file, via any medium is strictly prohibited + * Proprietary and confidential + */ + +package im.vector.matrix.android.sdk.internal.session.contentscanning.db + +import io.realm.Realm +import io.realm.kotlin.createObject +import io.realm.kotlin.where + +internal fun ContentScanResultEntity.Companion.get(realm: Realm, attachmentUrl: String, contentScannerUrl: String?): ContentScanResultEntity? { + return realm.where() + .equalTo(ContentScanResultEntityFields.MEDIA_URL, attachmentUrl) + .apply { + contentScannerUrl?.let { + equalTo(ContentScanResultEntityFields.SCANNER_URL, it) + } + } + .findFirst() +} + +internal fun ContentScanResultEntity.Companion.getOrCreate(realm: Realm, attachmentUrl: String, contentScannerUrl: String?): ContentScanResultEntity { + return ContentScanResultEntity.get(realm, attachmentUrl, contentScannerUrl) + ?: realm.createObject().also { + it.mediaUrl = attachmentUrl + it.scanDateTimestamp = System.currentTimeMillis() + it.scannerUrl = contentScannerUrl + } +} diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/db/ContentScannerInfoEntity.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/db/ContentScannerInfoEntity.kt new file mode 100644 index 0000000000..c54d4e3139 --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/db/ContentScannerInfoEntity.kt @@ -0,0 +1,17 @@ +/* + * Copyright 2020 New Vector Ltd - All Rights Reserved + * Unauthorized copying of this file, via any medium is strictly prohibited + * Proprietary and confidential + */ + +package im.vector.matrix.android.sdk.internal.session.contentscanning.db + +import io.realm.RealmObject + +internal open class ContentScannerInfoEntity( + var serverUrl: String? = null, + var enabled: Boolean? = null +) : RealmObject() { + + companion object +} diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/db/ContentScannerRealmModule.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/db/ContentScannerRealmModule.kt new file mode 100644 index 0000000000..eb40fa13fb --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/db/ContentScannerRealmModule.kt @@ -0,0 +1,19 @@ +/* + * Copyright 2020 New Vector Ltd - All Rights Reserved + * Unauthorized copying of this file, via any medium is strictly prohibited + * Proprietary and confidential + */ + +package im.vector.matrix.android.sdk.internal.session.contentscanning.db + +import io.realm.annotations.RealmModule + +/** + * Realm module for content scanner classes + */ +@RealmModule(library = true, + classes = [ + ContentScannerInfoEntity::class, + ContentScanResultEntity::class + ]) +internal class ContentScannerRealmModule diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/db/RealmContentScannerStore.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/db/RealmContentScannerStore.kt new file mode 100644 index 0000000000..a09343139a --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/db/RealmContentScannerStore.kt @@ -0,0 +1,132 @@ +/* + * Copyright 2020 New Vector Ltd - All Rights Reserved + * Unauthorized copying of this file, via any medium is strictly prohibited + * Proprietary and confidential + */ + +package im.vector.matrix.android.sdk.internal.session.contentscanning.db + +import androidx.lifecycle.LiveData +import androidx.lifecycle.Transformations +import com.zhuinden.monarchy.Monarchy +import im.vector.matrix.android.sdk.internal.session.contentscanning.data.ContentScanningStore +import org.matrix.android.sdk.api.session.contentscanning.ScanState +import org.matrix.android.sdk.api.session.contentscanning.ScanStatusInfo +import io.realm.Realm +import io.realm.RealmConfiguration +import io.realm.kotlin.createObject +import io.realm.kotlin.where +import org.matrix.android.sdk.api.extensions.orFalse +import org.matrix.android.sdk.api.util.Optional +import org.matrix.android.sdk.api.util.toOptional +import org.matrix.android.sdk.internal.di.ContentScannerDatabase +import org.matrix.android.sdk.internal.session.SessionScope +import org.matrix.android.sdk.internal.util.isValidUrl +import javax.inject.Inject + +@SessionScope +internal class RealmContentScannerStore @Inject constructor( + @ContentScannerDatabase + private val realmConfiguration: RealmConfiguration +) : ContentScanningStore { + + private val monarchy = Monarchy.Builder() + .setRealmConfiguration(realmConfiguration) + .build() + + override fun getScannerUrl(): String? { + return monarchy.fetchAllMappedSync( + { realm -> + realm.where() + }, { + it.serverUrl + } + ).firstOrNull() + } + + override fun setScannerUrl(url: String?) { + monarchy.runTransactionSync { realm -> + val info = realm.where().findFirst() + ?: realm.createObject() + info.serverUrl = url + } + } + + override fun enableScanning(enabled: Boolean) { + monarchy.runTransactionSync { realm -> + val info = realm.where().findFirst() + ?: realm.createObject() + info.enabled = enabled + } + } + + override fun isScanEnabled(): Boolean { + return monarchy.fetchAllMappedSync( + { realm -> + realm.where() + }, { + it.enabled.orFalse() && it.serverUrl?.isValidUrl().orFalse() + } + ).firstOrNull().orFalse() + } + + override fun updateStateForContent(mxcUrl: String, state: ScanState, scannerUrl: String?) { + monarchy.runTransactionSync { + ContentScanResultEntity.getOrCreate(it, mxcUrl, scannerUrl).scanResult = state + } + } + + override fun updateScanResultForContent(mxcUrl: String, scannerUrl: String?, state: ScanState, humanReadable: String) { + monarchy.runTransactionSync { + ContentScanResultEntity.getOrCreate(it, mxcUrl, scannerUrl).apply { + scanResult = state + scanDateTimestamp = System.currentTimeMillis() + humanReadableMessage = humanReadable + } + } + } + + override fun isScanResultKnownOrInProgress(mxcUrl: String, scannerUrl: String?): Boolean { + var isKnown = false + monarchy.runTransactionSync { + val info = ContentScanResultEntity.get(it, mxcUrl, scannerUrl)?.scanResult + isKnown = when (info) { + ScanState.IN_PROGRESS, + ScanState.TRUSTED, + ScanState.INFECTED -> true + else -> false + } + } + return isKnown + } + + override fun getScanResult(mxcUrl: String): ScanStatusInfo? { + return monarchy.fetchAllMappedSync({ realm -> + realm.where() + .equalTo(ContentScanResultEntityFields.MEDIA_URL, mxcUrl) + .apply { + getScannerUrl()?.let { + equalTo(ContentScanResultEntityFields.SCANNER_URL, it) + } + } + }, { + it.toModel() + }).firstOrNull() + } + + override fun getLiveScanResult(mxcUrl: String): LiveData> { + val liveData = monarchy.findAllMappedWithChanges( + { realm: Realm -> + realm.where() + .equalTo(ContentScanResultEntityFields.MEDIA_URL, mxcUrl) + .equalTo(ContentScanResultEntityFields.SCANNER_URL, getScannerUrl()) + }, + { entity -> + entity.toModel() + } + ) + return Transformations.map(liveData) { + it.firstOrNull().toOptional() + } + } +} diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/model/DownloadBody.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/model/DownloadBody.kt new file mode 100644 index 0000000000..1dac0a9ced --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/model/DownloadBody.kt @@ -0,0 +1,30 @@ +/* + * Copyright 2020 New Vector Ltd - All Rights Reserved + * Unauthorized copying of this file, via any medium is strictly prohibited + * Proprietary and confidential + */ + +package im.vector.matrix.android.sdk.internal.session.contentscanning.model + +import com.squareup.moshi.Json +import com.squareup.moshi.JsonClass +import org.matrix.android.sdk.internal.crypto.model.rest.EncryptedFileInfo +import org.matrix.android.sdk.internal.di.MoshiProvider +import org.matrix.android.sdk.internal.util.JsonCanonicalizer + +@JsonClass(generateAdapter = true) +data class DownloadBody( + @Json(name = "file") val file: EncryptedFileInfo? = null, + @Json(name = "encrypted_body") val encryptedBody: EncryptedBody? = null +) + +@JsonClass(generateAdapter = true) +data class EncryptedBody( + @Json(name = "ciphertext") val cipherText: String, + @Json(name = "mac") val mac: String, + @Json(name = "ephemeral") val ephemeral: String +) + +fun DownloadBody.toJson(): String = MoshiProvider.providesMoshi().adapter(DownloadBody::class.java).toJson(this) + +fun DownloadBody.toCanonicalJson() = JsonCanonicalizer.getCanonicalJson(DownloadBody::class.java, this) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/model/ScanResponse.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/model/ScanResponse.kt new file mode 100644 index 0000000000..ca4c072b84 --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/model/ScanResponse.kt @@ -0,0 +1,23 @@ +/* + * Copyright 2020 New Vector Ltd - All Rights Reserved + * Unauthorized copying of this file, via any medium is strictly prohibited + * Proprietary and confidential + */ + +package im.vector.matrix.android.sdk.internal.session.contentscanning.model + +import com.squareup.moshi.Json +import com.squareup.moshi.JsonClass + +/** + * { + * "clean": true, + * "info": "File clean at 6/7/2018, 6:02:40 PM" + * } + */ +@JsonClass(generateAdapter = true) +data class ScanResponse( + @Json(name = "clean") val clean: Boolean, + /** Human-readable information about the result. */ + @Json(name = "info") val info: String? +) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/model/ServerPublicKeyResponse.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/model/ServerPublicKeyResponse.kt new file mode 100644 index 0000000000..57ba2f8263 --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/model/ServerPublicKeyResponse.kt @@ -0,0 +1,16 @@ +/* + * Copyright 2020 New Vector Ltd - All Rights Reserved + * Unauthorized copying of this file, via any medium is strictly prohibited + * Proprietary and confidential + */ + +package im.vector.matrix.android.sdk.internal.session.contentscanning.model + +import com.squareup.moshi.Json +import com.squareup.moshi.JsonClass + +@JsonClass(generateAdapter = true) +data class ServerPublicKeyResponse( + @Json(name = "public_key") + val publicKey : String? +) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/tasks/DownloadEncryptedTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/tasks/DownloadEncryptedTask.kt new file mode 100644 index 0000000000..04f2174e67 --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/tasks/DownloadEncryptedTask.kt @@ -0,0 +1,41 @@ +/* + * Copyright 2020 New Vector Ltd - All Rights Reserved + * Unauthorized copying of this file, via any medium is strictly prohibited + * Proprietary and confidential + */ + +package im.vector.matrix.android.sdk.internal.session.contentscanning.tasks + +import im.vector.matrix.android.sdk.internal.session.contentscanning.ContentScanningApiProvider +import im.vector.matrix.android.sdk.internal.session.contentscanning.ScanEncryptorUtils +import okhttp3.ResponseBody +import org.matrix.android.sdk.internal.crypto.attachments.ElementToDecrypt +import org.matrix.android.sdk.internal.network.executeRequest +import org.matrix.android.sdk.internal.task.Task +import javax.inject.Inject + +internal interface DownloadEncryptedTask : Task { + data class Params( + val publicServerKey: String?, + val encryptedInfo: ElementToDecrypt, + val mxcUrl: String + ) +} + +internal class DefaultDownloadEncryptedTask @Inject constructor( + private val contentScanningApiProvider: ContentScanningApiProvider +) : DownloadEncryptedTask { + + override suspend fun execute(params: DownloadEncryptedTask.Params): ResponseBody { + val dlBody = ScanEncryptorUtils.getDownloadBodyAndEncryptIfNeeded( + params.publicServerKey, + params.mxcUrl, + params.encryptedInfo + ) + + val api = contentScanningApiProvider.contentScannerApi ?: throw IllegalArgumentException() + return executeRequest(null) { + api.downloadEncrypted(dlBody) + } + } +} diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/tasks/GetServerPublicKeyTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/tasks/GetServerPublicKeyTask.kt new file mode 100644 index 0000000000..bcf1fadb47 --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/tasks/GetServerPublicKeyTask.kt @@ -0,0 +1,29 @@ +/* + * Copyright 2020 New Vector Ltd - All Rights Reserved + * Unauthorized copying of this file, via any medium is strictly prohibited + * Proprietary and confidential + */ + +package im.vector.matrix.android.sdk.internal.session.contentscanning.tasks + +import im.vector.matrix.android.sdk.internal.session.contentscanning.ContentScanApi +import im.vector.matrix.android.sdk.internal.session.contentscanning.model.ServerPublicKeyResponse +import org.matrix.android.sdk.internal.network.executeRequest +import org.matrix.android.sdk.internal.task.Task + +import javax.inject.Inject + +internal interface GetServerPublicKeyTask : Task { + data class Params( + val contentScanApi: ContentScanApi + ) +} + +internal class DefaultGetServerPublicKeyTask @Inject constructor() : GetServerPublicKeyTask { + + override suspend fun execute(params: GetServerPublicKeyTask.Params): String? { + return executeRequest(null) { + params.contentScanApi.getServerPublicKey() + }.publicKey + } +} diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/tasks/ScanEncryptedTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/tasks/ScanEncryptedTask.kt new file mode 100644 index 0000000000..1078a131e2 --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/tasks/ScanEncryptedTask.kt @@ -0,0 +1,57 @@ +/* + * Copyright 2020 New Vector Ltd - All Rights Reserved + * Unauthorized copying of this file, via any medium is strictly prohibited + * Proprietary and confidential + */ + +package im.vector.matrix.android.sdk.internal.session.contentscanning.tasks + +import im.vector.matrix.android.sdk.internal.session.contentscanning.ContentScanningApiProvider +import im.vector.matrix.android.sdk.internal.session.contentscanning.ScanEncryptorUtils +import im.vector.matrix.android.sdk.internal.session.contentscanning.data.ContentScanningStore +import im.vector.matrix.android.sdk.internal.session.contentscanning.model.ScanResponse +import org.matrix.android.sdk.api.failure.toScanFailure +import org.matrix.android.sdk.api.session.contentscanning.ScanState +import org.matrix.android.sdk.internal.crypto.attachments.ElementToDecrypt +import org.matrix.android.sdk.internal.network.executeRequest +import org.matrix.android.sdk.internal.task.Task +import javax.inject.Inject + +internal interface ScanEncryptedTask : Task { + data class Params( + val mxcUrl: String, + val publicServerKey: String?, + val encryptedInfo: ElementToDecrypt + ) +} + +internal class DefaultScanEncryptedTask @Inject constructor( + private val contentScanningApiProvider: ContentScanningApiProvider, + private val contentScanningStore: ContentScanningStore +) : ScanEncryptedTask { + + override suspend fun execute(params: ScanEncryptedTask.Params): ScanResponse { + val mxcUrl = params.mxcUrl + val dlBody = ScanEncryptorUtils.getDownloadBodyAndEncryptIfNeeded(params.publicServerKey, params.mxcUrl, params.encryptedInfo) + + val scannerUrl = contentScanningStore.getScannerUrl() + contentScanningStore.updateStateForContent(params.mxcUrl, ScanState.IN_PROGRESS, scannerUrl) + + try { + val api = contentScanningApiProvider.contentScannerApi ?: throw IllegalArgumentException() + val executeRequest = executeRequest(null) { + api.scanFile(dlBody) + } + contentScanningStore.updateScanResultForContent( + mxcUrl, + scannerUrl, + ScanState.TRUSTED.takeIf { executeRequest.clean } ?: ScanState.INFECTED, + executeRequest.info ?: "" + ) + return executeRequest + } catch (failure: Throwable) { + contentScanningStore.updateStateForContent(params.mxcUrl, ScanState.UNKNOWN, scannerUrl) + throw failure.toScanFailure() ?: failure + } + } +} diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/tasks/ScanMediaTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/tasks/ScanMediaTask.kt new file mode 100644 index 0000000000..b7ed948afc --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/tasks/ScanMediaTask.kt @@ -0,0 +1,65 @@ +/* + * Copyright 2020 New Vector Ltd - All Rights Reserved + * Unauthorized copying of this file, via any medium is strictly prohibited + * Proprietary and confidential + */ + +package im.vector.matrix.android.sdk.internal.session.contentscanning.tasks + +import im.vector.matrix.android.sdk.internal.session.contentscanning.ContentScanningApiProvider +import im.vector.matrix.android.sdk.internal.session.contentscanning.data.ContentScanningStore +import im.vector.matrix.android.sdk.internal.session.contentscanning.model.ScanResponse +import org.matrix.android.sdk.api.failure.toScanFailure +import org.matrix.android.sdk.api.session.contentscanning.ScanState +import org.matrix.android.sdk.internal.network.executeRequest +import org.matrix.android.sdk.internal.task.Task +import javax.inject.Inject + +internal interface ScanMediaTask : Task { + data class Params( + val mxcUrl: String + ) +} + +internal class DefaultScanMediaTask @Inject constructor( + private val contentScanningApiProvider: ContentScanningApiProvider, + private val contentScanningStore: ContentScanningStore +) : ScanMediaTask { + + override suspend fun execute(params: ScanMediaTask.Params): ScanResponse { + // "mxc://server.org/QNDpzLopkoQYNikJfoZCQuCXJ" + if (!params.mxcUrl.startsWith("mxc://")) { + throw IllegalAccessException("Invalid mxc url") + } + val scannerUrl = contentScanningStore.getScannerUrl() + contentScanningStore.updateStateForContent(params.mxcUrl, ScanState.IN_PROGRESS, scannerUrl) + + var serverAndMediaId = params.mxcUrl.removePrefix("mxc://") + val fragmentOffset = serverAndMediaId.indexOf("#") + if (fragmentOffset >= 0) { + serverAndMediaId = serverAndMediaId.substring(0, fragmentOffset) + } + + val split = serverAndMediaId.split("/") + if (split.size != 2) { + throw IllegalAccessException("Invalid mxc url") + } + + try { + val scanResponse = executeRequest(null) { + val api = contentScanningApiProvider.contentScannerApi ?: throw IllegalArgumentException() + api.scanMedia(split[0], split[1]) + } + contentScanningStore.updateScanResultForContent( + params.mxcUrl, + scannerUrl, + ScanState.TRUSTED.takeIf { scanResponse.clean } ?: ScanState.INFECTED, + scanResponse.info ?: "" + ) + return scanResponse + } catch (failure: Throwable) { + contentScanningStore.updateStateForContent(params.mxcUrl, ScanState.UNKNOWN, scannerUrl) + throw failure.toScanFailure() ?: failure + } + } +} diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/failure/Extensions.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/failure/Extensions.kt index b2035bb2eb..dec396ad1b 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/failure/Extensions.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/failure/Extensions.kt @@ -18,6 +18,8 @@ package org.matrix.android.sdk.api.failure import org.matrix.android.sdk.api.auth.registration.RegistrationFlowResponse import org.matrix.android.sdk.api.extensions.tryOrNull +import org.matrix.android.sdk.api.session.contentscanning.ContentScanError +import org.matrix.android.sdk.api.session.contentscanning.ScanFailure import org.matrix.android.sdk.internal.di.MoshiProvider import java.io.IOException import javax.net.ssl.HttpsURLConnection @@ -100,3 +102,19 @@ fun Throwable.isRegistrationAvailabilityError(): Boolean { error.code == MatrixError.M_INVALID_USERNAME || error.code == MatrixError.M_EXCLUSIVE) } + +/** + * Try to convert to a ScanFailure. Return null in the cases it's not possible + */ +fun Throwable.toScanFailure(): ScanFailure? { + return if (this is Failure.OtherServerError) { + tryOrNull { + MoshiProvider.providesMoshi() + .adapter(ContentScanError::class.java) + .fromJson(errorBody) + } + ?.let { ScanFailure(it, httpCode, this) } + } else { + null + } +} diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/Session.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/Session.kt index dfe43aed6f..97197a3933 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/Session.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/Session.kt @@ -31,6 +31,7 @@ import org.matrix.android.sdk.api.session.cache.CacheService import org.matrix.android.sdk.api.session.call.CallSignalingService import org.matrix.android.sdk.api.session.content.ContentUploadStateTracker import org.matrix.android.sdk.api.session.content.ContentUrlResolver +import org.matrix.android.sdk.api.session.contentscanning.ContentScannerService import org.matrix.android.sdk.api.session.crypto.CryptoService import org.matrix.android.sdk.api.session.events.EventService import org.matrix.android.sdk.api.session.file.ContentDownloadStateTracker @@ -192,6 +193,11 @@ interface Session : */ fun cryptoService(): CryptoService + /** + * Returns the ContentScanningService associated with the session + */ + fun contentScanningService(): ContentScannerService + /** * Returns the identity service associated with the session */ diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/content/ContentUrlResolver.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/content/ContentUrlResolver.kt index 36c471bb2b..56e3cd9796 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/content/ContentUrlResolver.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/content/ContentUrlResolver.kt @@ -16,6 +16,8 @@ package org.matrix.android.sdk.api.session.content +import org.matrix.android.sdk.internal.crypto.attachments.ElementToDecrypt + /** * This interface defines methods for accessing content from the current session. */ @@ -39,6 +41,15 @@ interface ContentUrlResolver { */ fun resolveFullSize(contentUrl: String?): String? + /** + * Get the ResolvedMethod to download a URL + * + * @param contentUrl the Matrix media content URI (in the form of "mxc://..."). + * @param elementToDecrypt Encryption data may be required if you use a content scanner + * @return the Method to access resource, or null if invalid + */ + fun resolveForDownload(contentUrl: String?, elementToDecrypt: ElementToDecrypt? = null): ResolvedMethod? + /** * Get the actual URL for accessing the thumbnail image of a given Matrix media content URI. * @@ -49,4 +60,9 @@ interface ContentUrlResolver { * @return the URL to access the described resource, or null if the url is invalid. */ fun resolveThumbnail(contentUrl: String?, width: Int, height: Int, method: ThumbnailMethod): String? + + sealed class ResolvedMethod { + data class GET(val url: String) : ResolvedMethod() + data class POST(val url:String, val jsonBody: String): ResolvedMethod() + } } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/contentscanning/ContentScanError.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/contentscanning/ContentScanError.kt new file mode 100644 index 0000000000..aeb4d3c287 --- /dev/null +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/contentscanning/ContentScanError.kt @@ -0,0 +1,39 @@ +/* + * Copyright 2020 New Vector Ltd - All Rights Reserved + * Unauthorized copying of this file, via any medium is strictly prohibited + * Proprietary and confidential + */ + +package org.matrix.android.sdk.api.session.contentscanning + +import com.squareup.moshi.Json +import com.squareup.moshi.JsonClass + +@JsonClass(generateAdapter = true) +data class ContentScanError( + @Json(name = "info") val info: String? = null, + @Json(name = "reason") val reason: String? = null +) { + companion object { + // 502 The server failed to request media from the media repo. + const val REASON_MCS_MEDIA_REQUEST_FAILED = "MCS_MEDIA_REQUEST_FAILED" + + /* 400 The server failed to decrypt the encrypted media downloaded from the media repo.*/ + const val REASON_MCS_MEDIA_FAILED_TO_DECRYPT = "MCS_MEDIA_FAILED_TO_DECRYPT" + + /* 403 The server scanned the downloaded media but the antivirus script returned a non-zero exit code.*/ + const val REASON_MCS_MEDIA_NOT_CLEAN = "MCS_MEDIA_NOT_CLEAN" + + /* 403 The provided encrypted_body could not be decrypted. The client should request the public key of the server and then retry (once).*/ + const val REASON_MCS_BAD_DECRYPTION = "MCS_BAD_DECRYPTION" + + /* 400 The request body contains malformed JSON.*/ + const val REASON_MCS_MALFORMED_JSON = "MCS_MALFORMED_JSON" + } +} + +class ScanFailure(val error: ContentScanError, val httpCode: Int, cause: Throwable? = null) : Throwable(cause = cause) + +// For Glide, which deals with Exception and not with Throwable +fun ScanFailure.toException() = Exception(this) +fun Throwable.toScanFailure() = this.cause as? ScanFailure diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/contentscanning/ContentScannerService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/contentscanning/ContentScannerService.kt new file mode 100644 index 0000000000..53975f3706 --- /dev/null +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/contentscanning/ContentScannerService.kt @@ -0,0 +1,35 @@ +/* + * Copyright 2020 New Vector Ltd - All Rights Reserved + * Unauthorized copying of this file, via any medium is strictly prohibited + * Proprietary and confidential + */ + +package org.matrix.android.sdk.api.session.contentscanning + +import androidx.lifecycle.LiveData +import org.matrix.android.sdk.api.MatrixCallback +import org.matrix.android.sdk.api.util.Optional +import org.matrix.android.sdk.internal.crypto.attachments.ElementToDecrypt + +interface ContentScannerService { + + val serverPublicKey: String? + + fun getContentScannerServer(): String? + /** + * Get the current public curve25519 key that the AV server is advertising. + * @param callback on success callback containing the server public key + */ + fun getServerPublicKey(forceDownload: Boolean = false, callback: MatrixCallback) + + fun getScanResultForAttachment(mxcUrl: String, callback: MatrixCallback) + fun getScanResultForAttachment(mxcUrl: String, fileInfo: ElementToDecrypt, callback: MatrixCallback) + + fun setScannerUrl(url: String?) + + fun enableScanner(enabled: Boolean) + fun isScannerEnabled(): Boolean + fun getLiveStatusForFile(mxcUrl: String, fetchIfNeeded: Boolean = true): LiveData> + fun getLiveStatusForEncryptedFile(mxcUrl: String, fileInfo: ElementToDecrypt, fetchIfNeeded: Boolean = true): LiveData> + fun getCachedScanResultForFile(mxcUrl: String): ScanStatusInfo? +} diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/contentscanning/DisabledContentScannerService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/contentscanning/DisabledContentScannerService.kt new file mode 100644 index 0000000000..9b003b65fd --- /dev/null +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/contentscanning/DisabledContentScannerService.kt @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2021 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. + */ + +package org.matrix.android.sdk.internal.session.contentscanning + +import androidx.lifecycle.LiveData +import androidx.lifecycle.MutableLiveData +import org.matrix.android.sdk.api.MatrixCallback +import org.matrix.android.sdk.api.session.contentscanning.ContentScannerService +import org.matrix.android.sdk.api.session.contentscanning.ScanStatusInfo +import org.matrix.android.sdk.api.util.Optional +import org.matrix.android.sdk.internal.crypto.attachments.ElementToDecrypt + +/** + * Created to by-pass ProfileTask execution in LoginWizard. + */ +class DisabledContentScannerService : ContentScannerService { + + override val serverPublicKey: String? + get() = null + + override fun getContentScannerServer(): String? { + return null + } + + override fun getServerPublicKey(forceDownload: Boolean, callback: MatrixCallback) { + } + + override fun getScanResultForAttachment(mxcUrl: String, callback: MatrixCallback) { + } + + override fun getScanResultForAttachment(mxcUrl: String, fileInfo: ElementToDecrypt, callback: MatrixCallback) { + } + + override fun setScannerUrl(url: String?) { + } + + override fun enableScanner(enabled: Boolean) { + } + + override fun isScannerEnabled(): Boolean { + return false + } + + override fun getLiveStatusForFile(mxcUrl: String, fetchIfNeeded: Boolean): LiveData> { + return MutableLiveData() + } + + override fun getLiveStatusForEncryptedFile(mxcUrl: String, fileInfo: ElementToDecrypt, fetchIfNeeded: Boolean): LiveData> { + return MutableLiveData() + } + + override fun getCachedScanResultForFile(mxcUrl: String): ScanStatusInfo? { + return null + } +} diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/contentscanning/ScanState.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/contentscanning/ScanState.kt new file mode 100644 index 0000000000..3371878221 --- /dev/null +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/contentscanning/ScanState.kt @@ -0,0 +1,20 @@ +/* + * Copyright 2020 New Vector Ltd - All Rights Reserved + * Unauthorized copying of this file, via any medium is strictly prohibited + * Proprietary and confidential + */ + +package org.matrix.android.sdk.api.session.contentscanning + +enum class ScanState { + TRUSTED, + INFECTED, + UNKNOWN, + IN_PROGRESS +} + +data class ScanStatusInfo( + val state : ScanState, + val scanDateTimestamp: Long?, + val humanReadableMessage: String? +) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/login/DefaultLoginWizard.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/login/DefaultLoginWizard.kt index b72cff3cf1..111d1263fb 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/login/DefaultLoginWizard.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/login/DefaultLoginWizard.kt @@ -33,6 +33,7 @@ import org.matrix.android.sdk.internal.auth.registration.AddThreePidRegistration import org.matrix.android.sdk.internal.auth.registration.RegisterAddThreePidTask import org.matrix.android.sdk.internal.network.executeRequest import org.matrix.android.sdk.internal.session.content.DefaultContentUrlResolver +import org.matrix.android.sdk.internal.session.contentscanning.DisabledContentScannerService internal class DefaultLoginWizard( private val authAPI: AuthAPI, @@ -44,7 +45,7 @@ internal class DefaultLoginWizard( private val getProfileTask: GetProfileTask = DefaultGetProfileTask( authAPI, - DefaultContentUrlResolver(pendingSessionData.homeServerConnectionConfig) + DefaultContentUrlResolver(pendingSessionData.homeServerConnectionConfig, DisabledContentScannerService()) ) override suspend fun getProfileInfo(matrixId: String): LoginProfileInfo { diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/di/DbQualifiers.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/di/DbQualifiers.kt index 49e155add8..8b78cd0d42 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/di/DbQualifiers.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/di/DbQualifiers.kt @@ -37,3 +37,7 @@ internal annotation class CryptoDatabase @Qualifier @Retention(AnnotationRetention.RUNTIME) internal annotation class IdentityDatabase + +@Qualifier +@Retention(AnnotationRetention.RUNTIME) +internal annotation class ContentScannerDatabase diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/network/NetworkConstants.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/network/NetworkConstants.kt index 361a306d4f..1ab1042129 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/network/NetworkConstants.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/network/NetworkConstants.kt @@ -38,6 +38,9 @@ internal object NetworkConstants { // Integration const val URI_INTEGRATION_MANAGER_PATH = "_matrix/integrations/v1/" + // Content scanner + const val URI_API_PREFIX_PATH_MEDIA_PROXY_UNSTABLE = "_matrix/media_proxy/unstable/" + // Federation const val URI_FEDERATION_PATH = "_matrix/federation/v1/" } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultSession.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultSession.kt index c52462612a..7c352a8f99 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultSession.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultSession.kt @@ -35,6 +35,7 @@ import org.matrix.android.sdk.api.session.cache.CacheService import org.matrix.android.sdk.api.session.call.CallSignalingService import org.matrix.android.sdk.api.session.content.ContentUploadStateTracker import org.matrix.android.sdk.api.session.content.ContentUrlResolver +import org.matrix.android.sdk.api.session.contentscanning.ContentScannerService import org.matrix.android.sdk.api.session.crypto.CryptoService import org.matrix.android.sdk.api.session.events.EventService import org.matrix.android.sdk.api.session.file.ContentDownloadStateTracker @@ -124,6 +125,7 @@ internal class DefaultSession @Inject constructor( private val _sharedSecretStorageService: Lazy, private val accountService: Lazy, private val eventService: Lazy, + private val contentScannerService: Lazy, private val identityService: IdentityService, private val integrationManagerService: IntegrationManagerService, private val thirdPartyService: Lazy, @@ -275,6 +277,8 @@ internal class DefaultSession @Inject constructor( override fun cryptoService(): CryptoService = cryptoService.get() + override fun contentScanningService(): ContentScannerService = contentScannerService.get() + override fun identityService() = identityService override fun fileService(): FileService = defaultFileService.get() diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/SessionComponent.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/SessionComponent.kt index bc8a707530..7280744eeb 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/SessionComponent.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/SessionComponent.kt @@ -18,6 +18,7 @@ package org.matrix.android.sdk.internal.session import dagger.BindsInstance import dagger.Component +import im.vector.matrix.android.sdk.internal.session.contentscanning.ContentScannerModule import org.matrix.android.sdk.api.MatrixCoroutineDispatchers import org.matrix.android.sdk.api.auth.data.SessionParams import org.matrix.android.sdk.api.session.Session @@ -94,6 +95,7 @@ import org.matrix.android.sdk.internal.util.system.SystemModule AccountModule::class, FederationModule::class, CallModule::class, + ContentScannerModule::class, SearchModule::class, ThirdPartyModule::class, SpaceModule::class, diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/content/DefaultContentUrlResolver.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/content/DefaultContentUrlResolver.kt index 5c8cf99dc6..05abc372b4 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/content/DefaultContentUrlResolver.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/content/DefaultContentUrlResolver.kt @@ -20,11 +20,16 @@ import org.matrix.android.sdk.api.MatrixUrls import org.matrix.android.sdk.api.MatrixUrls.isMxcUrl import org.matrix.android.sdk.api.auth.data.HomeServerConnectionConfig import org.matrix.android.sdk.api.session.content.ContentUrlResolver +import org.matrix.android.sdk.api.session.contentscanning.ContentScannerService +import org.matrix.android.sdk.internal.crypto.attachments.ElementToDecrypt import org.matrix.android.sdk.internal.network.NetworkConstants import org.matrix.android.sdk.internal.util.ensureTrailingSlash import javax.inject.Inject -internal class DefaultContentUrlResolver @Inject constructor(homeServerConnectionConfig: HomeServerConnectionConfig) : ContentUrlResolver { +internal class DefaultContentUrlResolver @Inject constructor( + homeServerConnectionConfig: HomeServerConnectionConfig, + private val scannerService: ContentScannerService +) : ContentUrlResolver { private val baseUrl = homeServerConnectionConfig.homeServerUriBase.toString().ensureTrailingSlash() @@ -55,6 +60,10 @@ internal class DefaultContentUrlResolver @Inject constructor(homeServerConnectio } } + override fun resolveForDownload(contentUrl: String?, elementToDecrypt: ElementToDecrypt?): ContentUrlResolver.ResolvedMethod? { + return resolveFullSize(contentUrl)?.let { ContentUrlResolver.ResolvedMethod.GET(it) } + } + private fun resolve(contentUrl: String, prefix: String, params: String = ""): String? { From 0ad66446e7918669d434539b4cbe0de862a77497 Mon Sep 17 00:00:00 2001 From: yostyle Date: Tue, 2 Nov 2021 08:47:08 +0100 Subject: [PATCH 2/7] Move to content scanner matrix SDK to FOSS --- .../content/ElementContentUrlResolver.kt | 112 ------------------ .../sdk/internal/session/SessionComponent.kt | 2 +- .../content/DefaultContentUrlResolver.kt | 48 ++++++-- .../session/contentscanning/ContentScanApi.kt | 8 +- .../contentscanning/ContentScannerModule.kt | 24 ++-- .../ContentScanningApiProvider.kt | 3 +- .../DefaultContentScannerService.kt | 12 +- .../DisabledContentScannerService.kt | 0 .../contentscanning/ScanEncryptorUtils.kt | 8 +- .../data/ContentScanningStore.kt | 2 +- .../db/ContentScanResultEntity.kt | 2 +- .../db/ContentScannerEntityQueries.kt | 2 +- .../db/ContentScannerInfoEntity.kt | 2 +- .../db/ContentScannerRealmModule.kt | 2 +- .../db/RealmContentScannerStore.kt | 4 +- .../contentscanning/model/DownloadBody.kt | 2 +- .../contentscanning/model/ScanResponse.kt | 2 +- .../model/ServerPublicKeyResponse.kt | 2 +- .../tasks/DownloadEncryptedTask.kt | 6 +- .../tasks/GetServerPublicKeyTask.kt | 7 +- .../tasks/ScanEncryptedTask.kt | 10 +- .../contentscanning/tasks/ScanMediaTask.kt | 8 +- 22 files changed, 94 insertions(+), 174 deletions(-) delete mode 100644 matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/content/ElementContentUrlResolver.kt rename matrix-sdk-android/src/main/java/{im/vector => org}/matrix/android/sdk/internal/session/contentscanning/ContentScanApi.kt (76%) rename matrix-sdk-android/src/main/java/{im/vector => org}/matrix/android/sdk/internal/session/contentscanning/ContentScannerModule.kt (66%) rename matrix-sdk-android/src/main/java/{im/vector => org}/matrix/android/sdk/internal/session/contentscanning/ContentScanningApiProvider.kt (73%) rename matrix-sdk-android/src/main/java/{im/vector => org}/matrix/android/sdk/internal/session/contentscanning/DefaultContentScannerService.kt (92%) rename matrix-sdk-android/src/main/java/org/matrix/android/sdk/{api => internal}/session/contentscanning/DisabledContentScannerService.kt (100%) rename matrix-sdk-android/src/main/java/{im/vector => org}/matrix/android/sdk/internal/session/contentscanning/ScanEncryptorUtils.kt (85%) rename matrix-sdk-android/src/main/java/{im/vector => org}/matrix/android/sdk/internal/session/contentscanning/data/ContentScanningStore.kt (93%) rename matrix-sdk-android/src/main/java/{im/vector => org}/matrix/android/sdk/internal/session/contentscanning/db/ContentScanResultEntity.kt (94%) rename matrix-sdk-android/src/main/java/{im/vector => org}/matrix/android/sdk/internal/session/contentscanning/db/ContentScannerEntityQueries.kt (94%) rename matrix-sdk-android/src/main/java/{im/vector => org}/matrix/android/sdk/internal/session/contentscanning/db/ContentScannerInfoEntity.kt (83%) rename matrix-sdk-android/src/main/java/{im/vector => org}/matrix/android/sdk/internal/session/contentscanning/db/ContentScannerRealmModule.kt (86%) rename matrix-sdk-android/src/main/java/{im/vector => org}/matrix/android/sdk/internal/session/contentscanning/db/RealmContentScannerStore.kt (96%) rename matrix-sdk-android/src/main/java/{im/vector => org}/matrix/android/sdk/internal/session/contentscanning/model/DownloadBody.kt (93%) rename matrix-sdk-android/src/main/java/{im/vector => org}/matrix/android/sdk/internal/session/contentscanning/model/ScanResponse.kt (87%) rename matrix-sdk-android/src/main/java/{im/vector => org}/matrix/android/sdk/internal/session/contentscanning/model/ServerPublicKeyResponse.kt (83%) rename matrix-sdk-android/src/main/java/{im/vector => org}/matrix/android/sdk/internal/session/contentscanning/tasks/DownloadEncryptedTask.kt (83%) rename matrix-sdk-android/src/main/java/{im/vector => org}/matrix/android/sdk/internal/session/contentscanning/tasks/GetServerPublicKeyTask.kt (75%) rename matrix-sdk-android/src/main/java/{im/vector => org}/matrix/android/sdk/internal/session/contentscanning/tasks/ScanEncryptedTask.kt (82%) rename matrix-sdk-android/src/main/java/{im/vector => org}/matrix/android/sdk/internal/session/contentscanning/tasks/ScanMediaTask.kt (87%) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/content/ElementContentUrlResolver.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/content/ElementContentUrlResolver.kt deleted file mode 100644 index e2f0ac2499..0000000000 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/content/ElementContentUrlResolver.kt +++ /dev/null @@ -1,112 +0,0 @@ -/* - * Copyright 2020 The Matrix.org Foundation C.I.C. - * - * 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. - */ - -package im.vector.matrix.android.sdk.internal.session.content - -import im.vector.matrix.android.sdk.internal.session.contentscanning.ScanEncryptorUtils -import im.vector.matrix.android.sdk.internal.session.contentscanning.model.toJson -import org.matrix.android.sdk.api.MatrixUrls -import org.matrix.android.sdk.api.MatrixUrls.isMxcUrl -import org.matrix.android.sdk.api.auth.data.HomeServerConnectionConfig -import org.matrix.android.sdk.api.session.content.ContentUrlResolver -import org.matrix.android.sdk.api.session.contentscanning.ContentScannerService -import org.matrix.android.sdk.internal.crypto.attachments.ElementToDecrypt -import org.matrix.android.sdk.internal.network.NetworkConstants -import org.matrix.android.sdk.internal.util.ensureTrailingSlash -import javax.inject.Inject - -internal class ElementContentUrlResolver @Inject constructor( - homeServerConnectionConfig: HomeServerConnectionConfig, - private val scannerService: ContentScannerService -) : ContentUrlResolver { - - private val baseUrl = homeServerConnectionConfig.homeServerUriBase.toString().ensureTrailingSlash() - - override val uploadUrl = baseUrl + NetworkConstants.URI_API_MEDIA_PREFIX_PATH_R0 + "upload" - - override fun resolveForDownload(contentUrl: String?, elementToDecrypt: ElementToDecrypt?): ContentUrlResolver.ResolvedMethod? { - return if (scannerService.isScannerEnabled() && elementToDecrypt != null) { - val baseUrl = scannerService.getContentScannerServer() - val sep = if (baseUrl?.endsWith("/") == true) "" else "/" - - val url = baseUrl + sep + NetworkConstants.URI_API_PREFIX_PATH_MEDIA_PROXY_UNSTABLE + "download_encrypted" - - ContentUrlResolver.ResolvedMethod.POST( - url = url, - jsonBody = ScanEncryptorUtils - .getDownloadBodyAndEncryptIfNeeded(scannerService.serverPublicKey, contentUrl ?: "", elementToDecrypt) - .toJson() - ) - } else { - resolveFullSize(contentUrl)?.let { ContentUrlResolver.ResolvedMethod.GET(it) } - } - } - - override fun resolveFullSize(contentUrl: String?): String? { - return contentUrl - // do not allow non-mxc content URLs - ?.takeIf { it.isMxcUrl() } - ?.let { - resolve( - contentUrl = it, - toThumbnail = false - ) - } - } - - override fun resolveThumbnail(contentUrl: String?, width: Int, height: Int, method: ContentUrlResolver.ThumbnailMethod): String? { - return contentUrl - // do not allow non-mxc content URLs - ?.takeIf { it.isMxcUrl() } - ?.let { - resolve( - contentUrl = it, - toThumbnail = true, - params = "?width=$width&height=$height&method=${method.value}" - ) - } - } - - private fun resolve(contentUrl: String, - toThumbnail: Boolean, - params: String = ""): String? { - var serverAndMediaId = contentUrl.removePrefix(MatrixUrls.MATRIX_CONTENT_URI_SCHEME) - - val apiPath = if (scannerService.isScannerEnabled()) { - NetworkConstants.URI_API_PREFIX_PATH_MEDIA_PROXY_UNSTABLE - } else { - NetworkConstants.URI_API_MEDIA_PREFIX_PATH_R0 - } - val prefix = if (toThumbnail) { - apiPath + "thumbnail/" - } else { - apiPath + "download/" - } - val fragmentOffset = serverAndMediaId.indexOf("#") - var fragment = "" - if (fragmentOffset >= 0) { - fragment = serverAndMediaId.substring(fragmentOffset) - serverAndMediaId = serverAndMediaId.substring(0, fragmentOffset) - } - - val resolvedUrl = if (scannerService.isScannerEnabled()) { - scannerService.getContentScannerServer()!!.ensureTrailingSlash() - } else { - baseUrl - } - return resolvedUrl + prefix + serverAndMediaId + params + fragment - } -} diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/SessionComponent.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/SessionComponent.kt index 7280744eeb..8e6310026f 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/SessionComponent.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/SessionComponent.kt @@ -18,7 +18,7 @@ package org.matrix.android.sdk.internal.session import dagger.BindsInstance import dagger.Component -import im.vector.matrix.android.sdk.internal.session.contentscanning.ContentScannerModule +import org.matrix.android.sdk.internal.session.contentscanning.ContentScannerModule import org.matrix.android.sdk.api.MatrixCoroutineDispatchers import org.matrix.android.sdk.api.auth.data.SessionParams import org.matrix.android.sdk.api.session.Session diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/content/DefaultContentUrlResolver.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/content/DefaultContentUrlResolver.kt index 05abc372b4..4124d5c437 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/content/DefaultContentUrlResolver.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/content/DefaultContentUrlResolver.kt @@ -23,6 +23,8 @@ import org.matrix.android.sdk.api.session.content.ContentUrlResolver import org.matrix.android.sdk.api.session.contentscanning.ContentScannerService import org.matrix.android.sdk.internal.crypto.attachments.ElementToDecrypt import org.matrix.android.sdk.internal.network.NetworkConstants +import org.matrix.android.sdk.internal.session.contentscanning.ScanEncryptorUtils +import org.matrix.android.sdk.internal.session.contentscanning.model.toJson import org.matrix.android.sdk.internal.util.ensureTrailingSlash import javax.inject.Inject @@ -35,6 +37,24 @@ internal class DefaultContentUrlResolver @Inject constructor( override val uploadUrl = baseUrl + NetworkConstants.URI_API_MEDIA_PREFIX_PATH_R0 + "upload" + override fun resolveForDownload(contentUrl: String?, elementToDecrypt: ElementToDecrypt?): ContentUrlResolver.ResolvedMethod? { + return if (scannerService.isScannerEnabled() && elementToDecrypt != null) { + val baseUrl = scannerService.getContentScannerServer() + val sep = if (baseUrl?.endsWith("/") == true) "" else "/" + + val url = baseUrl + sep + NetworkConstants.URI_API_PREFIX_PATH_MEDIA_PROXY_UNSTABLE + "download_encrypted" + + ContentUrlResolver.ResolvedMethod.POST( + url = url, + jsonBody = ScanEncryptorUtils + .getDownloadBodyAndEncryptIfNeeded(scannerService.serverPublicKey, contentUrl ?: "", elementToDecrypt) + .toJson() + ) + } else { + resolveFullSize(contentUrl)?.let { ContentUrlResolver.ResolvedMethod.GET(it) } + } + } + override fun resolveFullSize(contentUrl: String?): String? { return contentUrl // do not allow non-mxc content URLs @@ -42,7 +62,7 @@ internal class DefaultContentUrlResolver @Inject constructor( ?.let { resolve( contentUrl = it, - prefix = NetworkConstants.URI_API_MEDIA_PREFIX_PATH_R0 + "download/" + toThumbnail = false ) } } @@ -54,20 +74,27 @@ internal class DefaultContentUrlResolver @Inject constructor( ?.let { resolve( contentUrl = it, - prefix = NetworkConstants.URI_API_MEDIA_PREFIX_PATH_R0 + "thumbnail/", + toThumbnail = true, params = "?width=$width&height=$height&method=${method.value}" ) } } - override fun resolveForDownload(contentUrl: String?, elementToDecrypt: ElementToDecrypt?): ContentUrlResolver.ResolvedMethod? { - return resolveFullSize(contentUrl)?.let { ContentUrlResolver.ResolvedMethod.GET(it) } - } - private fun resolve(contentUrl: String, - prefix: String, + toThumbnail: Boolean, params: String = ""): String? { var serverAndMediaId = contentUrl.removePrefix(MatrixUrls.MATRIX_CONTENT_URI_SCHEME) + + val apiPath = if (scannerService.isScannerEnabled()) { + NetworkConstants.URI_API_PREFIX_PATH_MEDIA_PROXY_UNSTABLE + } else { + NetworkConstants.URI_API_MEDIA_PREFIX_PATH_R0 + } + val prefix = if (toThumbnail) { + apiPath + "thumbnail/" + } else { + apiPath + "download/" + } val fragmentOffset = serverAndMediaId.indexOf("#") var fragment = "" if (fragmentOffset >= 0) { @@ -75,6 +102,11 @@ internal class DefaultContentUrlResolver @Inject constructor( serverAndMediaId = serverAndMediaId.substring(0, fragmentOffset) } - return baseUrl + prefix + serverAndMediaId + params + fragment + val resolvedUrl = if (scannerService.isScannerEnabled()) { + scannerService.getContentScannerServer()!!.ensureTrailingSlash() + } else { + baseUrl + } + return resolvedUrl + prefix + serverAndMediaId + params + fragment } } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/ContentScanApi.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/ContentScanApi.kt similarity index 76% rename from matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/ContentScanApi.kt rename to matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/ContentScanApi.kt index 44745e4c50..81cff8a177 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/ContentScanApi.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/ContentScanApi.kt @@ -4,13 +4,13 @@ * Proprietary and confidential */ -package im.vector.matrix.android.sdk.internal.session.contentscanning +package org.matrix.android.sdk.internal.session.contentscanning -import im.vector.matrix.android.sdk.internal.session.contentscanning.model.DownloadBody -import im.vector.matrix.android.sdk.internal.session.contentscanning.model.ScanResponse -import im.vector.matrix.android.sdk.internal.session.contentscanning.model.ServerPublicKeyResponse import okhttp3.ResponseBody import org.matrix.android.sdk.internal.network.NetworkConstants +import org.matrix.android.sdk.internal.session.contentscanning.model.DownloadBody +import org.matrix.android.sdk.internal.session.contentscanning.model.ScanResponse +import org.matrix.android.sdk.internal.session.contentscanning.model.ServerPublicKeyResponse import retrofit2.http.Body import retrofit2.http.GET import retrofit2.http.POST diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/ContentScannerModule.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/ContentScannerModule.kt similarity index 66% rename from matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/ContentScannerModule.kt rename to matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/ContentScannerModule.kt index 02a0c49455..0143a16937 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/ContentScannerModule.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/ContentScannerModule.kt @@ -4,22 +4,11 @@ * Proprietary and confidential */ -package im.vector.matrix.android.sdk.internal.session.contentscanning +package org.matrix.android.sdk.internal.session.contentscanning import dagger.Binds import dagger.Module import dagger.Provides -import im.vector.matrix.android.sdk.internal.session.contentscanning.data.ContentScanningStore -import im.vector.matrix.android.sdk.internal.session.contentscanning.db.ContentScannerRealmModule -import im.vector.matrix.android.sdk.internal.session.contentscanning.db.RealmContentScannerStore -import im.vector.matrix.android.sdk.internal.session.contentscanning.tasks.DefaultDownloadEncryptedTask -import im.vector.matrix.android.sdk.internal.session.contentscanning.tasks.DefaultGetServerPublicKeyTask -import im.vector.matrix.android.sdk.internal.session.contentscanning.tasks.DefaultScanEncryptedTask -import im.vector.matrix.android.sdk.internal.session.contentscanning.tasks.DefaultScanMediaTask -import im.vector.matrix.android.sdk.internal.session.contentscanning.tasks.DownloadEncryptedTask -import im.vector.matrix.android.sdk.internal.session.contentscanning.tasks.GetServerPublicKeyTask -import im.vector.matrix.android.sdk.internal.session.contentscanning.tasks.ScanEncryptedTask -import im.vector.matrix.android.sdk.internal.session.contentscanning.tasks.ScanMediaTask import io.realm.RealmConfiguration import org.matrix.android.sdk.api.session.contentscanning.ContentScannerService import org.matrix.android.sdk.internal.database.RealmKeysUtils @@ -28,6 +17,17 @@ import org.matrix.android.sdk.internal.di.SessionFilesDirectory import org.matrix.android.sdk.internal.di.UserMd5 import org.matrix.android.sdk.internal.session.SessionModule import org.matrix.android.sdk.internal.session.SessionScope +import org.matrix.android.sdk.internal.session.contentscanning.data.ContentScanningStore +import org.matrix.android.sdk.internal.session.contentscanning.db.ContentScannerRealmModule +import org.matrix.android.sdk.internal.session.contentscanning.db.RealmContentScannerStore +import org.matrix.android.sdk.internal.session.contentscanning.tasks.DefaultDownloadEncryptedTask +import org.matrix.android.sdk.internal.session.contentscanning.tasks.DefaultGetServerPublicKeyTask +import org.matrix.android.sdk.internal.session.contentscanning.tasks.DefaultScanEncryptedTask +import org.matrix.android.sdk.internal.session.contentscanning.tasks.DefaultScanMediaTask +import org.matrix.android.sdk.internal.session.contentscanning.tasks.DownloadEncryptedTask +import org.matrix.android.sdk.internal.session.contentscanning.tasks.GetServerPublicKeyTask +import org.matrix.android.sdk.internal.session.contentscanning.tasks.ScanEncryptedTask +import org.matrix.android.sdk.internal.session.contentscanning.tasks.ScanMediaTask import java.io.File @Module diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/ContentScanningApiProvider.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/ContentScanningApiProvider.kt similarity index 73% rename from matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/ContentScanningApiProvider.kt rename to matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/ContentScanningApiProvider.kt index 44330ce2d0..3f81c04173 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/ContentScanningApiProvider.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/ContentScanningApiProvider.kt @@ -4,9 +4,10 @@ * Proprietary and confidential */ -package im.vector.matrix.android.sdk.internal.session.contentscanning +package org.matrix.android.sdk.internal.session.contentscanning import org.matrix.android.sdk.internal.session.SessionScope +import org.matrix.android.sdk.internal.session.contentscanning.ContentScanApi import javax.inject.Inject @SessionScope diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/DefaultContentScannerService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/DefaultContentScannerService.kt similarity index 92% rename from matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/DefaultContentScannerService.kt rename to matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/DefaultContentScannerService.kt index e632e8873a..b7292605b1 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/DefaultContentScannerService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/DefaultContentScannerService.kt @@ -4,17 +4,14 @@ * Proprietary and confidential */ -package im.vector.matrix.android.sdk.internal.session.contentscanning +package org.matrix.android.sdk.internal.session.contentscanning import androidx.lifecycle.LiveData import dagger.Lazy -import im.vector.matrix.android.sdk.internal.session.contentscanning.data.ContentScanningStore -import im.vector.matrix.android.sdk.internal.session.contentscanning.tasks.GetServerPublicKeyTask -import im.vector.matrix.android.sdk.internal.session.contentscanning.tasks.ScanEncryptedTask -import im.vector.matrix.android.sdk.internal.session.contentscanning.tasks.ScanMediaTask import kotlinx.coroutines.launch import okhttp3.OkHttpClient import org.matrix.android.sdk.api.MatrixCallback +import org.matrix.android.sdk.api.MatrixCoroutineDispatchers import org.matrix.android.sdk.api.NoOpMatrixCallback import org.matrix.android.sdk.api.session.contentscanning.ContentScannerService import org.matrix.android.sdk.api.session.contentscanning.ScanState @@ -24,9 +21,12 @@ import org.matrix.android.sdk.internal.crypto.attachments.ElementToDecrypt import org.matrix.android.sdk.internal.di.Unauthenticated import org.matrix.android.sdk.internal.network.RetrofitFactory import org.matrix.android.sdk.internal.session.SessionScope +import org.matrix.android.sdk.internal.session.contentscanning.data.ContentScanningStore +import org.matrix.android.sdk.internal.session.contentscanning.tasks.GetServerPublicKeyTask +import org.matrix.android.sdk.internal.session.contentscanning.tasks.ScanEncryptedTask +import org.matrix.android.sdk.internal.session.contentscanning.tasks.ScanMediaTask import org.matrix.android.sdk.internal.task.TaskExecutor import org.matrix.android.sdk.internal.task.launchToCallback -import org.matrix.android.sdk.internal.util.MatrixCoroutineDispatchers import org.matrix.android.sdk.internal.util.awaitCallback import timber.log.Timber import javax.inject.Inject diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/contentscanning/DisabledContentScannerService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/DisabledContentScannerService.kt similarity index 100% rename from matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/contentscanning/DisabledContentScannerService.kt rename to matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/DisabledContentScannerService.kt diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/ScanEncryptorUtils.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/ScanEncryptorUtils.kt similarity index 85% rename from matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/ScanEncryptorUtils.kt rename to matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/ScanEncryptorUtils.kt index 7d9f833954..b96305f5ce 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/ScanEncryptorUtils.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/ScanEncryptorUtils.kt @@ -4,15 +4,15 @@ * Proprietary and confidential */ -package im.vector.matrix.android.sdk.internal.session.contentscanning +package org.matrix.android.sdk.internal.session.contentscanning import org.matrix.android.sdk.internal.crypto.attachments.ElementToDecrypt import org.matrix.android.sdk.internal.crypto.model.rest.EncryptedFileInfo import org.matrix.android.sdk.internal.crypto.model.rest.EncryptedFileKey import org.matrix.android.sdk.internal.crypto.tools.withOlmEncryption -import im.vector.matrix.android.sdk.internal.session.contentscanning.model.DownloadBody -import im.vector.matrix.android.sdk.internal.session.contentscanning.model.EncryptedBody -import im.vector.matrix.android.sdk.internal.session.contentscanning.model.toCanonicalJson +import org.matrix.android.sdk.internal.session.contentscanning.model.DownloadBody +import org.matrix.android.sdk.internal.session.contentscanning.model.EncryptedBody +import org.matrix.android.sdk.internal.session.contentscanning.model.toCanonicalJson object ScanEncryptorUtils { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/data/ContentScanningStore.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/data/ContentScanningStore.kt similarity index 93% rename from matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/data/ContentScanningStore.kt rename to matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/data/ContentScanningStore.kt index 74bd3e6db3..f0db66def4 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/data/ContentScanningStore.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/data/ContentScanningStore.kt @@ -4,7 +4,7 @@ * Proprietary and confidential */ -package im.vector.matrix.android.sdk.internal.session.contentscanning.data +package org.matrix.android.sdk.internal.session.contentscanning.data import androidx.lifecycle.LiveData import org.matrix.android.sdk.api.session.contentscanning.ScanState diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/db/ContentScanResultEntity.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/db/ContentScanResultEntity.kt similarity index 94% rename from matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/db/ContentScanResultEntity.kt rename to matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/db/ContentScanResultEntity.kt index 6765461fb1..3125e6c8a6 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/db/ContentScanResultEntity.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/db/ContentScanResultEntity.kt @@ -4,7 +4,7 @@ * Proprietary and confidential */ -package im.vector.matrix.android.sdk.internal.session.contentscanning.db +package org.matrix.android.sdk.internal.session.contentscanning.db import io.realm.RealmObject import io.realm.annotations.Index diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/db/ContentScannerEntityQueries.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/db/ContentScannerEntityQueries.kt similarity index 94% rename from matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/db/ContentScannerEntityQueries.kt rename to matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/db/ContentScannerEntityQueries.kt index 5056f1d1ba..402fc98e17 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/db/ContentScannerEntityQueries.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/db/ContentScannerEntityQueries.kt @@ -4,7 +4,7 @@ * Proprietary and confidential */ -package im.vector.matrix.android.sdk.internal.session.contentscanning.db +package org.matrix.android.sdk.internal.session.contentscanning.db import io.realm.Realm import io.realm.kotlin.createObject diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/db/ContentScannerInfoEntity.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/db/ContentScannerInfoEntity.kt similarity index 83% rename from matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/db/ContentScannerInfoEntity.kt rename to matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/db/ContentScannerInfoEntity.kt index c54d4e3139..d0a815cc44 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/db/ContentScannerInfoEntity.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/db/ContentScannerInfoEntity.kt @@ -4,7 +4,7 @@ * Proprietary and confidential */ -package im.vector.matrix.android.sdk.internal.session.contentscanning.db +package org.matrix.android.sdk.internal.session.contentscanning.db import io.realm.RealmObject diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/db/ContentScannerRealmModule.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/db/ContentScannerRealmModule.kt similarity index 86% rename from matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/db/ContentScannerRealmModule.kt rename to matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/db/ContentScannerRealmModule.kt index eb40fa13fb..3d6380c642 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/db/ContentScannerRealmModule.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/db/ContentScannerRealmModule.kt @@ -4,7 +4,7 @@ * Proprietary and confidential */ -package im.vector.matrix.android.sdk.internal.session.contentscanning.db +package org.matrix.android.sdk.internal.session.contentscanning.db import io.realm.annotations.RealmModule diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/db/RealmContentScannerStore.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/db/RealmContentScannerStore.kt similarity index 96% rename from matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/db/RealmContentScannerStore.kt rename to matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/db/RealmContentScannerStore.kt index a09343139a..16c0ce5075 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/db/RealmContentScannerStore.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/db/RealmContentScannerStore.kt @@ -4,12 +4,12 @@ * Proprietary and confidential */ -package im.vector.matrix.android.sdk.internal.session.contentscanning.db +package org.matrix.android.sdk.internal.session.contentscanning.db import androidx.lifecycle.LiveData import androidx.lifecycle.Transformations import com.zhuinden.monarchy.Monarchy -import im.vector.matrix.android.sdk.internal.session.contentscanning.data.ContentScanningStore +import org.matrix.android.sdk.internal.session.contentscanning.data.ContentScanningStore import org.matrix.android.sdk.api.session.contentscanning.ScanState import org.matrix.android.sdk.api.session.contentscanning.ScanStatusInfo import io.realm.Realm diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/model/DownloadBody.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/model/DownloadBody.kt similarity index 93% rename from matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/model/DownloadBody.kt rename to matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/model/DownloadBody.kt index 1dac0a9ced..6d0d401fd8 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/model/DownloadBody.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/model/DownloadBody.kt @@ -4,7 +4,7 @@ * Proprietary and confidential */ -package im.vector.matrix.android.sdk.internal.session.contentscanning.model +package org.matrix.android.sdk.internal.session.contentscanning.model import com.squareup.moshi.Json import com.squareup.moshi.JsonClass diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/model/ScanResponse.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/model/ScanResponse.kt similarity index 87% rename from matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/model/ScanResponse.kt rename to matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/model/ScanResponse.kt index ca4c072b84..549419f3ca 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/model/ScanResponse.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/model/ScanResponse.kt @@ -4,7 +4,7 @@ * Proprietary and confidential */ -package im.vector.matrix.android.sdk.internal.session.contentscanning.model +package org.matrix.android.sdk.internal.session.contentscanning.model import com.squareup.moshi.Json import com.squareup.moshi.JsonClass diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/model/ServerPublicKeyResponse.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/model/ServerPublicKeyResponse.kt similarity index 83% rename from matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/model/ServerPublicKeyResponse.kt rename to matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/model/ServerPublicKeyResponse.kt index 57ba2f8263..2706286f56 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/model/ServerPublicKeyResponse.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/model/ServerPublicKeyResponse.kt @@ -4,7 +4,7 @@ * Proprietary and confidential */ -package im.vector.matrix.android.sdk.internal.session.contentscanning.model +package org.matrix.android.sdk.internal.session.contentscanning.model import com.squareup.moshi.Json import com.squareup.moshi.JsonClass diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/tasks/DownloadEncryptedTask.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/tasks/DownloadEncryptedTask.kt similarity index 83% rename from matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/tasks/DownloadEncryptedTask.kt rename to matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/tasks/DownloadEncryptedTask.kt index 04f2174e67..8bd83b553d 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/tasks/DownloadEncryptedTask.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/tasks/DownloadEncryptedTask.kt @@ -4,13 +4,13 @@ * Proprietary and confidential */ -package im.vector.matrix.android.sdk.internal.session.contentscanning.tasks +package org.matrix.android.sdk.internal.session.contentscanning.tasks -import im.vector.matrix.android.sdk.internal.session.contentscanning.ContentScanningApiProvider -import im.vector.matrix.android.sdk.internal.session.contentscanning.ScanEncryptorUtils import okhttp3.ResponseBody import org.matrix.android.sdk.internal.crypto.attachments.ElementToDecrypt import org.matrix.android.sdk.internal.network.executeRequest +import org.matrix.android.sdk.internal.session.contentscanning.ContentScanningApiProvider +import org.matrix.android.sdk.internal.session.contentscanning.ScanEncryptorUtils import org.matrix.android.sdk.internal.task.Task import javax.inject.Inject diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/tasks/GetServerPublicKeyTask.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/tasks/GetServerPublicKeyTask.kt similarity index 75% rename from matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/tasks/GetServerPublicKeyTask.kt rename to matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/tasks/GetServerPublicKeyTask.kt index bcf1fadb47..6510065d79 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/tasks/GetServerPublicKeyTask.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/tasks/GetServerPublicKeyTask.kt @@ -4,13 +4,12 @@ * Proprietary and confidential */ -package im.vector.matrix.android.sdk.internal.session.contentscanning.tasks +package org.matrix.android.sdk.internal.session.contentscanning.tasks -import im.vector.matrix.android.sdk.internal.session.contentscanning.ContentScanApi -import im.vector.matrix.android.sdk.internal.session.contentscanning.model.ServerPublicKeyResponse import org.matrix.android.sdk.internal.network.executeRequest +import org.matrix.android.sdk.internal.session.contentscanning.ContentScanApi +import org.matrix.android.sdk.internal.session.contentscanning.model.ServerPublicKeyResponse import org.matrix.android.sdk.internal.task.Task - import javax.inject.Inject internal interface GetServerPublicKeyTask : Task { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/tasks/ScanEncryptedTask.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/tasks/ScanEncryptedTask.kt similarity index 82% rename from matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/tasks/ScanEncryptedTask.kt rename to matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/tasks/ScanEncryptedTask.kt index 1078a131e2..900f40f7c1 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/tasks/ScanEncryptedTask.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/tasks/ScanEncryptedTask.kt @@ -4,16 +4,16 @@ * Proprietary and confidential */ -package im.vector.matrix.android.sdk.internal.session.contentscanning.tasks +package org.matrix.android.sdk.internal.session.contentscanning.tasks -import im.vector.matrix.android.sdk.internal.session.contentscanning.ContentScanningApiProvider -import im.vector.matrix.android.sdk.internal.session.contentscanning.ScanEncryptorUtils -import im.vector.matrix.android.sdk.internal.session.contentscanning.data.ContentScanningStore -import im.vector.matrix.android.sdk.internal.session.contentscanning.model.ScanResponse import org.matrix.android.sdk.api.failure.toScanFailure import org.matrix.android.sdk.api.session.contentscanning.ScanState import org.matrix.android.sdk.internal.crypto.attachments.ElementToDecrypt import org.matrix.android.sdk.internal.network.executeRequest +import org.matrix.android.sdk.internal.session.contentscanning.ContentScanningApiProvider +import org.matrix.android.sdk.internal.session.contentscanning.ScanEncryptorUtils +import org.matrix.android.sdk.internal.session.contentscanning.data.ContentScanningStore +import org.matrix.android.sdk.internal.session.contentscanning.model.ScanResponse import org.matrix.android.sdk.internal.task.Task import javax.inject.Inject diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/tasks/ScanMediaTask.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/tasks/ScanMediaTask.kt similarity index 87% rename from matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/tasks/ScanMediaTask.kt rename to matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/tasks/ScanMediaTask.kt index b7ed948afc..5570e218fa 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/sdk/internal/session/contentscanning/tasks/ScanMediaTask.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/tasks/ScanMediaTask.kt @@ -4,14 +4,14 @@ * Proprietary and confidential */ -package im.vector.matrix.android.sdk.internal.session.contentscanning.tasks +package org.matrix.android.sdk.internal.session.contentscanning.tasks -import im.vector.matrix.android.sdk.internal.session.contentscanning.ContentScanningApiProvider -import im.vector.matrix.android.sdk.internal.session.contentscanning.data.ContentScanningStore -import im.vector.matrix.android.sdk.internal.session.contentscanning.model.ScanResponse import org.matrix.android.sdk.api.failure.toScanFailure import org.matrix.android.sdk.api.session.contentscanning.ScanState import org.matrix.android.sdk.internal.network.executeRequest +import org.matrix.android.sdk.internal.session.contentscanning.ContentScanningApiProvider +import org.matrix.android.sdk.internal.session.contentscanning.data.ContentScanningStore +import org.matrix.android.sdk.internal.session.contentscanning.model.ScanResponse import org.matrix.android.sdk.internal.task.Task import javax.inject.Inject From 016f3faeea5dfd430e43209d1a021244987aa6f9 Mon Sep 17 00:00:00 2001 From: yostyle Date: Thu, 4 Nov 2021 17:35:02 +0100 Subject: [PATCH 3/7] Update file service --- .../internal/session/DefaultFileService.kt | 24 +++++++++++++++---- .../session/contentscanning/ContentScanApi.kt | 6 ++--- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultFileService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultFileService.kt index 46c5967876..14dfc097cf 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultFileService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultFileService.kt @@ -23,8 +23,10 @@ import androidx.core.content.FileProvider import kotlinx.coroutines.CompletableDeferred import kotlinx.coroutines.completeWith import kotlinx.coroutines.withContext +import okhttp3.MediaType.Companion.toMediaType import okhttp3.OkHttpClient import okhttp3.Request +import okhttp3.RequestBody.Companion.toRequestBody import org.matrix.android.sdk.api.MatrixCoroutineDispatchers import org.matrix.android.sdk.api.failure.Failure import org.matrix.android.sdk.api.session.content.ContentUrlResolver @@ -118,12 +120,24 @@ internal class DefaultFileService @Inject constructor( val cachedFiles = getFiles(url, fileName, mimeType, elementToDecrypt != null) if (!cachedFiles.file.exists()) { - val resolvedUrl = contentUrlResolver.resolveFullSize(url) ?: throw IllegalArgumentException("url is null") + val resolvedUrl = contentUrlResolver.resolveForDownload(url, elementToDecrypt) ?: throw IllegalArgumentException("url is null") - val request = Request.Builder() - .url(resolvedUrl) - .header(DOWNLOAD_PROGRESS_INTERCEPTOR_HEADER, url) - .build() + val request = when (resolvedUrl) { + is ContentUrlResolver.ResolvedMethod.GET -> { + Request.Builder() + .url(resolvedUrl.url) + .header(DOWNLOAD_PROGRESS_INTERCEPTOR_HEADER, url) + .build() + } + + is ContentUrlResolver.ResolvedMethod.POST -> { + Request.Builder() + .url(resolvedUrl.url) + .header(DOWNLOAD_PROGRESS_INTERCEPTOR_HEADER, url) + .post(resolvedUrl.jsonBody.toRequestBody("application/json".toMediaType())) + .build() + } + } val response = try { okHttpClient.newCall(request).execute() diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/ContentScanApi.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/ContentScanApi.kt index 81cff8a177..f4ce3b440e 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/ContentScanApi.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/ContentScanApi.kt @@ -25,11 +25,11 @@ internal interface ContentScanApi { suspend fun downloadEncrypted(@Body info: DownloadBody): ResponseBody @POST(NetworkConstants.URI_API_PREFIX_PATH_MEDIA_PROXY_UNSTABLE + "scan_encrypted") - fun scanFile(@Body info: DownloadBody): ScanResponse + suspend fun scanFile(@Body info: DownloadBody): ScanResponse @GET(NetworkConstants.URI_API_PREFIX_PATH_MEDIA_PROXY_UNSTABLE + "public_key") - fun getServerPublicKey(): ServerPublicKeyResponse + suspend fun getServerPublicKey(): ServerPublicKeyResponse @GET(NetworkConstants.URI_API_PREFIX_PATH_MEDIA_PROXY_UNSTABLE + "scan/{domain}/{mediaId}") - fun scanMedia(@Path(value = "domain") domain: String, @Path(value = "mediaId") mediaId: String): ScanResponse + suspend fun scanMedia(@Path(value = "domain") domain: String, @Path(value = "mediaId") mediaId: String): ScanResponse } From 98ae9d0e8afa476da98aebe6bddb792fd4af25da Mon Sep 17 00:00:00 2001 From: yostyle Date: Thu, 4 Nov 2021 18:00:42 +0100 Subject: [PATCH 4/7] Refactoring --- .../android/sdk/api/failure/Extensions.kt | 6 +-- .../matrix/android/sdk/api/session/Session.kt | 6 +-- .../ContentScannerError.kt} | 6 +-- .../ContentScannerService.kt | 2 +- .../ScanState.kt | 2 +- .../internal/auth/login/DefaultLoginWizard.kt | 2 +- .../sdk/internal/session/DefaultSession.kt | 4 +- .../sdk/internal/session/SessionComponent.kt | 2 +- .../content/DefaultContentUrlResolver.kt | 6 +-- .../ContentScannerApi.kt} | 10 ++-- .../ContentScannerApiProvider.kt} | 7 ++- .../ContentScannerModule.kt | 28 +++++------ .../DefaultContentScannerService.kt | 46 +++++++++---------- .../DisabledContentScannerService.kt | 6 +-- .../ScanEncryptorUtils.kt | 8 ++-- .../data/ContentScannerStore.kt} | 8 ++-- .../db/ContentScanResultEntity.kt | 6 +-- .../db/ContentScannerEntityQueries.kt | 2 +- .../db/ContentScannerInfoEntity.kt | 2 +- .../db/ContentScannerRealmModule.kt | 2 +- .../db/RealmContentScannerStore.kt | 10 ++-- .../model/DownloadBody.kt | 2 +- .../model/ScanResponse.kt | 2 +- .../model/ServerPublicKeyResponse.kt | 2 +- .../tasks/DownloadEncryptedTask.kt | 10 ++-- .../tasks/GetServerPublicKeyTask.kt | 10 ++-- .../tasks/ScanEncryptedTask.kt | 26 +++++------ .../tasks/ScanMediaTask.kt | 24 +++++----- 28 files changed, 123 insertions(+), 124 deletions(-) rename matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/{contentscanning/ContentScanError.kt => contentscanner/ContentScannerError.kt} (87%) rename matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/{contentscanning => contentscanner}/ContentScannerService.kt (96%) rename matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/{contentscanning => contentscanner}/ScanState.kt (86%) rename matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/{contentscanning/ContentScanApi.kt => contentscanner/ContentScannerApi.kt} (75%) rename matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/{contentscanning/ContentScanningApiProvider.kt => contentscanner/ContentScannerApiProvider.kt} (51%) rename matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/{contentscanning => contentscanner}/ContentScannerModule.kt (64%) rename matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/{contentscanning => contentscanner}/DefaultContentScannerService.kt (72%) rename matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/{contentscanning => contentscanner}/DisabledContentScannerService.kt (90%) rename matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/{contentscanning => contentscanner}/ScanEncryptorUtils.kt (86%) rename matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/{contentscanning/data/ContentScanningStore.kt => contentscanner/data/ContentScannerStore.kt} (76%) rename matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/{contentscanning => contentscanner}/db/ContentScanResultEntity.kt (84%) rename matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/{contentscanning => contentscanner}/db/ContentScannerEntityQueries.kt (94%) rename matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/{contentscanning => contentscanner}/db/ContentScannerInfoEntity.kt (84%) rename matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/{contentscanning => contentscanner}/db/ContentScannerRealmModule.kt (87%) rename matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/{contentscanning => contentscanner}/db/RealmContentScannerStore.kt (93%) rename matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/{contentscanning => contentscanner}/model/DownloadBody.kt (93%) rename matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/{contentscanning => contentscanner}/model/ScanResponse.kt (88%) rename matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/{contentscanning => contentscanner}/model/ServerPublicKeyResponse.kt (84%) rename matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/{contentscanning => contentscanner}/tasks/DownloadEncryptedTask.kt (73%) rename matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/{contentscanning => contentscanner}/tasks/GetServerPublicKeyTask.kt (66%) rename matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/{contentscanning => contentscanner}/tasks/ScanEncryptedTask.kt (59%) rename matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/{contentscanning => contentscanner}/tasks/ScanMediaTask.kt (64%) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/failure/Extensions.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/failure/Extensions.kt index dec396ad1b..13a26c89c1 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/failure/Extensions.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/failure/Extensions.kt @@ -18,8 +18,8 @@ package org.matrix.android.sdk.api.failure import org.matrix.android.sdk.api.auth.registration.RegistrationFlowResponse import org.matrix.android.sdk.api.extensions.tryOrNull -import org.matrix.android.sdk.api.session.contentscanning.ContentScanError -import org.matrix.android.sdk.api.session.contentscanning.ScanFailure +import org.matrix.android.sdk.api.session.contentscanner.ContentScannerError +import org.matrix.android.sdk.api.session.contentscanner.ScanFailure import org.matrix.android.sdk.internal.di.MoshiProvider import java.io.IOException import javax.net.ssl.HttpsURLConnection @@ -110,7 +110,7 @@ fun Throwable.toScanFailure(): ScanFailure? { return if (this is Failure.OtherServerError) { tryOrNull { MoshiProvider.providesMoshi() - .adapter(ContentScanError::class.java) + .adapter(ContentScannerError::class.java) .fromJson(errorBody) } ?.let { ScanFailure(it, httpCode, this) } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/Session.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/Session.kt index 97197a3933..3f817ec4d2 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/Session.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/Session.kt @@ -31,7 +31,7 @@ import org.matrix.android.sdk.api.session.cache.CacheService import org.matrix.android.sdk.api.session.call.CallSignalingService import org.matrix.android.sdk.api.session.content.ContentUploadStateTracker import org.matrix.android.sdk.api.session.content.ContentUrlResolver -import org.matrix.android.sdk.api.session.contentscanning.ContentScannerService +import org.matrix.android.sdk.api.session.contentscanner.ContentScannerService import org.matrix.android.sdk.api.session.crypto.CryptoService import org.matrix.android.sdk.api.session.events.EventService import org.matrix.android.sdk.api.session.file.ContentDownloadStateTracker @@ -194,9 +194,9 @@ interface Session : fun cryptoService(): CryptoService /** - * Returns the ContentScanningService associated with the session + * Returns the ContentScannerService associated with the session */ - fun contentScanningService(): ContentScannerService + fun contentScannerService(): ContentScannerService /** * Returns the identity service associated with the session diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/contentscanning/ContentScanError.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/contentscanner/ContentScannerError.kt similarity index 87% rename from matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/contentscanning/ContentScanError.kt rename to matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/contentscanner/ContentScannerError.kt index aeb4d3c287..31c512721c 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/contentscanning/ContentScanError.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/contentscanner/ContentScannerError.kt @@ -4,13 +4,13 @@ * Proprietary and confidential */ -package org.matrix.android.sdk.api.session.contentscanning +package org.matrix.android.sdk.api.session.contentscanner import com.squareup.moshi.Json import com.squareup.moshi.JsonClass @JsonClass(generateAdapter = true) -data class ContentScanError( +data class ContentScannerError( @Json(name = "info") val info: String? = null, @Json(name = "reason") val reason: String? = null ) { @@ -32,7 +32,7 @@ data class ContentScanError( } } -class ScanFailure(val error: ContentScanError, val httpCode: Int, cause: Throwable? = null) : Throwable(cause = cause) +class ScanFailure(val error: ContentScannerError, val httpCode: Int, cause: Throwable? = null) : Throwable(cause = cause) // For Glide, which deals with Exception and not with Throwable fun ScanFailure.toException() = Exception(this) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/contentscanning/ContentScannerService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/contentscanner/ContentScannerService.kt similarity index 96% rename from matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/contentscanning/ContentScannerService.kt rename to matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/contentscanner/ContentScannerService.kt index 53975f3706..47e4e4296b 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/contentscanning/ContentScannerService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/contentscanner/ContentScannerService.kt @@ -4,7 +4,7 @@ * Proprietary and confidential */ -package org.matrix.android.sdk.api.session.contentscanning +package org.matrix.android.sdk.api.session.contentscanner import androidx.lifecycle.LiveData import org.matrix.android.sdk.api.MatrixCallback diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/contentscanning/ScanState.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/contentscanner/ScanState.kt similarity index 86% rename from matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/contentscanning/ScanState.kt rename to matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/contentscanner/ScanState.kt index 3371878221..578d8ca185 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/contentscanning/ScanState.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/contentscanner/ScanState.kt @@ -4,7 +4,7 @@ * Proprietary and confidential */ -package org.matrix.android.sdk.api.session.contentscanning +package org.matrix.android.sdk.api.session.contentscanner enum class ScanState { TRUSTED, diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/login/DefaultLoginWizard.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/login/DefaultLoginWizard.kt index 111d1263fb..0583951138 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/login/DefaultLoginWizard.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/login/DefaultLoginWizard.kt @@ -33,7 +33,7 @@ import org.matrix.android.sdk.internal.auth.registration.AddThreePidRegistration import org.matrix.android.sdk.internal.auth.registration.RegisterAddThreePidTask import org.matrix.android.sdk.internal.network.executeRequest import org.matrix.android.sdk.internal.session.content.DefaultContentUrlResolver -import org.matrix.android.sdk.internal.session.contentscanning.DisabledContentScannerService +import org.matrix.android.sdk.internal.session.contentscanner.DisabledContentScannerService internal class DefaultLoginWizard( private val authAPI: AuthAPI, diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultSession.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultSession.kt index 7c352a8f99..f0ad0f87b0 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultSession.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultSession.kt @@ -35,7 +35,7 @@ import org.matrix.android.sdk.api.session.cache.CacheService import org.matrix.android.sdk.api.session.call.CallSignalingService import org.matrix.android.sdk.api.session.content.ContentUploadStateTracker import org.matrix.android.sdk.api.session.content.ContentUrlResolver -import org.matrix.android.sdk.api.session.contentscanning.ContentScannerService +import org.matrix.android.sdk.api.session.contentscanner.ContentScannerService import org.matrix.android.sdk.api.session.crypto.CryptoService import org.matrix.android.sdk.api.session.events.EventService import org.matrix.android.sdk.api.session.file.ContentDownloadStateTracker @@ -277,7 +277,7 @@ internal class DefaultSession @Inject constructor( override fun cryptoService(): CryptoService = cryptoService.get() - override fun contentScanningService(): ContentScannerService = contentScannerService.get() + override fun contentScannerService(): ContentScannerService = contentScannerService.get() override fun identityService() = identityService diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/SessionComponent.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/SessionComponent.kt index 8e6310026f..a3f8f5dc6d 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/SessionComponent.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/SessionComponent.kt @@ -18,7 +18,7 @@ package org.matrix.android.sdk.internal.session import dagger.BindsInstance import dagger.Component -import org.matrix.android.sdk.internal.session.contentscanning.ContentScannerModule +import org.matrix.android.sdk.internal.session.contentscanner.ContentScannerModule import org.matrix.android.sdk.api.MatrixCoroutineDispatchers import org.matrix.android.sdk.api.auth.data.SessionParams import org.matrix.android.sdk.api.session.Session diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/content/DefaultContentUrlResolver.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/content/DefaultContentUrlResolver.kt index 4124d5c437..d710fd36c3 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/content/DefaultContentUrlResolver.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/content/DefaultContentUrlResolver.kt @@ -20,11 +20,11 @@ import org.matrix.android.sdk.api.MatrixUrls import org.matrix.android.sdk.api.MatrixUrls.isMxcUrl import org.matrix.android.sdk.api.auth.data.HomeServerConnectionConfig import org.matrix.android.sdk.api.session.content.ContentUrlResolver -import org.matrix.android.sdk.api.session.contentscanning.ContentScannerService +import org.matrix.android.sdk.api.session.contentscanner.ContentScannerService import org.matrix.android.sdk.internal.crypto.attachments.ElementToDecrypt import org.matrix.android.sdk.internal.network.NetworkConstants -import org.matrix.android.sdk.internal.session.contentscanning.ScanEncryptorUtils -import org.matrix.android.sdk.internal.session.contentscanning.model.toJson +import org.matrix.android.sdk.internal.session.contentscanner.ScanEncryptorUtils +import org.matrix.android.sdk.internal.session.contentscanner.model.toJson import org.matrix.android.sdk.internal.util.ensureTrailingSlash import javax.inject.Inject diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/ContentScanApi.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/ContentScannerApi.kt similarity index 75% rename from matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/ContentScanApi.kt rename to matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/ContentScannerApi.kt index f4ce3b440e..b7703a9a15 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/ContentScanApi.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/ContentScannerApi.kt @@ -4,13 +4,13 @@ * Proprietary and confidential */ -package org.matrix.android.sdk.internal.session.contentscanning +package org.matrix.android.sdk.internal.session.contentscanner import okhttp3.ResponseBody import org.matrix.android.sdk.internal.network.NetworkConstants -import org.matrix.android.sdk.internal.session.contentscanning.model.DownloadBody -import org.matrix.android.sdk.internal.session.contentscanning.model.ScanResponse -import org.matrix.android.sdk.internal.session.contentscanning.model.ServerPublicKeyResponse +import org.matrix.android.sdk.internal.session.contentscanner.model.DownloadBody +import org.matrix.android.sdk.internal.session.contentscanner.model.ScanResponse +import org.matrix.android.sdk.internal.session.contentscanner.model.ServerPublicKeyResponse import retrofit2.http.Body import retrofit2.http.GET import retrofit2.http.POST @@ -19,7 +19,7 @@ import retrofit2.http.Path /** * https://github.com/matrix-org/matrix-content-scanner */ -internal interface ContentScanApi { +internal interface ContentScannerApi { @POST(NetworkConstants.URI_API_PREFIX_PATH_MEDIA_PROXY_UNSTABLE + "download_encrypted") suspend fun downloadEncrypted(@Body info: DownloadBody): ResponseBody diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/ContentScanningApiProvider.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/ContentScannerApiProvider.kt similarity index 51% rename from matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/ContentScanningApiProvider.kt rename to matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/ContentScannerApiProvider.kt index 3f81c04173..45f63e21d7 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/ContentScanningApiProvider.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/ContentScannerApiProvider.kt @@ -4,13 +4,12 @@ * Proprietary and confidential */ -package org.matrix.android.sdk.internal.session.contentscanning +package org.matrix.android.sdk.internal.session.contentscanner import org.matrix.android.sdk.internal.session.SessionScope -import org.matrix.android.sdk.internal.session.contentscanning.ContentScanApi import javax.inject.Inject @SessionScope -internal class ContentScanningApiProvider @Inject constructor() { - var contentScannerApi: ContentScanApi? = null +internal class ContentScannerApiProvider @Inject constructor() { + var contentScannerApi: ContentScannerApi? = null } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/ContentScannerModule.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/ContentScannerModule.kt similarity index 64% rename from matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/ContentScannerModule.kt rename to matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/ContentScannerModule.kt index 0143a16937..a9b3809104 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/ContentScannerModule.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/ContentScannerModule.kt @@ -4,30 +4,30 @@ * Proprietary and confidential */ -package org.matrix.android.sdk.internal.session.contentscanning +package org.matrix.android.sdk.internal.session.contentscanner import dagger.Binds import dagger.Module import dagger.Provides import io.realm.RealmConfiguration -import org.matrix.android.sdk.api.session.contentscanning.ContentScannerService +import org.matrix.android.sdk.api.session.contentscanner.ContentScannerService import org.matrix.android.sdk.internal.database.RealmKeysUtils import org.matrix.android.sdk.internal.di.ContentScannerDatabase import org.matrix.android.sdk.internal.di.SessionFilesDirectory import org.matrix.android.sdk.internal.di.UserMd5 import org.matrix.android.sdk.internal.session.SessionModule import org.matrix.android.sdk.internal.session.SessionScope -import org.matrix.android.sdk.internal.session.contentscanning.data.ContentScanningStore -import org.matrix.android.sdk.internal.session.contentscanning.db.ContentScannerRealmModule -import org.matrix.android.sdk.internal.session.contentscanning.db.RealmContentScannerStore -import org.matrix.android.sdk.internal.session.contentscanning.tasks.DefaultDownloadEncryptedTask -import org.matrix.android.sdk.internal.session.contentscanning.tasks.DefaultGetServerPublicKeyTask -import org.matrix.android.sdk.internal.session.contentscanning.tasks.DefaultScanEncryptedTask -import org.matrix.android.sdk.internal.session.contentscanning.tasks.DefaultScanMediaTask -import org.matrix.android.sdk.internal.session.contentscanning.tasks.DownloadEncryptedTask -import org.matrix.android.sdk.internal.session.contentscanning.tasks.GetServerPublicKeyTask -import org.matrix.android.sdk.internal.session.contentscanning.tasks.ScanEncryptedTask -import org.matrix.android.sdk.internal.session.contentscanning.tasks.ScanMediaTask +import org.matrix.android.sdk.internal.session.contentscanner.data.ContentScannerStore +import org.matrix.android.sdk.internal.session.contentscanner.db.ContentScannerRealmModule +import org.matrix.android.sdk.internal.session.contentscanner.db.RealmContentScannerStore +import org.matrix.android.sdk.internal.session.contentscanner.tasks.DefaultDownloadEncryptedTask +import org.matrix.android.sdk.internal.session.contentscanner.tasks.DefaultGetServerPublicKeyTask +import org.matrix.android.sdk.internal.session.contentscanner.tasks.DefaultScanEncryptedTask +import org.matrix.android.sdk.internal.session.contentscanner.tasks.DefaultScanMediaTask +import org.matrix.android.sdk.internal.session.contentscanner.tasks.DownloadEncryptedTask +import org.matrix.android.sdk.internal.session.contentscanner.tasks.GetServerPublicKeyTask +import org.matrix.android.sdk.internal.session.contentscanner.tasks.ScanEncryptedTask +import org.matrix.android.sdk.internal.session.contentscanner.tasks.ScanMediaTask import java.io.File @Module @@ -58,7 +58,7 @@ internal abstract class ContentScannerModule { abstract fun bindContentScannerService(service: DefaultContentScannerService): ContentScannerService @Binds - abstract fun bindContentScannerStore(store: RealmContentScannerStore): ContentScanningStore + abstract fun bindContentScannerStore(store: RealmContentScannerStore): ContentScannerStore @Binds abstract fun bindDownloadEncryptedTask(task: DefaultDownloadEncryptedTask): DownloadEncryptedTask diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/DefaultContentScannerService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/DefaultContentScannerService.kt similarity index 72% rename from matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/DefaultContentScannerService.kt rename to matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/DefaultContentScannerService.kt index b7292605b1..adfa8ef4d6 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/DefaultContentScannerService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/DefaultContentScannerService.kt @@ -4,7 +4,7 @@ * Proprietary and confidential */ -package org.matrix.android.sdk.internal.session.contentscanning +package org.matrix.android.sdk.internal.session.contentscanner import androidx.lifecycle.LiveData import dagger.Lazy @@ -13,18 +13,18 @@ import okhttp3.OkHttpClient import org.matrix.android.sdk.api.MatrixCallback import org.matrix.android.sdk.api.MatrixCoroutineDispatchers import org.matrix.android.sdk.api.NoOpMatrixCallback -import org.matrix.android.sdk.api.session.contentscanning.ContentScannerService -import org.matrix.android.sdk.api.session.contentscanning.ScanState -import org.matrix.android.sdk.api.session.contentscanning.ScanStatusInfo +import org.matrix.android.sdk.api.session.contentscanner.ContentScannerService +import org.matrix.android.sdk.api.session.contentscanner.ScanState +import org.matrix.android.sdk.api.session.contentscanner.ScanStatusInfo import org.matrix.android.sdk.api.util.Optional import org.matrix.android.sdk.internal.crypto.attachments.ElementToDecrypt import org.matrix.android.sdk.internal.di.Unauthenticated import org.matrix.android.sdk.internal.network.RetrofitFactory import org.matrix.android.sdk.internal.session.SessionScope -import org.matrix.android.sdk.internal.session.contentscanning.data.ContentScanningStore -import org.matrix.android.sdk.internal.session.contentscanning.tasks.GetServerPublicKeyTask -import org.matrix.android.sdk.internal.session.contentscanning.tasks.ScanEncryptedTask -import org.matrix.android.sdk.internal.session.contentscanning.tasks.ScanMediaTask +import org.matrix.android.sdk.internal.session.contentscanner.data.ContentScannerStore +import org.matrix.android.sdk.internal.session.contentscanner.tasks.GetServerPublicKeyTask +import org.matrix.android.sdk.internal.session.contentscanner.tasks.ScanEncryptedTask +import org.matrix.android.sdk.internal.session.contentscanner.tasks.ScanMediaTask import org.matrix.android.sdk.internal.task.TaskExecutor import org.matrix.android.sdk.internal.task.launchToCallback import org.matrix.android.sdk.internal.util.awaitCallback @@ -36,8 +36,8 @@ internal class DefaultContentScannerService @Inject constructor( private val retrofitFactory: RetrofitFactory, @Unauthenticated private val okHttpClient: Lazy, - private val contentScanningApiProvider: ContentScanningApiProvider, - private val contentScanningStore: ContentScanningStore, + private val contentScannerApiProvider: ContentScannerApiProvider, + private val contentScannerStore: ContentScannerStore, // private val sessionParams: SessionParams, private val getServerPublicKeyTask: GetServerPublicKeyTask, private val scanEncryptedTask: ScanEncryptedTask, @@ -51,11 +51,11 @@ internal class DefaultContentScannerService @Inject constructor( private set override fun getContentScannerServer(): String? { - return contentScanningStore.getScannerUrl() + return contentScannerStore.getScannerUrl() } override fun getServerPublicKey(forceDownload: Boolean, callback: MatrixCallback) { - val api = contentScanningApiProvider.contentScannerApi ?: return Unit.also { + val api = contentScannerApiProvider.contentScannerApi ?: return Unit.also { callback.onFailure(IllegalArgumentException("No content scanner defined")) } @@ -102,15 +102,15 @@ internal class DefaultContentScannerService @Inject constructor( } } - override fun setScannerUrl(url: String?) = contentScanningStore.setScannerUrl(url).also { + override fun setScannerUrl(url: String?) = contentScannerStore.setScannerUrl(url).also { if (url == null) { - contentScanningApiProvider.contentScannerApi = null + contentScannerApiProvider.contentScannerApi = null serverPublicKey = null } else { val api = retrofitFactory .create(okHttpClient, url) - .create(ContentScanApi::class.java) - contentScanningApiProvider.contentScannerApi = api + .create(ContentScannerApi::class.java) + contentScannerApiProvider.contentScannerApi = api taskExecutor.executorScope.launch(coroutineDispatchers.io) { try { @@ -124,25 +124,25 @@ internal class DefaultContentScannerService @Inject constructor( } } - override fun enableScanner(enabled: Boolean) = contentScanningStore.enableScanning(enabled) + override fun enableScanner(enabled: Boolean) = contentScannerStore.enableScanning(enabled) - override fun isScannerEnabled(): Boolean = contentScanningStore.isScanEnabled() + override fun isScannerEnabled(): Boolean = contentScannerStore.isScanEnabled() override fun getCachedScanResultForFile(mxcUrl: String): ScanStatusInfo? { - return contentScanningStore.getScanResult(mxcUrl) + return contentScannerStore.getScanResult(mxcUrl) } override fun getLiveStatusForFile(mxcUrl: String, fetchIfNeeded: Boolean): LiveData> { - val data = contentScanningStore.getLiveScanResult(mxcUrl) - if (fetchIfNeeded && !contentScanningStore.isScanResultKnownOrInProgress(mxcUrl, getContentScannerServer())) { + val data = contentScannerStore.getLiveScanResult(mxcUrl) + if (fetchIfNeeded && !contentScannerStore.isScanResultKnownOrInProgress(mxcUrl, getContentScannerServer())) { getScanResultForAttachment(mxcUrl, NoOpMatrixCallback()) } return data } override fun getLiveStatusForEncryptedFile(mxcUrl: String, fileInfo: ElementToDecrypt, fetchIfNeeded: Boolean): LiveData> { - val data = contentScanningStore.getLiveScanResult(mxcUrl) - if (fetchIfNeeded && !contentScanningStore.isScanResultKnownOrInProgress(mxcUrl, getContentScannerServer())) { + val data = contentScannerStore.getLiveScanResult(mxcUrl) + if (fetchIfNeeded && !contentScannerStore.isScanResultKnownOrInProgress(mxcUrl, getContentScannerServer())) { getScanResultForAttachment(mxcUrl, fileInfo, NoOpMatrixCallback()) } return data diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/DisabledContentScannerService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/DisabledContentScannerService.kt similarity index 90% rename from matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/DisabledContentScannerService.kt rename to matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/DisabledContentScannerService.kt index 9b003b65fd..6daa094ed2 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/DisabledContentScannerService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/DisabledContentScannerService.kt @@ -14,13 +14,13 @@ * limitations under the License. */ -package org.matrix.android.sdk.internal.session.contentscanning +package org.matrix.android.sdk.internal.session.contentscanner import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import org.matrix.android.sdk.api.MatrixCallback -import org.matrix.android.sdk.api.session.contentscanning.ContentScannerService -import org.matrix.android.sdk.api.session.contentscanning.ScanStatusInfo +import org.matrix.android.sdk.api.session.contentscanner.ContentScannerService +import org.matrix.android.sdk.api.session.contentscanner.ScanStatusInfo import org.matrix.android.sdk.api.util.Optional import org.matrix.android.sdk.internal.crypto.attachments.ElementToDecrypt diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/ScanEncryptorUtils.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/ScanEncryptorUtils.kt similarity index 86% rename from matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/ScanEncryptorUtils.kt rename to matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/ScanEncryptorUtils.kt index b96305f5ce..567002750f 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/ScanEncryptorUtils.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/ScanEncryptorUtils.kt @@ -4,15 +4,15 @@ * Proprietary and confidential */ -package org.matrix.android.sdk.internal.session.contentscanning +package org.matrix.android.sdk.internal.session.contentscanner import org.matrix.android.sdk.internal.crypto.attachments.ElementToDecrypt import org.matrix.android.sdk.internal.crypto.model.rest.EncryptedFileInfo import org.matrix.android.sdk.internal.crypto.model.rest.EncryptedFileKey import org.matrix.android.sdk.internal.crypto.tools.withOlmEncryption -import org.matrix.android.sdk.internal.session.contentscanning.model.DownloadBody -import org.matrix.android.sdk.internal.session.contentscanning.model.EncryptedBody -import org.matrix.android.sdk.internal.session.contentscanning.model.toCanonicalJson +import org.matrix.android.sdk.internal.session.contentscanner.model.DownloadBody +import org.matrix.android.sdk.internal.session.contentscanner.model.EncryptedBody +import org.matrix.android.sdk.internal.session.contentscanner.model.toCanonicalJson object ScanEncryptorUtils { diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/data/ContentScanningStore.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/data/ContentScannerStore.kt similarity index 76% rename from matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/data/ContentScanningStore.kt rename to matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/data/ContentScannerStore.kt index f0db66def4..af43c75c04 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/data/ContentScanningStore.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/data/ContentScannerStore.kt @@ -4,14 +4,14 @@ * Proprietary and confidential */ -package org.matrix.android.sdk.internal.session.contentscanning.data +package org.matrix.android.sdk.internal.session.contentscanner.data import androidx.lifecycle.LiveData -import org.matrix.android.sdk.api.session.contentscanning.ScanState -import org.matrix.android.sdk.api.session.contentscanning.ScanStatusInfo +import org.matrix.android.sdk.api.session.contentscanner.ScanState +import org.matrix.android.sdk.api.session.contentscanner.ScanStatusInfo import org.matrix.android.sdk.api.util.Optional -internal interface ContentScanningStore { +internal interface ContentScannerStore { fun getScannerUrl(): String? diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/db/ContentScanResultEntity.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/db/ContentScanResultEntity.kt similarity index 84% rename from matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/db/ContentScanResultEntity.kt rename to matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/db/ContentScanResultEntity.kt index 3125e6c8a6..33284b2556 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/db/ContentScanResultEntity.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/db/ContentScanResultEntity.kt @@ -4,13 +4,13 @@ * Proprietary and confidential */ -package org.matrix.android.sdk.internal.session.contentscanning.db +package org.matrix.android.sdk.internal.session.contentscanner.db import io.realm.RealmObject import io.realm.annotations.Index import org.matrix.android.sdk.api.extensions.tryOrNull -import org.matrix.android.sdk.api.session.contentscanning.ScanState -import org.matrix.android.sdk.api.session.contentscanning.ScanStatusInfo +import org.matrix.android.sdk.api.session.contentscanner.ScanState +import org.matrix.android.sdk.api.session.contentscanner.ScanStatusInfo internal open class ContentScanResultEntity( @Index diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/db/ContentScannerEntityQueries.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/db/ContentScannerEntityQueries.kt similarity index 94% rename from matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/db/ContentScannerEntityQueries.kt rename to matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/db/ContentScannerEntityQueries.kt index 402fc98e17..fcabae730b 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/db/ContentScannerEntityQueries.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/db/ContentScannerEntityQueries.kt @@ -4,7 +4,7 @@ * Proprietary and confidential */ -package org.matrix.android.sdk.internal.session.contentscanning.db +package org.matrix.android.sdk.internal.session.contentscanner.db import io.realm.Realm import io.realm.kotlin.createObject diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/db/ContentScannerInfoEntity.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/db/ContentScannerInfoEntity.kt similarity index 84% rename from matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/db/ContentScannerInfoEntity.kt rename to matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/db/ContentScannerInfoEntity.kt index d0a815cc44..da0011db72 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/db/ContentScannerInfoEntity.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/db/ContentScannerInfoEntity.kt @@ -4,7 +4,7 @@ * Proprietary and confidential */ -package org.matrix.android.sdk.internal.session.contentscanning.db +package org.matrix.android.sdk.internal.session.contentscanner.db import io.realm.RealmObject diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/db/ContentScannerRealmModule.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/db/ContentScannerRealmModule.kt similarity index 87% rename from matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/db/ContentScannerRealmModule.kt rename to matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/db/ContentScannerRealmModule.kt index 3d6380c642..aec926beef 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/db/ContentScannerRealmModule.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/db/ContentScannerRealmModule.kt @@ -4,7 +4,7 @@ * Proprietary and confidential */ -package org.matrix.android.sdk.internal.session.contentscanning.db +package org.matrix.android.sdk.internal.session.contentscanner.db import io.realm.annotations.RealmModule diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/db/RealmContentScannerStore.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/db/RealmContentScannerStore.kt similarity index 93% rename from matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/db/RealmContentScannerStore.kt rename to matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/db/RealmContentScannerStore.kt index 16c0ce5075..6b561c3720 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/db/RealmContentScannerStore.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/db/RealmContentScannerStore.kt @@ -4,14 +4,14 @@ * Proprietary and confidential */ -package org.matrix.android.sdk.internal.session.contentscanning.db +package org.matrix.android.sdk.internal.session.contentscanner.db import androidx.lifecycle.LiveData import androidx.lifecycle.Transformations import com.zhuinden.monarchy.Monarchy -import org.matrix.android.sdk.internal.session.contentscanning.data.ContentScanningStore -import org.matrix.android.sdk.api.session.contentscanning.ScanState -import org.matrix.android.sdk.api.session.contentscanning.ScanStatusInfo +import org.matrix.android.sdk.internal.session.contentscanner.data.ContentScannerStore +import org.matrix.android.sdk.api.session.contentscanner.ScanState +import org.matrix.android.sdk.api.session.contentscanner.ScanStatusInfo import io.realm.Realm import io.realm.RealmConfiguration import io.realm.kotlin.createObject @@ -28,7 +28,7 @@ import javax.inject.Inject internal class RealmContentScannerStore @Inject constructor( @ContentScannerDatabase private val realmConfiguration: RealmConfiguration -) : ContentScanningStore { +) : ContentScannerStore { private val monarchy = Monarchy.Builder() .setRealmConfiguration(realmConfiguration) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/model/DownloadBody.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/model/DownloadBody.kt similarity index 93% rename from matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/model/DownloadBody.kt rename to matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/model/DownloadBody.kt index 6d0d401fd8..fbb0688225 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/model/DownloadBody.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/model/DownloadBody.kt @@ -4,7 +4,7 @@ * Proprietary and confidential */ -package org.matrix.android.sdk.internal.session.contentscanning.model +package org.matrix.android.sdk.internal.session.contentscanner.model import com.squareup.moshi.Json import com.squareup.moshi.JsonClass diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/model/ScanResponse.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/model/ScanResponse.kt similarity index 88% rename from matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/model/ScanResponse.kt rename to matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/model/ScanResponse.kt index 549419f3ca..0a730d614c 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/model/ScanResponse.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/model/ScanResponse.kt @@ -4,7 +4,7 @@ * Proprietary and confidential */ -package org.matrix.android.sdk.internal.session.contentscanning.model +package org.matrix.android.sdk.internal.session.contentscanner.model import com.squareup.moshi.Json import com.squareup.moshi.JsonClass diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/model/ServerPublicKeyResponse.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/model/ServerPublicKeyResponse.kt similarity index 84% rename from matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/model/ServerPublicKeyResponse.kt rename to matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/model/ServerPublicKeyResponse.kt index 2706286f56..ca6ffe96b3 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/model/ServerPublicKeyResponse.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/model/ServerPublicKeyResponse.kt @@ -4,7 +4,7 @@ * Proprietary and confidential */ -package org.matrix.android.sdk.internal.session.contentscanning.model +package org.matrix.android.sdk.internal.session.contentscanner.model import com.squareup.moshi.Json import com.squareup.moshi.JsonClass diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/tasks/DownloadEncryptedTask.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/tasks/DownloadEncryptedTask.kt similarity index 73% rename from matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/tasks/DownloadEncryptedTask.kt rename to matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/tasks/DownloadEncryptedTask.kt index 8bd83b553d..6a9e217740 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/tasks/DownloadEncryptedTask.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/tasks/DownloadEncryptedTask.kt @@ -4,13 +4,13 @@ * Proprietary and confidential */ -package org.matrix.android.sdk.internal.session.contentscanning.tasks +package org.matrix.android.sdk.internal.session.contentscanner.tasks import okhttp3.ResponseBody import org.matrix.android.sdk.internal.crypto.attachments.ElementToDecrypt import org.matrix.android.sdk.internal.network.executeRequest -import org.matrix.android.sdk.internal.session.contentscanning.ContentScanningApiProvider -import org.matrix.android.sdk.internal.session.contentscanning.ScanEncryptorUtils +import org.matrix.android.sdk.internal.session.contentscanner.ContentScannerApiProvider +import org.matrix.android.sdk.internal.session.contentscanner.ScanEncryptorUtils import org.matrix.android.sdk.internal.task.Task import javax.inject.Inject @@ -23,7 +23,7 @@ internal interface DownloadEncryptedTask : Task { data class Params( - val contentScanApi: ContentScanApi + val contentScannerApi: ContentScannerApi ) } @@ -22,7 +22,7 @@ internal class DefaultGetServerPublicKeyTask @Inject constructor() : GetServerPu override suspend fun execute(params: GetServerPublicKeyTask.Params): String? { return executeRequest(null) { - params.contentScanApi.getServerPublicKey() + params.contentScannerApi.getServerPublicKey() }.publicKey } } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/tasks/ScanEncryptedTask.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/tasks/ScanEncryptedTask.kt similarity index 59% rename from matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/tasks/ScanEncryptedTask.kt rename to matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/tasks/ScanEncryptedTask.kt index 900f40f7c1..c1185e02ba 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/tasks/ScanEncryptedTask.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/tasks/ScanEncryptedTask.kt @@ -4,16 +4,16 @@ * Proprietary and confidential */ -package org.matrix.android.sdk.internal.session.contentscanning.tasks +package org.matrix.android.sdk.internal.session.contentscanner.tasks import org.matrix.android.sdk.api.failure.toScanFailure -import org.matrix.android.sdk.api.session.contentscanning.ScanState +import org.matrix.android.sdk.api.session.contentscanner.ScanState import org.matrix.android.sdk.internal.crypto.attachments.ElementToDecrypt import org.matrix.android.sdk.internal.network.executeRequest -import org.matrix.android.sdk.internal.session.contentscanning.ContentScanningApiProvider -import org.matrix.android.sdk.internal.session.contentscanning.ScanEncryptorUtils -import org.matrix.android.sdk.internal.session.contentscanning.data.ContentScanningStore -import org.matrix.android.sdk.internal.session.contentscanning.model.ScanResponse +import org.matrix.android.sdk.internal.session.contentscanner.ContentScannerApiProvider +import org.matrix.android.sdk.internal.session.contentscanner.ScanEncryptorUtils +import org.matrix.android.sdk.internal.session.contentscanner.data.ContentScannerStore +import org.matrix.android.sdk.internal.session.contentscanner.model.ScanResponse import org.matrix.android.sdk.internal.task.Task import javax.inject.Inject @@ -26,23 +26,23 @@ internal interface ScanEncryptedTask : Task(null) { api.scanFile(dlBody) } - contentScanningStore.updateScanResultForContent( + contentScannerStore.updateScanResultForContent( mxcUrl, scannerUrl, ScanState.TRUSTED.takeIf { executeRequest.clean } ?: ScanState.INFECTED, @@ -50,7 +50,7 @@ internal class DefaultScanEncryptedTask @Inject constructor( ) return executeRequest } catch (failure: Throwable) { - contentScanningStore.updateStateForContent(params.mxcUrl, ScanState.UNKNOWN, scannerUrl) + contentScannerStore.updateStateForContent(params.mxcUrl, ScanState.UNKNOWN, scannerUrl) throw failure.toScanFailure() ?: failure } } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/tasks/ScanMediaTask.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/tasks/ScanMediaTask.kt similarity index 64% rename from matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/tasks/ScanMediaTask.kt rename to matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/tasks/ScanMediaTask.kt index 5570e218fa..66f5dfef63 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanning/tasks/ScanMediaTask.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/tasks/ScanMediaTask.kt @@ -4,14 +4,14 @@ * Proprietary and confidential */ -package org.matrix.android.sdk.internal.session.contentscanning.tasks +package org.matrix.android.sdk.internal.session.contentscanner.tasks import org.matrix.android.sdk.api.failure.toScanFailure -import org.matrix.android.sdk.api.session.contentscanning.ScanState +import org.matrix.android.sdk.api.session.contentscanner.ScanState import org.matrix.android.sdk.internal.network.executeRequest -import org.matrix.android.sdk.internal.session.contentscanning.ContentScanningApiProvider -import org.matrix.android.sdk.internal.session.contentscanning.data.ContentScanningStore -import org.matrix.android.sdk.internal.session.contentscanning.model.ScanResponse +import org.matrix.android.sdk.internal.session.contentscanner.ContentScannerApiProvider +import org.matrix.android.sdk.internal.session.contentscanner.data.ContentScannerStore +import org.matrix.android.sdk.internal.session.contentscanner.model.ScanResponse import org.matrix.android.sdk.internal.task.Task import javax.inject.Inject @@ -22,8 +22,8 @@ internal interface ScanMediaTask : Task { } internal class DefaultScanMediaTask @Inject constructor( - private val contentScanningApiProvider: ContentScanningApiProvider, - private val contentScanningStore: ContentScanningStore + private val contentScannerApiProvider: ContentScannerApiProvider, + private val contentScannerStore: ContentScannerStore ) : ScanMediaTask { override suspend fun execute(params: ScanMediaTask.Params): ScanResponse { @@ -31,8 +31,8 @@ internal class DefaultScanMediaTask @Inject constructor( if (!params.mxcUrl.startsWith("mxc://")) { throw IllegalAccessException("Invalid mxc url") } - val scannerUrl = contentScanningStore.getScannerUrl() - contentScanningStore.updateStateForContent(params.mxcUrl, ScanState.IN_PROGRESS, scannerUrl) + val scannerUrl = contentScannerStore.getScannerUrl() + contentScannerStore.updateStateForContent(params.mxcUrl, ScanState.IN_PROGRESS, scannerUrl) var serverAndMediaId = params.mxcUrl.removePrefix("mxc://") val fragmentOffset = serverAndMediaId.indexOf("#") @@ -47,10 +47,10 @@ internal class DefaultScanMediaTask @Inject constructor( try { val scanResponse = executeRequest(null) { - val api = contentScanningApiProvider.contentScannerApi ?: throw IllegalArgumentException() + val api = contentScannerApiProvider.contentScannerApi ?: throw IllegalArgumentException() api.scanMedia(split[0], split[1]) } - contentScanningStore.updateScanResultForContent( + contentScannerStore.updateScanResultForContent( params.mxcUrl, scannerUrl, ScanState.TRUSTED.takeIf { scanResponse.clean } ?: ScanState.INFECTED, @@ -58,7 +58,7 @@ internal class DefaultScanMediaTask @Inject constructor( ) return scanResponse } catch (failure: Throwable) { - contentScanningStore.updateStateForContent(params.mxcUrl, ScanState.UNKNOWN, scannerUrl) + contentScannerStore.updateStateForContent(params.mxcUrl, ScanState.UNKNOWN, scannerUrl) throw failure.toScanFailure() ?: failure } } From 7a78bc68666110f227eac886d438a671319ea789 Mon Sep 17 00:00:00 2001 From: yostyle Date: Wed, 10 Nov 2021 16:32:35 +0100 Subject: [PATCH 5/7] Replace matrix callbacks by coroutines --- .../contentscanner/ContentScannerError.kt | 16 ++- .../contentscanner/ContentScannerService.kt | 25 +++-- .../api/session/contentscanner/ScanState.kt | 16 ++- .../contentscanner/ContentScannerApi.kt | 16 ++- .../ContentScannerApiProvider.kt | 16 ++- .../contentscanner/ContentScannerModule.kt | 18 ++- .../DefaultContentScannerService.kt | 103 +++++++----------- .../DisabledContentScannerService.kt | 21 ++-- .../contentscanner/ScanEncryptorUtils.kt | 18 ++- .../data/ContentScannerStore.kt | 18 ++- .../db/ContentScanResultEntity.kt | 16 ++- .../db/ContentScannerEntityQueries.kt | 16 ++- .../db/ContentScannerInfoEntity.kt | 16 ++- .../db/ContentScannerRealmModule.kt | 16 ++- .../db/RealmContentScannerStore.kt | 18 ++- .../contentscanner/model/DownloadBody.kt | 24 ++-- .../contentscanner/model/ScanResponse.kt | 18 ++- .../model/ServerPublicKeyResponse.kt | 18 ++- .../tasks/DownloadEncryptedTask.kt | 16 ++- .../tasks/GetServerPublicKeyTask.kt | 16 ++- .../contentscanner/tasks/ScanEncryptedTask.kt | 16 ++- .../contentscanner/tasks/ScanMediaTask.kt | 16 ++- 22 files changed, 324 insertions(+), 149 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/contentscanner/ContentScannerError.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/contentscanner/ContentScannerError.kt index 31c512721c..cef5d41f2c 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/contentscanner/ContentScannerError.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/contentscanner/ContentScannerError.kt @@ -1,7 +1,17 @@ /* - * Copyright 2020 New Vector Ltd - All Rights Reserved - * Unauthorized copying of this file, via any medium is strictly prohibited - * Proprietary and confidential + * Copyright 2021 The Matrix.org Foundation C.I.C. + * + * 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. */ package org.matrix.android.sdk.api.session.contentscanner diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/contentscanner/ContentScannerService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/contentscanner/ContentScannerService.kt index 47e4e4296b..0c45e6a132 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/contentscanner/ContentScannerService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/contentscanner/ContentScannerService.kt @@ -1,13 +1,22 @@ /* - * Copyright 2020 New Vector Ltd - All Rights Reserved - * Unauthorized copying of this file, via any medium is strictly prohibited - * Proprietary and confidential + * Copyright 2021 The Matrix.org Foundation C.I.C. + * + * 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. */ package org.matrix.android.sdk.api.session.contentscanner import androidx.lifecycle.LiveData -import org.matrix.android.sdk.api.MatrixCallback import org.matrix.android.sdk.api.util.Optional import org.matrix.android.sdk.internal.crypto.attachments.ElementToDecrypt @@ -20,16 +29,14 @@ interface ContentScannerService { * Get the current public curve25519 key that the AV server is advertising. * @param callback on success callback containing the server public key */ - fun getServerPublicKey(forceDownload: Boolean = false, callback: MatrixCallback) + suspend fun getServerPublicKey(forceDownload: Boolean = false): String? - fun getScanResultForAttachment(mxcUrl: String, callback: MatrixCallback) - fun getScanResultForAttachment(mxcUrl: String, fileInfo: ElementToDecrypt, callback: MatrixCallback) + suspend fun getScanResultForAttachment(mxcUrl: String, fileInfo: ElementToDecrypt? = null): ScanStatusInfo fun setScannerUrl(url: String?) fun enableScanner(enabled: Boolean) fun isScannerEnabled(): Boolean - fun getLiveStatusForFile(mxcUrl: String, fetchIfNeeded: Boolean = true): LiveData> - fun getLiveStatusForEncryptedFile(mxcUrl: String, fileInfo: ElementToDecrypt, fetchIfNeeded: Boolean = true): LiveData> + fun getLiveStatusForFile(mxcUrl: String, fetchIfNeeded: Boolean = true, fileInfo: ElementToDecrypt? = null): LiveData> fun getCachedScanResultForFile(mxcUrl: String): ScanStatusInfo? } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/contentscanner/ScanState.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/contentscanner/ScanState.kt index 578d8ca185..aeb1551cf7 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/contentscanner/ScanState.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/contentscanner/ScanState.kt @@ -1,7 +1,17 @@ /* - * Copyright 2020 New Vector Ltd - All Rights Reserved - * Unauthorized copying of this file, via any medium is strictly prohibited - * Proprietary and confidential + * Copyright 2021 The Matrix.org Foundation C.I.C. + * + * 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. */ package org.matrix.android.sdk.api.session.contentscanner diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/ContentScannerApi.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/ContentScannerApi.kt index b7703a9a15..46f1705806 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/ContentScannerApi.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/ContentScannerApi.kt @@ -1,7 +1,17 @@ /* - * Copyright 2020 New Vector Ltd - All Rights Reserved - * Unauthorized copying of this file, via any medium is strictly prohibited - * Proprietary and confidential + * Copyright 2021 The Matrix.org Foundation C.I.C. + * + * 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. */ package org.matrix.android.sdk.internal.session.contentscanner diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/ContentScannerApiProvider.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/ContentScannerApiProvider.kt index 45f63e21d7..d8548bb238 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/ContentScannerApiProvider.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/ContentScannerApiProvider.kt @@ -1,7 +1,17 @@ /* - * Copyright 2020 New Vector Ltd - All Rights Reserved - * Unauthorized copying of this file, via any medium is strictly prohibited - * Proprietary and confidential + * Copyright 2021 The Matrix.org Foundation C.I.C. + * + * 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. */ package org.matrix.android.sdk.internal.session.contentscanner diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/ContentScannerModule.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/ContentScannerModule.kt index a9b3809104..7ea74225cd 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/ContentScannerModule.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/ContentScannerModule.kt @@ -1,7 +1,17 @@ /* - * Copyright 2020 New Vector Ltd - All Rights Reserved - * Unauthorized copying of this file, via any medium is strictly prohibited - * Proprietary and confidential + * Copyright 2021 The Matrix.org Foundation C.I.C. + * + * 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. */ package org.matrix.android.sdk.internal.session.contentscanner @@ -55,7 +65,7 @@ internal abstract class ContentScannerModule { } @Binds - abstract fun bindContentScannerService(service: DefaultContentScannerService): ContentScannerService + abstract fun bindContentScannerService(service: DisabledContentScannerService): ContentScannerService @Binds abstract fun bindContentScannerStore(store: RealmContentScannerStore): ContentScannerStore diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/DefaultContentScannerService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/DefaultContentScannerService.kt index adfa8ef4d6..4ecb337603 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/DefaultContentScannerService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/DefaultContentScannerService.kt @@ -1,7 +1,17 @@ /* - * Copyright 2020 New Vector Ltd - All Rights Reserved - * Unauthorized copying of this file, via any medium is strictly prohibited - * Proprietary and confidential + * Copyright 2021 The Matrix.org Foundation C.I.C. + * + * 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. */ package org.matrix.android.sdk.internal.session.contentscanner @@ -10,9 +20,6 @@ import androidx.lifecycle.LiveData import dagger.Lazy import kotlinx.coroutines.launch import okhttp3.OkHttpClient -import org.matrix.android.sdk.api.MatrixCallback -import org.matrix.android.sdk.api.MatrixCoroutineDispatchers -import org.matrix.android.sdk.api.NoOpMatrixCallback import org.matrix.android.sdk.api.session.contentscanner.ContentScannerService import org.matrix.android.sdk.api.session.contentscanner.ScanState import org.matrix.android.sdk.api.session.contentscanner.ScanStatusInfo @@ -26,8 +33,6 @@ import org.matrix.android.sdk.internal.session.contentscanner.tasks.GetServerPub import org.matrix.android.sdk.internal.session.contentscanner.tasks.ScanEncryptedTask import org.matrix.android.sdk.internal.session.contentscanner.tasks.ScanMediaTask import org.matrix.android.sdk.internal.task.TaskExecutor -import org.matrix.android.sdk.internal.task.launchToCallback -import org.matrix.android.sdk.internal.util.awaitCallback import timber.log.Timber import javax.inject.Inject @@ -38,12 +43,10 @@ internal class DefaultContentScannerService @Inject constructor( private val okHttpClient: Lazy, private val contentScannerApiProvider: ContentScannerApiProvider, private val contentScannerStore: ContentScannerStore, -// private val sessionParams: SessionParams, private val getServerPublicKeyTask: GetServerPublicKeyTask, private val scanEncryptedTask: ScanEncryptedTask, private val scanMediaTask: ScanMediaTask, - private val taskExecutor: TaskExecutor, - private val coroutineDispatchers: MatrixCoroutineDispatchers + private val taskExecutor: TaskExecutor ) : ContentScannerService { // Cache public key in memory @@ -54,52 +57,34 @@ internal class DefaultContentScannerService @Inject constructor( return contentScannerStore.getScannerUrl() } - override fun getServerPublicKey(forceDownload: Boolean, callback: MatrixCallback) { - val api = contentScannerApiProvider.contentScannerApi ?: return Unit.also { - callback.onFailure(IllegalArgumentException("No content scanner defined")) - } + override suspend fun getServerPublicKey(forceDownload: Boolean): String? { + val api = contentScannerApiProvider.contentScannerApi ?: throw IllegalArgumentException("No content scanner define") if (!forceDownload && serverPublicKey != null) { - callback.onSuccess(serverPublicKey) - return + return serverPublicKey } - taskExecutor.executorScope.launchToCallback(coroutineDispatchers.io, callback) { - getServerPublicKeyTask.execute(GetServerPublicKeyTask.Params(api)).also { - serverPublicKey = it - } + + return getServerPublicKeyTask.execute(GetServerPublicKeyTask.Params(api)).also { + serverPublicKey = it } } - override fun getScanResultForAttachment(mxcUrl: String, fileInfo: ElementToDecrypt, callback: MatrixCallback) { - taskExecutor.executorScope.launchToCallback(coroutineDispatchers.io, callback) { - val serverPublicKey = serverPublicKey ?: awaitCallback { - getServerPublicKey(false, it) - } - - val result = scanEncryptedTask.execute(ScanEncryptedTask.Params( + override suspend fun getScanResultForAttachment(mxcUrl: String, fileInfo: ElementToDecrypt?): ScanStatusInfo { + val result = if (fileInfo != null) { + scanEncryptedTask.execute(ScanEncryptedTask.Params( mxcUrl = mxcUrl, - publicServerKey = serverPublicKey, + publicServerKey = getServerPublicKey(false), encryptedInfo = fileInfo )) - - ScanStatusInfo( - state = if (result.clean) ScanState.TRUSTED else ScanState.INFECTED, - humanReadableMessage = result.info, - scanDateTimestamp = System.currentTimeMillis() - ) + } else { + scanMediaTask.execute(ScanMediaTask.Params(mxcUrl)) } - } - override fun getScanResultForAttachment(mxcUrl: String, callback: MatrixCallback) { - taskExecutor.executorScope.launchToCallback(coroutineDispatchers.io, callback) { - val result = scanMediaTask.execute(ScanMediaTask.Params(mxcUrl)) - - ScanStatusInfo( - state = if (result.clean) ScanState.TRUSTED else ScanState.INFECTED, - humanReadableMessage = result.info, - scanDateTimestamp = System.currentTimeMillis() - ) - } + return ScanStatusInfo( + state = if (result.clean) ScanState.TRUSTED else ScanState.INFECTED, + humanReadableMessage = result.info, + scanDateTimestamp = System.currentTimeMillis() + ) } override fun setScannerUrl(url: String?) = contentScannerStore.setScannerUrl(url).also { @@ -112,11 +97,9 @@ internal class DefaultContentScannerService @Inject constructor( .create(ContentScannerApi::class.java) contentScannerApiProvider.contentScannerApi = api - taskExecutor.executorScope.launch(coroutineDispatchers.io) { + taskExecutor.executorScope.launch { try { - awaitCallback { - getServerPublicKey(true, it) - } + getServerPublicKey(true) } catch (failure: Throwable) { Timber.e("Failed to get public server api") } @@ -124,7 +107,7 @@ internal class DefaultContentScannerService @Inject constructor( } } - override fun enableScanner(enabled: Boolean) = contentScannerStore.enableScanning(enabled) + override fun enableScanner(enabled: Boolean) = contentScannerStore.enableScanner(enabled) override fun isScannerEnabled(): Boolean = contentScannerStore.isScanEnabled() @@ -132,18 +115,16 @@ internal class DefaultContentScannerService @Inject constructor( return contentScannerStore.getScanResult(mxcUrl) } - override fun getLiveStatusForFile(mxcUrl: String, fetchIfNeeded: Boolean): LiveData> { + override fun getLiveStatusForFile(mxcUrl: String, fetchIfNeeded: Boolean, fileInfo: ElementToDecrypt?): LiveData> { val data = contentScannerStore.getLiveScanResult(mxcUrl) if (fetchIfNeeded && !contentScannerStore.isScanResultKnownOrInProgress(mxcUrl, getContentScannerServer())) { - getScanResultForAttachment(mxcUrl, NoOpMatrixCallback()) - } - return data - } - - override fun getLiveStatusForEncryptedFile(mxcUrl: String, fileInfo: ElementToDecrypt, fetchIfNeeded: Boolean): LiveData> { - val data = contentScannerStore.getLiveScanResult(mxcUrl) - if (fetchIfNeeded && !contentScannerStore.isScanResultKnownOrInProgress(mxcUrl, getContentScannerServer())) { - getScanResultForAttachment(mxcUrl, fileInfo, NoOpMatrixCallback()) + taskExecutor.executorScope.launch { + try { + getScanResultForAttachment(mxcUrl, fileInfo) + } catch (failure: Throwable) { + Timber.e("Failed to get file status : ${failure.localizedMessage}") + } + } } return data } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/DisabledContentScannerService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/DisabledContentScannerService.kt index 6daa094ed2..240a8deaba 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/DisabledContentScannerService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/DisabledContentScannerService.kt @@ -18,16 +18,18 @@ package org.matrix.android.sdk.internal.session.contentscanner import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData -import org.matrix.android.sdk.api.MatrixCallback import org.matrix.android.sdk.api.session.contentscanner.ContentScannerService import org.matrix.android.sdk.api.session.contentscanner.ScanStatusInfo import org.matrix.android.sdk.api.util.Optional import org.matrix.android.sdk.internal.crypto.attachments.ElementToDecrypt +import org.matrix.android.sdk.internal.session.SessionScope +import javax.inject.Inject /** * Created to by-pass ProfileTask execution in LoginWizard. */ -class DisabledContentScannerService : ContentScannerService { +@SessionScope +internal class DisabledContentScannerService @Inject constructor() : ContentScannerService { override val serverPublicKey: String? get() = null @@ -36,13 +38,12 @@ class DisabledContentScannerService : ContentScannerService { return null } - override fun getServerPublicKey(forceDownload: Boolean, callback: MatrixCallback) { + override suspend fun getServerPublicKey(forceDownload: Boolean): String? { + return null } - override fun getScanResultForAttachment(mxcUrl: String, callback: MatrixCallback) { - } - - override fun getScanResultForAttachment(mxcUrl: String, fileInfo: ElementToDecrypt, callback: MatrixCallback) { + override suspend fun getScanResultForAttachment(mxcUrl: String, fileInfo: ElementToDecrypt?): ScanStatusInfo { + TODO("Not yet implemented") } override fun setScannerUrl(url: String?) { @@ -55,11 +56,7 @@ class DisabledContentScannerService : ContentScannerService { return false } - override fun getLiveStatusForFile(mxcUrl: String, fetchIfNeeded: Boolean): LiveData> { - return MutableLiveData() - } - - override fun getLiveStatusForEncryptedFile(mxcUrl: String, fileInfo: ElementToDecrypt, fetchIfNeeded: Boolean): LiveData> { + override fun getLiveStatusForFile(mxcUrl: String, fetchIfNeeded: Boolean, fileInfo: ElementToDecrypt?): LiveData> { return MutableLiveData() } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/ScanEncryptorUtils.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/ScanEncryptorUtils.kt index 567002750f..8fc84a487e 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/ScanEncryptorUtils.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/ScanEncryptorUtils.kt @@ -1,7 +1,17 @@ /* - * Copyright 2020 New Vector Ltd - All Rights Reserved - * Unauthorized copying of this file, via any medium is strictly prohibited - * Proprietary and confidential + * Copyright 2021 The Matrix.org Foundation C.I.C. + * + * 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. */ package org.matrix.android.sdk.internal.session.contentscanner @@ -14,7 +24,7 @@ import org.matrix.android.sdk.internal.session.contentscanner.model.DownloadBody import org.matrix.android.sdk.internal.session.contentscanner.model.EncryptedBody import org.matrix.android.sdk.internal.session.contentscanner.model.toCanonicalJson -object ScanEncryptorUtils { +internal object ScanEncryptorUtils { fun getDownloadBodyAndEncryptIfNeeded(publicServerKey: String?, mxcUrl: String, elementToDecrypt: ElementToDecrypt): DownloadBody { // TODO, upstream refactoring changed the object model here... diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/data/ContentScannerStore.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/data/ContentScannerStore.kt index af43c75c04..5cfe851a5c 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/data/ContentScannerStore.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/data/ContentScannerStore.kt @@ -1,7 +1,17 @@ /* - * Copyright 2020 New Vector Ltd - All Rights Reserved - * Unauthorized copying of this file, via any medium is strictly prohibited - * Proprietary and confidential + * Copyright 2021 The Matrix.org Foundation C.I.C. + * + * 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. */ package org.matrix.android.sdk.internal.session.contentscanner.data @@ -17,7 +27,7 @@ internal interface ContentScannerStore { fun setScannerUrl(url: String?) - fun enableScanning(enabled: Boolean) + fun enableScanner(enabled: Boolean) fun isScanEnabled(): Boolean diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/db/ContentScanResultEntity.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/db/ContentScanResultEntity.kt index 33284b2556..8befba17e2 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/db/ContentScanResultEntity.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/db/ContentScanResultEntity.kt @@ -1,7 +1,17 @@ /* - * Copyright 2020 New Vector Ltd - All Rights Reserved - * Unauthorized copying of this file, via any medium is strictly prohibited - * Proprietary and confidential + * Copyright 2021 The Matrix.org Foundation C.I.C. + * + * 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. */ package org.matrix.android.sdk.internal.session.contentscanner.db diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/db/ContentScannerEntityQueries.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/db/ContentScannerEntityQueries.kt index fcabae730b..b47be235c6 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/db/ContentScannerEntityQueries.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/db/ContentScannerEntityQueries.kt @@ -1,7 +1,17 @@ /* - * Copyright 2020 New Vector Ltd - All Rights Reserved - * Unauthorized copying of this file, via any medium is strictly prohibited - * Proprietary and confidential + * Copyright 2021 The Matrix.org Foundation C.I.C. + * + * 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. */ package org.matrix.android.sdk.internal.session.contentscanner.db diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/db/ContentScannerInfoEntity.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/db/ContentScannerInfoEntity.kt index da0011db72..d1910de36a 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/db/ContentScannerInfoEntity.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/db/ContentScannerInfoEntity.kt @@ -1,7 +1,17 @@ /* - * Copyright 2020 New Vector Ltd - All Rights Reserved - * Unauthorized copying of this file, via any medium is strictly prohibited - * Proprietary and confidential + * Copyright 2021 The Matrix.org Foundation C.I.C. + * + * 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. */ package org.matrix.android.sdk.internal.session.contentscanner.db diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/db/ContentScannerRealmModule.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/db/ContentScannerRealmModule.kt index aec926beef..bb53140ad9 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/db/ContentScannerRealmModule.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/db/ContentScannerRealmModule.kt @@ -1,7 +1,17 @@ /* - * Copyright 2020 New Vector Ltd - All Rights Reserved - * Unauthorized copying of this file, via any medium is strictly prohibited - * Proprietary and confidential + * Copyright 2021 The Matrix.org Foundation C.I.C. + * + * 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. */ package org.matrix.android.sdk.internal.session.contentscanner.db diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/db/RealmContentScannerStore.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/db/RealmContentScannerStore.kt index 6b561c3720..11b9439c2f 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/db/RealmContentScannerStore.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/db/RealmContentScannerStore.kt @@ -1,7 +1,17 @@ /* - * Copyright 2020 New Vector Ltd - All Rights Reserved - * Unauthorized copying of this file, via any medium is strictly prohibited - * Proprietary and confidential + * Copyright 2021 The Matrix.org Foundation C.I.C. + * + * 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. */ package org.matrix.android.sdk.internal.session.contentscanner.db @@ -52,7 +62,7 @@ internal class RealmContentScannerStore @Inject constructor( } } - override fun enableScanning(enabled: Boolean) { + override fun enableScanner(enabled: Boolean) { monarchy.runTransactionSync { realm -> val info = realm.where().findFirst() ?: realm.createObject() diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/model/DownloadBody.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/model/DownloadBody.kt index fbb0688225..5bac96a0c0 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/model/DownloadBody.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/model/DownloadBody.kt @@ -1,7 +1,17 @@ /* - * Copyright 2020 New Vector Ltd - All Rights Reserved - * Unauthorized copying of this file, via any medium is strictly prohibited - * Proprietary and confidential + * Copyright 2021 The Matrix.org Foundation C.I.C. + * + * 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. */ package org.matrix.android.sdk.internal.session.contentscanner.model @@ -13,18 +23,18 @@ import org.matrix.android.sdk.internal.di.MoshiProvider import org.matrix.android.sdk.internal.util.JsonCanonicalizer @JsonClass(generateAdapter = true) -data class DownloadBody( +internal data class DownloadBody( @Json(name = "file") val file: EncryptedFileInfo? = null, @Json(name = "encrypted_body") val encryptedBody: EncryptedBody? = null ) @JsonClass(generateAdapter = true) -data class EncryptedBody( +internal data class EncryptedBody( @Json(name = "ciphertext") val cipherText: String, @Json(name = "mac") val mac: String, @Json(name = "ephemeral") val ephemeral: String ) -fun DownloadBody.toJson(): String = MoshiProvider.providesMoshi().adapter(DownloadBody::class.java).toJson(this) +internal fun DownloadBody.toJson(): String = MoshiProvider.providesMoshi().adapter(DownloadBody::class.java).toJson(this) -fun DownloadBody.toCanonicalJson() = JsonCanonicalizer.getCanonicalJson(DownloadBody::class.java, this) +internal fun DownloadBody.toCanonicalJson() = JsonCanonicalizer.getCanonicalJson(DownloadBody::class.java, this) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/model/ScanResponse.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/model/ScanResponse.kt index 0a730d614c..f783fe0a6c 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/model/ScanResponse.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/model/ScanResponse.kt @@ -1,7 +1,17 @@ /* - * Copyright 2020 New Vector Ltd - All Rights Reserved - * Unauthorized copying of this file, via any medium is strictly prohibited - * Proprietary and confidential + * Copyright 2021 The Matrix.org Foundation C.I.C. + * + * 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. */ package org.matrix.android.sdk.internal.session.contentscanner.model @@ -16,7 +26,7 @@ import com.squareup.moshi.JsonClass * } */ @JsonClass(generateAdapter = true) -data class ScanResponse( +internal data class ScanResponse( @Json(name = "clean") val clean: Boolean, /** Human-readable information about the result. */ @Json(name = "info") val info: String? diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/model/ServerPublicKeyResponse.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/model/ServerPublicKeyResponse.kt index ca6ffe96b3..688d030197 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/model/ServerPublicKeyResponse.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/model/ServerPublicKeyResponse.kt @@ -1,7 +1,17 @@ /* - * Copyright 2020 New Vector Ltd - All Rights Reserved - * Unauthorized copying of this file, via any medium is strictly prohibited - * Proprietary and confidential + * Copyright 2021 The Matrix.org Foundation C.I.C. + * + * 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. */ package org.matrix.android.sdk.internal.session.contentscanner.model @@ -10,7 +20,7 @@ import com.squareup.moshi.Json import com.squareup.moshi.JsonClass @JsonClass(generateAdapter = true) -data class ServerPublicKeyResponse( +internal data class ServerPublicKeyResponse( @Json(name = "public_key") val publicKey : String? ) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/tasks/DownloadEncryptedTask.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/tasks/DownloadEncryptedTask.kt index 6a9e217740..f92c869cb8 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/tasks/DownloadEncryptedTask.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/tasks/DownloadEncryptedTask.kt @@ -1,7 +1,17 @@ /* - * Copyright 2020 New Vector Ltd - All Rights Reserved - * Unauthorized copying of this file, via any medium is strictly prohibited - * Proprietary and confidential + * Copyright 2021 The Matrix.org Foundation C.I.C. + * + * 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. */ package org.matrix.android.sdk.internal.session.contentscanner.tasks diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/tasks/GetServerPublicKeyTask.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/tasks/GetServerPublicKeyTask.kt index 9efe98ba12..41c2ec9c38 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/tasks/GetServerPublicKeyTask.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/tasks/GetServerPublicKeyTask.kt @@ -1,7 +1,17 @@ /* - * Copyright 2020 New Vector Ltd - All Rights Reserved - * Unauthorized copying of this file, via any medium is strictly prohibited - * Proprietary and confidential + * Copyright 2021 The Matrix.org Foundation C.I.C. + * + * 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. */ package org.matrix.android.sdk.internal.session.contentscanner.tasks diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/tasks/ScanEncryptedTask.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/tasks/ScanEncryptedTask.kt index c1185e02ba..dab9b5538f 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/tasks/ScanEncryptedTask.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/tasks/ScanEncryptedTask.kt @@ -1,7 +1,17 @@ /* - * Copyright 2020 New Vector Ltd - All Rights Reserved - * Unauthorized copying of this file, via any medium is strictly prohibited - * Proprietary and confidential + * Copyright 2021 The Matrix.org Foundation C.I.C. + * + * 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. */ package org.matrix.android.sdk.internal.session.contentscanner.tasks diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/tasks/ScanMediaTask.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/tasks/ScanMediaTask.kt index 66f5dfef63..0cb56c1073 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/tasks/ScanMediaTask.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/tasks/ScanMediaTask.kt @@ -1,7 +1,17 @@ /* - * Copyright 2020 New Vector Ltd - All Rights Reserved - * Unauthorized copying of this file, via any medium is strictly prohibited - * Proprietary and confidential + * Copyright 2021 The Matrix.org Foundation C.I.C. + * + * 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. */ package org.matrix.android.sdk.internal.session.contentscanner.tasks From 6ddf3f6e5a491673fae542c0eed9cc300ff613cc Mon Sep 17 00:00:00 2001 From: yostyle Date: Wed, 10 Nov 2021 17:38:11 +0100 Subject: [PATCH 6/7] Fix lint errors --- .../sdk/api/session/content/ContentUrlResolver.kt | 2 +- .../contentscanner/ContentScannerService.kt | 14 ++++++-------- .../sdk/api/session/contentscanner/ScanState.kt | 2 +- .../android/sdk/internal/session/DefaultSession.kt | 2 +- .../sdk/internal/session/SessionComponent.kt | 2 +- .../DisabledContentScannerService.kt | 2 +- .../contentscanner/db/ContentScanResultEntity.kt | 2 +- .../contentscanner/db/RealmContentScannerStore.kt | 9 +++++---- .../model/ServerPublicKeyResponse.kt | 2 +- tools/check/forbidden_strings_in_code.txt | 2 +- 10 files changed, 19 insertions(+), 20 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/content/ContentUrlResolver.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/content/ContentUrlResolver.kt index 56e3cd9796..3dd096e144 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/content/ContentUrlResolver.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/content/ContentUrlResolver.kt @@ -63,6 +63,6 @@ interface ContentUrlResolver { sealed class ResolvedMethod { data class GET(val url: String) : ResolvedMethod() - data class POST(val url:String, val jsonBody: String): ResolvedMethod() + data class POST(val url: String, val jsonBody: String) : ResolvedMethod() } } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/contentscanner/ContentScannerService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/contentscanner/ContentScannerService.kt index 0c45e6a132..1dd7bab01c 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/contentscanner/ContentScannerService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/contentscanner/ContentScannerService.kt @@ -25,18 +25,16 @@ interface ContentScannerService { val serverPublicKey: String? fun getContentScannerServer(): String? + fun setScannerUrl(url: String?) + fun enableScanner(enabled: Boolean) + fun isScannerEnabled(): Boolean + fun getLiveStatusForFile(mxcUrl: String, fetchIfNeeded: Boolean = true, fileInfo: ElementToDecrypt? = null): LiveData> + fun getCachedScanResultForFile(mxcUrl: String): ScanStatusInfo? + /** * Get the current public curve25519 key that the AV server is advertising. * @param callback on success callback containing the server public key */ suspend fun getServerPublicKey(forceDownload: Boolean = false): String? - suspend fun getScanResultForAttachment(mxcUrl: String, fileInfo: ElementToDecrypt? = null): ScanStatusInfo - - fun setScannerUrl(url: String?) - - fun enableScanner(enabled: Boolean) - fun isScannerEnabled(): Boolean - fun getLiveStatusForFile(mxcUrl: String, fetchIfNeeded: Boolean = true, fileInfo: ElementToDecrypt? = null): LiveData> - fun getCachedScanResultForFile(mxcUrl: String): ScanStatusInfo? } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/contentscanner/ScanState.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/contentscanner/ScanState.kt index aeb1551cf7..da209080ac 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/contentscanner/ScanState.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/contentscanner/ScanState.kt @@ -24,7 +24,7 @@ enum class ScanState { } data class ScanStatusInfo( - val state : ScanState, + val state: ScanState, val scanDateTimestamp: Long?, val humanReadableMessage: String? ) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultSession.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultSession.kt index f0ad0f87b0..32e88bafb4 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultSession.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultSession.kt @@ -278,7 +278,7 @@ internal class DefaultSession @Inject constructor( override fun cryptoService(): CryptoService = cryptoService.get() override fun contentScannerService(): ContentScannerService = contentScannerService.get() - + override fun identityService() = identityService override fun fileService(): FileService = defaultFileService.get() diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/SessionComponent.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/SessionComponent.kt index a3f8f5dc6d..76e5d84e56 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/SessionComponent.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/SessionComponent.kt @@ -18,7 +18,6 @@ package org.matrix.android.sdk.internal.session import dagger.BindsInstance import dagger.Component -import org.matrix.android.sdk.internal.session.contentscanner.ContentScannerModule import org.matrix.android.sdk.api.MatrixCoroutineDispatchers import org.matrix.android.sdk.api.auth.data.SessionParams import org.matrix.android.sdk.api.session.Session @@ -37,6 +36,7 @@ import org.matrix.android.sdk.internal.session.cache.CacheModule import org.matrix.android.sdk.internal.session.call.CallModule import org.matrix.android.sdk.internal.session.content.ContentModule import org.matrix.android.sdk.internal.session.content.UploadContentWorker +import org.matrix.android.sdk.internal.session.contentscanner.ContentScannerModule import org.matrix.android.sdk.internal.session.filter.FilterModule import org.matrix.android.sdk.internal.session.group.GetGroupDataWorker import org.matrix.android.sdk.internal.session.group.GroupModule diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/DisabledContentScannerService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/DisabledContentScannerService.kt index 240a8deaba..9087c71566 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/DisabledContentScannerService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/DisabledContentScannerService.kt @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 New Vector Ltd + * Copyright 2021 The Matrix.org Foundation C.I.C. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/db/ContentScanResultEntity.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/db/ContentScanResultEntity.kt index 8befba17e2..0ffff441f8 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/db/ContentScanResultEntity.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/db/ContentScanResultEntity.kt @@ -43,7 +43,7 @@ internal open class ContentScanResultEntity( scanStatusString = result.name } - fun toModel() : ScanStatusInfo { + fun toModel(): ScanStatusInfo { return ScanStatusInfo( state = this.scanResult, humanReadableMessage = humanReadableMessage, diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/db/RealmContentScannerStore.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/db/RealmContentScannerStore.kt index 11b9439c2f..947a66c8b9 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/db/RealmContentScannerStore.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/db/RealmContentScannerStore.kt @@ -19,18 +19,18 @@ package org.matrix.android.sdk.internal.session.contentscanner.db import androidx.lifecycle.LiveData import androidx.lifecycle.Transformations import com.zhuinden.monarchy.Monarchy -import org.matrix.android.sdk.internal.session.contentscanner.data.ContentScannerStore -import org.matrix.android.sdk.api.session.contentscanner.ScanState -import org.matrix.android.sdk.api.session.contentscanner.ScanStatusInfo import io.realm.Realm import io.realm.RealmConfiguration import io.realm.kotlin.createObject import io.realm.kotlin.where import org.matrix.android.sdk.api.extensions.orFalse +import org.matrix.android.sdk.api.session.contentscanner.ScanState +import org.matrix.android.sdk.api.session.contentscanner.ScanStatusInfo import org.matrix.android.sdk.api.util.Optional import org.matrix.android.sdk.api.util.toOptional import org.matrix.android.sdk.internal.di.ContentScannerDatabase import org.matrix.android.sdk.internal.session.SessionScope +import org.matrix.android.sdk.internal.session.contentscanner.data.ContentScannerStore import org.matrix.android.sdk.internal.util.isValidUrl import javax.inject.Inject @@ -121,7 +121,8 @@ internal class RealmContentScannerStore @Inject constructor( } }, { it.toModel() - }).firstOrNull() + }) + .firstOrNull() } override fun getLiveScanResult(mxcUrl: String): LiveData> { diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/model/ServerPublicKeyResponse.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/model/ServerPublicKeyResponse.kt index 688d030197..2e97a85bca 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/model/ServerPublicKeyResponse.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/contentscanner/model/ServerPublicKeyResponse.kt @@ -22,5 +22,5 @@ import com.squareup.moshi.JsonClass @JsonClass(generateAdapter = true) internal data class ServerPublicKeyResponse( @Json(name = "public_key") - val publicKey : String? + val publicKey: String? ) diff --git a/tools/check/forbidden_strings_in_code.txt b/tools/check/forbidden_strings_in_code.txt index 29077c3a76..b135954f63 100644 --- a/tools/check/forbidden_strings_in_code.txt +++ b/tools/check/forbidden_strings_in_code.txt @@ -160,7 +160,7 @@ Formatter\.formatShortFileSize===1 # android\.text\.TextUtils ### This is not a rule, but a warning: the number of "enum class" has changed. For Json classes, it is mandatory that they have `@JsonClass(generateAdapter = false)`. If the enum is not used as a Json class, change the value in file forbidden_strings_in_code.txt -enum class===107 +enum class===108 ### Do not import temporary legacy classes import org.matrix.android.sdk.internal.legacy.riot===3 From 0fada97450598e82f8c25fb0f0852f73877c226a Mon Sep 17 00:00:00 2001 From: yostyle Date: Wed, 17 Nov 2021 11:11:13 +0100 Subject: [PATCH 7/7] Add changelog --- changelog.d/4392.removal | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 changelog.d/4392.removal diff --git a/changelog.d/4392.removal b/changelog.d/4392.removal new file mode 100644 index 0000000000..42ba81152a --- /dev/null +++ b/changelog.d/4392.removal @@ -0,0 +1,2 @@ +Add content scanner API from MSC1453 +API documentation : https://github.com/matrix-org/matrix-content-scanner#api \ No newline at end of file