diff options
| author | horus_arch | 2015-05-12 20:57:04 +0200 |
|---|---|---|
| committer | horus_arch | 2015-05-12 20:57:04 +0200 |
| commit | 2e8e63e4a719a2b3adeeed2f1f54a8f3388ac2e4 (patch) | |
| tree | 2ca8ba44cab2a662cccee50f0a8a616dde119027 | |
| parent | 5986015358e5b7cf523122bbdb6831dccf7ca306 (diff) | |
| download | uhttpd-2e8e63e4a719a2b3adeeed2f1f54a8f3388ac2e4.tar.gz | |
Add upload form.
| -rw-r--r-- | main.go | 45 | ||||
| -rw-r--r-- | template.go | 58 | ||||
| -rw-r--r-- | upload.go | 110 |
3 files changed, 163 insertions, 50 deletions
@@ -3,11 +3,9 @@ package main import ( "flag" "fmt" - "io" "log" "net/http" "os" - "strings" ) func accessLog(h http.Handler, quiet bool) http.Handler { @@ -21,49 +19,6 @@ func accessLog(h http.Handler, quiet bool) http.Handler { return http.HandlerFunc(fn) } -func uploadHandler(dir string, quiet bool) http.Handler { - fn := func(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Server", "uhttpd") - file, header, err := r.FormFile("file") - - if err != nil { - w.WriteHeader(500) - w.Write([]byte(err.Error())) - log.Println("ERROR", err.Error()) - return - } - - defer file.Close() - - if !strings.HasSuffix(dir, "/") { - dir = dir + "/" - } - - out, err := os.Create(dir + header.Filename) - if err != nil { - w.WriteHeader(500) - w.Write([]byte(err.Error())) - log.Println("ERROR", err.Error()) - return - } - - defer out.Close() - _, err = io.Copy(out, file) - if err != nil { - w.WriteHeader(500) - w.Write([]byte(err.Error())) - log.Println("ERROR", err.Error()) - return - } - - if !quiet { - log.Println(r.Method, r.URL.Path, header.Filename, r.RemoteAddr) - } - w.Write([]byte("Uploaded " + header.Filename)) - } - return http.HandlerFunc(fn) -} - func main() { ip_f := flag.String("ip", "0.0.0.0", "IP adress to listen on.") port_f := flag.String("port", "3000", "Port to listen on.") diff --git a/template.go b/template.go index 017690d..124854c 100644 --- a/template.go +++ b/template.go @@ -103,10 +103,6 @@ func execTemplate(w http.ResponseWriter, r *http.Request, data string) { } -func NewTemplateHandler(handler http.Handler, allowedHost string) *TemplateHandler { - return &TemplateHandler{handler: handler} -} - func getTemplate() string { return `<!doctype html> <html> @@ -164,7 +160,59 @@ func get404() string { <p class="section-description"> Please check for typos in your url. </p> <p class="section-description"> - <a href="/" class="button button-primary">Go back to root</a></p> + <a href="/" class="button button-primary">Go back to root</a> + </p> + </div> +</div> +</body> +</html>` +} + +func getUpload() string { + return `<!doctype html> +<html> +<head> + <meta charset="utf-8"> + <title>Upload Form</title> + <meta name="viewport" content="width=device-width, initial-scale=1"> +<style>/*! normalize.css v3.0.2 | MIT License | git.io/normalize */html{font-family:sans-serif;-ms-text-size-adjust:100%;-webkit-text-size-adjust:100%}body{margin:0}input{color:inherit;font:inherit;margin:0}input[type=submit]{-webkit-appearance:button}button::-moz-focus-inner,input::-moz-focus-inner{border:0;padding:0}input{line-height:normal}.container{position:relative;width:100%;max-width:960px;margin:0 auto;padding:0 20px;box-sizing:border-box}@media (min-width:400px){.container{width:85%;padding:0}}@media (min-width:550px){.container{width:80%}}html{font-size:62.5%}body{font-size:1.5em;line-height:1.6;font-weight:400;font-family:Raleway,HelveticaNeue,"Helvetica Neue",Helvetica,Arial,sans-serif;color:#222}h3{margin-top:0;margin-bottom:2rem;font-weight:300;font-size:3rem;line-height:1.3;letter-spacing:-.1rem}@media (min-width:550px){h3{font-size:3.6rem}}p{margin-top:0}input[type=submit]{display:inline-block;height:38px;padding:0 30px;color:#555;text-align:center;font-size:11px;font-weight:600;line-height:38px;letter-spacing:.1rem;text-transform:uppercase;text-decoration:none;white-space:nowrap;background-color:transparent;border-radius:4px;border:1px solid #bbb;cursor:pointer;box-sizing:border-box}input[type=submit]:focus,input[type=submit]:hover{color:#333;border-color:#888;outline:0}input[type=submit].button-primary{color:#FFF;background-color:#33C3F0;border-color:#33C3F0}input[type=submit].button-primary:focus,input[type=submit].button-primary:hover{color:#FFF;background-color:#1EAEDB;border-color:#1EAEDB}input{margin-bottom:1.5rem}form,p{margin-bottom:2.5rem}.container:after{content:"";display:table;clear:both}.section{padding:8rem 0 7rem;text-align:center}.section-description,.section-heading{margin-bottom:1.2rem}.section-description{max-width:60%;margin-left:auto;margin-right:auto}</style> +</head> +<body> +<div class="section"> + <div class="container"> + <h3 class="section-heading">Upload Form</h3> + <form class="section-description" method="POST" action="/upload?html=1" enctype="multipart/form-data"> + <p class="section-description" for="file">You are going to upload a file. Here you can search to add it.</p> + <input type="file" value="Search for your file" id="file" name="file"> + <br> + <input class="button button-primary" type="submit" value="Submit"> + </form> + </div> +</div> +</body> +</html> +` +} + +func getUploaded() string { + return `<!doctype html> +<html> +<head> + <meta charset="utf-8"> + <title>Uploaded {{.File}}</title> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <style>/*! normalize.css v3.0.2 | MIT License | git.io/normalize */html{font-family:sans-serif;-ms-text-size-adjust:100%;-webkit-text-size-adjust:100%}body{margin:0}a{background-color:transparent}a:active,a:hover{outline:0}button::-moz-focus-inner,input::-moz-focus-inner{border:0;padding:0}.container{position:relative;width:100%;max-width:960px;margin:0 auto;padding:0 20px;box-sizing:border-box}@media (min-width:400px){.container{width:85%;padding:0}}@media (min-width:550px){.container{width:80%}}html{font-size:62.5%}body{font-size:1.5em;line-height:1.6;font-weight:400;font-family:Raleway,HelveticaNeue,"Helvetica Neue",Helvetica,Arial,sans-serif;color:#222}h3{margin-top:0;margin-bottom:2rem;font-weight:300;font-size:3rem;line-height:1.3;letter-spacing:-.1rem}@media (min-width:550px){h3{font-size:3.6rem}}p{margin-top:0}a{color:#1EAEDB}a:hover{color:#0FA0CE}.button{display:inline-block;height:38px;padding:0 30px;color:#555;text-align:center;font-size:11px;font-weight:600;line-height:38px;letter-spacing:.1rem;text-transform:uppercase;text-decoration:none;white-space:nowrap;background-color:transparent;border-radius:4px;border:1px solid #bbb;cursor:pointer;box-sizing:border-box}.button:focus,.button:hover{color:#333;border-color:#888;outline:0}.button.button-primary{color:#FFF;background-color:#33C3F0;border-color:#33C3F0}.button.button-primary:focus,.button.button-primary:hover{color:#FFF;background-color:#1EAEDB;border-color:#1EAEDB}.button{margin-bottom:1rem}p{margin-bottom:2.5rem}.container:after{content:"";display:table;clear:both}.section{padding:8rem 0 8rem;text-align:center}.section-description,.section-heading{margin-bottom:1.2rem}</style> +</head> +<body> +<div class="section"> + <div class="container"> + <h3 class="section-heading">Uploaded!</h3> + <p class="section-description"> + <strong>{{.File}}</strong> was successfull uploaded. + </p> + <p class="section-description"> + <a href="/" class="button button-primary">Back to root</a> + </p> </div> </div> </body> diff --git a/upload.go b/upload.go new file mode 100644 index 0000000..6e0ce56 --- /dev/null +++ b/upload.go @@ -0,0 +1,110 @@ +package main + +import ( + "html/template" + "io" + "log" + "net/http" + "os" + "strings" +) + +func uploadHandler(dir string, quiet bool) http.Handler { + fn := func(w http.ResponseWriter, r *http.Request) { + + // uhttpd strong + w.Header().Set("Server", "uhttpd") + + // we are handling the upload + if r.Method == "POST" { + + // we need the input file named "file" + file, header, err := r.FormFile("file") + if err != nil { + w.WriteHeader(500) + w.Write([]byte(err.Error())) + log.Println("ERROR", err.Error()) + return + } + + defer file.Close() + + if !strings.HasSuffix(dir, "/") { + dir = dir + "/" + } + + // lets create the desired file + out, err := os.Create(dir + header.Filename) + if err != nil { + w.WriteHeader(500) + w.Write([]byte(err.Error())) + log.Println("ERROR", err.Error()) + return + } + + defer out.Close() + + // copy the old file in the created one + _, err = io.Copy(out, file) + if err != nil { + w.WriteHeader(500) + w.Write([]byte(err.Error())) + log.Println("ERROR", err.Error()) + return + } + + // do not log upload + if !quiet { + log.Println(r.Method, r.URL.Path, header.Filename, r.RemoteAddr) + } + + // print for cli client + if r.URL.Query().Get("html") != "1" { + w.Write([]byte("Uploaded: " + header.Filename)) + return + } + + // print for browser + w.Header().Set("Content-Type", "text/html") + tmpl := template.New("uploaded") + tmpl, err = tmpl.Parse(getUploaded()) + if err != nil { + log.Println(err.Error()) + w.WriteHeader(500) + w.Write([]byte(err.Error())) + return + } + + err = tmpl.Execute(w, struct{ File string }{File: header.Filename}) + if err != nil { + log.Println(err.Error()) + w.WriteHeader(500) + w.Write([]byte(err.Error())) + return + } + return + } else { + // we are just printing the upload form + + w.Header().Set("Content-Type", "text/html") + tmpl := template.New("upload") + tmpl, err := tmpl.Parse(getUpload()) + if err != nil { + log.Println(err.Error()) + w.WriteHeader(500) + w.Write([]byte(err.Error())) + return + } + + err = tmpl.Execute(w, nil) + if err != nil { + log.Println(err.Error()) + w.WriteHeader(500) + w.Write([]byte(err.Error())) + return + } + return + } + } + return http.HandlerFunc(fn) +} |
