+ if (!mimeType.startsWith("image/") && !mimeType.startsWith("text/html")) {
+ logger.fine { "Canceling download of $uri because of its MIME type, $mimeType." }
+ return true
+ }
+ return false
+ }
+
+ 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)