diff --git a/CachingThumbnailer.go b/CachingThumbnailer.go index 390408f..fa508b9 100644 --- a/CachingThumbnailer.go +++ b/CachingThumbnailer.go @@ -1,6 +1,9 @@ package thumbnail import ( + "mime" + "path/filepath" + lru "github.com/hashicorp/golang-lru" ) @@ -9,6 +12,8 @@ type CachingThumbnailer struct { cache *lru.Cache // threadsafe, might be nil } +var _ Thumbnailer = &CachingThumbnailer{} // interface assertion + func NewCachingThumbnailer(cacheSize int, opts *Config) (Thumbnailer, error) { upstream := NewThumbnailerEx(opts) @@ -29,7 +34,11 @@ func NewCachingThumbnailer(cacheSize int, opts *Config) (Thumbnailer, 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) if ok { return thumb.([]byte), nil diff --git a/DirectThumbnailer.go b/DirectThumbnailer.go index e78cd3a..864d840 100644 --- a/DirectThumbnailer.go +++ b/DirectThumbnailer.go @@ -11,6 +11,8 @@ type DirectThumbnailer struct { cfg Config } +var _ Thumbnailer = &DirectThumbnailer{} // interface assertion + func NewThumbnailer(w, h int) Thumbnailer { opts := DefaultConfig opts.Width = w @@ -29,6 +31,10 @@ func NewThumbnailerEx(opts *Config) Thumbnailer { func (this *DirectThumbnailer) RenderFile(absPath string) ([]byte, error) { mimeType := mime.TypeByExtension(filepath.Ext(absPath)) + return this.RenderFileAs(absPath, mimeType) +} + +func (this *DirectThumbnailer) RenderFileAs(absPath, mimeType string) ([]byte, error) { // Decode source diff --git a/Thumbnailer.go b/Thumbnailer.go index 0643023..c04bfc6 100644 --- a/Thumbnailer.go +++ b/Thumbnailer.go @@ -12,6 +12,7 @@ var ( type Thumbnailer interface { RenderFile(absPath string) ([]byte, error) + RenderFileAs(absPath, mimeType string) ([]byte, error) } func FiletypeSupported(ext string) bool {