Providing a thumbnail when sending a media is now optional.
This commit is contained in:
parent
c4aaaf6d5f
commit
55ce9068ef
7 changed files with 47 additions and 37 deletions
|
|
@ -100,9 +100,9 @@ interface MatrixRoom : Closeable {
|
||||||
|
|
||||||
suspend fun redactEvent(eventId: EventId, reason: String? = null): Result<Unit>
|
suspend fun redactEvent(eventId: EventId, reason: String? = null): Result<Unit>
|
||||||
|
|
||||||
suspend fun sendImage(file: File, thumbnailFile: File, imageInfo: ImageInfo, progressCallback: ProgressCallback?): Result<MediaUploadHandler>
|
suspend fun sendImage(file: File, thumbnailFile: File?, imageInfo: ImageInfo, progressCallback: ProgressCallback?): Result<MediaUploadHandler>
|
||||||
|
|
||||||
suspend fun sendVideo(file: File, thumbnailFile: File, videoInfo: VideoInfo, progressCallback: ProgressCallback?): Result<MediaUploadHandler>
|
suspend fun sendVideo(file: File, thumbnailFile: File?, videoInfo: VideoInfo, progressCallback: ProgressCallback?): Result<MediaUploadHandler>
|
||||||
|
|
||||||
suspend fun sendAudio(file: File, audioInfo: AudioInfo, progressCallback: ProgressCallback?): Result<MediaUploadHandler>
|
suspend fun sendAudio(file: File, audioInfo: AudioInfo, progressCallback: ProgressCallback?): Result<MediaUploadHandler>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -360,15 +360,25 @@ class RustMatrixRoom(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun sendImage(file: File, thumbnailFile: File, imageInfo: ImageInfo, progressCallback: ProgressCallback?): Result<MediaUploadHandler> {
|
override suspend fun sendImage(
|
||||||
return sendAttachment(listOf(file, thumbnailFile)) {
|
file: File,
|
||||||
innerTimeline.sendImage(file.path, thumbnailFile.path, imageInfo.map(), progressCallback?.toProgressWatcher())
|
thumbnailFile: File?,
|
||||||
|
imageInfo: ImageInfo,
|
||||||
|
progressCallback: ProgressCallback?,
|
||||||
|
): Result<MediaUploadHandler> {
|
||||||
|
return sendAttachment(listOfNotNull(file, thumbnailFile)) {
|
||||||
|
innerTimeline.sendImage(file.path, thumbnailFile?.path, imageInfo.map(), progressCallback?.toProgressWatcher())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun sendVideo(file: File, thumbnailFile: File, videoInfo: VideoInfo, progressCallback: ProgressCallback?): Result<MediaUploadHandler> {
|
override suspend fun sendVideo(
|
||||||
return sendAttachment(listOf(file, thumbnailFile)) {
|
file: File,
|
||||||
innerTimeline.sendVideo(file.path, thumbnailFile.path, videoInfo.map(), progressCallback?.toProgressWatcher())
|
thumbnailFile: File?,
|
||||||
|
videoInfo: VideoInfo,
|
||||||
|
progressCallback: ProgressCallback?,
|
||||||
|
): Result<MediaUploadHandler> {
|
||||||
|
return sendAttachment(listOfNotNull(file, thumbnailFile)) {
|
||||||
|
innerTimeline.sendVideo(file.path, thumbnailFile?.path, videoInfo.map(), progressCallback?.toProgressWatcher())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -299,14 +299,14 @@ class FakeMatrixRoom(
|
||||||
|
|
||||||
override suspend fun sendImage(
|
override suspend fun sendImage(
|
||||||
file: File,
|
file: File,
|
||||||
thumbnailFile: File,
|
thumbnailFile: File?,
|
||||||
imageInfo: ImageInfo,
|
imageInfo: ImageInfo,
|
||||||
progressCallback: ProgressCallback?
|
progressCallback: ProgressCallback?
|
||||||
): Result<MediaUploadHandler> = fakeSendMedia(progressCallback)
|
): Result<MediaUploadHandler> = fakeSendMedia(progressCallback)
|
||||||
|
|
||||||
override suspend fun sendVideo(
|
override suspend fun sendVideo(
|
||||||
file: File,
|
file: File,
|
||||||
thumbnailFile: File,
|
thumbnailFile: File?,
|
||||||
videoInfo: VideoInfo,
|
videoInfo: VideoInfo,
|
||||||
progressCallback: ProgressCallback?
|
progressCallback: ProgressCallback?
|
||||||
): Result<MediaUploadHandler> = fakeSendMedia(
|
): Result<MediaUploadHandler> = fakeSendMedia(
|
||||||
|
|
|
||||||
|
|
@ -101,7 +101,6 @@ class MediaSender @Inject constructor(
|
||||||
progressCallback = progressCallback
|
progressCallback = progressCallback
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
is MediaUploadInfo.Video -> {
|
is MediaUploadInfo.Video -> {
|
||||||
sendVideo(
|
sendVideo(
|
||||||
file = uploadInfo.file,
|
file = uploadInfo.file,
|
||||||
|
|
|
||||||
|
|
@ -26,8 +26,8 @@ sealed interface MediaUploadInfo {
|
||||||
|
|
||||||
val file: File
|
val file: File
|
||||||
|
|
||||||
data class Image(override val file: File, val imageInfo: ImageInfo, val thumbnailFile: File) : MediaUploadInfo
|
data class Image(override val file: File, val imageInfo: ImageInfo, val thumbnailFile: File?) : MediaUploadInfo
|
||||||
data class Video(override val file: File, val videoInfo: VideoInfo, val thumbnailFile: File) : MediaUploadInfo
|
data class Video(override val file: File, val videoInfo: VideoInfo, val thumbnailFile: File?) : MediaUploadInfo
|
||||||
data class Audio(override val file: File, val audioInfo: AudioInfo) : MediaUploadInfo
|
data class Audio(override val file: File, val audioInfo: AudioInfo) : MediaUploadInfo
|
||||||
data class VoiceMessage(override val file: File, val audioInfo: AudioInfo, val waveform: List<Float>) : MediaUploadInfo
|
data class VoiceMessage(override val file: File, val audioInfo: AudioInfo, val waveform: List<Float>) : MediaUploadInfo
|
||||||
data class AnyFile(override val file: File, val fileInfo: FileInfo) : MediaUploadInfo
|
data class AnyFile(override val file: File, val fileInfo: FileInfo) : MediaUploadInfo
|
||||||
|
|
|
||||||
|
|
@ -137,7 +137,7 @@ class AndroidMediaPreProcessor @Inject constructor(
|
||||||
resizeMode = ResizeMode.Approximate(IMAGE_SCALE_REF_SIZE, IMAGE_SCALE_REF_SIZE),
|
resizeMode = ResizeMode.Approximate(IMAGE_SCALE_REF_SIZE, IMAGE_SCALE_REF_SIZE),
|
||||||
orientation = orientation,
|
orientation = orientation,
|
||||||
).getOrThrow()
|
).getOrThrow()
|
||||||
val thumbnailResult: ThumbnailResult = thumbnailFactory.createImageThumbnail(compressionResult.file)
|
val thumbnailResult = thumbnailFactory.createImageThumbnail(compressionResult.file)
|
||||||
val imageInfo = compressionResult.toImageInfo(
|
val imageInfo = compressionResult.toImageInfo(
|
||||||
mimeType = mimeType,
|
mimeType = mimeType,
|
||||||
thumbnailResult = thumbnailResult
|
thumbnailResult = thumbnailResult
|
||||||
|
|
@ -146,13 +146,13 @@ class AndroidMediaPreProcessor @Inject constructor(
|
||||||
return MediaUploadInfo.Image(
|
return MediaUploadInfo.Image(
|
||||||
file = compressionResult.file,
|
file = compressionResult.file,
|
||||||
imageInfo = imageInfo,
|
imageInfo = imageInfo,
|
||||||
thumbnailFile = thumbnailResult.file
|
thumbnailFile = thumbnailResult?.file
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun processImageWithoutCompression(): MediaUploadInfo {
|
suspend fun processImageWithoutCompression(): MediaUploadInfo {
|
||||||
val file = copyToTmpFile(uri)
|
val file = copyToTmpFile(uri)
|
||||||
val thumbnailResult: ThumbnailResult = thumbnailFactory.createImageThumbnail(file)
|
val thumbnailResult = thumbnailFactory.createImageThumbnail(file)
|
||||||
val imageInfo = contentResolver.openInputStream(uri).use { input ->
|
val imageInfo = contentResolver.openInputStream(uri).use { input ->
|
||||||
val bitmap = BitmapFactory.decodeStream(input, null, null)!!
|
val bitmap = BitmapFactory.decodeStream(input, null, null)!!
|
||||||
ImageInfo(
|
ImageInfo(
|
||||||
|
|
@ -160,16 +160,16 @@ class AndroidMediaPreProcessor @Inject constructor(
|
||||||
height = bitmap.height.toLong(),
|
height = bitmap.height.toLong(),
|
||||||
mimetype = mimeType,
|
mimetype = mimeType,
|
||||||
size = file.length(),
|
size = file.length(),
|
||||||
thumbnailInfo = thumbnailResult.info,
|
thumbnailInfo = thumbnailResult?.info,
|
||||||
thumbnailSource = null,
|
thumbnailSource = null,
|
||||||
blurhash = thumbnailResult.blurhash,
|
blurhash = thumbnailResult?.blurhash,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
removeSensitiveImageMetadata(file)
|
removeSensitiveImageMetadata(file)
|
||||||
return MediaUploadInfo.Image(
|
return MediaUploadInfo.Image(
|
||||||
file = file,
|
file = file,
|
||||||
imageInfo = imageInfo,
|
imageInfo = imageInfo,
|
||||||
thumbnailFile = thumbnailResult.file
|
thumbnailFile = thumbnailResult?.file
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -197,7 +197,7 @@ class AndroidMediaPreProcessor @Inject constructor(
|
||||||
return MediaUploadInfo.Video(
|
return MediaUploadInfo.Video(
|
||||||
file = resultFile,
|
file = resultFile,
|
||||||
videoInfo = videoInfo,
|
videoInfo = videoInfo,
|
||||||
thumbnailFile = thumbnailInfo.file
|
thumbnailFile = thumbnailInfo?.file
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -235,7 +235,7 @@ class AndroidMediaPreProcessor @Inject constructor(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun extractVideoMetadata(file: File, mimeType: String?, thumbnailResult: ThumbnailResult): VideoInfo =
|
private fun extractVideoMetadata(file: File, mimeType: String?, thumbnailResult: ThumbnailResult?): VideoInfo =
|
||||||
MediaMetadataRetriever().runAndRelease {
|
MediaMetadataRetriever().runAndRelease {
|
||||||
setDataSource(context, Uri.fromFile(file))
|
setDataSource(context, Uri.fromFile(file))
|
||||||
VideoInfo(
|
VideoInfo(
|
||||||
|
|
@ -244,10 +244,10 @@ class AndroidMediaPreProcessor @Inject constructor(
|
||||||
height = extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT)?.toLong() ?: 0L,
|
height = extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT)?.toLong() ?: 0L,
|
||||||
mimetype = mimeType,
|
mimetype = mimeType,
|
||||||
size = file.length(),
|
size = file.length(),
|
||||||
thumbnailInfo = thumbnailResult.info,
|
thumbnailInfo = thumbnailResult?.info,
|
||||||
// Will be computed by the rust sdk
|
// Will be computed by the rust sdk
|
||||||
thumbnailSource = null,
|
thumbnailSource = null,
|
||||||
blurhash = thumbnailResult.blurhash,
|
blurhash = thumbnailResult?.blurhash,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -257,15 +257,15 @@ class AndroidMediaPreProcessor @Inject constructor(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun ImageCompressionResult.toImageInfo(mimeType: String, thumbnailResult: ThumbnailResult) = ImageInfo(
|
private fun ImageCompressionResult.toImageInfo(mimeType: String, thumbnailResult: ThumbnailResult?) = ImageInfo(
|
||||||
width = width.toLong(),
|
width = width.toLong(),
|
||||||
height = height.toLong(),
|
height = height.toLong(),
|
||||||
mimetype = mimeType,
|
mimetype = mimeType,
|
||||||
size = size,
|
size = size,
|
||||||
thumbnailInfo = thumbnailResult.info,
|
thumbnailInfo = thumbnailResult?.info,
|
||||||
// Will be computed by the rust sdk
|
// Will be computed by the rust sdk
|
||||||
thumbnailSource = null,
|
thumbnailSource = null,
|
||||||
blurhash = thumbnailResult.blurhash,
|
blurhash = thumbnailResult?.blurhash,
|
||||||
)
|
)
|
||||||
|
|
||||||
private fun MediaMetadataRetriever.extractDuration(): Duration {
|
private fun MediaMetadataRetriever.extractDuration(): Duration {
|
||||||
|
|
|
||||||
|
|
@ -61,7 +61,7 @@ class ThumbnailFactory @Inject constructor(
|
||||||
) {
|
) {
|
||||||
|
|
||||||
@SuppressLint("NewApi")
|
@SuppressLint("NewApi")
|
||||||
suspend fun createImageThumbnail(file: File): ThumbnailResult {
|
suspend fun createImageThumbnail(file: File): ThumbnailResult? {
|
||||||
return createThumbnail { cancellationSignal ->
|
return createThumbnail { cancellationSignal ->
|
||||||
// This API works correctly with GIF
|
// This API works correctly with GIF
|
||||||
if (sdkIntProvider.isAtLeast(Build.VERSION_CODES.Q)) {
|
if (sdkIntProvider.isAtLeast(Build.VERSION_CODES.Q)) {
|
||||||
|
|
@ -80,7 +80,7 @@ class ThumbnailFactory @Inject constructor(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun createVideoThumbnail(file: File): ThumbnailResult {
|
suspend fun createVideoThumbnail(file: File): ThumbnailResult? {
|
||||||
return createThumbnail {
|
return createThumbnail {
|
||||||
MediaMetadataRetriever().runAndRelease {
|
MediaMetadataRetriever().runAndRelease {
|
||||||
setDataSource(context, file.toUri())
|
setDataSource(context, file.toUri())
|
||||||
|
|
@ -89,32 +89,33 @@ class ThumbnailFactory @Inject constructor(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun createThumbnail(bitmapFactory: (CancellationSignal) -> Bitmap?): ThumbnailResult = suspendCancellableCoroutine { continuation ->
|
private suspend fun createThumbnail(bitmapFactory: (CancellationSignal) -> Bitmap?): ThumbnailResult? = suspendCancellableCoroutine { continuation ->
|
||||||
val cancellationSignal = CancellationSignal()
|
val cancellationSignal = CancellationSignal()
|
||||||
continuation.invokeOnCancellation {
|
continuation.invokeOnCancellation {
|
||||||
cancellationSignal.cancel()
|
cancellationSignal.cancel()
|
||||||
}
|
}
|
||||||
val bitmapThumbnail: Bitmap? = bitmapFactory(cancellationSignal)
|
val bitmapThumbnail: Bitmap? = bitmapFactory(cancellationSignal)
|
||||||
|
if (bitmapThumbnail == null) {
|
||||||
|
continuation.resume(null)
|
||||||
|
return@suspendCancellableCoroutine
|
||||||
|
}
|
||||||
val thumbnailFile = context.createTmpFile(extension = "jpeg")
|
val thumbnailFile = context.createTmpFile(extension = "jpeg")
|
||||||
thumbnailFile.outputStream().use { outputStream ->
|
thumbnailFile.outputStream().use { outputStream ->
|
||||||
bitmapThumbnail?.compress(Bitmap.CompressFormat.JPEG, 80, outputStream)
|
bitmapThumbnail.compress(Bitmap.CompressFormat.JPEG, 80, outputStream)
|
||||||
}
|
|
||||||
val blurhash = bitmapThumbnail?.let {
|
|
||||||
BlurHash.encode(it, 3, 3)
|
|
||||||
}
|
}
|
||||||
|
val blurhash = BlurHash.encode(bitmapThumbnail, 3, 3)
|
||||||
val thumbnailResult = ThumbnailResult(
|
val thumbnailResult = ThumbnailResult(
|
||||||
file = thumbnailFile,
|
file = thumbnailFile,
|
||||||
info = ThumbnailInfo(
|
info = ThumbnailInfo(
|
||||||
height = bitmapThumbnail?.height?.toLong(),
|
height = bitmapThumbnail.height.toLong(),
|
||||||
width = bitmapThumbnail?.width?.toLong(),
|
width = bitmapThumbnail.width.toLong(),
|
||||||
mimetype = MimeTypes.Jpeg,
|
mimetype = MimeTypes.Jpeg,
|
||||||
size = thumbnailFile.length()
|
size = thumbnailFile.length()
|
||||||
),
|
),
|
||||||
blurhash = blurhash
|
blurhash = blurhash
|
||||||
)
|
)
|
||||||
bitmapThumbnail?.recycle()
|
bitmapThumbnail.recycle()
|
||||||
continuation.resume(thumbnailResult)
|
continuation.resume(thumbnailResult)
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue