supporting video media

This commit is contained in:
2017-09-12 14:56:13 -05:00
parent ff1033dfb4
commit acbfdbe8eb
8 changed files with 370 additions and 218 deletions
+10 -36
View File
@@ -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
}