private val elementCache: Cache<String, LinkedElement> = CacheBuilder.newBuilder().build()
private val callback = object: FreenetInterface.BackgroundFetchCallback {
override fun shouldCancel(uri: FreenetURI, mimeType: String, size: Long): Boolean {
+ if (mimeType.startsWith("audio/")) {
+ elementCache.get(uri.toString().decode().normalize()) {
+ LinkedElement(uri.toString(), properties = mapOf(
+ "type" to "audio", "size" to size, "sizeHuman" to size.human
+ ))
+ }
+ }
return (size > 2097152) || (!mimeType.startsWith("image/") && !mimeType.startsWith("text/html"))
}
.firstOrNull { !it.first.tagName().startsWith("h", ignoreCase = true) }
?.second
- private val Int.human get() = when (this) {
+ private val Long.human get() = when (this) {
in 0..1023 -> "$this B"
in 1024..1048575 -> "${this / 1024} KiB"
in 1048576..1073741823 -> "${this / 1048576} MiB"
else -> "${this / 1073741824} GiB"
}
+ private val Int.human get() = toLong().human
+
override fun failed(uri: FreenetURI) {
failureCache.put(uri.toString().decode().normalize(), true)
removeLoadingLink(uri)
import net.pterodactylus.sone.test.mock
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.equalTo
-import org.hamcrest.Matchers.equalTo
-import org.junit.Test
import org.mockito.ArgumentMatchers.any
import org.mockito.ArgumentMatchers.eq
import org.mockito.Mockito.`when`
import org.mockito.Mockito.verify
import java.io.ByteArrayOutputStream
import java.util.concurrent.TimeUnit
+import kotlin.test.Test
/**
* Unit test for [DefaultElementLoaderTest].
}
@Test
+ fun `image loader returns element for audio data`() {
+ elementLoader.loadElement(audioKey)
+ verify(freenetInterface).startFetch(eq(FreenetURI(audioKey)), callback.capture())
+ callback.value.shouldCancel(FreenetURI(audioKey), "audio/mpeg", 123)
+ val linkedElement = elementLoader.loadElement(audioKey)
+ assertThat(linkedElement, equalTo(LinkedElement(audioKey, properties = mapOf(
+ "type" to "audio", "size" to 123L, "sizeHuman" to "123 B"
+ ))))
+ }
+
+ @Test
fun `element loader can extract description from description header`() {
elementLoader.loadElement(textKey)
verify(freenetInterface).startFetch(eq(FreenetURI(textKey)), callback.capture())
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 audioKey = "CHK@DCiVgTWW9nnWHJc9EVwtFJ6jAfBSVyy~rgiPvhUKbS4,mNY85V0x7dYcv7SnEYo1PCC6y2wNWMDNt-y9UWQx9fI,AAMC--8/audio.mp3"
private const val normalizedKey = "CHK@DCiVgTWW9nnWHJc9EVwtFJ6jAfBSVyy~rgiPvhUKbS4,mNY85V0x7dYcv7SnEYo1PCC6y2wNWMDNt-y9UWQx9fI,AAMC--8/frühstück.jpg"
private const val textKey = "KSK@gpl.html"
private const val sizeOkay = 2097152L