- 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 loaded(uri: FreenetURI, mimeTypeText: String, data: ByteArray) {
+ MimeType(mimeTypeText).also { mimeType ->
+ when {
+ mimeType.primaryType == "image" -> {
+ ByteArrayInputStream(data).use {
+ ImageIO.read(it)
+ }?.let {
+ elementCache.get(uri.toString().decode().normalize()) {
+ LinkedElement(uri.toString(), properties = mapOf("type" to "image", "size" to data.size, "sizeHuman" to data.size.human))
+ .apply {
+ logger.fine("Downloaded image from $link: size=${properties["size"]}.")
+ }
+ }
+ }
+ }
+ mimeType.baseType == "text/html" -> {
+ val document = Jsoup.parse(data.toString(Charset.forName(mimeType.getParameter("charset") ?: "UTF-8")))
+ elementCache.get(uri.toString().decode().normalize()) {
+ LinkedElement(uri.toString(), properties = mapOf(
+ "type" to "html", "size" to data.size, "sizeHuman" to data.size.human,
+ "title" to document.title().emptyToNull,
+ "description" to (document.metaDescription ?: document.firstNonHeadingParagraph)
+ )).apply {
+ logger.fine { "Extracted information from $link: title=${properties["title"]}, description=${properties["description"]}." }
+ }
+ }
+ }
+ }
+ removeLoadingLink(uri)
+ }