From: David ‘Bombe’ Roden Date: Sun, 20 Nov 2016 01:30:45 +0000 (+0100) Subject: Normalize URI in element loader X-Git-Tag: 0.9.7^2~414 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=5b0a3b1498a972ec1dedfcb97d6b831d03e58b07;p=Sone.git Normalize URI in element loader --- diff --git a/src/main/kotlin/net/pterodactylus/sone/core/DefaultElementLoader.kt b/src/main/kotlin/net/pterodactylus/sone/core/DefaultElementLoader.kt index 3f33efe..9eae597 100644 --- a/src/main/kotlin/net/pterodactylus/sone/core/DefaultElementLoader.kt +++ b/src/main/kotlin/net/pterodactylus/sone/core/DefaultElementLoader.kt @@ -4,6 +4,8 @@ import com.google.common.base.Ticker import com.google.common.cache.CacheBuilder import freenet.keys.FreenetURI import java.io.ByteArrayInputStream +import java.net.URLDecoder +import java.text.Normalizer import java.util.concurrent.TimeUnit.MINUTES import javax.imageio.ImageIO import javax.inject.Inject @@ -33,44 +35,48 @@ class DefaultElementLoader(private val freenetInterface: FreenetInterface, ticke ImageIO.read(it) }?.let { println("Successfully parsed images from $uri.") - imageCache.get(uri.toString()) { LinkedElement(uri.toString()) } + imageCache.get(uri.toString().decode().normalize()) { LinkedElement(uri.toString()) } } removeLoadingLink(uri) } override fun failed(uri: FreenetURI) { println("Failed to load $uri.") - failureCache.put(uri.toString(), true) + failureCache.put(uri.toString().decode().normalize(), true) removeLoadingLink(uri) } private fun removeLoadingLink(uri: FreenetURI) { println("Not loading anymore: $uri.") synchronized(loadingLinks) { - loadingLinks.invalidate(uri.toString()) + loadingLinks.invalidate(uri.toString().decode().normalize()) } } } override fun loadElement(link: String): LinkedElement { - println("Checking for $link...") + val normalizedLink = link.decode().normalize() + println("Checking for $normalizedLink...") synchronized(loadingLinks) { - imageCache.getIfPresent(link)?.run { - println("In the Image Cache: $link") + imageCache.getIfPresent(normalizedLink)?.run { + println("In the Image Cache: $normalizedLink") return this } - failureCache.getIfPresent(link)?.run { - println("In the Failure Cache: $link") + failureCache.getIfPresent(normalizedLink)?.run { + println("In the Failure Cache: $normalizedLink") return LinkedElement(link, failed = true) } - if (loadingLinks.getIfPresent(link) == null) { - println("Not loading: $link") - loadingLinks.put(link, true) + if (loadingLinks.getIfPresent(normalizedLink) == null) { + println("Not loading: $normalizedLink") + loadingLinks.put(normalizedLink, true) freenetInterface.startFetch(FreenetURI(link), callback) } } - println("Returning loading element: $link") + println("Returning loading element: $normalizedLink") return LinkedElement(link, loading = true) } + private fun String.decode() = URLDecoder.decode(this, "UTF-8") + private fun String.normalize() = Normalizer.normalize(this, Normalizer.Form.NFC) + } diff --git a/src/test/kotlin/net/pterodactylus/sone/core/DefaultElementLoaderTest.kt b/src/test/kotlin/net/pterodactylus/sone/core/DefaultElementLoaderTest.kt index 5f8cb88..fe3f512 100644 --- a/src/test/kotlin/net/pterodactylus/sone/core/DefaultElementLoaderTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/core/DefaultElementLoaderTest.kt @@ -25,6 +25,8 @@ class DefaultElementLoaderTest { companion object { private const val IMAGE_ID = "KSK@gpl.png" private val freenetURI = FreenetURI(IMAGE_ID) + private const val decomposedKey = "CHK@DCiVgTWW9nnWHJc9EVwtFJ6jAfBSVyy~rgiPvhUKbS4,mNY85V0x7dYcv7SnEYo1PCC6y2wNWMDNt-y9UWQx9fI,AAMC--8/fru%CC%88hstu%CC%88ck.jpg" + private const val normalizedKey = "CHK@DCiVgTWW9nnWHJc9EVwtFJ6jAfBSVyy~rgiPvhUKbS4,mNY85V0x7dYcv7SnEYo1PCC6y2wNWMDNt-y9UWQx9fI,AAMC--8/frühstück.jpg" } private val freenetInterface = mock() @@ -80,11 +82,11 @@ class DefaultElementLoaderTest { @Test fun `image loader can load image`() { - elementLoader.loadElement(IMAGE_ID) - verify(freenetInterface).startFetch(eq(freenetURI), callback.capture()) - callback.value.loaded(freenetURI, "image/png", read("/static/images/unknown-image-0.png")) - val linkedElement = elementLoader.loadElement(IMAGE_ID) - assertThat(linkedElement, `is`(LinkedElement(IMAGE_ID))) + elementLoader.loadElement(decomposedKey) + verify(freenetInterface).startFetch(eq(FreenetURI(decomposedKey)), callback.capture()) + callback.value.loaded(FreenetURI(normalizedKey), "image/png", read("/static/images/unknown-image-0.png")) + val linkedElement = elementLoader.loadElement(decomposedKey) + assertThat(linkedElement, `is`(LinkedElement(normalizedKey))) } @Test