partage

Unnamed repository; edit this file 'description' to name the repository.
git clone git://git.z3bra.org/partage.git
Log | Files | Refs

commit 47748d678a02b48e37f607d670bca067d6d7d59c
parent bd7d7c3397dafb954e0f452f75179227cbec4226
Author: Willy Goiffon <dev@z3bra.org>
Date:   Fri, 15 Oct 2021 19:23:50 +0200

Print a report page when uploading files from a form

Diffstat:
Mpartage.go | 55+++++++++++++++++++++++++++++++++++++++++++++++++++++--
Mtemplates/index.html | 7++++---
Atemplates/upload.html | 21+++++++++++++++++++++
3 files changed, 78 insertions(+), 5 deletions(-)

diff --git a/partage.go b/partage.go @@ -14,6 +14,8 @@ import ( ) type templatedata struct { + Links []string + Size string Maxsize string } @@ -115,7 +117,7 @@ func servetemplate(w http.ResponseWriter, f string, d templatedata) { } func uploaderPut(w http.ResponseWriter, r *http.Request) { - // Max 15 Gb uploads + /* limit upload size */ if r.ContentLength > conf.maxsize { w.WriteHeader(http.StatusRequestEntityTooLarge) w.Write([]byte("File is too big")) @@ -134,10 +136,57 @@ func uploaderPut(w http.ResponseWriter, r *http.Request) { return } - resp := conf.baseuri + conf.filectx + filepath.Base(tmp.Name()) + "\r\n" + resp := conf.baseuri + conf.filectx + filepath.Base(tmp.Name()) w.Write([]byte(resp)) } +func uploaderPost(w http.ResponseWriter, r *http.Request) { + /* read 32Mb at a time */ + r.ParseMultipartForm(32 << 20) + + links := []string{} + for _, h := range r.MultipartForm.File["uck"] { + if h.Size > conf.maxsize { + w.WriteHeader(http.StatusRequestEntityTooLarge) + w.Write([]byte("File is too big")) + return + } + + post, err := h.Open() + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + return + } + defer post.Close() + + tmp, _ := ioutil.TempFile(conf.filepath, "*"+path.Ext(h.Filename)) + f, err := os.Create(tmp.Name()) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + return + } + defer f.Close() + + if writefile(f, post, h.Size) < 0 { + w.WriteHeader(http.StatusInternalServerError) + return + } + + link := conf.baseuri + conf.filectx + filepath.Base(tmp.Name()) + links = append(links, link) + } + + if (r.PostFormValue("output") == "html") { + data := templatedata{ Links: links } + servetemplate(w, "/upload.html", data) + return + } else { + for _, link := range links { + w.Write([]byte(link + "\r\n")) + } + } +} + func uploaderGet(w http.ResponseWriter, r *http.Request) { // r.URL.Path is sanitized regarding "." and ".." filename := r.URL.Path @@ -160,6 +209,8 @@ func uploaderGet(w http.ResponseWriter, r *http.Request) { func uploader(w http.ResponseWriter, r *http.Request) { switch r.Method { + case "POST": + uploaderPost(w, r) case "PUT": uploaderPut(w, r) case "GET": diff --git a/templates/index.html b/templates/index.html @@ -4,7 +4,6 @@ <meta charset="utf-8"> <meta name="author" content="z3bra"> <meta name="viewport" content="width=device-width"> - <link rel="icon" type="image/png" href="/favicon.png" /> <title>Partage</title> </head> <body> @@ -13,9 +12,11 @@ Use the box below to upload and share files. File size is limited to {{.Maxsize}}. </p> - <form> + <form enctype="multipart/form-data" method="post"> <label for="uck">Select file(s) to share</label> - <input id="uck" type="file" multiple=true/> + <input id="uck" name="uck" type="file" multiple/> + <input id="output" name="output" type="hidden" value='html' /> <input type="submit" value="Upload!"/> </form> </body> +</html> diff --git a/templates/upload.html b/templates/upload.html @@ -0,0 +1,21 @@ +<!DOCTYPE HTML> +<html> +<head> + <meta charset="utf-8"> + <meta name="author" content="z3bra"> + <meta name="viewport" content="width=device-width"> + <title>Partage</title> +</head> +<body> + <h1>File uploaded!</h1> + <p> + Your files have been uploaded. Access and download it from the + following links: + </p> + <ul> + {{range .Links}} + <li><a href="{{.}}">{{.}}</a></li> + {{end}} + </ul> +</body> +</html>