diff --git a/go.mod b/go.mod index 31ec353..618430c 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module git.z3bra.org/partage go 1.17 + +require github.com/dustin/go-humanize v1.0.0 // indirect diff --git a/partage.go b/partage.go index 8e04244..ce0af20 100644 --- a/partage.go +++ b/partage.go @@ -8,17 +8,26 @@ import ( "os" "path" "path/filepath" + "html/template" + + "github.com/dustin/go-humanize" ) +type templatedata struct { + Maxsize string +} + var conf struct { bind string + baseuri string filepath string rootdir string - baseuri string + templatedir string filectx string maxsize int64 } + func contenttype(f *os.File) string { buffer := make([]byte, 512) @@ -92,6 +101,19 @@ func servefile(f *os.File, w http.ResponseWriter) { } } +func servetemplate(w http.ResponseWriter, f string, d templatedata) { + t, err := template.ParseFiles(conf.templatedir + "/" + f) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + return + } + + err = t.Execute(w, d) + if err != nil { + fmt.Println(err) + } +} + func uploaderPut(w http.ResponseWriter, r *http.Request) { // Max 15 Gb uploads if r.ContentLength > conf.maxsize { @@ -119,8 +141,10 @@ func uploaderPut(w http.ResponseWriter, r *http.Request) { func uploaderGet(w http.ResponseWriter, r *http.Request) { // r.URL.Path is sanitized regarding "." and ".." filename := r.URL.Path - if r.URL.Path == "/" { - filename = "/index.html" + if r.URL.Path == "/" || r.URL.Path == "/index" { + data := templatedata{ Maxsize: humanize.IBytes(uint64(conf.maxsize))} + servetemplate(w, "/index.html", data) + return } f, err := os.Open(conf.rootdir + filename) @@ -145,11 +169,12 @@ func uploader(w http.ResponseWriter, r *http.Request) { func main() { conf.bind = "0.0.0.0:8080" - conf.maxsize = 28 * 1024 * 1024 + conf.maxsize = 30064771072 // 28Gib conf.filepath = "/tmp" conf.rootdir = "./static" conf.baseuri = "http://192.168.0.3:8080" conf.filectx = "/f/" + conf.templatedir = "./templates" http.HandleFunc("/", uploader) http.Handle(conf.filectx, http.StripPrefix(conf.filectx, http.FileServer(http.Dir(conf.filepath)))) diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..ed9ca1f --- /dev/null +++ b/templates/index.html @@ -0,0 +1,21 @@ + + +
+ + + + ++ Use the box below to upload and share files. File size is + limited to {{.Maxsize}}. +
+ +