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
All checks were successful
gitea/video-server-backend/pipeline/head This commit looks good
This commit is contained in:
parent
298624c673
commit
5cefad8df3
@ -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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user