~jan0sch/smederee

Showing details for patch 7ea213dbe8aeedb64df800d762682a3fa9ec4d02.
2023-05-17 (Wed), 11:56 AM - Jens Grassel - 7ea213dbe8aeedb64df800d762682a3fa9ec4d02

VCS: Only view file in file browser if it is a text file.

Summary of changes
1 files modified with 23 lines added and 1 lines removed
  • modules/hub/src/main/scala/de/smederee/hub/VcsRepositoryRoutes.scala with 23 added and 1 removed lines
diff -rN -u old-smederee/modules/hub/src/main/scala/de/smederee/hub/VcsRepositoryRoutes.scala new-smederee/modules/hub/src/main/scala/de/smederee/hub/VcsRepositoryRoutes.scala
--- old-smederee/modules/hub/src/main/scala/de/smederee/hub/VcsRepositoryRoutes.scala	2025-01-16 12:49:21.521068636 +0000
+++ new-smederee/modules/hub/src/main/scala/de/smederee/hub/VcsRepositoryRoutes.scala	2025-01-16 12:49:21.521068636 +0000
@@ -478,7 +478,27 @@
             )
           )
         )
-      viewFile <- Sync[F].delay(requestedFilePath.map(os.isFile).getOrElse(false))
+      fileType <- requestedFilePath
+        .filter(os.isFile) // Only determine the mime type for regular files.
+        .fold(Sync[F].pure(none[MediaType]))(path =>
+          // TODO: Refactor this into a dedicated function and clean it up.
+          Sync[F].delay(
+            MediaType
+              .parse(
+                os.proc("file", "--mime-type", path.toString)
+                  .call()
+                  .out
+                  .text()
+                  .trim
+                  .reverse
+                  .takeWhile(char => !char.isWhitespace)
+                  .reverse
+              )
+              .toOption
+          )
+        )
+      _        <- Sync[F].delay(log.debug(s"Requested to view file type: $fileType"))
+      viewFile <- Sync[F].delay(fileType.exists(_.isText)) // Only display certain file types.
       listing <-
         requestedFilePath match {
           case None                => Sync[F].delay(IndexedSeq.empty)
@@ -499,6 +519,8 @@
                     Sync[F].delay(os.Generator("File is too big!"))
                 lines <- Sync[F].delay(stream.toVector)
               } yield lines
+            else if (os.isFile(path))
+              Sync[F].delay(Vector("Sorry, but displaying such file types is currently not implemented!"))
             else
               Sync[F].delay(IndexedSeq.empty)
         }