From b88ca836efac9c46ebfa7a7b280311dabd2c3beb Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Wed, 13 Sep 2023 13:13:13 +0300 Subject: [PATCH] feat: basic active url validation: - Does not check if the link is actually a video. Change isValidURL() for better validation Signed-off-by: HeshamTB --- main.go | 30 ++++++++++++++++++++++++++++++ templates/download.html | 21 ++++++++++++++------- templates/url-validation.html | 11 +++++++++++ 3 files changed, 55 insertions(+), 7 deletions(-) create mode 100644 templates/url-validation.html diff --git a/main.go b/main.go index 0b7c707..05bcdc7 100644 --- a/main.go +++ b/main.go @@ -20,6 +20,11 @@ type DownloadFormats struct { audioOnly bool } +type URLValidationCtx struct { + URL string + Valid bool +} + type apiMessageResponse struct { Message string } @@ -169,6 +174,7 @@ func main() { } }, ) + handler.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { ctx := NewContext(r) formats := []DownloadFormats{} @@ -185,6 +191,30 @@ func main() { } }) + handler.HandleFunc("/valid-link", func(w http.ResponseWriter, r *http.Request) { + + if r.Method != "POST" { + w.WriteHeader(400) + return + } + + err := r.ParseForm() + if err != nil { + log.Println(err.Error()) + w.WriteHeader(400) + return + } + + url := r.FormValue("URL") + + ctx := URLValidationCtx{ + URL: url, + Valid: isValidURL(url), + } + + templates.ExecuteTemplate(w, "url-validation.html", ctx) + }) + wrappedHandler := NewLogger(handler) srv := http.Server{ ReadTimeout: 10 * time.Second, diff --git a/templates/download.html b/templates/download.html index b27bc81..f5a88ba 100644 --- a/templates/download.html +++ b/templates/download.html @@ -13,14 +13,21 @@

Viddle your fiddle

-
- - - +
+ + +
+