Refact: inject ThumbnailExtractor

This commit is contained in:
Benoit Marty 2021-05-26 13:55:29 +02:00 committed by Benoit Marty
parent cd66e62454
commit 898c8cc3a7
3 changed files with 11 additions and 6 deletions

View File

@ -23,8 +23,11 @@ import org.matrix.android.sdk.api.session.content.ContentAttachmentData
import org.matrix.android.sdk.api.util.MimeTypes
import timber.log.Timber
import java.io.ByteArrayOutputStream
import javax.inject.Inject
internal object ThumbnailExtractor {
internal class ThumbnailExtractor @Inject constructor(
private val context: Context
) {
class ThumbnailData(
val width: Int,
@ -34,15 +37,15 @@ internal object ThumbnailExtractor {
val mimeType: String
)
fun extractThumbnail(context: Context, attachment: ContentAttachmentData): ThumbnailData? {
fun extractThumbnail(attachment: ContentAttachmentData): ThumbnailData? {
return if (attachment.type == ContentAttachmentData.Type.VIDEO) {
extractVideoThumbnail(context, attachment)
extractVideoThumbnail(attachment)
} else {
null
}
}
private fun extractVideoThumbnail(context: Context, attachment: ContentAttachmentData): ThumbnailData? {
private fun extractVideoThumbnail(attachment: ContentAttachmentData): ThumbnailData? {
var thumbnailData: ThumbnailData? = null
val mediaMetadataRetriever = MediaMetadataRetriever()
try {

View File

@ -82,6 +82,7 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter
@Inject lateinit var cancelSendTracker: CancelSendTracker
@Inject lateinit var imageCompressor: ImageCompressor
@Inject lateinit var videoCompressor: VideoCompressor
@Inject lateinit var thumbnailExtractor: ThumbnailExtractor
@Inject lateinit var localEchoRepository: LocalEchoRepository
@Inject lateinit var temporaryFileCreator: TemporaryFileCreator
@ -302,7 +303,7 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter
* If appropriate, it will create and upload a thumbnail
*/
private suspend fun dealWithThumbnail(params: Params): UploadThumbnailResult? {
return ThumbnailExtractor.extractThumbnail(context, params.attachment)
return thumbnailExtractor.extractThumbnail(params.attachment)
?.let { thumbnailData ->
val thumbnailProgressListener = object : ProgressRequestBody.Listener {
override fun onProgress(current: Long, total: Long) {

View File

@ -73,6 +73,7 @@ internal class LocalEchoEventFactory @Inject constructor(
@UserId private val userId: String,
private val markdownParser: MarkdownParser,
private val textPillsUtils: TextPillsUtils,
private val thumbnailExtractor: ThumbnailExtractor,
private val localEchoRepository: LocalEchoRepository,
private val permalinkFactory: PermalinkFactory
) {
@ -261,7 +262,7 @@ internal class LocalEchoEventFactory @Inject constructor(
val width = firstFrame?.width ?: 0
mediaDataRetriever.release()
val thumbnailInfo = ThumbnailExtractor.extractThumbnail(context, attachment)?.let {
val thumbnailInfo = thumbnailExtractor.extractThumbnail(attachment)?.let {
ThumbnailInfo(
width = it.width,
height = it.height,