thumb: compatibility fixes for thumbnail library
This commit is contained in:
parent
be864235cb
commit
fe4aace777
46
thumb.go
46
thumb.go
@ -10,52 +10,66 @@ import (
|
|||||||
"code.ivysaur.me/thumbnail"
|
"code.ivysaur.me/thumbnail"
|
||||||
)
|
)
|
||||||
|
|
||||||
func thumbnailer(t byte) (*thumbnail.Thumbnailer, error) {
|
func getThumbnailerConfig(t byte) (*thumbnail.Config, error) {
|
||||||
// Modelled on what imgur.com offers
|
// Modelled on what imgur.com offers
|
||||||
// @ref https://api.imgur.com/models/image#thumbs
|
// @ref https://api.imgur.com/models/image#thumbs
|
||||||
|
|
||||||
const (
|
opts := thumbnail.Config{
|
||||||
cacheSize = 1
|
Aspect: thumbnail.FitOutside,
|
||||||
outputFmt = thumbnail.OUTPUT_JPG
|
Output: thumbnail.Jpeg,
|
||||||
scaleFmt = thumbnail.SCALEFMT_BILINEAR
|
Scale: thumbnail.Bicubic,
|
||||||
)
|
}
|
||||||
|
|
||||||
switch t {
|
switch t {
|
||||||
case 's':
|
case 's':
|
||||||
return thumbnail.NewThumbnailerEx(90, 90, cacheSize, outputFmt, thumbnail.ASPECT_CROP_TO_DIMENSIONS, scaleFmt), nil
|
opts.Width = 90
|
||||||
|
opts.Height = 90
|
||||||
case 'b':
|
case 'b':
|
||||||
return thumbnail.NewThumbnailerEx(160, 160, cacheSize, outputFmt, thumbnail.ASPECT_CROP_TO_DIMENSIONS, scaleFmt), nil
|
opts.Width = 160
|
||||||
|
opts.Height = 160
|
||||||
case 't':
|
case 't':
|
||||||
return thumbnail.NewThumbnailerEx(160, 160, cacheSize, outputFmt, thumbnail.ASPECT_RESPECT_MAX_DIMENSION_ONLY, scaleFmt), nil
|
opts.Width = 160
|
||||||
|
opts.Height = 160
|
||||||
|
// thumbnail.ASPECT_RESPECT_MAX_DIMENSION_ONLY
|
||||||
case 'm':
|
case 'm':
|
||||||
return thumbnail.NewThumbnailerEx(340, 340, cacheSize, outputFmt, thumbnail.ASPECT_RESPECT_MAX_DIMENSION_ONLY, scaleFmt), nil
|
opts.Width = 340
|
||||||
|
opts.Height = 340
|
||||||
|
// thumbnail.ASPECT_RESPECT_MAX_DIMENSION_ONLY
|
||||||
case 'l':
|
case 'l':
|
||||||
return thumbnail.NewThumbnailerEx(640, 640, cacheSize, outputFmt, thumbnail.ASPECT_RESPECT_MAX_DIMENSION_ONLY, scaleFmt), nil
|
opts.Width = 640
|
||||||
|
opts.Height = 640
|
||||||
|
// thumbnail.ASPECT_RESPECT_MAX_DIMENSION_ONLY
|
||||||
case 'h':
|
case 'h':
|
||||||
return thumbnail.NewThumbnailerEx(1024, 1024, cacheSize, outputFmt, thumbnail.ASPECT_RESPECT_MAX_DIMENSION_ONLY, scaleFmt), nil
|
opts.Width = 1024
|
||||||
|
opts.Height = 1024
|
||||||
|
// thumbnail.ASPECT_RESPECT_MAX_DIMENSION_ONLY
|
||||||
default:
|
default:
|
||||||
return nil, errors.New("Unsupported thumbnail type (should be s/b/t/m/l/h)")
|
return nil, errors.New("Unsupported thumbnail type (should be s/b/t/m/l/h)")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return &opts, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Server) handleThumb(w http.ResponseWriter, r *http.Request, thumbnailType byte, fileId string) {
|
func (this *Server) handleThumb(w http.ResponseWriter, r *http.Request, thumbnailType byte, fileId string) {
|
||||||
t, err := thumbnailer(thumbnailType)
|
opts, err := getThumbnailerConfig(thumbnailType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("%s Thumbnail failed: %s\n", this.remoteIP(r), err.Error())
|
log.Printf("%s Thumbnail failed: %s\n", this.remoteIP(r), err.Error())
|
||||||
http.Error(w, err.Error(), 400)
|
http.Error(w, err.Error(), 400)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
t := thumbnail.NewThumbnailerEx(opts)
|
||||||
|
|
||||||
err = this.handleThumbInternal(w, t, fileId)
|
err = this.handleThumbInternal(w, t, fileId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("%s Thumbnail failed: %s\n", this.remoteIP(r), err.Error())
|
log.Printf("%s Thumbnail failed: %s\n", this.remoteIP(r), err.Error())
|
||||||
|
|
||||||
w.Header().Set(`Location`, fmt.Sprintf(`/nothumb_%d.png`, t.Height()))
|
w.Header().Set(`Location`, fmt.Sprintf(`/nothumb_%d.png`, opts.Height))
|
||||||
w.WriteHeader(302)
|
w.WriteHeader(302)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Server) handleThumbInternal(w http.ResponseWriter, t *thumbnail.Thumbnailer, fileId string) error {
|
func (this *Server) handleThumbInternal(w http.ResponseWriter, t thumbnail.Thumbnailer, fileId string) error {
|
||||||
|
|
||||||
// Load metadata
|
// Load metadata
|
||||||
m, err := this.Metadata(fileId)
|
m, err := this.Metadata(fileId)
|
||||||
@ -64,7 +78,7 @@ func (this *Server) handleThumbInternal(w http.ResponseWriter, t *thumbnail.Thum
|
|||||||
}
|
}
|
||||||
|
|
||||||
filePath := filepath.Join(this.opts.DataDirectory, m.FileHash)
|
filePath := filepath.Join(this.opts.DataDirectory, m.FileHash)
|
||||||
thumb, err := t.RenderFile_NoCache_MimeType(filePath, m.MimeType)
|
thumb, err := t.RenderFileAs(filePath, m.MimeType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user