List files when folder is empty
alex wennerberg alex@alexwennerberg.com
Sun, 27 Dec 2020 15:28:39 -0800
4 files changed,
81 insertions(+),
5 deletions(-)
M
http.go
→
http.go
@@ -525,11 +525,12 @@ return ""
} // 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 @@ if p == "/" || isDir {
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 @@ }
_, 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 @@ PageTitle string
URI string }{template.HTML(htmlString), favicon, userName + p, URI} t.ExecuteTemplate(w, "user_page.html", data) + file.Close() } else { http.ServeFile(w, r, fileName) }
A
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>
A
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>
M
templates/my_site.html
→
templates/my_site.html
@@ -16,7 +16,7 @@ {{ if gt (len .Children) 0 }}
<td colspan="3"> <details> <summary> - {{.Name}}/ + <a href="//{{.Creator}}.{{.Host}}/{{.Name}}">{{.Name}}/</a> </summary> <table> {{ range .Children }}