Skip to content

Commit

Permalink
fix: if Tika fails to initialise, catch the exception and disable con…
Browse files Browse the repository at this point in the history
…tent type detection #1245
  • Loading branch information
Ronald Holshausen committed Nov 15, 2020
1 parent 4c86cd0 commit 0190004
Showing 1 changed file with 14 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import org.apache.commons.codec.binary.Hex
import org.apache.tika.config.TikaConfig
import org.apache.tika.io.TikaInputStream
import org.apache.tika.metadata.Metadata
import java.lang.RuntimeException
import java.util.Base64

/**
Expand Down Expand Up @@ -114,12 +115,16 @@ data class OptionalBody(

fun detectContentType(): ContentType? = when {
this.isPresent() -> {
val metadata = Metadata()
val mimetype = tika.detector.detect(TikaInputStream.get(value!!), metadata)
if (mimetype.baseType.type == "text") {
detectStandardTextContentType() ?: ContentType(mimetype)
if (tika != null) {
val metadata = Metadata()
val mimetype = tika.detector.detect(TikaInputStream.get(value!!), metadata)
if (mimetype.baseType.type == "text") {
detectStandardTextContentType() ?: ContentType(mimetype)
} else {
ContentType(mimetype)
}
} else {
ContentType(mimetype)
detectStandardTextContentType()
}
}
else -> null
Expand Down Expand Up @@ -185,7 +190,10 @@ data class OptionalBody(
}
}

private val tika = TikaConfig()
private val tika = try { TikaConfig() } catch (e: RuntimeException) {
logger.warn(e) { "Could not initialise Tika, detecting content types will be disabled" }
null
}
}
}

Expand Down

0 comments on commit 0190004

Please sign in to comment.