Skip to content
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions app/src/main/java/to/bitkit/data/PubkyImageFetcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/to/bitkit/repositories/PubkyRepo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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"
Expand Down
12 changes: 12 additions & 0 deletions app/src/test/java/to/bitkit/data/PubkyImageFetcherTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions changelog.d/next/2252.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed Pubky profile pictures over 1 MB not loading.
4 changes: 2 additions & 2 deletions docs/pubky.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
Loading