Skip to content

Commit e85f892

Browse files
authored
Merge pull request #2250 from DimensionDev/bugfix/pixiv-original-image-url
Fix Pixiv original image quality
2 parents 087fd8d + 513b512 commit e85f892

2 files changed

Lines changed: 92 additions & 2 deletions

File tree

  • social/pixiv/src

social/pixiv/src/commonMain/kotlin/dev/dimension/flare/data/datasource/pixiv/PixivMapper.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ private fun PixivIllust.renderContent(): String =
258258
}
259259
}
260260

261-
private fun PixivIllust.toUiMedia(): List<UiMedia> {
261+
internal fun PixivIllust.toUiMedia(): List<UiMedia> {
262262
val headers = persistentMapOf("Referer" to PIXIV_IMAGE_REFERER)
263263
return if (metaPages.isNotEmpty()) {
264264
metaPages.mapNotNull { page ->
@@ -276,6 +276,7 @@ private fun PixivIllust.toUiMedia(): List<UiMedia> {
276276
height = height.toFloat(),
277277
sensitive = xRestrict > 0 || sanityLevel >= 6,
278278
headers = headers,
279+
preferredOriginalUrl = metaSinglePage?.originalImageUrl,
279280
)?.let(::listOf)
280281
.orEmpty()
281282
}
@@ -286,8 +287,9 @@ private fun dev.dimension.flare.data.network.pixiv.model.PixivImageUrls.toUiImag
286287
height: Float,
287288
sensitive: Boolean,
288289
headers: kotlinx.collections.immutable.ImmutableMap<String, String>,
290+
preferredOriginalUrl: String? = null,
289291
): UiMedia.Image? {
290-
val url = original ?: large ?: medium ?: squareMedium ?: return null
292+
val url = preferredOriginalUrl?.takeIf { it.isNotBlank() } ?: original ?: large ?: medium ?: squareMedium ?: return null
291293
return UiMedia.Image(
292294
url = url,
293295
previewUrl = medium ?: squareMedium ?: url,
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package dev.dimension.flare.data.datasource.pixiv
2+
3+
import dev.dimension.flare.data.network.pixiv.model.PixivIllust
4+
import dev.dimension.flare.data.network.pixiv.model.PixivImageUrls
5+
import dev.dimension.flare.data.network.pixiv.model.PixivMetaPage
6+
import dev.dimension.flare.data.network.pixiv.model.PixivMetaSinglePage
7+
import dev.dimension.flare.data.network.pixiv.model.PixivUser
8+
import dev.dimension.flare.ui.model.UiMedia
9+
import kotlin.test.Test
10+
import kotlin.test.assertEquals
11+
import kotlin.test.assertIs
12+
13+
class PixivMapperTest {
14+
@Test
15+
fun singlePageIllustUsesMetaSinglePageOriginalImageUrl() {
16+
val post =
17+
pixivIllust(
18+
imageUrls =
19+
PixivImageUrls(
20+
squareMedium = "https://example.com/square.jpg",
21+
medium = "https://example.com/medium.jpg",
22+
large = "https://example.com/large.jpg",
23+
),
24+
metaSinglePage =
25+
PixivMetaSinglePage(
26+
originalImageUrl = "https://example.com/original.png",
27+
),
28+
).toUiMedia()
29+
30+
val image = assertIs<UiMedia.Image>(post.single())
31+
assertEquals("https://example.com/original.png", image.url)
32+
assertEquals("https://example.com/medium.jpg", image.previewUrl)
33+
}
34+
35+
@Test
36+
fun multiPageIllustUsesPageImageUrls() {
37+
val post =
38+
pixivIllust(
39+
imageUrls =
40+
PixivImageUrls(
41+
medium = "https://example.com/cover-medium.jpg",
42+
large = "https://example.com/cover-large.jpg",
43+
),
44+
metaSinglePage =
45+
PixivMetaSinglePage(
46+
originalImageUrl = "https://example.com/cover-original.png",
47+
),
48+
metaPages =
49+
listOf(
50+
PixivMetaPage(
51+
imageUrls =
52+
PixivImageUrls(
53+
medium = "https://example.com/page-0-medium.jpg",
54+
large = "https://example.com/page-0-large.jpg",
55+
original = "https://example.com/page-0-original.png",
56+
),
57+
),
58+
),
59+
).toUiMedia()
60+
61+
val image = assertIs<UiMedia.Image>(post.single())
62+
assertEquals("https://example.com/page-0-original.png", image.url)
63+
assertEquals("https://example.com/page-0-medium.jpg", image.previewUrl)
64+
}
65+
66+
private fun pixivIllust(
67+
imageUrls: PixivImageUrls,
68+
metaSinglePage: PixivMetaSinglePage? = null,
69+
metaPages: List<PixivMetaPage> = emptyList(),
70+
): PixivIllust =
71+
PixivIllust(
72+
id = 146347478,
73+
title = "As if in a dream",
74+
type = "illust",
75+
imageUrls = imageUrls,
76+
user =
77+
PixivUser(
78+
id = 21714218,
79+
name = "Hyde",
80+
account = "hidetohyde",
81+
),
82+
createDate = "2026-06-22T22:09:00+00:00",
83+
width = 4000,
84+
height = 3000,
85+
metaSinglePage = metaSinglePage,
86+
metaPages = metaPages,
87+
)
88+
}

0 commit comments

Comments
 (0)