17
17
package im.vector.matrix.android.internal.session.content
18
18
19
19
import android.content.Context
20
+ import android.graphics.BitmapFactory
21
+ import android.os.Build
20
22
import androidx.work.CoroutineWorker
21
23
import androidx.work.WorkerParameters
22
24
import com.squareup.moshi.JsonClass
25
+ import id.zelory.compressor.Compressor
26
+ import id.zelory.compressor.constraint.default
23
27
import im.vector.matrix.android.api.session.content.ContentAttachmentData
24
28
import im.vector.matrix.android.api.session.events.model.Event
25
29
import im.vector.matrix.android.api.session.events.model.toContent
@@ -38,6 +42,9 @@ import im.vector.matrix.android.internal.worker.WorkerParamsFactory
38
42
import im.vector.matrix.android.internal.worker.getSessionComponent
39
43
import timber.log.Timber
40
44
import java.io.ByteArrayInputStream
45
+ import java.io.File
46
+ import java.io.FileOutputStream
47
+ import java.util.UUID
41
48
import javax.inject.Inject
42
49
43
50
private data class NewImageAttributes (
@@ -154,26 +161,70 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter
154
161
var uploadedFileEncryptedFileInfo: EncryptedFileInfo ? = null
155
162
156
163
return try {
157
- val contentUploadResponse = if (params.isEncrypted) {
158
- Timber .v(" Encrypt file" )
159
- notifyTracker(params) { contentUploadStateTracker.setEncrypting(it) }
164
+ // Temporary disable compressing for Android 10 and above.
165
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .Q ) {
166
+ val contentUploadResponse = if (params.isEncrypted) {
167
+ Timber .v(" Encrypt file" )
168
+ notifyTracker(params) { contentUploadStateTracker.setEncrypting(it) }
160
169
161
- val encryptionResult = MXEncryptedAttachments .encryptAttachment(inputStream, attachment.getSafeMimeType())
162
- uploadedFileEncryptedFileInfo = encryptionResult.encryptedFileInfo
170
+ val encryptionResult = MXEncryptedAttachments .encryptAttachment(inputStream, attachment.getSafeMimeType())
171
+ uploadedFileEncryptedFileInfo = encryptionResult.encryptedFileInfo
163
172
164
- fileUploader
165
- .uploadByteArray(encryptionResult.encryptedByteArray, attachment.name, " application/octet-stream" , progressListener)
173
+ fileUploader
174
+ .uploadByteArray(encryptionResult.encryptedByteArray, attachment.name, " application/octet-stream" , progressListener)
175
+ } else {
176
+ fileUploader
177
+ .uploadByteArray(inputStream.readBytes(), attachment.name, attachment.getSafeMimeType(), progressListener)
178
+ }
179
+ handleSuccess(params,
180
+ contentUploadResponse.contentUri,
181
+ uploadedFileEncryptedFileInfo,
182
+ uploadedThumbnailUrl,
183
+ uploadedThumbnailEncryptedFileInfo,
184
+ newImageAttributes)
166
185
} else {
167
- fileUploader
168
- .uploadByteArray(inputStream.readBytes(), attachment.name, attachment.getSafeMimeType(), progressListener)
169
- }
186
+ val cacheFile = File .createTempFile(attachment.name ? : UUID .randomUUID().toString(), " .jpg" , context.cacheDir)
187
+ cacheFile.parentFile?.mkdirs()
188
+ if (cacheFile.exists()) {
189
+ cacheFile.delete()
190
+ }
191
+ cacheFile.createNewFile()
192
+ cacheFile.deleteOnExit()
193
+
194
+ val outputStream = FileOutputStream (cacheFile)
195
+ outputStream.use {
196
+ inputStream.copyTo(outputStream)
197
+ }
198
+
199
+ val contentUploadResponse = if (attachment.type == ContentAttachmentData .Type .IMAGE && params.compressBeforeSending) {
200
+ Compressor .compress(context, cacheFile) {
201
+ default(
202
+ width = MAX_IMAGE_SIZE ,
203
+ height = MAX_IMAGE_SIZE
204
+ )
205
+ }.also { compressedFile ->
206
+ val options = BitmapFactory .Options ().apply { inJustDecodeBounds = true }
207
+ BitmapFactory .decodeFile(compressedFile.absolutePath, options)
208
+ val fileSize = compressedFile.length().toInt()
209
+ newImageAttributes = NewImageAttributes (
210
+ options.outWidth,
211
+ options.outHeight,
212
+ fileSize
213
+ )
214
+ }.let { compressedFile ->
215
+ fileUploader.uploadFile(compressedFile, attachment.name, attachment.getSafeMimeType(), progressListener)
216
+ }
217
+ } else {
218
+ fileUploader.uploadFile(cacheFile, attachment.name, attachment.getSafeMimeType(), progressListener)
219
+ }
170
220
171
- handleSuccess(params,
172
- contentUploadResponse.contentUri,
173
- uploadedFileEncryptedFileInfo,
174
- uploadedThumbnailUrl,
175
- uploadedThumbnailEncryptedFileInfo,
176
- newImageAttributes)
221
+ handleSuccess(params,
222
+ contentUploadResponse.contentUri,
223
+ uploadedFileEncryptedFileInfo,
224
+ uploadedThumbnailUrl,
225
+ uploadedThumbnailEncryptedFileInfo,
226
+ newImageAttributes)
227
+ }
177
228
} catch (t: Throwable ) {
178
229
Timber .e(t)
179
230
handleFailure(params, t)
0 commit comments