Refactors Cover's Extension cleaning in Album

This commit is contained in:
2016-01-25 15:20:19 +01:00
parent c858e99d06
commit 1c39ad27a5
3 changed files with 33 additions and 7 deletions

View File

@@ -1,8 +1,10 @@
package main
import (
"path"
"regexp"
"strconv"
"strings"
"time"
)
@@ -101,3 +103,14 @@ func (a *Album) String() string {
}
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)
}

View File

@@ -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))
}
}

View File

@@ -8,9 +8,7 @@ import (
"os"
"path"
"path/filepath"
"regexp"
"strconv"
"strings"
"ponyo.epfl.ch/gitlab/alexandre.tuleu/narco"
@@ -20,8 +18,6 @@ import (
"golang.org/x/net/context"
)
var rxExt = regexp.MustCompile(`[0-9]+`)
func (a *appData) buildRouter() http.Handler {
router := httprouter.New()
@@ -79,9 +75,7 @@ func (a *appData) buildRouter() http.Handler {
}
album := *albumUnsafe
ext := path.Ext(album.CoverURL)
ext = strings.ToLower(rxExt.ReplaceAllString(ext, ""))
album.CoverURL = fmt.Sprintf("/covers/%d%s", album.ID, ext)
album.CoverURL = fmt.Sprintf("/covers/%d%s", album.ID, album.CoverExt())
enc := json.NewEncoder(w)
if err := enc.Encode(album); err != nil {