summary refs log tree commit diff
diff options
context:
space:
mode:
authorEmile <hanemile@protonmail.com>2020-01-24 23:15:16 +0100
committerEmile <hanemile@protonmail.com>2020-01-24 23:15:16 +0100
commit6ec37b88b5691e0c80d666a237f35552a4e72e12 (patch)
tree2ebf26c9d1fb47fa60a681204eed4dfcf7dc4816
-rw-r--r--src/.main.go.swpbin0 -> 12288 bytes
-rw-r--r--src/main.go78
2 files changed, 78 insertions, 0 deletions
diff --git a/src/.main.go.swp b/src/.main.go.swp
new file mode 100644
index 0000000..a07d72c
--- /dev/null
+++ b/src/.main.go.swp
Binary files differdiff --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 := `<html>
+  <head>
+  </head>
+  <body>
+	 <form action="/" method="POST">
+      gify url:<br>
+      <input type="text" name="url"><br>
+    </form> 	
+  </body>
+</html>`
+	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)
+}