Improve handling of shared media (#2388)

* support file:// uris

* support subjects on image shares

* improve code

* improve code

* improve code
This commit is contained in:
Konrad Pozniak 2022-03-28 18:39:05 +02:00 committed by GitHub
parent 106585f4fe
commit c47804997c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 74 additions and 38 deletions

View File

@ -236,7 +236,7 @@ class ComposeActivity :
} }
} }
} }
} else if (type == "text/plain" && intent.action == Intent.ACTION_SEND) { }
val subject = intent.getStringExtra(Intent.EXTRA_SUBJECT) val subject = intent.getStringExtra(Intent.EXTRA_SUBJECT)
val text = intent.getStringExtra(Intent.EXTRA_TEXT).orEmpty() val text = intent.getStringExtra(Intent.EXTRA_TEXT).orEmpty()
@ -259,7 +259,6 @@ class ComposeActivity :
} }
} }
} }
}
private fun setupReplyViews(replyingStatusAuthor: String?, replyingStatusContent: String?) { private fun setupReplyViews(replyingStatusAuthor: String?, replyingStatusContent: String?) {
if (replyingStatusAuthor != null) { if (replyingStatusAuthor != null) {

View File

@ -15,6 +15,7 @@
package com.keylesspalace.tusky.components.compose package com.keylesspalace.tusky.components.compose
import android.content.ContentResolver
import android.content.Context import android.content.Context
import android.net.Uri import android.net.Uri
import android.os.Environment import android.os.Environment
@ -37,6 +38,7 @@ import io.reactivex.rxjava3.schedulers.Schedulers
import okhttp3.MediaType.Companion.toMediaTypeOrNull import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody import okhttp3.MultipartBody
import java.io.File import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream import java.io.FileOutputStream
import java.io.IOException import java.io.IOException
import java.util.Date import java.util.Date
@ -83,13 +85,18 @@ class MediaUploader @Inject constructor(
fun prepareMedia(inUri: Uri): Single<PreparedMedia> { fun prepareMedia(inUri: Uri): Single<PreparedMedia> {
return Single.fromCallable { return Single.fromCallable {
var mediaSize = getMediaSize(contentResolver, inUri) var mediaSize = MEDIA_SIZE_UNKNOWN
var uri = inUri var uri = inUri
val mimeType = contentResolver.getType(uri) var mimeType: String? = null
try {
when (inUri.scheme) {
ContentResolver.SCHEME_CONTENT -> {
mimeType = contentResolver.getType(uri)
val suffix = "." + MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType ?: "tmp") val suffix = "." + MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType ?: "tmp")
try {
contentResolver.openInputStream(inUri).use { input -> contentResolver.openInputStream(inUri).use { input ->
if (input == null) { if (input == null) {
Log.w(TAG, "Media input is null") Log.w(TAG, "Media input is null")
@ -107,12 +114,41 @@ class MediaUploader @Inject constructor(
mediaSize = getMediaSize(contentResolver, uri) mediaSize = getMediaSize(contentResolver, uri)
} }
} }
}
ContentResolver.SCHEME_FILE -> {
val path = uri.path
if (path == null) {
Log.w(TAG, "empty uri path $uri")
throw CouldNotOpenFileException()
}
val inputFile = File(path)
val suffix = inputFile.name.substringAfterLast('.', "tmp")
mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(suffix)
val file = File.createTempFile("randomTemp1", ".$suffix", context.cacheDir)
val input = FileInputStream(inputFile)
FileOutputStream(file.absoluteFile).use { out ->
input.copyTo(out)
uri = FileProvider.getUriForFile(
context,
BuildConfig.APPLICATION_ID + ".fileprovider",
file
)
mediaSize = getMediaSize(contentResolver, uri)
}
}
else -> {
Log.w(TAG, "Unknown uri scheme $uri")
throw CouldNotOpenFileException()
}
}
} catch (e: IOException) { } catch (e: IOException) {
Log.w(TAG, e) Log.w(TAG, e)
uri = inUri throw CouldNotOpenFileException()
} }
if (mediaSize == MEDIA_SIZE_UNKNOWN) { if (mediaSize == MEDIA_SIZE_UNKNOWN) {
throw CouldNotOpenFileException() Log.w(TAG, "Could not determine file size of upload")
throw MediaTypeException()
} }
if (mimeType != null) { if (mimeType != null) {
@ -138,6 +174,7 @@ class MediaUploader @Inject constructor(
} }
} }
} else { } else {
Log.w(TAG, "Could not determine mime type of upload")
throw MediaTypeException() throw MediaTypeException()
} }
} }