X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Ftext%2FSoneTextParser.kt;h=6af0fded74d25da45c2c9e5f52c8f24a48083017;hb=feb4aeda86e2c3d7deff163eda53191d6baa5650;hp=b711ca8fdbd760782d702c57640dbbf8ad683647;hpb=eb0c51b3cf0b1aeb3a2fe747c57cbd5f624f5744;p=Sone.git diff --git a/src/main/kotlin/net/pterodactylus/sone/text/SoneTextParser.kt b/src/main/kotlin/net/pterodactylus/sone/text/SoneTextParser.kt index b711ca8..6af0fde 100644 --- a/src/main/kotlin/net/pterodactylus/sone/text/SoneTextParser.kt +++ b/src/main/kotlin/net/pterodactylus/sone/text/SoneTextParser.kt @@ -1,7 +1,6 @@ package net.pterodactylus.sone.text import freenet.keys.* -import freenet.support.* import net.pterodactylus.sone.data.* import net.pterodactylus.sone.data.impl.* import net.pterodactylus.sone.database.* @@ -33,7 +32,7 @@ class SoneTextParser @Inject constructor(private val soneProvider: SoneProvider? else LinkType.values() .mapNotNull { it.findNext(remainder.second) } - .minBy { it.position } + .minByOrNull { it.position } .let { when { it == null -> PlainTextPart(remainder.second) to "" @@ -67,13 +66,15 @@ class SoneTextParser @Inject constructor(private val soneProvider: SoneProvider? } SSK, USK -> try { - FreenetURI(linkWithoutBacklink).let { uri -> - uri.allMetaStrings - ?.takeIf { (it.size > 1) || ((it.size == 1) && (it.single() != "")) } - ?.lastOrNull() - ?: uri.docName - ?: "${uri.keyType}@${uri.routingKey.asFreenetBase64}" - }.let { FreenetLinkPart(linkWithoutBacklink.removeSuffix("/"), it, trusted = context?.routingKey?.contentEquals(FreenetURI(linkWithoutBacklink).routingKey) == true) } + FreenetURI(linkWithoutBacklink) + .workaroundForFaultyConstructorInFred1485AndBelow() + .let { uri -> + uri.allMetaStrings + ?.takeIf { (it.size > 1) || ((it.size == 1) && (it.single() != "")) } + ?.lastOrNull() + ?: uri.docName + ?: "${uri.keyType}@${uri.routingKey.asFreenetBase64}" + }.let { FreenetLinkPart(linkWithoutBacklink.removeSuffix("/"), it, trusted = context?.routingKey?.contentEquals(FreenetURI(linkWithoutBacklink).routingKey) == true) } } catch (e: MalformedURLException) { PlainTextPart(linkWithoutBacklink) } @@ -94,6 +95,9 @@ class SoneTextParser @Inject constructor(private val soneProvider: SoneProvider? } +private fun FreenetURI.workaroundForFaultyConstructorInFred1485AndBelow() = + also { if (it.routingKey == null) throw MalformedURLException("SSK/USK without routing key") } + private fun List.mergeMultipleEmptyLines() = fold(emptyList()) { previous, current -> if (previous.isEmpty()) { previous + current