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
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)
+
}
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<FreenetInterface>()
@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