mirror of
https://github.com/SchildiChat/SchildiChat-android.git
synced 2024-11-26 11:26:01 +03:00
Create TemporaryFileCreator to avoid code duplication
This commit is contained in:
parent
590d8fc849
commit
e28556dd1b
5 changed files with 58 additions and 30 deletions
|
@ -41,10 +41,10 @@ import org.matrix.android.sdk.internal.network.ProgressRequestBody
|
|||
import org.matrix.android.sdk.internal.network.awaitResponse
|
||||
import org.matrix.android.sdk.internal.network.toFailure
|
||||
import org.matrix.android.sdk.internal.session.homeserver.DefaultHomeServerCapabilitiesService
|
||||
import org.matrix.android.sdk.internal.util.TemporaryFileCreator
|
||||
import java.io.File
|
||||
import java.io.FileNotFoundException
|
||||
import java.io.IOException
|
||||
import java.util.UUID
|
||||
import javax.inject.Inject
|
||||
|
||||
internal class FileUploader @Inject constructor(@Authenticated
|
||||
|
@ -52,6 +52,7 @@ internal class FileUploader @Inject constructor(@Authenticated
|
|||
private val globalErrorReceiver: GlobalErrorReceiver,
|
||||
private val homeServerCapabilitiesService: DefaultHomeServerCapabilitiesService,
|
||||
private val context: Context,
|
||||
private val temporaryFileCreator: TemporaryFileCreator,
|
||||
contentUrlResolver: ContentUrlResolver,
|
||||
moshi: Moshi) {
|
||||
|
||||
|
@ -110,7 +111,7 @@ internal class FileUploader @Inject constructor(@Authenticated
|
|||
val inputStream = withContext(Dispatchers.IO) {
|
||||
context.contentResolver.openInputStream(uri)
|
||||
} ?: throw FileNotFoundException()
|
||||
val workingFile = File.createTempFile(UUID.randomUUID().toString(), null, context.cacheDir)
|
||||
val workingFile = temporaryFileCreator.create()
|
||||
workingFile.outputStream().use {
|
||||
inputStream.copyTo(it)
|
||||
}
|
||||
|
|
|
@ -16,19 +16,20 @@
|
|||
|
||||
package org.matrix.android.sdk.internal.session.content
|
||||
|
||||
import android.content.Context
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.BitmapFactory
|
||||
import android.graphics.Matrix
|
||||
import androidx.exifinterface.media.ExifInterface
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import org.matrix.android.sdk.internal.util.TemporaryFileCreator
|
||||
import timber.log.Timber
|
||||
import java.io.File
|
||||
import java.util.UUID
|
||||
import javax.inject.Inject
|
||||
|
||||
internal class ImageCompressor @Inject constructor(private val context: Context) {
|
||||
internal class ImageCompressor @Inject constructor(
|
||||
private val temporaryFileCreator: TemporaryFileCreator
|
||||
) {
|
||||
suspend fun compress(
|
||||
imageFile: File,
|
||||
desiredWidth: Int,
|
||||
|
@ -45,7 +46,7 @@ internal class ImageCompressor @Inject constructor(private val context: Context)
|
|||
}
|
||||
} ?: return@withContext imageFile
|
||||
|
||||
val destinationFile = createDestinationFile()
|
||||
val destinationFile = temporaryFileCreator.create()
|
||||
|
||||
runCatching {
|
||||
destinationFile.outputStream().use {
|
||||
|
@ -53,7 +54,7 @@ internal class ImageCompressor @Inject constructor(private val context: Context)
|
|||
}
|
||||
}
|
||||
|
||||
return@withContext destinationFile
|
||||
destinationFile
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -64,16 +65,16 @@ internal class ImageCompressor @Inject constructor(private val context: Context)
|
|||
val orientation = exifInfo.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL)
|
||||
val matrix = Matrix()
|
||||
when (orientation) {
|
||||
ExifInterface.ORIENTATION_ROTATE_270 -> matrix.postRotate(270f)
|
||||
ExifInterface.ORIENTATION_ROTATE_180 -> matrix.postRotate(180f)
|
||||
ExifInterface.ORIENTATION_ROTATE_90 -> matrix.postRotate(90f)
|
||||
ExifInterface.ORIENTATION_ROTATE_270 -> matrix.postRotate(270f)
|
||||
ExifInterface.ORIENTATION_ROTATE_180 -> matrix.postRotate(180f)
|
||||
ExifInterface.ORIENTATION_ROTATE_90 -> matrix.postRotate(90f)
|
||||
ExifInterface.ORIENTATION_FLIP_HORIZONTAL -> matrix.preScale(-1f, 1f)
|
||||
ExifInterface.ORIENTATION_FLIP_VERTICAL -> matrix.preScale(1f, -1f)
|
||||
ExifInterface.ORIENTATION_TRANSPOSE -> {
|
||||
ExifInterface.ORIENTATION_FLIP_VERTICAL -> matrix.preScale(1f, -1f)
|
||||
ExifInterface.ORIENTATION_TRANSPOSE -> {
|
||||
matrix.preRotate(-90f)
|
||||
matrix.preScale(-1f, 1f)
|
||||
}
|
||||
ExifInterface.ORIENTATION_TRANSVERSE -> {
|
||||
ExifInterface.ORIENTATION_TRANSVERSE -> {
|
||||
matrix.preRotate(90f)
|
||||
matrix.preScale(-1f, 1f)
|
||||
}
|
||||
|
@ -116,8 +117,4 @@ internal class ImageCompressor @Inject constructor(private val context: Context)
|
|||
null
|
||||
}
|
||||
}
|
||||
|
||||
private fun createDestinationFile(): File {
|
||||
return File.createTempFile(UUID.randomUUID().toString(), null, context.cacheDir)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -44,13 +44,13 @@ import org.matrix.android.sdk.internal.session.room.send.CancelSendTracker
|
|||
import org.matrix.android.sdk.internal.session.room.send.LocalEchoIdentifiers
|
||||
import org.matrix.android.sdk.internal.session.room.send.LocalEchoRepository
|
||||
import org.matrix.android.sdk.internal.session.room.send.MultipleEventSendingDispatcherWorker
|
||||
import org.matrix.android.sdk.internal.util.TemporaryFileCreator
|
||||
import org.matrix.android.sdk.internal.util.toMatrixErrorStr
|
||||
import org.matrix.android.sdk.internal.worker.SessionSafeCoroutineWorker
|
||||
import org.matrix.android.sdk.internal.worker.SessionWorkerParams
|
||||
import org.matrix.android.sdk.internal.worker.WorkerParamsFactory
|
||||
import timber.log.Timber
|
||||
import java.io.File
|
||||
import java.util.UUID
|
||||
import javax.inject.Inject
|
||||
|
||||
private data class NewAttachmentAttributes(
|
||||
|
@ -83,6 +83,7 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter
|
|||
@Inject lateinit var imageCompressor: ImageCompressor
|
||||
@Inject lateinit var videoCompressor: VideoCompressor
|
||||
@Inject lateinit var localEchoRepository: LocalEchoRepository
|
||||
@Inject lateinit var temporaryFileCreator: TemporaryFileCreator
|
||||
|
||||
override fun injectWith(injector: SessionComponent) {
|
||||
injector.inject(this)
|
||||
|
@ -125,7 +126,7 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter
|
|||
)
|
||||
|
||||
// always use a temporary file, it guaranties that we could report progress on upload and simplifies the flows
|
||||
val workingFile = File.createTempFile(UUID.randomUUID().toString(), null, context.cacheDir)
|
||||
val workingFile = temporaryFileCreator.create()
|
||||
.also { filesToDelete.add(it) }
|
||||
workingFile.outputStream().use { outputStream ->
|
||||
inputStream.use { inputStream ->
|
||||
|
@ -228,7 +229,7 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter
|
|||
val contentUploadResponse = if (params.isEncrypted) {
|
||||
Timber.v("## Encrypt file")
|
||||
|
||||
encryptedFile = File.createTempFile(UUID.randomUUID().toString(), null, context.cacheDir)
|
||||
encryptedFile = temporaryFileCreator.create()
|
||||
.also { filesToDelete.add(it) }
|
||||
|
||||
uploadedFileEncryptedFileInfo =
|
||||
|
|
|
@ -16,25 +16,24 @@
|
|||
|
||||
package org.matrix.android.sdk.internal.session.content
|
||||
|
||||
import android.content.Context
|
||||
import com.otaliastudios.transcoder.Transcoder
|
||||
import com.otaliastudios.transcoder.TranscoderListener
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.withContext
|
||||
import org.matrix.android.sdk.api.listeners.ProgressListener
|
||||
import org.matrix.android.sdk.internal.util.TemporaryFileCreator
|
||||
import timber.log.Timber
|
||||
import java.io.File
|
||||
import java.util.UUID
|
||||
import javax.inject.Inject
|
||||
|
||||
internal class VideoCompressor @Inject constructor(private val context: Context) {
|
||||
internal class VideoCompressor @Inject constructor(
|
||||
private val temporaryFileCreator: TemporaryFileCreator
|
||||
) {
|
||||
|
||||
suspend fun compress(videoFile: File,
|
||||
progressListener: ProgressListener?): VideoCompressionResult {
|
||||
val destinationFile = withContext(Dispatchers.IO) {
|
||||
createDestinationFile()
|
||||
}
|
||||
val destinationFile = temporaryFileCreator.create()
|
||||
|
||||
val job = Job()
|
||||
|
||||
|
@ -117,8 +116,4 @@ internal class VideoCompressor @Inject constructor(private val context: Context)
|
|||
file.delete()
|
||||
}
|
||||
}
|
||||
|
||||
private fun createDestinationFile(): File {
|
||||
return File.createTempFile(UUID.randomUUID().toString(), null, context.cacheDir)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,34 @@
|
|||
/*
|
||||
* Copyright (c) 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.util
|
||||
|
||||
import android.content.Context
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.io.File
|
||||
import java.util.UUID
|
||||
import javax.inject.Inject
|
||||
|
||||
internal class TemporaryFileCreator @Inject constructor(
|
||||
private val context: Context
|
||||
) {
|
||||
suspend fun create(): File {
|
||||
return withContext(Dispatchers.IO) {
|
||||
File.createTempFile(UUID.randomUUID().toString(), null, context.cacheDir)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue