supporting video media

pull/16/head
Stephen Searles 7 years ago
parent ff1033dfb4
commit acbfdbe8eb
  1. 46
      media.go
  2. 50
      media/image.go
  3. 227
      media/media.go
  4. 88
      media/size.go
  5. 110
      media/thumb.go
  6. 42
      media/video.go
  7. 4
      theme-additions/bindata.go
  8. 15
      theme-additions/layouts/shortcodes/thumb.html

@ -2,11 +2,8 @@ package caddyhugo
import (
"fmt"
"image"
"io"
"net/http"
"path"
"strings"
"git.stephensearles.com/stephen/caddy-hugo2/media"
)
@ -85,12 +82,18 @@ func (ch *CaddyHugo) serveMediaPage(w http.ResponseWriter, r *http.Request) (int
for _, m := range media.Set(mm).ByDate() {
src, size, err := ch.Media.ThumbMax(*m, 100)
size, err := ch.Media.ThumbMax(*m, 100)
if err != nil {
fmt.Fprintf(w, `<div class="img">error rendering %q: %v</div>`, m.Name, err)
continue
}
fmt.Fprintf(w, `<div class="img"><img width=%d height=%d src=%q data-filename=%q /><br /><input type="text" readonly value=%q /><span class="copy">&#x1F4CB;</span></div>`, size.Dx(), size.Dy(), src, m.Name, src)
switch m.Type {
case media.TypeImage:
fmt.Fprintf(w, `<div class="img"><img width=%d height=%d src=%q data-filename=%q /><br /><input type="text" readonly value=%q /><span class="copy">&#x1F4CB;</span></div>`, size.Dx(), size.Dy(), m.ThumbPath(size), m.Name, m.ThumbPath(size))
case media.TypeVideo:
// TODO: onmouseover sucks for mobile
fmt.Fprintf(w, `<div class="img"><video width=%d height=%d src=%q data-filename=%q onmouseover="this.play()" onmouseout="this.pause();this.currentTime=0;"></video><br /><input type="text" readonly value=%q /><span class="copy">&#x1F4CB;</span></div>`, size.Dx(), size.Dy(), m.ThumbPath(size), m.Name, m.ThumbPath(size))
}
}
}
io.WriteString(w, `<script>
@ -102,7 +105,7 @@ func (ch *CaddyHugo) serveMediaPage(w http.ResponseWriter, r *http.Request) (int
evt.target.previousSibling.select();
document.execCommand("copy");
}
if (evt.target.tagName === "IMG") {
if (evt.target.tagName === "IMG" || evt.target.tagName === "VIDEO") {
var current = document.querySelector(".img.selected");
if (current) {
current.classList = "img";
@ -128,35 +131,6 @@ func (ch *CaddyHugo) serveMedia(w http.ResponseWriter, r *http.Request) (int, er
return 404, nil
}
segs := strings.Split(r.URL.Path, "/")
name := segs[len(segs)-1] // the last segment is the filename
size := image.Rectangle{}
m := ch.Media.ByName(name)
if len(segs) >= 4 && len(segs) > 2 {
var err error
size, err = media.ParseSizeString(segs[len(segs)-2], m.Size)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return 400, nil
}
}
file, err := ch.Media.Thumb(*m, size)
if err != nil {
http.Error(w, fmt.Sprintf("unable to load thumb"), http.StatusInternalServerError)
return 500, nil
}
if file[0] == '/' {
file = file[1:]
}
file = path.Join(ch.Media.ThumbDir, file)
http.ServeFile(w, r, file)
ch.Media.ServeHTTP(w, r)
return 200, nil
}

@ -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
}

@ -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
func (m Media) ThumbFilename(size image.Rectangle) string {
size = m.NormalizeSize(size)
return thumbFilename(m.ms.ThumbDir, size, m.Name)
}
if w != 0 {
ws = fmt.Sprint(w)
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
}
if h != 0 {
hs = fmt.Sprint(h)
sizeRequested, err := SizeRequested(r.URL.Path, m.Size)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
thumbSlug := filepath.Join(fmt.Sprintf("%sx%s", ws, hs), m.Name)
return path.Join("/media", thumbSlug)
}
size, err := ms.Thumb(*m, sizeRequested)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
func (ms *MediaSource) ThumbFilename(m Media, size image.Rectangle) string {
return filepath.Join(ms.ThumbDir, ms.ThumbPath(m, size))
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
}
width := cfg.Width
height := cfg.Height
return image.Rect(0, 0, width, height), nil
}
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()
cfg, _, err := image.DecodeConfig(f)
if err != nil {
return "", image.ZR, err
}
width := cfg.Width
height := cfg.Height
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
switch filepath.Ext(name) {
case ".mp4":
return VideoSize(name)
}
defer f.Close()
return ms.thumbReader(f, m, size)
return imageSize(name)
}
func (ms *MediaSource) thumbReader(r io.Reader, m Media, size image.Rectangle) (string, error) {
img, _, err := image.Decode(r)
if err != nil {
return "", err
}
func (ms *MediaSource) ByName(name string) (*Media, error) {
ext := filepath.Ext(name)
typ := TypeImage
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
switch ext {
case ".mp4":
typ = TypeVideo
}
img = resize.Resize(uint(size.Dx()), uint(size.Dy()), img, resize.Bilinear)
err = jpeg.Encode(fthumb, img, nil)
if err != nil {
return "", err
}
fullName := path.Join(ms.StorageDir, name)
size, _ := ms.Size(fullName)
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
}

@ -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
}

@ -152,7 +152,7 @@ func layoutsShortcodesCommentsHtml() (*asset, error) {
return a, nil
}
var _layoutsShortcodesThumbHtml = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x74\x90\xc1\x6a\xec\x30\x0c\x45\xf7\xf9\x0a\xa1\xd5\x7b\x8b\x99\x4c\xf7\x49\xb6\xfd\x8c\xa2\x26\x8a\x2d\xea\xb8\x69\x22\x98\x82\xd0\xbf\x17\xa7\x66\x98\x0e\xed\xc6\x20\x38\xe7\xfa\x72\xbb\xb5\x31\x03\x99\x81\x3f\xe0\xdf\xf9\x99\x15\x90\x92\x84\x8c\xff\x01\x13\xcf\x8a\xe0\x3e\x26\xda\xf7\xfe\x38\x65\x09\x68\xc6\x69\xe7\xe2\x3c\x0a\x9b\x84\xa8\x78\x13\x8e\xb3\x1a\x79\x82\x93\x7b\x33\x74\x04\x71\xe3\xb9\xc7\x76\xe1\x49\xa8\x35\x83\xef\x90\x59\x12\x67\x5a\x18\xdd\x11\x94\xb6\xc0\xda\xe3\xcb\x6b\xa2\xfc\x86\x43\x27\x4b\x80\x7d\x1b\x6f\x5a\x63\x76\x82\xab\x68\xac\xf6\x55\x26\x8d\xe8\x6e\x76\x2e\x4f\x29\xe8\xfe\x74\xb9\x1c\x3f\xbb\x7f\x9a\xdd\xc1\x91\x6b\xcf\x1f\x74\x45\x7f\x69\x54\x9a\x63\x99\xe9\x2e\x63\xa4\x55\xe5\x3d\x97\x79\x54\x34\x71\x8f\x47\x18\xd6\x94\xbf\x69\x4a\xfa\xc8\xb6\x43\xd7\xd2\xd0\x74\xed\x3a\x34\x5f\x01\x00\x00\xff\xff\x52\x23\x84\xaa\x90\x01\x00\x00")
var _layoutsShortcodesThumbHtml = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xc4\x90\xcf\x6e\x83\x30\x0c\xc6\xef\x3c\x85\x65\xed\xd0\x1e\x5a\x3a\xa9\xa7\x09\xb8\xee\x31\xa6\x14\x0c\x44\x33\x81\x11\x77\xad\x14\xf9\xdd\xa7\xa4\xac\xff\xa4\x9d\x77\x89\x64\xe7\xfb\x3e\xff\xec\x10\xe0\xa5\xb5\x4c\xce\x0c\x04\x6f\x25\x6c\xdf\x49\x00\x7f\x3b\x08\x1b\xd5\x2c\x6a\xe8\x2c\xf1\xdb\x1f\x0f\x5e\xe6\x3b\xcb\xca\x1f\x0f\xb0\x62\x72\xb7\xde\x1a\xf6\x6b\x50\xcd\x8a\x29\x3a\x6d\x0b\xf4\x05\xab\x4b\xae\x61\xdb\x39\x5c\x03\x32\xb5\x82\xa0\x5a\xb3\xf1\xbe\x4c\xa5\x1d\x3a\x0c\x81\xd8\x53\xf4\x3c\x1b\x66\xdb\xf5\x82\x57\x43\x2a\x17\x87\x6b\x40\xb5\xba\xcd\x4a\xac\xb8\x1d\xa6\x3d\x26\x8c\x6f\xdb\xd0\x08\x7e\xae\x4b\xcc\x07\x6a\xac\xc9\xb3\x10\x36\x70\xb2\xd2\x2f\xeb\x9e\x6c\x23\x3d\xaa\x86\xb0\x8d\x4f\x64\x50\x7d\xdd\xed\x52\xb8\xea\x39\x84\x3b\x71\x4f\x0b\xca\x83\x7a\x91\xe6\x0f\xf7\xdc\xa8\x62\x56\x8f\x4e\xe6\x91\x3d\x18\xe6\xf1\xd4\x1e\x99\x7d\x3d\x13\xb9\xac\x2a\xf2\x84\x96\xd0\xd3\xde\x91\xd6\x40\x3f\x53\x7b\x45\x7d\xc8\x53\x45\x10\x33\x77\x24\x25\x7e\x1c\xd8\xb8\x4f\xac\x0a\x3b\x74\xff\xb7\x5c\x08\xf7\xa3\x6a\x33\x89\x1d\x5d\x3c\xbb\x58\x61\x2a\x31\xc5\xe0\xe2\xff\x5b\x6d\x58\x9e\xb5\x79\x55\xe4\x26\x9e\xe6\x52\x16\xf9\x54\x65\x3f\x01\x00\x00\xff\xff\xca\x5c\x71\xf7\xad\x02\x00\x00")
func layoutsShortcodesThumbHtmlBytes() ([]byte, error) {
return bindataRead(
@ -167,7 +167,7 @@ func layoutsShortcodesThumbHtml() (*asset, error) {
return nil, err
}
info := bindataFileInfo{name: "layouts/shortcodes/thumb.html", size: 400, mode: os.FileMode(420), modTime: time.Unix(1504974306, 0)}
info := bindataFileInfo{name: "layouts/shortcodes/thumb.html", size: 685, mode: os.FileMode(420), modTime: time.Unix(1505244603, 0)}
a := &asset{bytes: bytes, info: info}
return a, nil
}

@ -1,8 +1,17 @@
{{ $filename := .Get "filename" -}}
{{ $ext := substr $filename (sub (len $filename) 4) }}
<p
{{ if eq (.Get "align") "left" }}class="leftimg"{{else if (.Get "align") "right"}}class="rightimg"{{end -}}
><a href="/media/{{ .Get "filename"}}" target="_blank"><img src="/media/
{{- with .Get "width"}}{{.}}{{else}}100{{end}}x{{with .Get "height"}}{{.}}{{else}}{{end}}/{{ .Get "filename" -}}"
{{ if eq (.Get "align") "left" }}class="leftimg"{{else if (.Get "align") "right"}}class="rightimg"{{end }}>
{{ if eq $ext ".mp4" }}
<video src="/media/
{{- with .Get "width"}}{{.}}{{else}}100{{end}}x{{with .Get "height"}}{{.}}{{else}}{{end}}/{{ $filename -}}"
controls allowfullscreen
></video>
{{ else }}
<a href="/media/{{ $filename }}" target="_blank"><img src="/media/
{{- with .Get "width"}}{{.}}{{else}}100{{end}}x{{with .Get "height"}}{{.}}{{else}}{{end}}/{{ $filename -}}"
{{ with .Get "caption" }}title="{{.}}"{{end}}
{{ with .Get "caption" }}alt="{{.}}"{{end}}
/></a>
{{end}}
</p>

Loading…
Cancel
Save