Skip to content

Commit

Permalink
Fix mimetype handling with Tika.
Browse files Browse the repository at this point in the history
Also fixed some unit tests and exception handling.

Fixes #5
  • Loading branch information
mrletourneau committed Dec 3, 2020
1 parent 5257aa1 commit 6dd2755
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
2 changes: 2 additions & 0 deletions earl/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ dependencies {
// This dependency is used by the application.
implementation 'com.google.guava:guava:29.0-jre'

implementation 'org.apache.tika:tika-parsers:1.25'

// Use the Kotlin test library.
testImplementation 'org.jetbrains.kotlin:kotlin-test'

Expand Down
8 changes: 3 additions & 5 deletions earl/src/main/kotlin/com/mrletourneau/earl/EarlServer.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.mrletourneau.earl

import org.apache.tika.Tika
import java.io.*
import java.io.File
import java.net.ServerSocket
import java.net.Socket
import java.net.URI
import java.nio.file.Files
import java.text.SimpleDateFormat
import java.time.Instant
import java.util.*
Expand Down Expand Up @@ -127,7 +127,7 @@ class EarlServer constructor(serverSocket: ServerSocket?) : Runnable {
return path
}
catch(e: Exception) {
e.printStackTrace()
println("INFO - ${e.message}")
throw IOException("Malformed Header")
}
}
Expand Down Expand Up @@ -159,11 +159,9 @@ class EarlServer constructor(serverSocket: ServerSocket?) : Runnable {
private fun isGeminiFile(fileName: String) = fileName.endsWith(".gmi") || fileName.endsWith(".gemini")

fun getMimeType(file: File): String {
val path = file.toPath()

if (isGeminiFile(file.name)) return "text/gemini"

return when (val mimeType = Files.probeContentType(path)) {
return when (val mimeType = Tika().detect(file)) {
null -> "text/plain"
else -> mimeType
}
Expand Down
7 changes: 5 additions & 2 deletions earl/src/test/kotlin/com/mrletourneau/earl/EarlServerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import junit.framework.Assert.assertEquals
import org.junit.Test
import java.io.*
import java.net.ServerSocket
import java.nio.file.Paths
import kotlin.test.assertFailsWith

internal class EarlServerTest {
Expand Down Expand Up @@ -40,6 +39,10 @@ internal class EarlServerTest {
getPathTestHelper("test")
}

assertFailsWith<IOException> {
getPathTestHelper("http://test")
}

assertFailsWith<IOException> {
getPathTestHelper("af3*#Hfjd1::fdf3")
}
Expand All @@ -63,6 +66,6 @@ internal class EarlServerTest {
}

private fun getPath(fileName: String): String {
return "${Paths.get("").toAbsolutePath()}/earl/src/test/resources/${fileName}"
return "src/test/resources/${fileName}"
}
}

0 comments on commit 6dd2755

Please sign in to comment.