Encrypt file WIP

This commit is contained in:
Benoit Marty 2019-07-02 12:34:56 +02:00
parent 0c2d3f36c3
commit f0e43d31f5
3 changed files with 33 additions and 20 deletions

View File

@ -54,7 +54,6 @@ internal class FileUploader @Inject constructor(@Authenticated


val uploadBody = RequestBody.create(MediaType.parse(mimeType), byteArray) val uploadBody = RequestBody.create(MediaType.parse(mimeType), byteArray)
return upload(uploadBody, filename, progressListener) return upload(uploadBody, filename, progressListener)

} }





View File

@ -72,7 +72,7 @@ internal class UploadContentWorker(context: Context, params: WorkerParameters) :
var uploadedThumbnailEncryptedFileInfo: EncryptedFileInfo? = null var uploadedThumbnailEncryptedFileInfo: EncryptedFileInfo? = null


if (thumbnailData != null) { if (thumbnailData != null) {
if (isRoomEncrypted) { val contentUploadResponse = if (isRoomEncrypted) {
Timber.v("Encrypt thumbnail") Timber.v("Encrypt thumbnail")
val encryptionResult = MXEncryptedAttachments.encryptAttachment(ByteArrayInputStream(thumbnailData.bytes), thumbnailData.mimeType) val encryptionResult = MXEncryptedAttachments.encryptAttachment(ByteArrayInputStream(thumbnailData.bytes), thumbnailData.mimeType)
?: return Result.failure() ?: return Result.failure()
@ -85,11 +85,12 @@ internal class UploadContentWorker(context: Context, params: WorkerParameters) :
fileUploader fileUploader
.uploadByteArray(thumbnailData.bytes, "thumb_${attachment.name}", thumbnailData.mimeType) .uploadByteArray(thumbnailData.bytes, "thumb_${attachment.name}", thumbnailData.mimeType)
} }

contentUploadResponse
.fold( .fold(
{ Timber.e(it) }, { Timber.e(it) },
{ uploadedThumbnailUrl = it.contentUri } { uploadedThumbnailUrl = it.contentUri }
) )

} }


val progressListener = object : ProgressRequestBody.Listener { val progressListener = object : ProgressRequestBody.Listener {
@ -100,7 +101,7 @@ internal class UploadContentWorker(context: Context, params: WorkerParameters) :


var uploadedFileEncryptedFileInfo: EncryptedFileInfo? = null var uploadedFileEncryptedFileInfo: EncryptedFileInfo? = null


return if (isRoomEncrypted) { val contentUploadResponse = if (isRoomEncrypted) {
Timber.v("Encrypt file") Timber.v("Encrypt file")


val encryptionResult = MXEncryptedAttachments.encryptAttachment(FileInputStream(attachmentFile), attachment.mimeType) val encryptionResult = MXEncryptedAttachments.encryptAttachment(FileInputStream(attachmentFile), attachment.mimeType)
@ -114,11 +115,12 @@ internal class UploadContentWorker(context: Context, params: WorkerParameters) :
fileUploader fileUploader
.uploadFile(attachmentFile, attachment.name, attachment.mimeType, progressListener) .uploadFile(attachmentFile, attachment.name, attachment.mimeType, progressListener)
} }

return contentUploadResponse
.fold( .fold(
{ handleFailure(params) }, { handleFailure(params) },
{ handleSuccess(params, it.contentUri, uploadedFileEncryptedFileInfo, uploadedThumbnailUrl, uploadedThumbnailEncryptedFileInfo) } { handleSuccess(params, it.contentUri, uploadedFileEncryptedFileInfo, uploadedThumbnailUrl, uploadedThumbnailEncryptedFileInfo) }
) )

} }


private fun createAttachmentFile(attachment: ContentAttachmentData): File? { private fun createAttachmentFile(attachment: ContentAttachmentData): File? {
@ -165,8 +167,8 @@ internal class UploadContentWorker(context: Context, params: WorkerParameters) :
private fun MessageImageContent.update(url: String, private fun MessageImageContent.update(url: String,
encryptedFileInfo: EncryptedFileInfo?): MessageImageContent { encryptedFileInfo: EncryptedFileInfo?): MessageImageContent {
return copy( return copy(
url = url, url = if (encryptedFileInfo == null) url else null,
encryptedFileInfo = encryptedFileInfo encryptedFileInfo = encryptedFileInfo?.copy(url = url)
) )
} }


@ -175,11 +177,10 @@ internal class UploadContentWorker(context: Context, params: WorkerParameters) :
thumbnailUrl: String?, thumbnailUrl: String?,
thumbnailEncryptedFileInfo: EncryptedFileInfo?): MessageVideoContent { thumbnailEncryptedFileInfo: EncryptedFileInfo?): MessageVideoContent {
return copy( return copy(
url = url, url = if (encryptedFileInfo == null) url else null,
encryptedFileInfo = encryptedFileInfo,
videoInfo = videoInfo?.copy( videoInfo = videoInfo?.copy(
thumbnailUrl = thumbnailUrl, thumbnailUrl = if (thumbnailEncryptedFileInfo == null) thumbnailUrl else null,
thumbnailFile = thumbnailEncryptedFileInfo thumbnailFile = thumbnailEncryptedFileInfo?.copy(url = url)
) )
) )
} }
@ -187,16 +188,16 @@ internal class UploadContentWorker(context: Context, params: WorkerParameters) :
private fun MessageFileContent.update(url: String, private fun MessageFileContent.update(url: String,
encryptedFileInfo: EncryptedFileInfo?): MessageFileContent { encryptedFileInfo: EncryptedFileInfo?): MessageFileContent {
return copy( return copy(
url = url, url = if (encryptedFileInfo == null) url else null,
encryptedFileInfo = encryptedFileInfo encryptedFileInfo = encryptedFileInfo?.copy(url = url)
) )
} }


private fun MessageAudioContent.update(url: String, private fun MessageAudioContent.update(url: String,
encryptedFileInfo: EncryptedFileInfo?): MessageAudioContent { encryptedFileInfo: EncryptedFileInfo?): MessageAudioContent {
return copy( return copy(
url = url, url = if (encryptedFileInfo == null) url else null,
encryptedFileInfo = encryptedFileInfo encryptedFileInfo = encryptedFileInfo?.copy(url = url)
) )
} }



View File

@ -101,13 +101,26 @@ internal class DefaultSendService @Inject constructor(private val context: Conte
val event = localEchoEventFactory.createMediaEvent(roomId, attachment).also { val event = localEchoEventFactory.createMediaEvent(roomId, attachment).also {
saveLocalEcho(it) 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) val sendWork = createSendEventWork(event)


WorkManager.getInstance(context) if (isRoomEncrypted) {
.beginUniqueWork(buildWorkIdentifier(UPLOAD_WORK), ExistingWorkPolicy.APPEND, uploadWork) val encryptWork = createEncryptEventWork(event)
.then(sendWork)
.enqueue() 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) return CancelableWork(context, sendWork.id)
} }