Refactors Cover's Extension cleaning in Album
This commit is contained in:
13
album.go
13
album.go
@@ -1,8 +1,10 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"path"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -101,3 +103,14 @@ func (a *Album) String() string {
|
|||||||
}
|
}
|
||||||
return res + a.Title
|
return res + a.Title
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var rxExt = regexp.MustCompile(`\.([0-9]+)([a-zA-Z]+)\z`)
|
||||||
|
|
||||||
|
func (a *Album) CoverExt() string {
|
||||||
|
ext := path.Ext(a.CoverURL)
|
||||||
|
m := rxExt.FindStringSubmatch(ext)
|
||||||
|
if m != nil {
|
||||||
|
ext = "." + m[2]
|
||||||
|
}
|
||||||
|
return strings.ToLower(ext)
|
||||||
|
}
|
||||||
|
|||||||
@@ -38,3 +38,22 @@ func (s *AlbumSuite) TestAuthors(c *C) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *AlbumSuite) TestExt(c *C) {
|
||||||
|
type TestData struct {
|
||||||
|
A *Album
|
||||||
|
Res string
|
||||||
|
}
|
||||||
|
data := []TestData{
|
||||||
|
{&Album{CoverURL: "foo/bar/test.jpg"}, ".jpg"},
|
||||||
|
{&Album{CoverURL: "foo/bar/foo.JPG"}, ".jpg"},
|
||||||
|
{&Album{CoverURL: "foo/bar/foo.JPEG"}, ".jpeg"},
|
||||||
|
{&Album{CoverURL: "foo/bar/foo.023879jpg"}, ".jpg"},
|
||||||
|
{&Album{CoverURL: "foo/bar/foo.PNG"}, ".png"},
|
||||||
|
{&Album{CoverURL: "foo/bar/foo.gif"}, ".gif"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, d := range data {
|
||||||
|
c.Check(d.A.CoverExt(), Equals, d.Res, Commentf("With URL %s", d.A.CoverURL))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -8,9 +8,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"ponyo.epfl.ch/gitlab/alexandre.tuleu/narco"
|
"ponyo.epfl.ch/gitlab/alexandre.tuleu/narco"
|
||||||
|
|
||||||
@@ -20,8 +18,6 @@ import (
|
|||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
)
|
)
|
||||||
|
|
||||||
var rxExt = regexp.MustCompile(`[0-9]+`)
|
|
||||||
|
|
||||||
func (a *appData) buildRouter() http.Handler {
|
func (a *appData) buildRouter() http.Handler {
|
||||||
router := httprouter.New()
|
router := httprouter.New()
|
||||||
|
|
||||||
@@ -79,9 +75,7 @@ func (a *appData) buildRouter() http.Handler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
album := *albumUnsafe
|
album := *albumUnsafe
|
||||||
ext := path.Ext(album.CoverURL)
|
album.CoverURL = fmt.Sprintf("/covers/%d%s", album.ID, album.CoverExt())
|
||||||
ext = strings.ToLower(rxExt.ReplaceAllString(ext, ""))
|
|
||||||
album.CoverURL = fmt.Sprintf("/covers/%d%s", album.ID, ext)
|
|
||||||
|
|
||||||
enc := json.NewEncoder(w)
|
enc := json.NewEncoder(w)
|
||||||
if err := enc.Encode(album); err != nil {
|
if err := enc.Encode(album); err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user