From 4382e11378c550d9a598c49d43ce38a7de88944a Mon Sep 17 00:00:00 2001 From: Pierre-Emmanuel Jacquier <15922119+pierre-emmanuelJ@users.noreply.github.com> Date: Fri, 21 Jan 2022 15:23:36 +0000 Subject: [PATCH] Bump m3u package to v.0.4.0 (fix panic) Signed-off-by: Pierre-Emmanuel Jacquier <15922119+pierre-emmanuelJ@users.noreply.github.com> --- go.mod | 2 +- go.sum | 4 +- vendor/github.com/jamesnetherton/m3u/m3u.go | 77 ++++++++++++++++----- vendor/modules.txt | 4 +- 4 files changed, 63 insertions(+), 24 deletions(-) diff --git a/go.mod b/go.mod index 5bf2cdd2..211642b0 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ require ( github.com/gin-contrib/cors v0.0.0-20190226021855-50921afdc5c1 github.com/gin-gonic/gin v1.7.4 github.com/inconshreveable/mousetrap v1.0.0 // indirect - github.com/jamesnetherton/m3u v0.1.1-0.20180924175816-16741c7f081c + github.com/jamesnetherton/m3u v0.4.0 github.com/mitchellh/go-homedir v1.1.0 github.com/satori/go.uuid v1.2.0 github.com/spf13/cobra v1.2.1 diff --git a/go.sum b/go.sum index a3624c09..8ed6524a 100644 --- a/go.sum +++ b/go.sum @@ -185,8 +185,8 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1: github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= -github.com/jamesnetherton/m3u v0.1.1-0.20180924175816-16741c7f081c h1:TlVVjzolJLEe3TDyTlIuCCTuApI5vxpaLihVC/Hrexk= -github.com/jamesnetherton/m3u v0.1.1-0.20180924175816-16741c7f081c/go.mod h1:YFaujUKOi5qfiqY3rjkB6ty3YWXBcys/6MuSOX5TrHs= +github.com/jamesnetherton/m3u v0.4.0 h1:whEon8Bj6AAtejE7lkhE83qKxyZR6xFkADGcerYDWlA= +github.com/jamesnetherton/m3u v0.4.0/go.mod h1:wiM6FnmepH4gnwvaeBvppddHiTHmy5u/hx7wPa140mA= github.com/json-iterator/go v1.1.5/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/json-iterator/go v1.1.11 h1:uVUAXhF2To8cbw/3xN3pxj6kk7TYKs98NIrTqPlMWAQ= diff --git a/vendor/github.com/jamesnetherton/m3u/m3u.go b/vendor/github.com/jamesnetherton/m3u/m3u.go index 14ee9d1f..1b30bf14 100644 --- a/vendor/github.com/jamesnetherton/m3u/m3u.go +++ b/vendor/github.com/jamesnetherton/m3u/m3u.go @@ -2,7 +2,9 @@ package m3u import ( "bufio" + "bytes" "errors" + "fmt" "io" "net/http" "os" @@ -18,7 +20,7 @@ type Playlist struct { // A Tag is a simple key/value pair type Tag struct { - Name string + Name string Value string } @@ -31,31 +33,36 @@ type Track struct { } // Parse parses an m3u playlist with the given file name and returns a Playlist -func Parse(fileName string) (playlist Playlist, err error) { +func Parse(fileName string) (Playlist, error) { var f io.ReadCloser - var data *http.Response + if strings.HasPrefix(fileName, "http://") || strings.HasPrefix(fileName, "https://") { - data, err = http.Get(fileName) + data, err := http.Get(fileName) + if err != nil { + return Playlist{}, + fmt.Errorf("unable to open playlist URL: %v", err) + } f = data.Body } else { - f, err = os.Open(fileName) - } - - if err != nil { - err = errors.New("Unable to open playlist file") - return + file, err := os.Open(fileName) + if err != nil { + return Playlist{}, + fmt.Errorf("unable to open playlist file: %v", err) + } + f = file } defer f.Close() onFirstLine := true scanner := bufio.NewScanner(f) tagsRegExp, _ := regexp.Compile("([a-zA-Z0-9-]+?)=\"([^\"]+)\"") - + playlist := Playlist{} + for scanner.Scan() { line := scanner.Text() if onFirstLine && !strings.HasPrefix(line, "#EXTM3U") { - err = errors.New("Invalid m3u file format. Expected #EXTM3U file header") - return + return Playlist{}, + errors.New("invalid m3u file format. Expected #EXTM3U file header") } onFirstLine = false @@ -64,13 +71,12 @@ func Parse(fileName string) (playlist Playlist, err error) { line := strings.Replace(line, "#EXTINF:", "", -1) trackInfo := strings.Split(line, ",") if len(trackInfo) < 2 { - err = errors.New("Invalid m3u file format. Expected EXTINF metadata to contain track length and name data") - return + return Playlist{}, + errors.New("invalid m3u file format. Expected EXTINF metadata to contain track length and name data") } length, parseErr := strconv.Atoi(strings.Split(trackInfo[0], " ")[0]) if parseErr != nil { - err = errors.New("Unable to parse length") - return + return Playlist{}, errors.New("unable to parse length") } track := &Track{strings.Trim(trackInfo[1], " "), length, "", nil} tagList := tagsRegExp.FindAllString(line, -1) @@ -83,8 +89,9 @@ func Parse(fileName string) (playlist Playlist, err error) { } else if strings.HasPrefix(line, "#") || line == "" { continue } else if len(playlist.Tracks) == 0 { - err = errors.New("URI provided for playlist with no tracks") - return + return Playlist{}, + errors.New("URI provided for playlist with no tracks") + } else { playlist.Tracks[len(playlist.Tracks)-1].URI = strings.Trim(line, " ") } @@ -92,3 +99,35 @@ func Parse(fileName string) (playlist Playlist, err error) { return playlist, nil } + +// Marshall Playlist to an m3u file. +func Marshall(p Playlist) (io.Reader, error) { + buf := new(bytes.Buffer) + w := bufio.NewWriter(buf) + if err := MarshallInto(p, w); err != nil { + return nil, err + } + + return buf, nil +} + +// MarshallInto a *bufio.Writer a Playlist. +func MarshallInto(p Playlist, into *bufio.Writer) error { + into.WriteString("#EXTM3U\n") + for _, track := range p.Tracks { + into.WriteString("#EXTINF:") + into.WriteString(fmt.Sprintf("%d ", track.Length)) + for i := range track.Tags { + if i == len(track.Tags)-1 { + into.WriteString(fmt.Sprintf("%s=%q", track.Tags[i].Name, track.Tags[i].Value)) + continue + } + into.WriteString(fmt.Sprintf("%s=%q ", track.Tags[i].Name, track.Tags[i].Value)) + } + into.WriteString(", ") + + into.WriteString(fmt.Sprintf("%s\n%s\n", track.Name, track.URI)) + } + + return into.Flush() +} diff --git a/vendor/modules.txt b/vendor/modules.txt index fff26c64..ac001abf 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -42,8 +42,8 @@ github.com/hashicorp/hcl/json/token # github.com/inconshreveable/mousetrap v1.0.0 ## explicit github.com/inconshreveable/mousetrap -# github.com/jamesnetherton/m3u v0.1.1-0.20180924175816-16741c7f081c -## explicit +# github.com/jamesnetherton/m3u v0.4.0 +## explicit; go 1.17 github.com/jamesnetherton/m3u # github.com/json-iterator/go v1.1.11 ## explicit; go 1.12