From 47a393422125f6cf017488b75ee521a35324f2cb Mon Sep 17 00:00:00 2001 From: TimIsOverpowered Date: Tue, 30 Jul 2024 09:04:44 -0500 Subject: [PATCH] allow pull for video files --- server/gin.go | 110 +++++++++++++++++++++++++------------------------- 1 file changed, 56 insertions(+), 54 deletions(-) diff --git a/server/gin.go b/server/gin.go index 55d5746..6de1964 100644 --- a/server/gin.go +++ b/server/gin.go @@ -1,14 +1,18 @@ package server import ( + "encoding/json" "errors" + "log" "regexp" "strings" "time" + "github.com/angelthump/cache-main/api" "github.com/angelthump/cache-main/client" utils "github.com/angelthump/cache-main/utils" "github.com/gin-gonic/gin" + "github.com/go-resty/resty/v2" ) func Initalize() { @@ -82,69 +86,67 @@ func Initalize() { return } - c.AbortWithStatus(404) + //c.AbortWithStatus(404) - /* - regex := regexp.MustCompile(`_src|_medium|_low`) - base64Channel := regex.ReplaceAllString(c.Param("channel"), "") + endPathRegex := regexp.MustCompile(`.ts|.mp4|.m4s`) + if !endPathRegex.MatchString(endUrl) { + c.AbortWithStatus(500) + } - data, err = client.Rdb.Get(client.Ctx, base64Channel).Result() - if err != nil { - c.AbortWithStatus(500) - return - } + regex := regexp.MustCompile(`_src|_medium|_low`) + base64Channel := regex.ReplaceAllString(c.Param("channel"), "") - var streamData api.Stream - err = json.Unmarshal([]byte(data), &streamData) - if err != nil { - log.Printf("Unmarshal Error %v", err) - c.AbortWithStatus(500) - return - } + data, err = client.Rdb.Get(client.Ctx, base64Channel).Result() + if err != nil { + c.AbortWithStatus(500) + return + } - if !streamData.Ingest.Mediamtx { - c.AbortWithStatus(404) - return - } + var streamData api.Stream + err = json.Unmarshal([]byte(data), &streamData) + if err != nil { + log.Printf("Unmarshal Error %v", err) + c.AbortWithStatus(500) + return + } - fullEndUrl := strings.Replace(c.Param("endUrl"), "index", "stream", 1) - query := c.Request.URL.Query().Encode() - if len(query) > 0 { - fullEndUrl += "?" + query - } - mediamtxEndUrl := "https://" + utils.Config.IngestAPI.Username + ":" + utils.Config.IngestAPI.Password + "@" + streamData.Ingest.Server + ".angelthump.com/hls/live/" + streamData.User.StreamKey + "/" + fullEndUrl + if !streamData.Ingest.Mediamtx { + c.AbortWithStatus(404) + return + } - restyClient := resty.New() - resp, _ := restyClient.R(). - SetHeader("X-Api-Key", utils.Config.StreamsAPI.AuthKey). - Get(mediamtxEndUrl) + fullEndUrl := strings.Replace(c.Param("endUrl"), "index", "stream", 1) + query := c.Request.URL.Query().Encode() + if len(query) > 0 { + fullEndUrl += "?" + query + } + mediamtxEndUrl := "https://" + utils.Config.IngestAPI.Username + ":" + utils.Config.IngestAPI.Password + "@" + streamData.Ingest.Server + ".angelthump.com/hls/live/" + streamData.User.StreamKey + "/" + fullEndUrl - statusCode := resp.StatusCode() - if statusCode >= 400 { - c.AbortWithStatus(statusCode) - return - } + restyClient := resty.New() + resp, _ := restyClient.R(). + SetHeader("X-Api-Key", utils.Config.StreamsAPI.AuthKey). + Get(mediamtxEndUrl) - c.Header("Access-Control-Allow-Origin", "*") + statusCode := resp.StatusCode() + if statusCode >= 400 { + c.AbortWithStatus(statusCode) + return + } - if strings.HasSuffix(endUrl, ".ts") { - client.Rdb.Set(client.Ctx, key, resp.Body(), 30*time.Second) - c.Data(200, "video/mp2t", []byte(resp.Body())) - } else if strings.HasSuffix(endUrl, "init.mp4") { - client.Rdb.Set(client.Ctx, key, resp.Body(), 24*time.Hour) - c.Data(200, "video/mp4", []byte(resp.Body())) - } else if strings.HasSuffix(endUrl, ".mp4") { - client.Rdb.Set(client.Ctx, key, resp.Body(), 30*time.Second) - c.Data(200, "video/mp4", []byte(resp.Body())) - } else if strings.HasSuffix(endUrl, ".m4s") { - client.Rdb.Set(client.Ctx, key, resp.Body(), 30*time.Second) - c.Data(200, "video/mp4", []byte(resp.Body())) - } else if strings.HasSuffix(endUrl, ".m3u8") { - client.Rdb.Set(client.Ctx, key, resp.Body(), 1*time.Second) - c.Data(200, "application/x-mpegURL", []byte(resp.Body())) - } else { - c.AbortWithStatus(400) - }*/ + c.Header("Access-Control-Allow-Origin", "*") + + if strings.HasSuffix(endUrl, ".ts") { + client.Rdb.Set(client.Ctx, key, resp.Body(), 30*time.Second) + c.Data(200, "video/mp2t", []byte(resp.Body())) + } else if strings.HasSuffix(endUrl, ".mp4") { + client.Rdb.Set(client.Ctx, key, resp.Body(), 30*time.Second) + c.Data(200, "video/mp4", []byte(resp.Body())) + } else if strings.HasSuffix(endUrl, ".m4s") { + client.Rdb.Set(client.Ctx, key, resp.Body(), 30*time.Second) + c.Data(200, "video/mp4", []byte(resp.Body())) + } else { + c.AbortWithStatus(400) + } }) router.Run(":" + utils.Config.Port)