Skip to content

Commit

Permalink
allow pull for video files
Browse files Browse the repository at this point in the history
  • Loading branch information
TimIsOverpowered committed Jul 30, 2024
1 parent db22a7e commit 47a3934
Showing 1 changed file with 56 additions and 54 deletions.
110 changes: 56 additions & 54 deletions server/gin.go
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 47a3934

Please sign in to comment.