Skip to content

Commit

Permalink
Refacto m3u
Browse files Browse the repository at this point in the history
Signed-off-by: Pierre-Emmanuel Jacquier <pierre-emmanuel.jacquier@epitech.eu>
  • Loading branch information
pierre-emmanuelJ committed Feb 27, 2019
1 parent 05de608 commit 44a9a75
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
21 changes: 14 additions & 7 deletions pkg/m3u/m3u.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import (
)

// Marshall m3u.playlist struct to m3u file
// And replace original track url by proxy url
func Marshall(p *m3u.Playlist, config *config.HostConfiguration) (string, error) {
func Marshall(p *m3u.Playlist) (string, error) {
result := "#EXTM3U\n"
for _, track := range p.Tracks {
result += "#EXTINF:"
Expand All @@ -22,19 +21,27 @@ func Marshall(p *m3u.Playlist, config *config.HostConfiguration) (string, error)
}
result += fmt.Sprintf("%s=%q ", track.Tags[i].Name, track.Tags[i].Value)
}
result += fmt.Sprintf("%s\n", track.Name)

result += fmt.Sprintf("%s\n%s\n", track.Name, track.URI)
}
return result, nil
}

// ReplaceURL replace original playlist url by proxy url
func ReplaceURL(playlist m3u.Playlist, config *config.HostConfiguration) (*m3u.Playlist, error) {
for i, track := range playlist.Tracks {
oriURL, err := url.Parse(track.URI)
if err != nil {
return "", err
return nil, err
}

destURL, err := url.Parse(fmt.Sprintf("http://%s:%d%s", config.Hostname, config.Port, oriURL.RequestURI()))
if err != nil {
return "", err
return nil, err
}

result += fmt.Sprintf("%s\n", destURL.String())
playlist.Tracks[i].URI = destURL.String()
}
return result, nil

return &playlist, nil
}
8 changes: 7 additions & 1 deletion pkg/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,13 @@ func (p *proxy) reverseProxy(c *gin.Context) {
}

func (p *proxy) getM3U(c *gin.Context) {
result, err := proxyM3U.Marshall(p.Playlist, p.HostConfig)
playlist, err := proxyM3U.ReplaceURL(*p.Playlist, p.HostConfig)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}

result, err := proxyM3U.Marshall(playlist)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
Expand Down

0 comments on commit 44a9a75

Please sign in to comment.