commit c151228b11c1cd78e406d351d08607cf02949e85
parent c1ba1a1d082df5c03376ebb44d07234e0b8b1cc6
Author: alex wennerberg <alex@alexwennerberg.com>
Date: Sun, 27 Dec 2020 15:28:39 -0800
List files when folder is empty
Diffstat:
4 files changed, 81 insertions(+), 5 deletions(-)
diff --git a/http.go b/http.go
@@ -525,11 +525,12 @@ func getFavicon(user string) string {
}
// Server a user's file
+// Here be dragons
func userFile(w http.ResponseWriter, r *http.Request) {
userName := filepath.Clean(strings.Split(r.Host, ".")[0]) // Clean probably unnecessary
p := filepath.Clean(r.URL.Path)
var isDir bool
- fileName := path.Join(c.FilesDirectory, userName, p)
+ fileName := path.Join(c.FilesDirectory, userName, p) // TODO rename filepath
stat, err := os.Stat(fileName)
if stat != nil {
isDir = stat.IsDir()
@@ -538,7 +539,7 @@ func userFile(w http.ResponseWriter, r *http.Request) {
fileName = path.Join(fileName, "index.gmi")
}
- if strings.HasPrefix(p, "/.hidden") {
+ if strings.HasPrefix(p, "/"+HIDDEN_FOLDER) {
renderDefaultError(w, http.StatusForbidden)
return
}
@@ -549,8 +550,40 @@ func userFile(w http.ResponseWriter, r *http.Request) {
_, err = os.Stat(fileName)
if os.IsNotExist(err) {
- renderDefaultError(w, http.StatusNotFound)
- return
+ if p == "/" || isDir {
+ fileName := path.Join(c.FilesDirectory, userName, p)
+ favicon := getFavicon(userName)
+ files, _ := ioutil.ReadDir(fileName)
+ renderedFiles := []File{}
+ for _, file := range files {
+ n := file.Name()
+ if file.IsDir() {
+ n += "/"
+ }
+ newFile := File{
+ Name: n,
+ UpdatedTime: file.ModTime(),
+ Host: c.Host,
+ Creator: getCreator(fileName),
+ }
+ renderedFiles = append(renderedFiles, newFile)
+ }
+ hostname := strings.Split(r.Host, ":")[0]
+ URI := hostname + r.URL.String()
+ data := struct {
+ Folder string
+ Files []File
+ Favicon string
+ PageTitle string
+ URI string
+ }{p, renderedFiles, favicon, userName + p, URI}
+ // TODO check if gemlog
+ t.ExecuteTemplate(w, "folder.html", data)
+ return
+ } else {
+ renderDefaultError(w, http.StatusNotFound)
+ return
+ }
}
// Dumb content negotiation
@@ -569,6 +602,7 @@ func userFile(w http.ResponseWriter, r *http.Request) {
URI string
}{template.HTML(htmlString), favicon, userName + p, URI}
t.ExecuteTemplate(w, "user_page.html", data)
+ file.Close()
} else {
http.ServeFile(w, r, fileName)
}
diff --git a/templates/emptyfolder.html b/templates/emptyfolder.html
@@ -0,0 +1,19 @@
+<!DOCTYPE html>
+<html lang="en">
+ <head>
+ <meta charset="utf-8" />
+ <title>{{.PageTitle }}</title>
+ <meta name="viewport" content="width=device-width" />
+ <link rel="stylesheet" type="text/css" href="/style.css" />
+ <link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>{{.Favicon}}</text></svg>">
+ </head>
+ <body>
+<main>
+{{range .Files}}
+{{end}}
+<br>
+<div class="footer">
+ Proxied from the original at <a href="gemini://{{.URI}}">gemini://{{.URI}}</a> (<a href="https://admin.flounder.online/gemini.gmi">about Gemini</a>)</div>
+</main>
+</body>
+</html>
diff --git a/templates/folder.html b/templates/folder.html
@@ -0,0 +1,23 @@
+<!DOCTYPE html>
+<html lang="en">
+ <head>
+ <meta charset="utf-8" />
+ <title>{{.PageTitle }}</title>
+ <meta name="viewport" content="width=device-width" />
+ <link rel="stylesheet" type="text/css" href="/style.css" />
+ <link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>{{.Favicon}}</text></svg>">
+ </head>
+ <body>
+<main>
+<h1>{{.PageTitle}}</h1>
+{{range .Files}}
+<p>
+<a href="//{{.Creator}}.{{.Host}}/{{.Name}}">{{.Name}}</a>
+</p>
+{{end}}
+<br>
+<a href="/">home</a>
+<br>
+</main>
+</body>
+</html>
diff --git a/templates/my_site.html b/templates/my_site.html
@@ -16,7 +16,7 @@
<td colspan="3">
<details>
<summary>
- {{.Name}}/
+ <a href="//{{.Creator}}.{{.Host}}/{{.Name}}">{{.Name}}/</a>
</summary>
<table>
{{ range .Children }}