fixup! Fix and add HLS support for Xtream provider service

Signed-off-by: Pierre-Emmanuel Jacquier <pierre-emmanuel.jacquier@exoscale.ch>
This commit is contained in:
Pierre-Emmanuel Jacquier
2019-11-10 21:26:40 +00:00
parent d39796a976
commit 020e718c66

View File

@@ -2,6 +2,7 @@ package routes
import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
@@ -123,6 +124,28 @@ func (p *proxy) reverseProxy(c *gin.Context) {
}
func (p *proxy) stream(c *gin.Context, oriURL *url.URL) {
id := c.Param("id")
if strings.HasSuffix(id, ".m3u8") {
p.hlsStream(c, oriURL)
return
}
resp, err := http.Get(oriURL.String())
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
defer resp.Body.Close()
copyHTTPHeader(c, resp.Header)
c.Status(resp.StatusCode)
c.Stream(func(w io.Writer) bool {
io.Copy(w, resp.Body)
return false
})
}
func (p *proxy) hlsStream(c *gin.Context, oriURL *url.URL) {
client := &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
@@ -165,14 +188,11 @@ func (p *proxy) stream(c *gin.Context, oriURL *url.URL) {
c.Data(http.StatusOK, hlsResp.Header.Get("Content-Type"), []byte(body))
return
}
c.AbortWithError(http.StatusInternalServerError, errors.New("Unable to HLS stream"))
return
}
copyHTTPHeader(c, resp.Header)
c.Status(resp.StatusCode)
c.Stream(func(w io.Writer) bool {
io.Copy(w, resp.Body)
return false
})
}
func copyHTTPHeader(c *gin.Context, header http.Header) {