43 lines
763 B
Go
43 lines
763 B
Go
|
package thumbnail
|
||
|
|
||
|
import (
|
||
|
"mime"
|
||
|
"path/filepath"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
type DirectThumbnailer struct {
|
||
|
cfg Config
|
||
|
}
|
||
|
|
||
|
func NewThumbnailer(w, h int) Thumbnailer {
|
||
|
opts := DefaultConfig
|
||
|
opts.Width = w
|
||
|
opts.Height = h
|
||
|
|
||
|
return NewThumbnailerEx(&opts)
|
||
|
}
|
||
|
|
||
|
func NewThumbnailerEx(opts *Config) Thumbnailer {
|
||
|
if opts == nil {
|
||
|
opts = &DefaultConfig
|
||
|
}
|
||
|
|
||
|
return &DirectThumbnailer{cfg: *opts}
|
||
|
}
|
||
|
|
||
|
func (this *DirectThumbnailer) RenderFile(absPath string) ([]byte, error) {
|
||
|
mimeType := mime.TypeByExtension(filepath.Ext(absPath))
|
||
|
|
||
|
if strings.HasPrefix(mimeType, `image/`) {
|
||
|
return this.renderImageFile(absPath, mimeType)
|
||
|
|
||
|
} else if strings.HasPrefix(mimeType, `video/`) {
|
||
|
return this.renderScaledFfmpeg(absPath)
|
||
|
|
||
|
} else {
|
||
|
return nil, ErrUnsupportedFiletype
|
||
|
|
||
|
}
|
||
|
}
|