all repos — gopipe @ 8ec2c473fa23fd6f7418f00fad24c8c800d0ba87

Embed YouTube videos on Telegram, Discord and more!

set up redirects
Marco Andronaco andronacomarco@gmail.com
Fri, 18 Oct 2024 16:39:57 +0200
commit

8ec2c473fa23fd6f7418f00fad24c8c800d0ba87

parent

2c659855b1c3bb20d334660e1f0c2aee94652db3

2 files changed, 19 insertions(+), 3 deletions(-)

jump to
M src/app/handlers.gosrc/app/handlers.go

@@ -55,7 +55,7 @@ return

} } -func videoHandler(w http.ResponseWriter, r *http.Request) { +func redirectHandler(w http.ResponseWriter, r *http.Request) { videoID := r.URL.Query().Get("v") if videoID == "" { videoID = r.PathValue("videoID")

@@ -63,6 +63,22 @@ if videoID == "" {

http.Error(w, "Missing video ID", http.StatusBadRequest) return } + } + + if !videoRegex.MatchString(videoID) { + log.Println("Invalid video ID:", videoID) + http.Error(w, err404, http.StatusNotFound) + return + } + + http.Redirect(w, r, "/"+videoID, http.StatusFound) +} + +func videoHandler(w http.ResponseWriter, r *http.Request) { + videoID := r.PathValue("videoID") + if videoID == "" { + http.Error(w, "Missing video ID", http.StatusBadRequest) + return } if !videoRegex.MatchString(videoID) {
M src/app/main.gosrc/app/main.go

@@ -56,8 +56,8 @@ }

r.HandleFunc("GET /", indexHandler) - r.HandleFunc("GET /watch", videoHandler) - r.HandleFunc("GET /shorts/{videoID}", videoHandler) + r.HandleFunc("GET /watch", redirectHandler) + r.HandleFunc("GET /shorts/{videoID}", redirectHandler) r.HandleFunc("GET /{videoID}", videoHandler) r.HandleFunc("GET /{videoID}/{formatID}", videoHandler)