From 6ec37b88b5691e0c80d666a237f35552a4e72e12 Mon Sep 17 00:00:00 2001 From: Emile Date: Fri, 24 Jan 2020 23:15:16 +0100 Subject: done --- src/main.go | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 src/main.go (limited to 'src/main.go') diff --git a/src/main.go b/src/main.go new file mode 100644 index 0000000..1b021c5 --- /dev/null +++ b/src/main.go @@ -0,0 +1,78 @@ +package main + +import ( + "fmt" + "github.com/gorilla/mux" + "io/ioutil" + "log" + "net/http" + "os/exec" + "time" +) + +var ( + fileindex int +) + +func main() { + r := mux.NewRouter() + + r.HandleFunc("/", indexGET).Methods("GET") + r.HandleFunc("/", indexPOST).Methods("POST") + + log.Fatal(http.ListenAndServe(":8080", r)) +} + +func indexGET(w http.ResponseWriter, r *http.Request) { + log.Println("GET") + site := ` + + + +
+ gify url:
+
+
+ +` + fmt.Fprintf(w, "%s", site) +} + +func indexPOST(w http.ResponseWriter, r *http.Request) { + log.Println("recievend gif, downloading...") + url := r.PostFormValue("url") + + resp, err := http.Get(url) + defer resp.Body.Close() + if err != nil { + log.Printf("Could not makr http request to %s", url) + } + + // read the response content + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + log.Println("Could not parse the body") + } + + // write the content to a file + ioutil.WriteFile(fmt.Sprintf("gifs/%d", fileindex), body, 0644) + bodyText := string(body) + + log.Println("Response length: ", len(bodyText)) + + // Convert to rune slice to take substring. + runeSlice := []rune(bodyText) + fmt.Println("First chars: ", string(runeSlice[0:10])) + + cmd := exec.Command("killall", "mpv") + cmd.Start() + + time.Sleep(1 * time.Second) + + cmd = exec.Command("mpv", "--loop-file=inf", "--fs", fmt.Sprintf("gifs/%d", fileindex)) + cmd.Start() + + fileindex++ + + http.Redirect(w, r, "/", 302) +} -- cgit 1.4.1