about summary refs log tree commit diff homepage
path: root/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'main.go')
-rw-r--r--main.go62
1 files changed, 52 insertions, 10 deletions
diff --git a/main.go b/main.go
index 4fa807b..c60be5b 100644
--- a/main.go
+++ b/main.go
@@ -22,8 +22,8 @@ import (
 	"archive/zip"
 	"embed"
 	"html/template"
-	"log"
 	"io"
+	"log"
 	"net/http"
 	"os"
 	"path"
@@ -39,15 +39,30 @@ var templates embed.FS
 
 type Page struct {
 	Index int
-	Name string
+	Name  string
 }
 
 type Archive struct {
-	Path string
-	Title string
+	Title   string
+	Prev    template.URL
+	Next    template.URL
 	Entries []Page
 }
 
+type Directory struct {
+	Title   string
+	Entries []string
+}
+
+func find(entries []os.DirEntry, name string) int {
+	for i, entry := range entries {
+		if entry.Name() == name {
+			return i
+		}
+	}
+	return -1
+}
+
 func main() {
 	http.Handle("/static/", http.FileServer(http.FS(static)))
 	t, err := template.ParseFS(templates, "templates/*.html")
@@ -56,7 +71,7 @@ func main() {
 	}
 	lib := os.Getenv("PHYLACTERY_LIBRARY")
 	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
-		p := path.Clean(lib + r.URL.Path)
+		p := path.Join(lib, path.Clean(r.URL.Path))
 		stat, err := os.Stat(p)
 		if err != nil {
 			http.NotFound(w, r)
@@ -64,13 +79,34 @@ func main() {
 		}
 
 		if stat.IsDir() {
-			// TODO
+			if !strings.HasSuffix(r.URL.Path, "/") {
+				http.Redirect(w, r, r.URL.Path+"/",
+					http.StatusMovedPermanently)
+				return
+			}
+
+			entries, _ := os.ReadDir(p)
+			var names []string
+			for _, entry := range entries {
+				if entry.IsDir() {
+					names = append(names, entry.Name()+"/")
+				} else {
+					names = append(names, entry.Name())
+				}
+			}
+			dir := Directory{stat.Name(), names}
+			t.ExecuteTemplate(w, "directory.html", dir)
+			return
+		} else if strings.HasSuffix(r.URL.Path, "/") {
+			http.Redirect(w, r, r.URL.Path[:len(r.URL.Path)-1],
+				http.StatusMovedPermanently)
+			return
 		}
 
 		// TODO: LRU caching
 		cbz, err := zip.OpenReader(p)
 		if err != nil {
-			http.Error(w, "invalid cbz", 406)
+			http.Error(w, "invalid cbz", http.StatusNotAcceptable)
 			return
 		}
 		defer cbz.Close()
@@ -88,6 +124,8 @@ func main() {
 			return
 		}
 
+		entries, _ := os.ReadDir(path.Join(p, ".."))
+		index := find(entries, stat.Name())
 		var pages []Page
 		for i, f := range cbz.File {
 			image, _ := cbz.File[i].Open()
@@ -96,11 +134,15 @@ func main() {
 			n, _ := image.Read(buf)
 			mime := http.DetectContentType(buf[:n])
 			if strings.HasPrefix(mime, "image/") {
-				pages = append(pages, Page{ i, f.Name })
+				pages = append(pages, Page{i, f.Name})
 			}
 		}
-		archive := Archive{ r.URL.Path, stat.Name(), pages }
-		t.ExecuteTemplate(w, "archive.html", archive)
+		t.ExecuteTemplate(w, "archive.html", Archive{
+			stat.Name(),
+			template.URL(entries[index-1].Name()),
+			template.URL(entries[index+1].Name()),
+			pages,
+		})
 	})
 
 	addr := os.Getenv("PHYLACTERY_ADDRESS")