From fe6af31f05f59ed1ef162bc16ffc99600df51ad7 Mon Sep 17 00:00:00 2001
From: Marvin Borner
Date: Wed, 17 Apr 2019 00:10:53 +0200
Subject: Fixed viewing of PDFs and other binary files

Co-authored-by: LarsVomMars <lars@kroenner.eu>
---
 src/main/kotlin/App.kt            |  8 --------
 src/main/kotlin/FileController.kt | 19 ++++++++++++++++---
 2 files changed, 16 insertions(+), 11 deletions(-)

(limited to 'src/main')

diff --git a/src/main/kotlin/App.kt b/src/main/kotlin/App.kt
index ac9127b..bb69db2 100644
--- a/src/main/kotlin/App.kt
+++ b/src/main/kotlin/App.kt
@@ -127,14 +127,6 @@ fun roleManager(handler: Handler, ctx: Context, permittedRoles: Set<Role>) {
     }
 }
 
-/*
-fun indexAllFiles(ctx: Context) {
-    Files.list(Paths.get("$fileHome/${getVerifiedUserId(ctx)}").forEach {
-        // TODO: Add file indexing function
-    }
-}
-*/
-
 /**
  * Declares the roles in which a user can be in
  */
diff --git a/src/main/kotlin/FileController.kt b/src/main/kotlin/FileController.kt
index 08f0763..528df3a 100644
--- a/src/main/kotlin/FileController.kt
+++ b/src/main/kotlin/FileController.kt
@@ -57,7 +57,10 @@ class FileController {
                             "extension", File("$usersFileHome/${ctx.splats()[0]}").extension
                         )
                     )
-                else -> ctx.result(FileInputStream(File("$usersFileHome/${ctx.splats()[0]}")))
+                else -> {
+                    ctx.contentType(Files.probeContentType(Paths.get("$usersFileHome/${ctx.splats()[0]}")))
+                    ctx.result(FileInputStream(File("$usersFileHome/${ctx.splats()[0]}")))
+                }
             }
         } catch (_: Exception) {
             throw NotFoundResponse("Error: File or directory does not exist.")
@@ -114,6 +117,14 @@ class FileController {
         }
     }
 
+    /*
+    fun indexAll(ctx: Context) {
+        Files.list(Paths.get("$fileHome/${getVerifiedUserId(ctx)}").forEach {
+            // TODO: Add file indexing function
+        }
+    }
+    */
+
     /**
      * Deletes the requested file
      */
@@ -156,10 +167,12 @@ class FileController {
                         "extension", File(sharedFileLocation).extension
                     )
                 )
-            } else ctx.result(FileInputStream(File(sharedFileLocation)))  // TODO: other file types
+            } else {
+                ctx.contentType(Files.probeContentType(Paths.get(sharedFileLocation)))
+                ctx.result(FileInputStream(File(sharedFileLocation)))
+            }
         } else {
             log.info("Unknown file!")
         }
     }
-
 }
-- 
cgit v1.2.3