~jan0sch/smederee

Showing details for patch 68e4554788d54ffb331a97bfd8f81a6bee3bba43.
2022-08-07 (Sun), 3:33 PM - Jens Grassel - 68e4554788d54ffb331a97bfd8f81a6bee3bba43

VCS: Show raw files from repository

Summary of changes
1 files modified with 53 lines added and 9 lines removed
  • modules/hub/src/main/scala/de/smederee/hub/VcsRepositoryRoutes.scala with 53 added and 9 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-02-02 19:57:02.129707593 +0000
+++ new-smederee/modules/hub/src/main/scala/de/smederee/hub/VcsRepositoryRoutes.scala	2025-02-02 19:57:02.129707593 +0000
@@ -209,6 +209,7 @@
           else
             IndexedSeq.empty
         }
+        viewFile <- Sync[F].delay(os.isFile(directory))
         actionBaseUriAppendix <- Sync[F].delay {
           filePathParts.map(part => Uri.Path.Segment(part)).toVector
         }
@@ -232,15 +233,58 @@
             )
           )
         )
-        resp <- Ok(
-          views.html.showRepository()(
-            actionBaseUri,
-            csrf,
-            Option(goBackUri),
-            s"Smederee/~$repositoryOwner/$repositoryName".some,
-            user
-          )(listing, repositoryName)
+        resp <- viewFile match {
+          case false =>
+            Ok(
+              views.html.showRepository()(
+                actionBaseUri,
+                csrf,
+                Option(goBackUri),
+                s"Smederee/~$repositoryOwner/$repositoryName".some,
+                user
+              )(listing, repositoryName)
+            )
+          case true =>
+            SeeOther.apply(Location(Uri(path = actionBaseUri.path.addSegment("raw"))))
+        }
+      } yield resp
+  }
+
+  private val showRepositoryRawFile: AuthedRoutes[Account, F] = AuthedRoutes.of {
+    case ar @ GET -> Root / UsernamePathParameter(repositoryOwner) / VcsRepositoryNamePathParameter(
+          repositoryName
+        ) / filePath / "raw" as user => // FIXME `/ filePath` only captures on parameter and `/:` doesn't compile!
+      for {
+        csrf          <- Sync[F].delay(ar.req.getCsrfToken)
+        filePathParts <- Sync[F].delay(filePath.split("/"))
+        pathToFile <- Sync[F].delay(
+          os.Path(
+            Paths.get(
+              config.repositoriesDirectory.toPath.toString,
+              repositoryOwner.toString,
+              repositoryName.toString,
+              filePath
+            )
+          )
+        )
+        content  <- Sync[F].delay(os.read.lines(pathToFile))
+        viewFile <- Sync[F].delay(os.isFile(pathToFile))
+        actionBaseUri <- Sync[F].delay(
+          Uri(path =
+            Uri.Path.Root |+| Uri.Path(
+              Vector(
+                Uri.Path.Segment(s"~$repositoryOwner"),
+                Uri.Path.Segment(repositoryName.toString)
+              )
+            )
+          )
         )
+        resp <- viewFile match {
+          case false =>
+            SeeOther.apply(Location(actionBaseUri))
+          case true =>
+            Ok(content.mkString("\n"))
+        }
       } yield resp
   }
 
@@ -293,6 +337,6 @@
   }
 
   val protectedRoutes =
-    showRepositories <+> parseCreateRepositoryForm <+> showCreateRepositoryForm <+> showRepositoryHistory <+> showRepositoryFiles <+> showRepository
+    showRepositories <+> parseCreateRepositoryForm <+> showCreateRepositoryForm <+> showRepositoryHistory <+> showRepositoryRawFile <+> showRepositoryFiles <+> showRepository
 
 }