From f0e43d31f531eddb7ceba72489fbe754cebd8215 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Tue, 2 Jul 2019 12:34:56 +0200 Subject: [PATCH] Encrypt file WIP --- .../internal/session/content/FileUploader.kt | 1 - .../session/content/UploadContentWorker.kt | 29 ++++++++++--------- .../session/room/send/DefaultSendService.kt | 23 +++++++++++---- 3 files changed, 33 insertions(+), 20 deletions(-) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/content/FileUploader.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/content/FileUploader.kt index 61052084..2ec17248 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/content/FileUploader.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/content/FileUploader.kt @@ -54,7 +54,6 @@ internal class FileUploader @Inject constructor(@Authenticated val uploadBody = RequestBody.create(MediaType.parse(mimeType), byteArray) return upload(uploadBody, filename, progressListener) - } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/content/UploadContentWorker.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/content/UploadContentWorker.kt index 68dac276..a6bdda3c 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/content/UploadContentWorker.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/content/UploadContentWorker.kt @@ -72,7 +72,7 @@ internal class UploadContentWorker(context: Context, params: WorkerParameters) : var uploadedThumbnailEncryptedFileInfo: EncryptedFileInfo? = null if (thumbnailData != null) { - if (isRoomEncrypted) { + val contentUploadResponse = if (isRoomEncrypted) { Timber.v("Encrypt thumbnail") val encryptionResult = MXEncryptedAttachments.encryptAttachment(ByteArrayInputStream(thumbnailData.bytes), thumbnailData.mimeType) ?: return Result.failure() @@ -85,11 +85,12 @@ internal class UploadContentWorker(context: Context, params: WorkerParameters) : fileUploader .uploadByteArray(thumbnailData.bytes, "thumb_${attachment.name}", thumbnailData.mimeType) } + + contentUploadResponse .fold( { Timber.e(it) }, { uploadedThumbnailUrl = it.contentUri } ) - } val progressListener = object : ProgressRequestBody.Listener { @@ -100,7 +101,7 @@ internal class UploadContentWorker(context: Context, params: WorkerParameters) : var uploadedFileEncryptedFileInfo: EncryptedFileInfo? = null - return if (isRoomEncrypted) { + val contentUploadResponse = if (isRoomEncrypted) { Timber.v("Encrypt file") val encryptionResult = MXEncryptedAttachments.encryptAttachment(FileInputStream(attachmentFile), attachment.mimeType) @@ -114,11 +115,12 @@ internal class UploadContentWorker(context: Context, params: WorkerParameters) : fileUploader .uploadFile(attachmentFile, attachment.name, attachment.mimeType, progressListener) } + + return contentUploadResponse .fold( { handleFailure(params) }, { handleSuccess(params, it.contentUri, uploadedFileEncryptedFileInfo, uploadedThumbnailUrl, uploadedThumbnailEncryptedFileInfo) } ) - } private fun createAttachmentFile(attachment: ContentAttachmentData): File? { @@ -165,8 +167,8 @@ internal class UploadContentWorker(context: Context, params: WorkerParameters) : private fun MessageImageContent.update(url: String, encryptedFileInfo: EncryptedFileInfo?): MessageImageContent { return copy( - url = url, - encryptedFileInfo = encryptedFileInfo + url = if (encryptedFileInfo == null) url else null, + encryptedFileInfo = encryptedFileInfo?.copy(url = url) ) } @@ -175,11 +177,10 @@ internal class UploadContentWorker(context: Context, params: WorkerParameters) : thumbnailUrl: String?, thumbnailEncryptedFileInfo: EncryptedFileInfo?): MessageVideoContent { return copy( - url = url, - encryptedFileInfo = encryptedFileInfo, + url = if (encryptedFileInfo == null) url else null, videoInfo = videoInfo?.copy( - thumbnailUrl = thumbnailUrl, - thumbnailFile = thumbnailEncryptedFileInfo + thumbnailUrl = if (thumbnailEncryptedFileInfo == null) thumbnailUrl else null, + thumbnailFile = thumbnailEncryptedFileInfo?.copy(url = url) ) ) } @@ -187,16 +188,16 @@ internal class UploadContentWorker(context: Context, params: WorkerParameters) : private fun MessageFileContent.update(url: String, encryptedFileInfo: EncryptedFileInfo?): MessageFileContent { return copy( - url = url, - encryptedFileInfo = encryptedFileInfo + url = if (encryptedFileInfo == null) url else null, + encryptedFileInfo = encryptedFileInfo?.copy(url = url) ) } private fun MessageAudioContent.update(url: String, encryptedFileInfo: EncryptedFileInfo?): MessageAudioContent { return copy( - url = url, - encryptedFileInfo = encryptedFileInfo + url = if (encryptedFileInfo == null) url else null, + encryptedFileInfo = encryptedFileInfo?.copy(url = url) ) } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/DefaultSendService.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/DefaultSendService.kt index eaf0c414..27e9c62e 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/DefaultSendService.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/DefaultSendService.kt @@ -101,13 +101,26 @@ internal class DefaultSendService @Inject constructor(private val context: Conte val event = localEchoEventFactory.createMediaEvent(roomId, attachment).also { saveLocalEcho(it) } - val uploadWork = createUploadMediaWork(event, attachment, cryptoService.isRoomEncrypted(roomId)) + + val isRoomEncrypted = cryptoService.isRoomEncrypted(roomId) + + val uploadWork = createUploadMediaWork(event, attachment, isRoomEncrypted) val sendWork = createSendEventWork(event) - WorkManager.getInstance(context) - .beginUniqueWork(buildWorkIdentifier(UPLOAD_WORK), ExistingWorkPolicy.APPEND, uploadWork) - .then(sendWork) - .enqueue() + if (isRoomEncrypted) { + val encryptWork = createEncryptEventWork(event) + + WorkManager.getInstance(context) + .beginUniqueWork(buildWorkIdentifier(UPLOAD_WORK), ExistingWorkPolicy.APPEND, uploadWork) + .then(encryptWork) + .then(sendWork) + .enqueue() + } else { + WorkManager.getInstance(context) + .beginUniqueWork(buildWorkIdentifier(UPLOAD_WORK), ExistingWorkPolicy.APPEND, uploadWork) + .then(sendWork) + .enqueue() + } return CancelableWork(context, sendWork.id) }