feat: basic active url validation:

- Does not check if the link is actually
	a video. Change isValidURL() for better
	validation

Signed-off-by: HeshamTB <hishaminv@gmail.com>
This commit is contained in:
HeshamTB 2023-09-13 13:13:13 +03:00
parent 72e6543415
commit b88ca836ef
3 changed files with 55 additions and 7 deletions

30
main.go
View File

@ -20,6 +20,11 @@ type DownloadFormats struct {
audioOnly bool audioOnly bool
} }
type URLValidationCtx struct {
URL string
Valid bool
}
type apiMessageResponse struct { type apiMessageResponse struct {
Message string Message string
} }
@ -169,6 +174,7 @@ func main() {
} }
}, },
) )
handler.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { handler.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
ctx := NewContext(r) ctx := NewContext(r)
formats := []DownloadFormats{} 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) wrappedHandler := NewLogger(handler)
srv := http.Server{ srv := http.Server{
ReadTimeout: 10 * time.Second, ReadTimeout: 10 * time.Second,

View File

@ -13,14 +13,21 @@
<main class="container"> <main class="container">
<h1>Viddle your fiddle</h1> <h1>Viddle your fiddle</h1>
<form hx-post="/download" hx-target="#get-btn" hx-swap="outerHTML" hx-indicator="#progress"> <form hx-post="/download" hx-target="#get-btn" hx-swap="outerHTML" hx-indicator="#progress">
<label for="URL">Link</label>
<div class="grid"> <div class="grid">
<input type="url" id="URL" name="URL" placeholder="Video Link" required> <div hx-target="this" hx-swap="outerHTML">
<!-- Select --> <label>Link
<input
type="url" id="URL" name="URL"
hx-post="/valid-link" hx-indicator="#url-validation-progress" required>
</label>
<progress class="htmx-indicator" id="url-validation-progress"></progress>
</div>
<label>Format
<select required> <select required>
<option value="" disabled selected>Format</option> <option value="" disabled selected>Format</option>
{{ range .Formats }} <option>{{ .VideoRes }}</option>{{ end }} {{ range .Formats }} <option>{{ .VideoRes }}</option>{{ end }}
</select> </select>
</label>
</div> </div>
<button id="get-btn">Get <button id="get-btn">Get
<!-- <a class="htmx-indicator" href="#" aria-busy="true"></a> --> <!-- <a class="htmx-indicator" href="#" aria-busy="true"></a> -->

View File

@ -0,0 +1,11 @@
<div hx-target="this" hx-swap="outerHTML">
<label>
Link
<input name="URL" id="URL" type="url" hx-post="/valid-link"
aria-invalid="{{ if .Valid }}false{{ else }}true{{ end }}"
required value="{{ .URL }}"
hx-indicator="#url-validation-progress"
>
</label>
<progress class="htmx-indicator" id="url-validation-progress"></progress>
</div>