supporting video media
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
package media
|
||||
|
||||
import (
|
||||
"image"
|
||||
"image/jpeg"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
"github.com/nfnt/resize"
|
||||
)
|
||||
|
||||
func imageSize(name string) (image.Rectangle, error) {
|
||||
f, err := os.Open(name)
|
||||
if err != nil {
|
||||
return image.ZR, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
cfg, _, err := image.DecodeConfig(f)
|
||||
if err != nil {
|
||||
return image.ZR, err
|
||||
}
|
||||
|
||||
width := cfg.Width
|
||||
height := cfg.Height
|
||||
|
||||
return image.Rect(0, 0, width, height), nil
|
||||
}
|
||||
|
||||
func ThumbImage(filename string, img image.Image, size image.Rectangle) error {
|
||||
os.MkdirAll(path.Dir(filename), 0755)
|
||||
fthumb, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0655)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
img = resize.Resize(uint(size.Dx()), uint(size.Dy()), img, resize.Bilinear)
|
||||
|
||||
err = jpeg.Encode(fthumb, img, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = fthumb.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
+56
-177
@@ -1,26 +1,27 @@
|
||||
package media
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"image"
|
||||
"image/jpeg"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
// for processing images
|
||||
_ "image/gif"
|
||||
_ "image/png"
|
||||
|
||||
"github.com/nfnt/resize"
|
||||
"github.com/tajtiattila/metadata"
|
||||
)
|
||||
|
||||
const (
|
||||
TypeImage = "image"
|
||||
TypeVideo = "video"
|
||||
)
|
||||
|
||||
type MediaSource struct {
|
||||
StorageDir string
|
||||
ThumbDir string
|
||||
@@ -34,32 +35,39 @@ type Media struct {
|
||||
Size image.Rectangle
|
||||
FullName string
|
||||
|
||||
ms *MediaSource
|
||||
metadata *metadata.Metadata
|
||||
}
|
||||
|
||||
func (ms *MediaSource) LocationOrig(m Media) string {
|
||||
return path.Join(ms.StorageDir, m.Name)
|
||||
func (m Media) ThumbPath(size image.Rectangle) string {
|
||||
return "/" + thumbPath(size, m.Name)
|
||||
}
|
||||
|
||||
func (ms *MediaSource) ThumbPath(m Media, size image.Rectangle) string {
|
||||
w := size.Dx()
|
||||
h := size.Dy()
|
||||
|
||||
var ws, hs string
|
||||
|
||||
if w != 0 {
|
||||
ws = fmt.Sprint(w)
|
||||
}
|
||||
if h != 0 {
|
||||
hs = fmt.Sprint(h)
|
||||
}
|
||||
|
||||
thumbSlug := filepath.Join(fmt.Sprintf("%sx%s", ws, hs), m.Name)
|
||||
return path.Join("/media", thumbSlug)
|
||||
func (m Media) ThumbFilename(size image.Rectangle) string {
|
||||
size = m.NormalizeSize(size)
|
||||
return thumbFilename(m.ms.ThumbDir, size, m.Name)
|
||||
}
|
||||
|
||||
func (ms *MediaSource) ThumbFilename(m Media, size image.Rectangle) string {
|
||||
return filepath.Join(ms.ThumbDir, ms.ThumbPath(m, size))
|
||||
func (ms *MediaSource) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
m, err := ms.ByName(path.Base(r.URL.Path))
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
sizeRequested, err := SizeRequested(r.URL.Path, m.Size)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
size, err := ms.Thumb(*m, sizeRequested)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
http.ServeFile(w, r, m.ThumbFilename(size))
|
||||
}
|
||||
|
||||
func (ms *MediaSource) ReceiveNewMedia(name string, r io.Reader) error {
|
||||
@@ -118,123 +126,33 @@ func (m *Media) getMetadata() error {
|
||||
}
|
||||
|
||||
func (ms *MediaSource) Size(name string) (image.Rectangle, error) {
|
||||
f, err := os.Open(name)
|
||||
if err != nil {
|
||||
return image.ZR, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
cfg, _, err := image.DecodeConfig(f)
|
||||
if err != nil {
|
||||
return image.ZR, err
|
||||
switch filepath.Ext(name) {
|
||||
case ".mp4":
|
||||
return VideoSize(name)
|
||||
}
|
||||
|
||||
width := cfg.Width
|
||||
height := cfg.Height
|
||||
|
||||
return image.Rect(0, 0, width, height), nil
|
||||
return imageSize(name)
|
||||
}
|
||||
|
||||
func (ms *MediaSource) ThumbMax(m Media, maxDim int) (string, image.Rectangle, error) {
|
||||
f, err := os.Open(ms.LocationOrig(m))
|
||||
if err != nil {
|
||||
return "", image.ZR, err
|
||||
}
|
||||
defer f.Close()
|
||||
func (ms *MediaSource) ByName(name string) (*Media, error) {
|
||||
ext := filepath.Ext(name)
|
||||
typ := TypeImage
|
||||
|
||||
cfg, _, err := image.DecodeConfig(f)
|
||||
if err != nil {
|
||||
return "", image.ZR, err
|
||||
switch ext {
|
||||
case ".mp4":
|
||||
typ = TypeVideo
|
||||
}
|
||||
|
||||
width := cfg.Width
|
||||
height := cfg.Height
|
||||
fullName := path.Join(ms.StorageDir, name)
|
||||
size, _ := ms.Size(fullName)
|
||||
|
||||
if width > height {
|
||||
height = height * maxDim / width
|
||||
width = maxDim
|
||||
} else {
|
||||
width = width * maxDim / height
|
||||
height = maxDim
|
||||
}
|
||||
|
||||
size := image.Rect(0, 0, width, height)
|
||||
if ms.HasThumb(m, size) {
|
||||
return ms.ThumbPath(m, size), size, nil
|
||||
}
|
||||
|
||||
_, err = f.Seek(0, io.SeekStart)
|
||||
if err != nil {
|
||||
return "", image.ZR, err
|
||||
}
|
||||
|
||||
src, err := ms.thumbReader(f, m, size)
|
||||
return src, size, err
|
||||
}
|
||||
|
||||
func (ms *MediaSource) HasThumb(m Media, size image.Rectangle) bool {
|
||||
fi, err := os.Stat(ms.ThumbFilename(m, size))
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return m.Date().Before(fi.ModTime())
|
||||
}
|
||||
|
||||
func (ms *MediaSource) ByName(name string) *Media {
|
||||
size, _ := ms.Size(path.Join(ms.StorageDir, name))
|
||||
m := Media{
|
||||
Type: "image",
|
||||
Name: name,
|
||||
Size: size,
|
||||
}
|
||||
m.FullName = ms.LocationOrig(m)
|
||||
return &m
|
||||
}
|
||||
|
||||
func (ms *MediaSource) Thumb(m Media, size image.Rectangle) (string, error) {
|
||||
if ms.HasThumb(m, size) {
|
||||
return ms.ThumbPath(m, size), nil
|
||||
}
|
||||
|
||||
f, err := os.Open(ms.LocationOrig(m))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
return ms.thumbReader(f, m, size)
|
||||
}
|
||||
|
||||
func (ms *MediaSource) thumbReader(r io.Reader, m Media, size image.Rectangle) (string, error) {
|
||||
img, _, err := image.Decode(r)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return ms.ThumbImage(img, m, size)
|
||||
}
|
||||
|
||||
func (ms *MediaSource) ThumbImage(img image.Image, m Media, size image.Rectangle) (string, error) {
|
||||
thumbLoc := ms.ThumbFilename(m, size)
|
||||
os.MkdirAll(path.Dir(thumbLoc), 0755)
|
||||
fthumb, err := os.OpenFile(thumbLoc, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0655)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
img = resize.Resize(uint(size.Dx()), uint(size.Dy()), img, resize.Bilinear)
|
||||
|
||||
err = jpeg.Encode(fthumb, img, nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
err = fthumb.Close()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return ms.ThumbPath(m, size), nil
|
||||
return &Media{
|
||||
Type: typ,
|
||||
Name: name,
|
||||
Size: size,
|
||||
FullName: fullName,
|
||||
ms: ms,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (ms *MediaSource) Walk() ([]*Media, error) {
|
||||
@@ -248,7 +166,11 @@ func (ms *MediaSource) Walk() ([]*Media, error) {
|
||||
if fi.IsDir() {
|
||||
return nil
|
||||
}
|
||||
media = append(media, ms.ByName(path.Base(name)))
|
||||
m, err := ms.ByName(path.Base(name))
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
media = append(media, m)
|
||||
return nil
|
||||
})
|
||||
|
||||
@@ -271,49 +193,6 @@ func (s Set) ByDate() Set {
|
||||
return s
|
||||
}
|
||||
|
||||
var (
|
||||
sizeString = regexp.MustCompile(`([0-9]*)(x)?([0-9]*)`)
|
||||
)
|
||||
|
||||
func ParseSizeString(str string, actual image.Rectangle) (image.Rectangle, error) {
|
||||
var err = fmt.Errorf("expected a size string {width}x{height}, saw %q", str)
|
||||
|
||||
strs := sizeString.FindStringSubmatch(str)
|
||||
if len(strs) < 4 {
|
||||
return image.ZR, err
|
||||
}
|
||||
|
||||
var w, h int
|
||||
var strconvErr error
|
||||
|
||||
if strs[1] != "" {
|
||||
w, strconvErr = strconv.Atoi(strs[1])
|
||||
if strconvErr != nil {
|
||||
return image.ZR, err
|
||||
}
|
||||
}
|
||||
|
||||
if strs[3] != "" {
|
||||
h, strconvErr = strconv.Atoi(strs[3])
|
||||
if strconvErr != nil {
|
||||
return image.ZR, err
|
||||
}
|
||||
}
|
||||
|
||||
if strs[2] != "x" {
|
||||
// w was the only dimension given, so set it to the greater dimension
|
||||
// of the actual image size
|
||||
if actual.Dx() > actual.Dy() {
|
||||
h = 0
|
||||
} else {
|
||||
h = w
|
||||
w = 0
|
||||
}
|
||||
}
|
||||
|
||||
return image.Rect(0, 0, w, h), nil
|
||||
}
|
||||
|
||||
func removeExtension(name string) string {
|
||||
ext := path.Ext(name)
|
||||
return name[:len(name)-len(ext)]
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
package media
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"image"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
sizeString = regexp.MustCompile(`([0-9]*)(x)?([0-9]*)`)
|
||||
)
|
||||
|
||||
func SizeRequested(urlpath string, actual image.Rectangle) (image.Rectangle, error) {
|
||||
segments := strings.Count(urlpath, "/")
|
||||
if segments < 3 {
|
||||
return actual, nil
|
||||
}
|
||||
sizeSpec := strings.Split(urlpath, "/")[2]
|
||||
return ParseSizeString(sizeSpec, actual)
|
||||
}
|
||||
|
||||
func ParseSizeString(str string, actual image.Rectangle) (image.Rectangle, error) {
|
||||
var err = fmt.Errorf("expected a size string {width}x{height}, saw %q", str)
|
||||
|
||||
strs := sizeString.FindStringSubmatch(str)
|
||||
if len(strs) < 4 {
|
||||
return image.ZR, err
|
||||
}
|
||||
|
||||
var w, h int
|
||||
var strconvErr error
|
||||
|
||||
if strs[1] != "" {
|
||||
w, strconvErr = strconv.Atoi(strs[1])
|
||||
if strconvErr != nil {
|
||||
return image.ZR, err
|
||||
}
|
||||
}
|
||||
|
||||
if strs[3] != "" {
|
||||
h, strconvErr = strconv.Atoi(strs[3])
|
||||
if strconvErr != nil {
|
||||
return image.ZR, err
|
||||
}
|
||||
}
|
||||
|
||||
if strs[2] != "x" {
|
||||
// w was the only dimension given, so set it to the greater dimension
|
||||
// of the actual image size
|
||||
if actual.Dx() > actual.Dy() {
|
||||
h = 0
|
||||
} else {
|
||||
h = w
|
||||
w = 0
|
||||
}
|
||||
}
|
||||
|
||||
return image.Rect(0, 0, w, h), nil
|
||||
}
|
||||
|
||||
func NormalizeSize(media, requested image.Rectangle) image.Rectangle {
|
||||
if requested.Dx() == 0 && requested.Dy() == 0 {
|
||||
return media
|
||||
}
|
||||
if requested.Dy()%2 == 1 {
|
||||
requested.Max.Y--
|
||||
}
|
||||
if requested.Dx() != 0 && requested.Dy() != 0 {
|
||||
return requested
|
||||
}
|
||||
scaled := image.Rectangle{}
|
||||
if requested.Dx() == 0 {
|
||||
scaled.Max.Y = requested.Dy()
|
||||
scaled.Max.X = requested.Dy() * media.Dx() / media.Dy()
|
||||
}
|
||||
if requested.Dy() == 0 {
|
||||
scaled.Max.X = requested.Dx()
|
||||
scaled.Max.Y = requested.Dx() * media.Dy() / media.Dx()
|
||||
for scaled.Max.Y%2 == 1 {
|
||||
requested.Max.X--
|
||||
scaled.Max.X = requested.Dx()
|
||||
scaled.Max.Y = requested.Dx() * media.Dy() / media.Dx()
|
||||
}
|
||||
}
|
||||
return scaled
|
||||
}
|
||||
+110
@@ -0,0 +1,110 @@
|
||||
package media
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"image"
|
||||
"io"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func (ms *MediaSource) ThumbPath(m Media, size image.Rectangle) string {
|
||||
size = m.NormalizeSize(size)
|
||||
return thumbPath(size, m.Name)
|
||||
}
|
||||
|
||||
func (ms *MediaSource) ThumbFilename(m Media, size image.Rectangle) string {
|
||||
size = m.NormalizeSize(size)
|
||||
return thumbFilename(ms.ThumbDir, size, m.Name)
|
||||
}
|
||||
|
||||
func thumbFilename(dir string, size image.Rectangle, name string) string {
|
||||
return filepath.Join(dir, thumbPath(size, name))
|
||||
}
|
||||
|
||||
func thumbPath(size image.Rectangle, name string) string {
|
||||
w := size.Dx()
|
||||
h := size.Dy()
|
||||
|
||||
var ws, hs string
|
||||
|
||||
if w != 0 {
|
||||
ws = fmt.Sprint(w)
|
||||
}
|
||||
if h != 0 {
|
||||
hs = fmt.Sprint(h)
|
||||
}
|
||||
|
||||
thumbSlug := filepath.Join(fmt.Sprintf("%sx%s", ws, hs), name)
|
||||
return path.Join("", "media", thumbSlug)
|
||||
}
|
||||
|
||||
func (ms *MediaSource) ThumbMax(m Media, maxDim int) (image.Rectangle, error) {
|
||||
width := m.Size.Dx()
|
||||
height := m.Size.Dy()
|
||||
|
||||
if width == 0 && height == 0 {
|
||||
return m.Size, fmt.Errorf("invalid media")
|
||||
}
|
||||
|
||||
if width > height {
|
||||
height = height * maxDim / width
|
||||
width = maxDim
|
||||
} else {
|
||||
width = width * maxDim / height
|
||||
height = maxDim
|
||||
}
|
||||
|
||||
size := image.Rect(0, 0, width, height)
|
||||
actual, err := ms.Thumb(m, size)
|
||||
return actual, err
|
||||
}
|
||||
|
||||
func (ms *MediaSource) HasThumb(m Media, size image.Rectangle) bool {
|
||||
fi, err := os.Stat(ms.ThumbFilename(m, size))
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return m.Date().Before(fi.ModTime())
|
||||
}
|
||||
|
||||
func (m Media) NormalizeSize(size image.Rectangle) image.Rectangle {
|
||||
return NormalizeSize(m.Size, size)
|
||||
}
|
||||
|
||||
func (ms *MediaSource) Thumb(m Media, size image.Rectangle) (image.Rectangle, error) {
|
||||
size = m.NormalizeSize(size)
|
||||
|
||||
if ms.HasThumb(m, size) {
|
||||
return size, nil
|
||||
}
|
||||
|
||||
f, err := os.Open(m.FullName)
|
||||
if err != nil {
|
||||
return size, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
return size, ms.thumbReader(f, m, size)
|
||||
}
|
||||
|
||||
func (ms *MediaSource) thumbReader(r io.Reader, m Media, size image.Rectangle) error {
|
||||
size = m.NormalizeSize(size)
|
||||
|
||||
switch m.Type {
|
||||
case TypeImage:
|
||||
img, _, err := image.Decode(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
filename := ms.ThumbFilename(m, size)
|
||||
return ThumbImage(filename, img, size)
|
||||
case TypeVideo:
|
||||
return VideoEncode(m.FullName, size, ms.ThumbDir)
|
||||
|
||||
default:
|
||||
return fmt.Errorf("cannot thumb media type %q", m.Type)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package media
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"image"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func VideoFrame(filename string) (image.Image, error) {
|
||||
cmd := exec.Command("ffmpeg", "-i", filename, "-vframes", "1", "-f", "singlejpeg", "-")
|
||||
buffer := new(bytes.Buffer)
|
||||
cmd.Stdout = buffer
|
||||
if cmd.Run() != nil {
|
||||
return nil, fmt.Errorf("could not generate frame")
|
||||
}
|
||||
|
||||
img, _, err := image.Decode(buffer)
|
||||
return img, err
|
||||
}
|
||||
|
||||
func VideoSize(filename string) (image.Rectangle, error) {
|
||||
img, err := VideoFrame(filename)
|
||||
if err != nil {
|
||||
return image.Rectangle{}, err
|
||||
}
|
||||
return img.Bounds(), nil
|
||||
}
|
||||
|
||||
func VideoEncode(filename string, size image.Rectangle, thumbDir string) error {
|
||||
dest := thumbFilename(thumbDir, size, path.Base(filename))
|
||||
os.MkdirAll(filepath.Dir(dest), 0755)
|
||||
cmd := exec.Command("ffmpeg", "-i", filename, "-vf", fmt.Sprintf("scale=%d:%d", size.Dx(), size.Dy()), dest)
|
||||
if out, err := cmd.CombinedOutput(); err != nil {
|
||||
os.Remove(dest)
|
||||
return fmt.Errorf("could not thumb video: %s", string(out))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user