Skip to content

Commit

Permalink
bug: handle 404 for manifest, ts files, improve unhandled routes hand…
Browse files Browse the repository at this point in the history
…ling and cleanup
  • Loading branch information
chiraglulla committed Jun 9, 2024
1 parent b55a0d3 commit 926b3b1
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 17 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"vue.features.codeActions.enable": false
}
37 changes: 25 additions & 12 deletions controllers/video.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand All @@ -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
}
}
Expand All @@ -100,15 +100,15 @@ 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
}

fileInfo, err := tmpFile.Stat()

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
}

Expand Down Expand Up @@ -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
}

Expand All @@ -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)
Expand All @@ -209,23 +209,26 @@ 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,
Expand Down Expand Up @@ -275,6 +278,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 {
Expand Down Expand Up @@ -323,6 +331,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 {
Expand Down
8 changes: 6 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package main

import (
"html"
"fmt"
"database/sql"
"fmt"
"html"
"log"
"net/http"
"regexp"
Expand All @@ -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")
Expand All @@ -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)
}
}
}
Expand Down
3 changes: 0 additions & 3 deletions utils/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 926b3b1

Please sign in to comment.