Skip to content

Commit

Permalink
Add support for folders
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruben Sierra committed Apr 12, 2020
1 parent 671f4a9 commit ae7088a
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 25 deletions.
65 changes: 45 additions & 20 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package main

import (
"flag"
"fmt"
auth "github.com/abbot/go-http-auth"
"html/template"
"io/ioutil"
Expand All @@ -20,16 +21,16 @@ import (
)

var port = flag.String("p", "8100", "port to serve on")
var directory = flag.String("d", ".", "the directory of files to host")
var root_folder = flag.String("d", ".", "the root folder of files to host")

func main() {
flag.Parse()

fs := http.FileServer(http.Dir(*directory))
http.Handle("/files/", http.StripPrefix("/files/", fs))
fs := http.FileServer(http.Dir(*root_folder))
http.Handle("/_app/files/", http.StripPrefix("/_app/files/", fs))

ss := http.FileServer(http.Dir("statics"))
http.Handle("/statics/", http.StripPrefix("/statics/", ss))
http.Handle("/_app/statics/", http.StripPrefix("/_app/statics/", ss))

htppasswd := os.Getenv("HTPASSWD_FILE")
if htppasswd == "" {
Expand All @@ -41,7 +42,7 @@ func main() {
http.Handle("/", a.Wrap(serveAuthTemplate))
}

log.Printf("Serving %s on HTTP port: %s\n", *directory, *port)
log.Printf("Serving %s on HTTP port: %s\n", *root_folder, *port)
log.Fatal(http.ListenAndServe(":"+*port, nil))
}

Expand All @@ -50,33 +51,57 @@ func serveAuthTemplate(w http.ResponseWriter, ar *auth.AuthenticatedRequest) {
}

func serveTemplate(w http.ResponseWriter, r *http.Request) {
var template_name string
if template_name = filepath.Clean(r.URL.Path); template_name == "/" {
template_name = "/index.html"
}

lp := filepath.Join("templates", "layout.html")
fp := filepath.Join("templates", template_name)
request_path := filepath.Clean(r.URL.Path)

files, err := IOReadDir(*directory)
files, folders, err := ListFilesAndFolders(filepath.Clean(filepath.Join(*root_folder, request_path)))
if err != nil {
log.Fatal(err)
errorHandler(w, r, http.StatusNotFound)
return
}

log.Printf("Template %s\n", template_name)
data := struct {
CurrentFolder string
ParentFolder string
Files []string
Folders []string
}{FolderToWeb(request_path), FolderToWeb(filepath.Dir(request_path)), files, folders}

lp := filepath.Join("templates", "layout.html")
fp := filepath.Join("templates", "index.html")
tmpl, _ := template.ParseFiles(lp, fp)
tmpl.ExecuteTemplate(w, "layout", files)
tmpl.ExecuteTemplate(w, "layout", data)
}

func IOReadDir(root string) ([]string, error) {
func FolderToWeb(folder string) string {
if folder == "/" {
return folder
} else {
return folder + "/"
}
}

func ListFilesAndFolders(root string) ([]string, []string, error) {
var files []string
var folders []string
fileInfo, err := ioutil.ReadDir(root)
if err != nil {
return files, err
return files, folders, err
}

for _, file := range fileInfo {
files = append(files, file.Name())
if file.IsDir() {
folders = append(folders, file.Name())
} else {
files = append(files, file.Name())
}
}

return files, folders, nil
}

func errorHandler(w http.ResponseWriter, r *http.Request, status int) {
w.WriteHeader(status)
if status == http.StatusNotFound {
fmt.Fprint(w, "Resource not found")
}
return files, nil
}
21 changes: 18 additions & 3 deletions templates/index.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
{{define "title"}}Files list{{end}}

{{define "body"}}
{{with $data := .}}
{{if eq $data.CurrentFolder "/"}}
<h1>Files list</h1>
{{else}}
<h1>Files list in <b>'{{$data.CurrentFolder}}'</b></h1>
<a href="{{$data.ParentFolder}}">Back <i class="fa fa-arrow-circle-left"></i></a>
{{end}}

<ul>
{{range .}}
{{range $folder := $data.Folders}}
<li>
{{.}}:
<a href="{{$data.CurrentFolder}}{{$folder}}"><i class="fa fa-folder"></i></a>
</li>
{{end}}
{{range $file := $data.Files}}
<li>
{{.}}:
<a href="/files/{{.}}" download><i class="fa fa-download"></i></a>
<a href="/files/{{.}}" class="qr-link"><i class="fa fa-qrcode"></i></a></li>
<a href="/_app/files{{$data.CurrentFolder}}{{$file}}" download><i class="fa fa-download"></i></a>
<a href="/_app/files{{$data.CurrentFolder}}{{$file}}" class="qr-link"><i class="fa fa-qrcode"></i></a></li>
</li>
{{end}}
</ul>
{{end}}
{{end}}
4 changes: 2 additions & 2 deletions templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<head>
<link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="statics/css/app.css">
<link rel="stylesheet" type="text/css" href="/_app/statics/css/app.css">
<link
href="data:image/x-icon;base64,AAABAAEAEBACAAAAAACwAAAAFgAAACgAAAAQAAAAIAAAAAEAAQAAAAAAQAAAAAAAAAAAAAAAAgAAAAAAAAAAAAAA////AAKnAAB6MgAASlIAAEtCAAB7AAAAAnkAAP/YAACDBQAAUGMAAPy/AAACQAAAel4AAEpSAABK0gAAel4AAAJAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
rel="icon" type="image/x-icon" />
Expand All @@ -16,7 +16,7 @@

<body>
{{template "body" .}}
<script type='text/javascript' src="statics/js/app.js"></script>
<script type='text/javascript' src="/_app/statics/js/app.js"></script>
</body>

</html>
Expand Down

0 comments on commit ae7088a

Please sign in to comment.