Skip to content

Commit

Permalink
When converting image uri to upload to stream for response body manag…
Browse files Browse the repository at this point in the history
…e exceptions fix #1

instead of crashing, and throw exception later when uploading images to be managed with other type of exception
And do less uri to string to uri conversion
  • Loading branch information
e-marchand committed Jan 29, 2024
1 parent ca0afae commit 09a8e25
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ class ActionRepository(private val apiService: ApiService) {
* Performs $upload request
*/
fun uploadImage(
imagesToUpload: Map<String, RequestBody?>,
imagesToUpload: Map<String, Result<RequestBody>>,
onImageUploaded:
(isSuccess: Boolean, parameterName: String, response: Response<ResponseBody>?, error: Any?) -> Unit,
(isSuccess: Boolean, parameterName: String, response: Response<ResponseBody>?, error: Throwable?) -> Unit,
onAllUploadFinished: () -> Unit
) {
disposable.add(
Expand All @@ -53,12 +53,12 @@ class ActionRepository(private val apiService: ApiService) {
}
.concatMapSingle {
val parameterName = it.key
it.value?.let { requestBody ->
apiService.uploadImage(body = requestBody)
.map { response ->
parameterName to response
}
}
val result = it.value
val requestBody = result.getOrThrow() // we have postponed error here to be managed by listeners
apiService.uploadImage(body = requestBody)
.map { response ->
parameterName to response
}
}
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,15 @@ object FileHelper {
return result
}

fun repairUri(uriString: String): Uri {
fun repairUri(uriData: Any): Uri {
return when (uriData) {
is Uri -> uriData as Uri // do not transform uri

Check warning on line 43 in qmobileapi/src/main/java/com/qmobile/qmobileapi/utils/FileHelper.kt

View workflow job for this annotation

GitHub Actions / build

No cast needed�[0K

Check warning on line 43 in qmobileapi/src/main/java/com/qmobile/qmobileapi/utils/FileHelper.kt

View workflow job for this annotation

GitHub Actions / build

No cast needed
is String -> repairUriString(uriData as String)

Check warning on line 44 in qmobileapi/src/main/java/com/qmobile/qmobileapi/utils/FileHelper.kt

View workflow job for this annotation

GitHub Actions / build

No cast needed�[0K

Check warning on line 44 in qmobileapi/src/main/java/com/qmobile/qmobileapi/utils/FileHelper.kt

View workflow job for this annotation

GitHub Actions / build

No cast needed
else -> repairUriString(uriData.toString())
}
}

private fun repairUriString(uriString: String): Uri {
val uriBuilder: String = when {
uriString.startsWith("/") -> "file://$uriString"
uriString.startsWith("content://") -> uriString
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,42 @@ import androidx.fragment.app.FragmentActivity
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import timber.log.Timber
import java.io.FileNotFoundException
import java.io.IOException
import java.io.InputStream

object UploadHelper {

const val UPLOADED_METADATA_STRING = "uploaded"

fun HashMap<String, *>.getBodies(activity: FragmentActivity?): Map<String, RequestBody?> = this.mapValues {
val uri = FileHelper.repairUri(it.value.toString())
val stream = activity?.contentResolver?.openInputStream(uri)
stream?.readBytes()?.toRequestBody(APP_OCTET.toMediaTypeOrNull()).also { stream?.close() }
fun HashMap<String, *>.getBodies(activity: FragmentActivity): Map<String, Result<RequestBody>> {
val contentResolver = activity.contentResolver
return this.mapValues {
val uri = FileHelper.repairUri(it.value)
try {
val stream = contentResolver.openInputStream(uri)
if (stream != null)
return@mapValues Result.success(stream.readBytes().toRequestBody(APP_OCTET.toMediaTypeOrNull()).also { stream.close() })
Timber.e("Failed to open stream on file $uri (null)")
return@mapValues Result.failure(IOException("Failed to open stream on file $uri (null)"))
}
catch (exception: FileNotFoundException) {
Timber.e("Failed to upload file due to file not found:" + exception.message)
return@mapValues Result.failure(exception)
}
catch(exception: IOException) {
Timber.e("Failed to upload file:" + exception.message)
return@mapValues Result.failure(exception)
}
catch (exception: SecurityException) {
Timber.e("Failed to upload file, no more access to it due to security issue:" + exception.message)
return@mapValues Result.failure(exception)
}
catch (throwable: Throwable) { // it catch maybe two much...
Timber.e("Failed to upload file, unknown error" + throwable.message)
return@mapValues Result.failure(throwable)
}
}
}
}

0 comments on commit 09a8e25

Please sign in to comment.