From: David ‘Bombe’ Roden Date: Mon, 9 Sep 2019 09:18:26 +0000 (+0200) Subject: ✨ Recognize audio files as linked elements X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=98c4c698a74b8cc4424a394d12c4f2721dbac136;p=Sone.git ✨ Recognize audio files as linked elements --- diff --git a/src/main/kotlin/net/pterodactylus/sone/core/DefaultElementLoader.kt b/src/main/kotlin/net/pterodactylus/sone/core/DefaultElementLoader.kt index 409d18c..eb90b34 100644 --- a/src/main/kotlin/net/pterodactylus/sone/core/DefaultElementLoader.kt +++ b/src/main/kotlin/net/pterodactylus/sone/core/DefaultElementLoader.kt @@ -28,6 +28,13 @@ class DefaultElementLoader(private val freenetInterface: FreenetInterface, ticke private val elementCache: Cache = 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")) } @@ -74,13 +81,15 @@ class DefaultElementLoader(private val freenetInterface: FreenetInterface, ticke .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) diff --git a/src/test/kotlin/net/pterodactylus/sone/core/DefaultElementLoaderTest.kt b/src/test/kotlin/net/pterodactylus/sone/core/DefaultElementLoaderTest.kt index 726ceb7..0e6a0c0 100644 --- a/src/test/kotlin/net/pterodactylus/sone/core/DefaultElementLoaderTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/core/DefaultElementLoaderTest.kt @@ -8,8 +8,6 @@ import net.pterodactylus.sone.test.capture 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` @@ -17,6 +15,7 @@ import org.mockito.Mockito.times import org.mockito.Mockito.verify import java.io.ByteArrayOutputStream import java.util.concurrent.TimeUnit +import kotlin.test.Test /** * Unit test for [DefaultElementLoaderTest]. @@ -100,6 +99,17 @@ class 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()) @@ -193,6 +203,7 @@ class DefaultElementLoaderTest { 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