add RenderFileAs() for user-supplied mime-type
This commit is contained in:
parent
a91911f351
commit
6cfd09fce0
@ -1,6 +1,9 @@
|
|||||||
package thumbnail
|
package thumbnail
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"mime"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
lru "github.com/hashicorp/golang-lru"
|
lru "github.com/hashicorp/golang-lru"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -9,6 +12,8 @@ type CachingThumbnailer struct {
|
|||||||
cache *lru.Cache // threadsafe, might be nil
|
cache *lru.Cache // threadsafe, might be nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var _ Thumbnailer = &CachingThumbnailer{} // interface assertion
|
||||||
|
|
||||||
func NewCachingThumbnailer(cacheSize int, opts *Config) (Thumbnailer, error) {
|
func NewCachingThumbnailer(cacheSize int, opts *Config) (Thumbnailer, error) {
|
||||||
|
|
||||||
upstream := NewThumbnailerEx(opts)
|
upstream := NewThumbnailerEx(opts)
|
||||||
@ -29,7 +34,11 @@ func NewCachingThumbnailer(cacheSize int, opts *Config) (Thumbnailer, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (this *CachingThumbnailer) RenderFile(absPath string) ([]byte, error) {
|
func (this *CachingThumbnailer) RenderFile(absPath string) ([]byte, error) {
|
||||||
|
mimeType := mime.TypeByExtension(filepath.Ext(absPath))
|
||||||
|
return this.RenderFileAs(absPath, mimeType)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *CachingThumbnailer) RenderFileAs(absPath, mimeType string) ([]byte, error) {
|
||||||
thumb, ok := this.cache.Get(absPath)
|
thumb, ok := this.cache.Get(absPath)
|
||||||
if ok {
|
if ok {
|
||||||
return thumb.([]byte), nil
|
return thumb.([]byte), nil
|
||||||
|
@ -11,6 +11,8 @@ type DirectThumbnailer struct {
|
|||||||
cfg Config
|
cfg Config
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var _ Thumbnailer = &DirectThumbnailer{} // interface assertion
|
||||||
|
|
||||||
func NewThumbnailer(w, h int) Thumbnailer {
|
func NewThumbnailer(w, h int) Thumbnailer {
|
||||||
opts := DefaultConfig
|
opts := DefaultConfig
|
||||||
opts.Width = w
|
opts.Width = w
|
||||||
@ -29,6 +31,10 @@ func NewThumbnailerEx(opts *Config) Thumbnailer {
|
|||||||
|
|
||||||
func (this *DirectThumbnailer) RenderFile(absPath string) ([]byte, error) {
|
func (this *DirectThumbnailer) RenderFile(absPath string) ([]byte, error) {
|
||||||
mimeType := mime.TypeByExtension(filepath.Ext(absPath))
|
mimeType := mime.TypeByExtension(filepath.Ext(absPath))
|
||||||
|
return this.RenderFileAs(absPath, mimeType)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *DirectThumbnailer) RenderFileAs(absPath, mimeType string) ([]byte, error) {
|
||||||
|
|
||||||
// Decode source
|
// Decode source
|
||||||
|
|
||||||
|
@ -12,6 +12,7 @@ var (
|
|||||||
|
|
||||||
type Thumbnailer interface {
|
type Thumbnailer interface {
|
||||||
RenderFile(absPath string) ([]byte, error)
|
RenderFile(absPath string) ([]byte, error)
|
||||||
|
RenderFileAs(absPath, mimeType string) ([]byte, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func FiletypeSupported(ext string) bool {
|
func FiletypeSupported(ext string) bool {
|
||||||
|
Loading…
Reference in New Issue
Block a user