snippets

More or less useful code snippets
Log | Files | Refs

commit c447e59f5704a3fde4a6d67635235b8afb7e3e42
parent 3370271083208975489d82da6cc01386879cf1ee
Author: Vetle Haflan <vetle@haflan.dev>
Date:   Fri, 15 Oct 2021 18:53:58 +0200

Add gimme.go

Diffstat:
Agimme.go | 70++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 70 insertions(+), 0 deletions(-)

diff --git a/gimme.go b/gimme.go @@ -0,0 +1,70 @@ +package main + +// gimme hosts a minimal page that lets you upload stuff from the web browser +// directly to the host computer + +import ( + "fmt" + "io" + "net/http" + "os" + "time" +) + +var index []byte = []byte(` +<!DOCTYPE html> +<html lang="en"> +<head> + <meta charset="UTF-8"> + <meta http-equiv="X-UA-Compatible" content="IE=edge"> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + <title>Del filer</title> +</head> +<body> + <input id="image-file" type="file" /> + <button onclick="upload()">Last opp</button> + <script type="text/javascript"> + function upload() { + let photo = document.getElementById("image-file").files[0]; + let formData = new FormData(); + formData.append("file", photo); + fetch('/upload', {method: "POST", body: formData}); + } + </script> +</body> +</html> +`) + +func handleSite(w http.ResponseWriter, r *http.Request) { + w.Write(index) +} + +func handleUpload(w http.ResponseWriter, r *http.Request) { + file, header, err := r.FormFile("file") + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + fmt.Println("Error:", err) + return + } + fmt.Println(header.Filename) + fileContents, err := io.ReadAll(file) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + fmt.Println("Error:", err) + return + } + fmt.Println(time.Now()) + fmt.Println(len(fileContents)) + os.WriteFile("/tmp/gimme/"+header.Filename, fileContents, 0644) + //body. + // fmt.Println(len(body)) +} + +func main() { + http.HandleFunc("/", handleSite) + http.HandleFunc("/upload", handleUpload) + if err := http.ListenAndServe(":8888", nil); err != nil { + fmt.Println(err) + os.Exit(1) + } +}