~jan0sch/smederee

Showing details for patch a507c316bb2d381ef67e01788bd8374bcb96bf0e.
2022-08-08 (Mon), 9:11 AM - Jens Grassel - a507c316bb2d381ef67e01788bd8374bcb96bf0e

VCS: Fix deeper levels and file rendering

- switch to right associative (`/:`)
- remove now obsolete extra route for file rendering
Summary of changes
2 files modified with 49 lines added and 71 lines removed
  • modules/hub/src/main/scala/de/smederee/hub/VcsRepositoryRoutes.scala with 42 added and 70 removed lines
  • modules/hub/src/main/twirl/de/smederee/hub/views/showRepository.scala.html with 7 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-02-02 20:08:51.762936492 +0000
+++ new-smederee/modules/hub/src/main/scala/de/smederee/hub/VcsRepositoryRoutes.scala	2025-02-02 20:08:51.762936492 +0000
@@ -209,30 +209,44 @@
             None,
             s"Smederee/~$repositoryOwner/$repositoryName".some,
             user
-          )(listing, repositoryBaseUri, repositoryName)
+          )(None, listing, repositoryBaseUri, repositoryName)
         )
       } yield resp
   }
 
   private val showRepositoryFiles: AuthedRoutes[Account, F] = AuthedRoutes.of {
-    case ar @ GET -> Root / UsernamePathParameter(repositoryOwner) / VcsRepositoryNamePathParameter(
+    case ar @ GET -> UsernamePathParameter(repositoryOwner) /: VcsRepositoryNamePathParameter(
           repositoryName
-        ) / filePath as user => // FIXME `/ filePath` only captures on parameter and `/:` doesn't compile!
+        ) /: filePath as user =>
       for {
-        csrf          <- Sync[F].delay(ar.req.getCsrfToken)
-        filePathParts <- Sync[F].delay(filePath.split("/"))
-        directory <- Sync[F].delay(
+        csrf <- Sync[F].delay(ar.req.getCsrfToken)
+        requestedFilePath <- Sync[F].delay(
           os.Path(
             Paths.get(
               config.repositoriesDirectory.toPath.toString,
               repositoryOwner.toString,
               repositoryName.toString,
-              filePath
+              filePath.segments.mkString("/")
             )
           )
         )
-        listing  <- listFiles(directory)
-        viewFile <- Sync[F].delay(os.isFile(directory))
+        viewFile <- Sync[F].delay(os.isFile(requestedFilePath))
+        listing <-
+          if (viewFile)
+            Sync[F].delay(IndexedSeq.empty)
+          else
+            listFiles(requestedFilePath)
+        content <-
+          if (viewFile)
+            Sync[F].delay(os.read.lines(requestedFilePath))
+          else
+            Sync[F].delay(IndexedSeq.empty)
+        fileContent <- Sync[F].delay {
+          if (content.isEmpty)
+            None
+          else
+            Option(content.mkString("\n"))
+        }
         repositoryBaseUri <- Sync[F].delay(
           Uri(path =
             Uri.Path.Root |+| Uri.Path(
@@ -240,68 +254,26 @@
             )
           )
         )
-        actionBaseUriAppendix <- Sync[F].delay {
-          filePathParts.map(part => Uri.Path.Segment(part)).toVector
-        }
-        actionBaseUri <- Sync[F].delay(Uri(path = repositoryBaseUri.path |+| Uri.Path(actionBaseUriAppendix)))
+        actionBaseUri <- Sync[F].delay(Uri(path = repositoryBaseUri.path |+| filePath))
         goBackUri <- Sync[F].delay(
-          Uri(path = repositoryBaseUri.path |+| Uri.Path(actionBaseUriAppendix.reverse.drop(1).reverse))
+          Uri(path = repositoryBaseUri.path |+| Uri.Path(filePath.segments.reverse.drop(1).reverse))
         )
-        resp <- viewFile match {
-          case false =>
-            if (filePath.startsWith("_darcs") || filePath.startsWith("/_darcs"))
-              NotFound()
-            else
-              Ok(
-                views.html.showRepository()(
-                  actionBaseUri,
-                  csrf,
-                  Option(goBackUri),
-                  s"Smederee/~$repositoryOwner/$repositoryName".some,
-                  user
-                )(listing, repositoryBaseUri, 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
+        resp <-
+          if (
+            filePath.segments.mkString
+              .startsWith("_darcs") || filePath.segments.mkString.startsWith("/_darcs")
+          )
+            NotFound()
+          else
+            Ok(
+              views.html.showRepository()(
+                actionBaseUri,
+                csrf,
+                Option(goBackUri),
+                s"Smederee/~$repositoryOwner/$repositoryName".some,
+                user
+              )(fileContent, listing, repositoryBaseUri, repositoryName)
             )
-          )
-        )
-        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
   }
 
@@ -353,6 +325,6 @@
   }
 
   val protectedRoutes =
-    showRepositories <+> parseCreateRepositoryForm <+> showCreateRepositoryForm <+> showRepositoryHistory <+> showRepositoryRawFile <+> showRepositoryFiles <+> showRepository
+    showRepositories <+> parseCreateRepositoryForm <+> showCreateRepositoryForm <+> showRepositoryHistory <+> showRepositoryFiles <+> showRepository
 
 }
diff -rN -u old-smederee/modules/hub/src/main/twirl/de/smederee/hub/views/showRepository.scala.html new-smederee/modules/hub/src/main/twirl/de/smederee/hub/views/showRepository.scala.html
--- old-smederee/modules/hub/src/main/twirl/de/smederee/hub/views/showRepository.scala.html	2025-02-02 20:08:51.762936492 +0000
+++ new-smederee/modules/hub/src/main/twirl/de/smederee/hub/views/showRepository.scala.html	2025-02-02 20:08:51.762936492 +0000
@@ -1,4 +1,4 @@
-@(lang: LanguageCode = LanguageCode("en"), pathPrefix: Option[Uri] = None)(actionBaseUri: Uri, csrf: Option[CsrfToken] = None, goBackUri: Option[Uri] = None, title: Option[String] = None, user: Account)(listing: IndexedSeq[(os.RelPath, os.StatInfo)], repositoryBaseUri: Uri, repositoryName: VcsRepositoryName)
+@(lang: LanguageCode = LanguageCode("en"), pathPrefix: Option[Uri] = None)(actionBaseUri: Uri, csrf: Option[CsrfToken] = None, goBackUri: Option[Uri] = None, title: Option[String] = None, user: Account)(fileContent: Option[String], listing: IndexedSeq[(os.RelPath, os.StatInfo)], repositoryBaseUri: Uri, repositoryName: VcsRepositoryName)
 @main(lang, pathPrefix)()(csrf, title, user.some) {
 @defining(lang.toLocale) { implicit locale =>
   <div class="content">
@@ -11,6 +11,7 @@
   <div class="content">
     <div class="pure-g">
       <div class="l-box pure-u-1-1 pure-u-md-1-1">
+        @if(fileContent.isEmpty) {
         <table class="pure-table pure-table-horizontal">
           <thead>
             <tr>
@@ -38,6 +39,11 @@
             }
           </tbody>
         </table>
+        } else {
+          @for(content <- fileContent) {
+            <pre>@content</pre>
+          }
+        }
       </div>
     </div>
   </div>