Yokan / rebuild
Course 1 8624561

SourceCast.kt

composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/services/extensions/SourceCast.kt

implementation51 lines
1package eu.friedcutlet.yokan.services.extensions23import eu.friedcutlet.yokan.domain.TitleCredit4import io.ktor.http.URLBuilder5import io.ktor.http.URLProtocol6import io.ktor.http.URLParserException7import io.ktor.http.takeFrom89/** Source portraits may be web resources; local-file access belongs to the local scanner. */10internal fun normalizeSourceCast(credits: List<TitleCredit>, baseUrl: String): List<TitleCredit> =11    credits.take(500).mapNotNull { credit ->12        val name = credit.name.trim().takeIf(String::isNotEmpty) ?: return@mapNotNull null13        credit.copy(14            name = name,15            role = credit.role?.trim()?.takeIf(String::isNotEmpty),16            character = credit.character?.trim()?.takeIf(String::isNotEmpty),17            imageUrl = sourceImageUrl(credit.imageUrl, baseUrl),18        )19    }.distinctBy { Triple(it.name, it.role, it.character) }2021internal fun sourceImageUrl(value: String?, baseUrl: String): String? {22    val reference = value?.trim()?.takeIf(String::isNotEmpty) ?: return null23    if (reference.any { it <= ' ' || it == '\\' || it == '\u007f' }) return null24    val scheme = SourcePortraitScheme.find(reference)?.value?.dropLast(1)?.lowercase()25    if (scheme != null && scheme != "http" && scheme != "https") return null26    if (scheme != null && !reference.startsWith("$scheme://", ignoreCase = true)) return null27    fun hasAuthority(url: String) = url.substringAfter("//").takeWhile { it != '/' && it != '?' && it != '#' }.isNotEmpty()28    if ((scheme != null || reference.startsWith("//")) && !hasAuthority(reference)) return null29    return try {30        val builder = if (scheme != null) URLBuilder() else {31            if (!baseUrl.startsWith("https://", ignoreCase = true) && !baseUrl.startsWith("http://", ignoreCase = true)) return null32            if (!hasAuthority(baseUrl) || baseUrl.any { it <= ' ' || it == '\\' || it == '\u007f' }) return null33            URLBuilder(baseUrl.substringBefore('#').substringBefore('?').trimEnd('/') + "/")34        }35        val url = builder.takeFrom(reference).build()36        // Ktor's parser accepts an unmatched IPv6 bracket; do not persist that address.37        val validHostBrackets = if (url.host.startsWith('[')) {38            url.host.endsWith(']') && ':' in url.host && url.host.count { it == '[' || it == ']' } == 239        } else '[' !in url.host && ']' !in url.host40        url.takeIf {41            (it.protocol == URLProtocol.HTTP || it.protocol == URLProtocol.HTTPS) &&42                it.host.isNotBlank() && validHostBrackets && it.user == null && it.password == null43        }?.toString()44    } catch (_: URLParserException) {45        null46    } catch (_: IllegalArgumentException) {47        null48    }49}5051private val SourcePortraitScheme = Regex("^[A-Za-z][A-Za-z0-9+.-]*:")52

Search chapters, reference and source filenames.