Implements GET for images

This commit is contained in:
2016-01-25 15:59:45 +01:00
parent 21e6b41f95
commit 6ecaabf955
5 changed files with 59 additions and 23 deletions

View File

@@ -9,6 +9,7 @@ import (
"path"
"path/filepath"
"strconv"
"strings"
"ponyo.epfl.ch/gitlab/alexandre.tuleu/narco"
@@ -75,7 +76,7 @@ func (a *appData) buildRouter() http.Handler {
}
album := *albumUnsafe
album.CoverURL = fmt.Sprintf("/covers/%d%s", album.ID, album.CoverExt())
album.CoverURL = fmt.Sprintf("/covers/%d%s", album.ID, AlbumCoverExt(album.CoverURL))
enc := json.NewEncoder(w)
if err := enc.Encode(album); err != nil {
@@ -83,6 +84,36 @@ func (a *appData) buildRouter() http.Handler {
}
})))
router.GET("/covers/:name", narco.EndChain(ch, narco.HandlerFunc(
func(ctx context.Context, w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
var ID uint64
var err error = fmt.Errorf("Not Found")
var requestedExt string
name := ps.ByName("name")
if len(name) > 0 {
requestedExt = path.Ext(name)
ID, err = strconv.ParseUint(strings.TrimSuffix(name, requestedExt), 10, 64)
}
if err != nil {
narco.Error(ctx, w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
}
rc, ext, err := a.cover.GetCover(AlbumID(ID))
if rc != nil {
defer closeOrPanic(rc, "Album cover "+name)
}
if err != nil {
narco.Error(ctx, w, err, http.StatusInternalServerError)
}
if ext != requestedExt {
narco.Error(ctx, w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
}
io.Copy(w, rc)
})))
dirs := []string{"css", "js", "img"}
for _, d := range dirs {
router.ServeFiles(path.Join("/", d, "/*filepath"), http.Dir(filepath.Join("static", d)))