~jan0sch/smederee

Showing details for patch e74c0029ad898f3b394e7c0ace01f4ebe01d2271.
2022-08-07 (Sun), 10:34 AM - Jens Grassel - e74c0029ad898f3b394e7c0ace01f4ebe01d2271

VCS: Basic repo content rendering

- only folders
- only 1 level deep (bug in http4s routing parameters?)
Summary of changes
1 files added
  • modules/hub/src/main/twirl/de/smederee/hub/views/showRepository.scala.html
1 files modified with 79 lines added and 3 lines removed
  • modules/hub/src/main/scala/de/smederee/hub/VcsRepositoryRoutes.scala with 79 added and 3 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 23:03:59.963940682 +0000
+++ new-smederee/modules/hub/src/main/scala/de/smederee/hub/VcsRepositoryRoutes.scala	2025-02-02 23:03:59.963940682 +0000
@@ -127,13 +127,89 @@
         )
         listing <- Sync[F].delay(
           os.walk
-            .attrs(directory, skip = (path, _) => path.baseName === "_darcs", maxDepth = 2)
+            .attrs(directory, skip = (path, _) => path.baseName === "_darcs", maxDepth = 1)
             .map((path, attrs) => (path.relativeTo(directory), attrs))
         )
-        resp <- Ok(listing.mkString("\n"))
+        actionBaseUri <- Sync[F].delay(
+          Uri(path =
+            Uri.Path.Root |+| Uri.Path(
+              Vector(Uri.Path.Segment(s"~$repositoryOwner"), Uri.Path.Segment(repositoryName.toString))
+            )
+          )
+        )
+        resp <- Ok(
+          views.html.showRepository()(
+            actionBaseUri,
+            csrf,
+            None,
+            s"Smederee/~$repositoryOwner/$repositoryName".some,
+            user
+          )(listing, repositoryName)
+        )
+      } yield resp
+  }
+
+  private val showRepositoryFiles: AuthedRoutes[Account, F] = AuthedRoutes.of {
+    case ar @ GET -> Root / UsernamePathParameter(repositoryOwner) / VcsRepositoryNamePathParameter(
+          repositoryName
+        ) / filePath 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("/"))
+        directory <- Sync[F].delay(
+          os.Path(
+            Paths.get(
+              config.repositoriesDirectory.toPath.toString,
+              repositoryOwner.toString,
+              repositoryName.toString,
+              filePath
+            )
+          )
+        )
+        listing <- Sync[F].delay {
+          if (os.isDir(directory))
+            os.walk
+              .attrs(directory, skip = (path, _) => path.baseName === "_darcs", maxDepth = 1)
+              .map((path, attrs) => (path.relativeTo(directory), attrs))
+          else
+            IndexedSeq.empty
+        }
+        actionBaseUriAppendix <- Sync[F].delay {
+          filePathParts.map(part => Uri.Path.Segment(part)).toVector
+        }
+        actionBaseUri <- Sync[F].delay(
+          Uri(path =
+            Uri.Path.Root |+| Uri.Path(
+              Vector(
+                Uri.Path.Segment(s"~$repositoryOwner"),
+                Uri.Path.Segment(repositoryName.toString)
+              ) ++ actionBaseUriAppendix
+            )
+          )
+        )
+        goBackUri <- Sync[F].delay(
+          Uri(path =
+            Uri.Path.Root |+| Uri.Path(
+              Vector(
+                Uri.Path.Segment(s"~$repositoryOwner"),
+                Uri.Path.Segment(repositoryName.toString)
+              ) ++ actionBaseUriAppendix.reverse.drop(1).reverse
+            )
+          )
+        )
+        resp <- Ok(
+          views.html.showRepository()(
+            actionBaseUri,
+            csrf,
+            Option(goBackUri),
+            s"Smederee/~$repositoryOwner/$repositoryName".some,
+            user
+          )(listing, repositoryName)
+        )
       } yield resp
   }
 
-  val protectedRoutes = parseCreateRepositoryForm <+> showCreateRepositoryForm <+> showRepository
+  val protectedRoutes =
+    parseCreateRepositoryForm <+> showCreateRepositoryForm <+> 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	1970-01-01 00:00:00.000000000 +0000
+++ new-smederee/modules/hub/src/main/twirl/de/smederee/hub/views/showRepository.scala.html	2025-02-02 23:03:59.963940682 +0000
@@ -0,0 +1,41 @@
+@(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)], repositoryName: VcsRepositoryName)
+@main(lang, pathPrefix)()(csrf, title, user.some) {
+@defining(lang.toLocale) { implicit locale =>
+  <div class="content">
+    <div class="pure-g">
+      <div class="l-box pure-u-1-1 pure-u-md-1-1">
+        <table class="pure-table pure-table-horizontal">
+          <thead>
+            <tr>
+              <th></th>
+              <th>Name</th>
+              <th>Size</th>
+              <th>Created</th>
+              <th>Modified</th>
+            </tr>
+          </thead>
+          <tbody>
+            @for(link <- goBackUri) {
+            <tr>
+              <td><a href="@createFullPath(pathPrefix)(link)"><i class="fa-solid fa-angle-up"></i></i></a></td>
+              <td><a href="@createFullPath(pathPrefix)(link)">..</a></td>
+              <td></td>
+              <td></td>
+            </tr>
+            }
+            @for(entry <- listing) {
+            <tr>
+              <td>@if(entry._2.isDir) { <i class="fa-solid fa-folder"></i> } else { <i class="fa-solid fa-file"></i> }</td>
+              <td><a href="@createFullPath(pathPrefix)(actionBaseUri.addSegment(entry._1.last))">@{entry._1}</a></td>
+              <td>@{entry._2.size}</td>
+              <td>@{entry._2.ctime}</td>
+              <td>@{entry._2.mtime}</td>
+            </tr>
+            }
+          </tbody>
+        </table>
+      </div>
+    </div>
+  </div>
+}
+}