remove search tasks and use socket directly, only send mp4 files
All checks were successful
gitea/video-server-backend/pipeline/head This commit looks good

This commit is contained in:
Jose134 2024-08-11 02:29:16 +02:00
parent 298624c673
commit 5cefad8df3

View File

@ -7,11 +7,9 @@ import (
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
"slices"
"strconv" "strconv"
"strings" "strings"
"github.com/google/uuid"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
) )
@ -23,8 +21,6 @@ var upgrader = websocket.Upgrader{
}, },
} }
var tasks = []string{}
const VIDEOS_DIR = "./videos" const VIDEOS_DIR = "./videos"
type Video struct { type Video struct {
@ -35,32 +31,13 @@ func notFound(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Endpoint not found", http.StatusNotFound) 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") query := r.URL.Query().Get("q")
if query == "" { if query == "" {
http.Error(w, "Query is required.", http.StatusBadRequest) http.Error(w, "Query is required.", http.StatusBadRequest)
return 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) conn, err := upgrader.Upgrade(w, r, nil)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
@ -81,7 +58,10 @@ func connectSearchSocket(w http.ResponseWriter, r *http.Request) {
if err != nil { if err != nil {
return err return err
} }
files = append(files, path)
if strings.HasSuffix(path, ".mp4") {
files = append(files, path)
}
return nil return nil
}) })
@ -91,7 +71,7 @@ func connectSearchSocket(w http.ResponseWriter, r *http.Request) {
} }
for _, file := range files { for _, file := range files {
if _, res := strings.CutSuffix(file, ".lvix"); !res { if strings.Contains(file, query) {
jsonMsg, err := json.Marshal(Video{Filename: file}) jsonMsg, err := json.Marshal(Video{Filename: file})
if err != nil { if err != nil {
log.Println(err) 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) { func streamVideo(w http.ResponseWriter, r *http.Request) {
@ -188,8 +165,7 @@ func addCORSHeaders(next http.Handler) http.Handler {
func createMux() *http.ServeMux { func createMux() *http.ServeMux {
var mux *http.ServeMux = http.NewServeMux() var mux *http.ServeMux = http.NewServeMux()
mux.HandleFunc("/video", streamVideo) mux.HandleFunc("/video", streamVideo)
mux.HandleFunc("/search", searchVideo) mux.HandleFunc("/search", connectSearchSocket)
mux.HandleFunc("/search_socket", connectSearchSocket)
mux.HandleFunc("/", notFound) mux.HandleFunc("/", notFound)
return mux return mux