From 6b1bc73f71b44221236552ec005fab02515e30a8 Mon Sep 17 00:00:00 2001 From: chiraglulla Date: Thu, 30 May 2024 00:06:28 +0530 Subject: [PATCH] bug: handle 404 for manifest, ts files, improve unhandled routes handling and cleanup --- .vscode/settings.json | 3 +++ controllers/video.go | 38 ++++++++++++++++++++++++++------------ main.go | 8 ++++++-- utils/util.go | 3 --- 4 files changed, 35 insertions(+), 17 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..52a9888 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "vue.features.codeActions.enable": false +} \ No newline at end of file diff --git a/controllers/video.go b/controllers/video.go index 0591d36..ec1a7d2 100644 --- a/controllers/video.go +++ b/controllers/video.go @@ -68,7 +68,7 @@ func UploadVideo(w http.ResponseWriter, r *http.Request, db *sql.DB) { if err != nil { log.Println(err) - http.Error(w, "Error Processing File", http.StatusInternalServerError) + http.Error(w, "Error processing file", http.StatusInternalServerError) return } else { log.Println("Database record created.") @@ -84,14 +84,14 @@ func UploadVideo(w http.ResponseWriter, r *http.Request, db *sql.DB) { tmpFile, err = os.Create("./video/" + serverFileName) if err != nil { log.Println("Error creating a temp file on the server:", err) - http.Error(w, "Error Processing File", http.StatusInternalServerError) + http.Error(w, "Error processing file", http.StatusInternalServerError) return } } else { tmpFile, err = os.OpenFile("./video/"+serverFileName, os.O_APPEND|os.O_WRONLY, os.ModeAppend) if err != nil { log.Println("Error opening the temp file on the server for appending chunks:", err) - http.Error(w, "Error Processing File", http.StatusInternalServerError) + http.Error(w, "Error processing file", http.StatusInternalServerError) return } } @@ -100,7 +100,7 @@ func UploadVideo(w http.ResponseWriter, r *http.Request, db *sql.DB) { if err != nil { log.Println("Error appending chunks to file:", err) - http.Error(w, "Error Processing File", http.StatusInternalServerError) + http.Error(w, "Error processing file", http.StatusInternalServerError) return } @@ -108,7 +108,7 @@ func UploadVideo(w http.ResponseWriter, r *http.Request, db *sql.DB) { if err != nil { log.Println("Error getting file info:", err) - http.Error(w, "Error Processing File", http.StatusInternalServerError) + http.Error(w, "Error processing file", http.StatusInternalServerError) return } @@ -170,7 +170,7 @@ func GetVideos(w http.ResponseWriter, r *http.Request, db *sql.DB) { if err != nil { log.Println("Error scanning rows") - http.Error(w, "Error Retreiving Records", http.StatusInternalServerError) + http.Error(w, "Error retreiving records", http.StatusInternalServerError) return } @@ -187,7 +187,7 @@ func GetVideos(w http.ResponseWriter, r *http.Request, db *sql.DB) { if err != nil { log.Println(err) - http.Error(w, "Error Retreiving Records", http.StatusInternalServerError) + http.Error(w, "Error retreiving records", http.StatusInternalServerError) } else { w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) @@ -209,21 +209,25 @@ func GetVideo(w http.ResponseWriter, r *http.Request, db *sql.DB) { if err != nil { log.Println(err) - http.Error(w, "Error Retreiving Record", http.StatusInternalServerError) + http.Error(w, "Error retreiving record", http.StatusInternalServerError) } defer detailsQuery.Close() var title, description string err = detailsQuery.QueryRow(video_id).Scan(&title, &description) + if err != nil { + if err == sql.ErrNoRows { + log.Println(err) + http.Error(w, "Video record not found", http.StatusNotFound) + return + } log.Println(err) - http.Error(w, "Error Retreiving Record", http.StatusInternalServerError) + http.Error(w, "Error retreiving record", http.StatusInternalServerError) return } - log.Println("Video ID: " + video_id) - log.Println("Title: " + title) - log.Println("Description: " + description) + videoDetails := &Video{ ID: video_id, Title: title, @@ -273,6 +277,11 @@ func GetManifestFile(w http.ResponseWriter, r *http.Request, db *sql.DB) { defer response.Body.Close() + if response.StatusCode == 404 { + http.Error(w, "Video record not found", http.StatusNotFound) + return + } + bodyBytes, err := io.ReadAll(response.Body) if err != nil { @@ -321,6 +330,11 @@ func GetTSFiles(w http.ResponseWriter, r *http.Request, db *sql.DB) { } defer response.Body.Close() + if response.StatusCode == 404 { + http.Error(w, "Video chunk not found", http.StatusNotFound) + return + } + bodyBytes, err := io.ReadAll(response.Body) if err != nil { diff --git a/main.go b/main.go index b84accc..8b4eeb3 100644 --- a/main.go +++ b/main.go @@ -1,9 +1,9 @@ package main import ( - "html" - "fmt" "database/sql" + "fmt" + "html" "log" "net/http" "regexp" @@ -30,6 +30,7 @@ func videoHandler(w http.ResponseWriter, r *http.Request, db *sql.DB) { log.Println("GET: " + path) if path == "/video/" { + log.Println("Get all video details") controllers.GetVideos(w, r, db) } else if matched, err := regexp.MatchString("^/video/[a-zA-B0-9-]+/?$", path); err == nil && matched { log.Println("Get video details") @@ -42,6 +43,9 @@ func videoHandler(w http.ResponseWriter, r *http.Request, db *sql.DB) { log.Print("Segment Request:") log.Println(regexp.MatchString("^/video/[a-zA-B0-9-]+/stream/[a-zA-B0-9_-]+.ts/?$", path)) controllers.GetTSFiles(w, r, db) + } else { + response := fmt.Sprintf("Error: handler for %s not found", html.EscapeString(r.URL.Path)) + http.Error(w, response, http.StatusNotFound) } } } diff --git a/utils/util.go b/utils/util.go index 4774cce..c20a244 100644 --- a/utils/util.go +++ b/utils/util.go @@ -97,9 +97,6 @@ func UploadToAppwrite(folderName string, db *sql.DB) { log.Println("Now uploading chunks of " + folderName + " to Appwrite Storage...") var count int = -1 for idx, file := range files { - x, err:= os.Stat(fmt.Sprintf("segments/%s/%s", folderName, file.Name())) - log.Println(x.Size(), err) - log.Println(x.Name()) fileToUpload, err := os.ReadFile(fmt.Sprintf("segments/%s/%s", folderName, file.Name())) if err != nil {