Skip to content

Commit

Permalink
Merge pull request #583 from WideChat/pjl_imageupload
Browse files Browse the repository at this point in the history
fixing problems with uploading images and files
  • Loading branch information
ear-dev authored Aug 7, 2019
2 parents 395c267 + 0e70c0a commit 704fff0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.threeten.bp.Instant
import timber.log.Timber
import java.io.InvalidObjectException
import java.util.*
import javax.inject.Inject

Expand Down Expand Up @@ -475,7 +476,7 @@ class ChatRoomPresenter @Inject constructor(
view.showInvalidFileMessage()
} else {
val byteArray =
bitmap.getByteArray(mimeType, 100, settings.uploadMaxFileSize())
bitmap.getByteArray(mimeType, 60, settings.uploadMaxFileSize())
retryIO("uploadFile($roomId, $fileName, $mimeType") {
client.uploadFile(
roomId,
Expand All @@ -495,6 +496,7 @@ class ChatRoomPresenter @Inject constructor(
Timber.d(ex, "Error uploading image")
when (ex) {
is RocketChatException -> view.showMessage(ex)
is InvalidObjectException -> view.showInvalidFileSize(bitmap.getByteCount(), settings.uploadMaxFileSize())
else -> view.showGenericErrorMessage()
}
} finally {
Expand Down
27 changes: 20 additions & 7 deletions util/src/main/java/chat/rocket/android/util/extension/Image.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.InputStream
import java.io.IOException
import java.io.File
import java.io.*
import java.text.SimpleDateFormat
import java.util.*

Expand All @@ -37,13 +33,14 @@ suspend fun Bitmap.compressImageAndGetInputStream(mimeType: String): InputStream
return inputStream
}


/**
* Returns a [ByteArray] of a [Bitmap].
*
* @param mimeType The MIME type of the [Bitmap].
* @param quality The quality of the [Bitmap] for the resulting [ByteArray].
* @param maxFileSizeAllowed The max file size allowed by the server. Note: The [quality] will be
* decreased minus 10 until the [ByteArray] size fits the [maxFileSizeAllowed] value.
* decreased minus 20 until the [ByteArray] size fits the [maxFileSizeAllowed] value.
* @return A [ByteArray] of a [Bitmap]
*/
suspend fun Bitmap.getByteArray(
Expand All @@ -55,7 +52,11 @@ suspend fun Bitmap.getByteArray(

compressImageAndGetByteArray(mimeType, quality)?.let {
if (it.size > maxFileSizeAllowed && maxFileSizeAllowed !in -1..0) {
getByteArray(mimeType, quality - 10, maxFileSizeAllowed)
if (quality == 0 || !mimeType.lossyCompressible()) {
throw InvalidObjectException ("File size too big.")
}
// call this method recursively with lower quality
byteArray = getByteArray(mimeType, quality - 20, maxFileSizeAllowed)
} else {
byteArray = it
}
Expand Down Expand Up @@ -96,6 +97,18 @@ fun String.getCompressFormat(): Bitmap.CompressFormat {
}
}

/**
* Returns true if lossy compressible format based on mimeType
*/
fun String.lossyCompressible(): Boolean {
return when {
this.contains("jpeg") -> true
this.contains("png") -> true
this.contains("webp") -> true
else -> false
}
}

fun Fragment.dispatchImageSelection(requestCode: Int) {
val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.type = "image/*"
Expand Down

0 comments on commit 704fff0

Please sign in to comment.