Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure openInputStream cannot kill calling process #2340

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,19 @@
*/
package com.squareup.picasso3

import android.annotation.SuppressLint
import android.content.ContentResolver
import android.content.Context
import android.net.Uri
import android.os.DeadObjectException
import androidx.exifinterface.media.ExifInterface
import androidx.exifinterface.media.ExifInterface.ORIENTATION_NORMAL
import androidx.exifinterface.media.ExifInterface.TAG_ORIENTATION
import com.squareup.picasso3.Picasso.LoadedFrom.DISK
import okio.Source
import okio.source
import java.io.FileNotFoundException
import java.io.IOException

internal open class ContentStreamRequestHandler(val context: Context) : RequestHandler() {
override fun canHandleRequest(data: Request): Boolean =
Expand All @@ -51,11 +54,21 @@ internal open class ContentStreamRequestHandler(val context: Context) : RequestH
}
}

@SuppressLint("Recycle")
fun getSource(uri: Uri): Source {
val contentResolver = context.contentResolver
val inputStream = contentResolver.openInputStream(uri)
?: throw FileNotFoundException("can't open input stream, uri: $uri")
return inputStream.source()
return if (ContentResolver.SCHEME_CONTENT == uri.scheme) {
context.contentResolver.acquireContentProviderClient(uri)?.use {
try {
it.openTypedAssetFileDescriptor(uri, "r", null)?.createInputStream()
} catch (e: IOException) {
null
} catch (e: DeadObjectException) {
null
}
}
} else {
context.contentResolver.openInputStream(uri)
}?.source() ?: throw FileNotFoundException("can't open input stream, uri: $uri")
}

protected open fun getExifOrientation(uri: Uri): Int {
Expand Down