From 598fd51412384e9bece03bbca19389b1217e3bed Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Mon, 11 Sep 2023 02:46:14 +0300 Subject: [PATCH] feat: Working download with mp3-convx --- main.go | 70 +++++++++++++++++++++++++++++++-- templates/download-result.html | 1 + templates/download.html | 30 ++++++++++++++ templates/index-pico.html | 4 +- {css => templates}/pico.min.css | 0 5 files changed, 98 insertions(+), 7 deletions(-) create mode 100644 templates/download-result.html create mode 100644 templates/download.html rename {css => templates}/pico.min.css (100%) diff --git a/main.go b/main.go index f23a6e6..04a93c5 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,8 @@ package main import ( "encoding/json" "fmt" + "html/template" + "io" "log" "net/http" "time" @@ -13,6 +15,12 @@ const ( DEFAULT_HTTPS_PORT = "4433" ) +type DownloadFormats struct { + VideoRes string + videoOnly bool + audioOnly bool +} + type apiMessageResponse struct { Message string } @@ -53,6 +61,7 @@ func NewLogger(handler http.Handler) *Logger { func writeJSONResponse(w http.ResponseWriter, s string) http.ResponseWriter { w.WriteHeader(http.StatusBadRequest) + w.Header().Set("Content-Type", "application/json") jsonResp, err := json.Marshal(apiMessageResponse{Message: s}) if err != nil { w.WriteHeader(http.StatusInternalServerError) @@ -75,18 +84,71 @@ func main() { handler.HandleFunc( "/download", func(w http.ResponseWriter, r *http.Request) { - userURL := r.URL.Query().Get("url") - w.Header().Set("Content-Type", "application/json") + if r.Method != "POST" { + w.WriteHeader(400) + return + } + err := r.ParseForm() + if err != nil { + w.WriteHeader(400) + return + } + userURL := r.FormValue("URL") if userURL == "" { w = writeJSONResponse(w, "Provide URL as query") return } - w = writeJSONResponse(w, fmt.Sprintf("You sent %s ", userURL))}, + // Get URL from convx + req, err := http.Get(fmt.Sprintf("http://localhost:80?url=%s", userURL)) + + if err != nil { + log.Println(err.Error()) + w.WriteHeader(500) + return + } + if req.StatusCode != 200 { + log.Printf("Got %v from convx\n", req) + w.WriteHeader(500) + return + } + body, err := io.ReadAll(req.Body) + if err != nil { + w.WriteHeader(500) + log.Printf("Error while reading convx response body. \n%v", err.Error()) + return + } + downloadURL := string(body) + log.Println("URL from convx", downloadURL) + + // Serve Button Template + tmpl := template.Must(template.ParseFiles("templates/download-result.html")) + err = tmpl.Execute(w, downloadURL) + if err != nil { + log.Println(err.Error()) + w.WriteHeader(500) + return + } + }, ) + handler.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + tmpl := template.Must(template.ParseFiles("templates/download.html")) + formats := []DownloadFormats{} + formats = append(formats, DownloadFormats{ + VideoRes: "720p", + audioOnly: false, + videoOnly: false, + }) + err := tmpl.Execute(w, formats) + if err != nil { + log.Println(err.Error()) + w.WriteHeader(500) + return + } + }) wrappedHandler := NewLogger(handler) srv := http.Server{ - ReadTimeout: 3 * time.Second, + ReadTimeout: 10 * time.Second, WriteTimeout: 10 * time.Second, Addr: ":" + DEFAULT_HTTP_PORT, Handler: wrappedHandler, diff --git a/templates/download-result.html b/templates/download-result.html new file mode 100644 index 0000000..9fbcfc4 --- /dev/null +++ b/templates/download-result.html @@ -0,0 +1 @@ +Download diff --git a/templates/download.html b/templates/download.html new file mode 100644 index 0000000..1a4d45f --- /dev/null +++ b/templates/download.html @@ -0,0 +1,30 @@ + + + + + + + + YT Download + +
+
+ +
+ + + +
+ +
+ +
+ + diff --git a/templates/index-pico.html b/templates/index-pico.html index 08f7e87..e3473fe 100644 --- a/templates/index-pico.html +++ b/templates/index-pico.html @@ -12,7 +12,7 @@ - + @@ -85,8 +85,6 @@ - - diff --git a/css/pico.min.css b/templates/pico.min.css similarity index 100% rename from css/pico.min.css rename to templates/pico.min.css