From 5cefad8df344b7a80cbf20a5427b4f225ee27cd5 Mon Sep 17 00:00:00 2001 From: Jose134 Date: Sun, 11 Aug 2024 02:29:16 +0200 Subject: [PATCH] remove search tasks and use socket directly, only send mp4 files --- cmd/video_server_backend/main.go | 38 ++++++-------------------------- 1 file changed, 7 insertions(+), 31 deletions(-) diff --git a/cmd/video_server_backend/main.go b/cmd/video_server_backend/main.go index 21dbc21..8d245f1 100644 --- a/cmd/video_server_backend/main.go +++ b/cmd/video_server_backend/main.go @@ -7,11 +7,9 @@ import ( "net/http" "os" "path/filepath" - "slices" "strconv" "strings" - "github.com/google/uuid" "github.com/gorilla/websocket" ) @@ -23,8 +21,6 @@ var upgrader = websocket.Upgrader{ }, } -var tasks = []string{} - const VIDEOS_DIR = "./videos" type Video struct { @@ -35,32 +31,13 @@ func notFound(w http.ResponseWriter, r *http.Request) { http.Error(w, "Endpoint not found", http.StatusNotFound) } -func searchVideo(w http.ResponseWriter, r *http.Request) { +func connectSearchSocket(w http.ResponseWriter, r *http.Request) { query := r.URL.Query().Get("q") if query == "" { http.Error(w, "Query is required.", http.StatusBadRequest) return } - task := uuid.New().String() - tasks = append(tasks, task) - - w.Header().Set("Content-Type", "application/json") - w.WriteHeader(http.StatusOK) - w.Write([]byte(task)) - - fmt.Printf("Task %s created\n", task) -} - -func connectSearchSocket(w http.ResponseWriter, r *http.Request) { - taskId := r.URL.Query().Get("task") - if !slices.Contains(tasks, taskId) { - http.Error(w, "Task not found.", http.StatusNotFound) - return - } - - fmt.Printf("Task %s started\n", taskId) - conn, err := upgrader.Upgrade(w, r, nil) if err != nil { fmt.Println(err) @@ -81,7 +58,10 @@ func connectSearchSocket(w http.ResponseWriter, r *http.Request) { if err != nil { return err } - files = append(files, path) + + if strings.HasSuffix(path, ".mp4") { + files = append(files, path) + } return nil }) @@ -91,7 +71,7 @@ func connectSearchSocket(w http.ResponseWriter, r *http.Request) { } for _, file := range files { - if _, res := strings.CutSuffix(file, ".lvix"); !res { + if strings.Contains(file, query) { jsonMsg, err := json.Marshal(Video{Filename: file}) if err != nil { log.Println(err) @@ -103,9 +83,6 @@ func connectSearchSocket(w http.ResponseWriter, r *http.Request) { } } } - - tasks = slices.DeleteFunc(tasks, func(task string) bool { return task == taskId }) - fmt.Printf("Task %s finished\n", taskId) } func streamVideo(w http.ResponseWriter, r *http.Request) { @@ -188,8 +165,7 @@ func addCORSHeaders(next http.Handler) http.Handler { func createMux() *http.ServeMux { var mux *http.ServeMux = http.NewServeMux() mux.HandleFunc("/video", streamVideo) - mux.HandleFunc("/search", searchVideo) - mux.HandleFunc("/search_socket", connectSearchSocket) + mux.HandleFunc("/search", connectSearchSocket) mux.HandleFunc("/", notFound) return mux