diff --git a/app/src/main/java/to/bitkit/data/PubkyImageFetcher.kt b/app/src/main/java/to/bitkit/data/PubkyImageFetcher.kt index 71f9315d4d..1a859286ae 100644 --- a/app/src/main/java/to/bitkit/data/PubkyImageFetcher.kt +++ b/app/src/main/java/to/bitkit/data/PubkyImageFetcher.kt @@ -26,8 +26,8 @@ import kotlin.time.Duration.Companion.seconds private const val TAG = "PubkyImageFetcher" private const val PUBKY_SCHEME = "pubky://" -/** Maximum successful response body accepted for a displayed Pubky image. */ -internal const val PUBKY_IMAGE_MAX_BYTES = 1_048_576uL +/** Maximum successful response body accepted for a Pubky image: Pubky App's 5 MiB image upload limit. */ +internal const val PUBKY_IMAGE_MAX_BYTES = 5_242_880uL /** How long a Pubky image whose fetch failed for a reason that can pass, such as a network error, is not fetched. */ internal val PUBKY_IMAGE_TRANSIENT_FAILURE_TTL = 60.seconds diff --git a/app/src/main/java/to/bitkit/repositories/PubkyRepo.kt b/app/src/main/java/to/bitkit/repositories/PubkyRepo.kt index eb30e3fb24..2afb33859b 100644 --- a/app/src/main/java/to/bitkit/repositories/PubkyRepo.kt +++ b/app/src/main/java/to/bitkit/repositories/PubkyRepo.kt @@ -46,6 +46,7 @@ import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.withLock import kotlinx.coroutines.withContext import to.bitkit.async.appScope +import to.bitkit.data.PUBKY_IMAGE_MAX_BYTES import to.bitkit.data.PubkyCachedProfile import to.bitkit.data.PubkyImageCacheEpoch import to.bitkit.data.PubkyStore @@ -215,7 +216,6 @@ class PubkyRepo @Inject constructor( private const val PUBKY_APP_NAME_MAX_LENGTH = 50 private const val PUBKY_APP_BIO_MAX_LENGTH = 160 private const val PUBKY_APP_LINKS_MAX_COUNT = 5 - private const val PUBKY_APP_AVATAR_MAX_BYTES = 5_242_880uL } private val scope = appScope(ioDispatcher, TAG) @@ -1551,7 +1551,7 @@ class PubkyRepo @Inject constructor( snapshot: PubkyProfileSnapshot, generation: Long, ): PubkyProfileSnapshot? = runSuspendCatching { - val bytes = pubkyService.fetchFile(uri, PUBKY_APP_AVATAR_MAX_BYTES) + val bytes = pubkyService.fetchFile(uri, PUBKY_IMAGE_MAX_BYTES) val contentType = when (uri.substringAfterLast('.').lowercase()) { "png" -> "image/png" "webp" -> "image/webp" diff --git a/app/src/test/java/to/bitkit/data/PubkyImageFetcherTest.kt b/app/src/test/java/to/bitkit/data/PubkyImageFetcherTest.kt index 8efdce0f9b..5f600532bd 100644 --- a/app/src/test/java/to/bitkit/data/PubkyImageFetcherTest.kt +++ b/app/src/test/java/to/bitkit/data/PubkyImageFetcherTest.kt @@ -119,6 +119,18 @@ class PubkyImageFetcherTest : BaseUnitTest() { verify(pubkyService).fetchFile(BLOB_URI, PUBKY_IMAGE_MAX_BYTES) } + @Test + fun `fetch should allow images up to the pubky app upload limit`() = test { + val pubkyAppUploadLimit = 5_242_880uL + whenever(pubkyService.fetchFile(IMAGE_URI, pubkyAppUploadLimit)).thenReturn(DESCRIPTOR_BYTES) + whenever(pubkyService.fetchFile(BLOB_URI, pubkyAppUploadLimit)).thenReturn(BLOB_BYTES) + val fetcher = createFetcher() + + val result = fetcher.fetch() + + assertContentEquals(BLOB_BYTES, result.bytes()) + } + @Test fun `fetch should preserve cancellation when following file descriptor`() = test { whenever(pubkyService.fetchFile(IMAGE_URI, PUBKY_IMAGE_MAX_BYTES)).thenReturn(DESCRIPTOR_BYTES) diff --git a/changelog.d/next/2252.fixed.md b/changelog.d/next/2252.fixed.md new file mode 100644 index 0000000000..7f887068b6 --- /dev/null +++ b/changelog.d/next/2252.fixed.md @@ -0,0 +1 @@ +Fixed Pubky profile pictures over 1 MB not loading. diff --git a/docs/pubky.md b/docs/pubky.md index 6da7c24284..4f2ddb454a 100644 --- a/docs/pubky.md +++ b/docs/pubky.md @@ -147,7 +147,7 @@ Disk cache rules: Failed fetches are remembered by URI in `PubkyImageFailureCache`, owned by the fetcher factory, so an avatar that cannot load is not fetched again each time a row recomposes or reappears: -- A missing file or blob (`PubkyFileNotFoundError`, `PaykitException.NotFound`) and an invalid response, such as a file over the 1 MiB limit (`PaykitException.Protocol`), are remembered until the image cache is next cleared +- A missing file or blob (`PubkyFileNotFoundError`, `PaykitException.NotFound`) and an invalid response, such as a file over the 5 MiB limit (`PaykitException.Protocol`), are remembered until the image cache is next cleared - Any other failure, such as a transport error, is remembered for 60 seconds (`PUBKY_IMAGE_TRANSIENT_FAILURE_TTL`). The SDK reports a key it cannot resolve the same way as a network failure, so such a key is retried every minute - A descriptor whose blob fetch failed counts as a failure of the descriptor URI, with the blob's failure deciding how long it is kept - While a failure is remembered, the fetcher fails at once with `PubkyImageRecentlyFailedError` and the image shows its error state. A disk cache hit still wins @@ -158,7 +158,7 @@ Failed fetches are remembered by URI in `PubkyImageFailureCache`, owned by the f 1. Coil checks memory cache → return if hit 2. `PubkyImageFetcher.fetch()` checks the disk cache → return if hit, without waiting for Paykit setup or using the network 3. A URI whose fetch failed recently fails at once, without using the network -4. Otherwise the fetcher limits the successful response body to 1 MiB +4. Otherwise the fetcher limits the successful response body to 5 MiB, Pubky App's image upload limit 5. If the response is a JSON file descriptor with a Pubky `src`, follow the indirection with the same limit 6. The fetcher writes a fully successful result to the disk cache unless the directory was cleared during the fetch, then Coil decodes it and caches it in memory