Skip to content

Images not cached when using custom Decoder #2539

@suhaibroomy

Description

@suhaibroomy

#2501 # Description

I am using a custom decoder for encrypted images. My Decoder decrypts the encrypted input stream and then decodes Images just like it is done in DefaultImageDecoder. I noticed that images fetched using this decoder are not saved in cache and is fetched from network every time. I would like to cache these images just like others

Reproduction

class MyImageDecoder(private val elementToDecrypt: ElementToDecrypt?) : ImageDecoder {

private val poolFactory: PoolFactory by inject(PoolFactory::class.java)

val defaultDecoder: DefaultImageDecoder by inject(DefaultImageDecoder::class.java)

override fun decode(
    encodedImage: EncodedImage,
    length: Int,
    qualityInfo: QualityInfo,
    options: ImageDecodeOptions
): CloseableImage {

    var decryptedInputStream: InputStream? = null
    var clearEncodedImage: EncodedImage? = null

    try {
        val inputStream = encodedImage.inputStream

        decryptedInputStream = elementToDecrypt?.let {
            DecryptStore.decryptStream(inputStream, elementToDecrypt)
        } ?: inputStream

        val bytes = poolFactory.pooledByteBufferFactory.newByteBuffer(decryptedInputStream)
        val bytesClosable = CloseableReference.of(bytes)
        clearEncodedImage = EncodedImage(bytesClosable)

        var imageFormat = encodedImage.imageFormat
        if (imageFormat == null || imageFormat === ImageFormat.UNKNOWN) {
            imageFormat =
                ImageFormatChecker.getImageFormat_WrapIOException(clearEncodedImage.inputStream)
            encodedImage.imageFormat = imageFormat
        }

        when {
            imageFormat === DefaultImageFormats.JPEG -> {
                return defaultDecoder.decodeJpeg(clearEncodedImage, length, qualityInfo, options)
            }
            imageFormat === DefaultImageFormats.GIF -> {
                return defaultDecoder.decodeGif(clearEncodedImage, length, qualityInfo, options)
            }
            imageFormat === DefaultImageFormats.WEBP_ANIMATED -> {
                return defaultDecoder.decodeAnimatedWebp(clearEncodedImage, length, qualityInfo, options)
            }
            imageFormat === ImageFormat.UNKNOWN -> {
                throw DecodeException("unknown image format", clearEncodedImage)
            }
            else -> {
                throw DecodeException("unknown image format", clearEncodedImage)
            }
        }
    } catch (ex: Exception) {
        Timber.e("Something went wrong decoding the image")
        throw ex
    } finally {
        decryptedInputStream?.close()
        clearEncodedImage?.close()
    }
}
}

Additional Information

  • Fresco version: 2.3.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions