rewrite extension handling, initial video support

This commit is contained in:
mappu 2016-11-18 20:02:22 +13:00
parent 676a71f527
commit 5f096e52a4
2 changed files with 48 additions and 13 deletions

View File

@ -60,22 +60,34 @@ func (this *Thumbnailer) RenderFile_NoCache(absPath string) ([]byte, error) {
defer fh.Close() defer fh.Close()
var src image.Image if len(absPath) < 4 {
err = fmt.Errorf("No thumbnailer for file type") return nil, fmt.Errorf("No extension on filename")
}
extension := strings.ToLower(absPath[len(absPath)-4:])
comparePath := strings.ToLower(absPath) switch extension {
case ".jpg", "jpeg":
src, err := jpeg.Decode(fh)
if err != nil {
return nil, err
}
if strings.HasSuffix(comparePath, "jpg") { return this.RenderScaledImage(src)
src, err = jpeg.Decode(fh)
} else if strings.HasSuffix(comparePath, "png") { case ".png":
src, err = png.Decode(fh) src, err := png.Decode(fh)
if err != nil {
return nil, err
}
return this.RenderScaledImage(src)
case ".avi", ".mkv", ".mp4", ".ogm", ".wmv":
return this.RenderScaledFfmpeg(absPath)
default:
return nil, fmt.Errorf("No thumbnailer for file type")
} }
if err != nil {
return nil, err
}
return this.RenderScaledImage(src)
} }

View File

@ -1,6 +1,8 @@
package thumbnail package thumbnail
import ( import (
"bytes"
"io"
"os/exec" "os/exec"
) )
@ -18,5 +20,26 @@ func (this *Thumbnailer) RenderScaledFfmpeg(absPath string) ([]byte, error) {
"-", "-",
) )
cmd.StdoutPipe() stdout, err := cmd.StdoutPipe()
if err != nil {
return nil, err
}
err = cmd.Start()
if err != nil {
return nil, err
}
out := bytes.Buffer{}
_, err = io.Copy(&out, stdout)
if err != nil {
return nil, err
}
err = cmd.Wait()
if err != nil {
return nil, err
}
return out.Bytes(), nil
} }