From 5a1d1f95e7d122752a405bf1730e67cafb0df177 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sun, 9 Apr 2017 22:08:59 +0200 Subject: [PATCH] Store size properties in linked element --- .../net/pterodactylus/sone/core/DefaultElementLoader.kt | 11 ++++++++++- src/main/kotlin/net/pterodactylus/sone/core/ElementLoader.kt | 2 +- .../net/pterodactylus/sone/core/DefaultElementLoaderTest.kt | 2 +- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/net/pterodactylus/sone/core/DefaultElementLoader.kt b/src/main/kotlin/net/pterodactylus/sone/core/DefaultElementLoader.kt index 5758c47..67d5538 100644 --- a/src/main/kotlin/net/pterodactylus/sone/core/DefaultElementLoader.kt +++ b/src/main/kotlin/net/pterodactylus/sone/core/DefaultElementLoader.kt @@ -33,11 +33,20 @@ class DefaultElementLoader(private val freenetInterface: FreenetInterface, ticke ByteArrayInputStream(data).use { ImageIO.read(it) }?.let { - imageCache.get(uri.toString().decode().normalize()) { LinkedElement(uri.toString()) } + imageCache.get(uri.toString().decode().normalize()) { + LinkedElement(uri.toString(), properties = mapOf("size" to data.size, "sizeHuman" to data.size.human)) + } } removeLoadingLink(uri) } + private val Int.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" + } + override fun failed(uri: FreenetURI) { failureCache.put(uri.toString().decode().normalize(), true) removeLoadingLink(uri) diff --git a/src/main/kotlin/net/pterodactylus/sone/core/ElementLoader.kt b/src/main/kotlin/net/pterodactylus/sone/core/ElementLoader.kt index 95178ed..aec021d 100644 --- a/src/main/kotlin/net/pterodactylus/sone/core/ElementLoader.kt +++ b/src/main/kotlin/net/pterodactylus/sone/core/ElementLoader.kt @@ -12,4 +12,4 @@ interface ElementLoader { } -data class LinkedElement(val link: String, val failed: Boolean = false, val loading: Boolean = false) +data class LinkedElement(val link: String, val failed: Boolean = false, val loading: Boolean = false, val properties: Map = emptyMap()) diff --git a/src/test/kotlin/net/pterodactylus/sone/core/DefaultElementLoaderTest.kt b/src/test/kotlin/net/pterodactylus/sone/core/DefaultElementLoaderTest.kt index 424aaff..03b42c3 100644 --- a/src/test/kotlin/net/pterodactylus/sone/core/DefaultElementLoaderTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/core/DefaultElementLoaderTest.kt @@ -95,7 +95,7 @@ class DefaultElementLoaderTest { 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))) + assertThat(linkedElement, `is`(LinkedElement(normalizedKey, properties = mapOf("size" to 2451, "sizeHuman" to "2 KiB")))) } @Test -- 2.7.4