|
|
|
@ -11,6 +11,7 @@ import ( |
|
|
|
|
"os" |
|
|
|
|
"path" |
|
|
|
|
"path/filepath" |
|
|
|
|
"regexp" |
|
|
|
|
"strconv" |
|
|
|
|
"strings" |
|
|
|
|
|
|
|
|
@ -26,6 +27,27 @@ func (ms *MediaSource) LocationOrig(m Media) string { |
|
|
|
|
return path.Join(ms.StorageDir, 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 (ms *MediaSource) ThumbFilename(m Media, size image.Rectangle) string { |
|
|
|
|
return filepath.Join(ms.ThumbDir, ms.ThumbPath(m, size)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (ms *MediaSource) receiveNewMedia(name string, r io.Reader) error { |
|
|
|
|
dest := path.Join(ms.StorageDir, name) |
|
|
|
|
f, err := os.OpenFile(dest, os.O_WRONLY|os.O_CREATE, 0644) |
|
|
|
@ -46,21 +68,20 @@ type Media struct { |
|
|
|
|
Name string |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (ms *MediaSource) ThumbMax(m Media, maxDim int) (string, int, int, error) { |
|
|
|
|
func (ms *MediaSource) ThumbMax(m Media, maxDim int) (string, image.Rectangle, error) { |
|
|
|
|
f, err := os.Open(ms.LocationOrig(m)) |
|
|
|
|
if err != nil { |
|
|
|
|
return "", 0, 0, err |
|
|
|
|
return "", image.ZR, err |
|
|
|
|
} |
|
|
|
|
defer f.Close() |
|
|
|
|
|
|
|
|
|
img, _, err := image.Decode(f) |
|
|
|
|
cfg, _, err := image.DecodeConfig(f) |
|
|
|
|
if err != nil { |
|
|
|
|
return "", 0, 0, err |
|
|
|
|
return "", image.ZR, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
rect := img.Bounds() |
|
|
|
|
width := rect.Dx() |
|
|
|
|
height := rect.Dy() |
|
|
|
|
width := cfg.Width |
|
|
|
|
height := cfg.Height |
|
|
|
|
|
|
|
|
|
if width > height { |
|
|
|
|
height = height * maxDim / width |
|
|
|
@ -70,8 +91,28 @@ func (ms *MediaSource) ThumbMax(m Media, maxDim int) (string, int, int, error) { |
|
|
|
|
height = maxDim |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
src, err := ms.ThumbImage(img, m, width, height) |
|
|
|
|
return src, width, height, err |
|
|
|
|
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 |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
img, _, err := image.Decode(f) |
|
|
|
|
if err != nil { |
|
|
|
|
return "", image.ZR, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
src, err := ms.ThumbImage(img, m, size) |
|
|
|
|
return src, size, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (ms *MediaSource) HasThumb(m Media, size image.Rectangle) bool { |
|
|
|
|
_, err := os.Stat(ms.ThumbFilename(m, size)) |
|
|
|
|
return err == nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (ms *MediaSource) ByName(name string) *Media { |
|
|
|
@ -81,7 +122,11 @@ func (ms *MediaSource) ByName(name string) *Media { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (ms *MediaSource) Thumb(m Media, width, height int) (string, error) { |
|
|
|
|
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 |
|
|
|
@ -93,21 +138,19 @@ func (ms *MediaSource) Thumb(m Media, width, height int) (string, error) { |
|
|
|
|
return "", err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return ms.ThumbImage(img, m, width, height) |
|
|
|
|
return ms.ThumbImage(img, m, size) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (ms *MediaSource) ThumbImage(img image.Image, m Media, width, height int) (string, error) { |
|
|
|
|
|
|
|
|
|
thumbSlug := filepath.Join(m.Name, fmt.Sprintf("%d/%d.jpg", width, height)) |
|
|
|
|
thumbLoc := filepath.Join(ms.ThumbDir, thumbSlug) |
|
|
|
|
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(width), uint(height), img, resize.Bilinear) |
|
|
|
|
img = resize.Resize(uint(size.Dx()), uint(size.Dy()), img, resize.Bilinear) |
|
|
|
|
|
|
|
|
|
err = jpeg.Encode(fthumb, img, nil) |
|
|
|
|
if err != nil { |
|
|
|
@ -119,7 +162,7 @@ func (ms *MediaSource) ThumbImage(img image.Image, m Media, width, height int) ( |
|
|
|
|
return "", err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return path.Join("/media", thumbSlug), nil |
|
|
|
|
return ms.ThumbPath(m, size), nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (ms *MediaSource) Walk() ([]*Media, error) { |
|
|
|
@ -197,12 +240,12 @@ func (ch *CaddyHugo) serveMediaPage(w http.ResponseWriter, r *http.Request) (int |
|
|
|
|
|
|
|
|
|
for _, m := range media { |
|
|
|
|
|
|
|
|
|
src, width, height, err := ch.Media.ThumbMax(*m, 100) |
|
|
|
|
src, 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 /><br /><input type="text" readonly value=%q /><span class="copy">📋</span></div>`, width, height, src, src) |
|
|
|
|
fmt.Fprintf(w, `<div class="img"><img width=%d height=%d src=%q /><br /><input type="text" readonly value=%q /><span class="copy">📋</span></div>`, size.Dx(), size.Dy(), src, src) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
io.WriteString(w, `<script> |
|
|
|
@ -224,6 +267,38 @@ func (ch *CaddyHugo) serveMediaPage(w http.ResponseWriter, r *http.Request) (int |
|
|
|
|
return 200, nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var ( |
|
|
|
|
sizeString = regexp.MustCompile(`([0-9]*)x([0-9]*)`) |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
func parseSizeString(str string) (image.Rectangle, error) { |
|
|
|
|
var err = fmt.Errorf("expected a size string {width}x{height}, saw %q", str) |
|
|
|
|
|
|
|
|
|
strs := sizeString.FindStringSubmatch(str) |
|
|
|
|
if len(strs) < 3 { |
|
|
|
|
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[2] != "" { |
|
|
|
|
h, strconvErr = strconv.Atoi(strs[2]) |
|
|
|
|
if strconvErr != nil { |
|
|
|
|
return image.ZR, err |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return image.Rect(0, 0, w, h), nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (ch *CaddyHugo) serveMedia(w http.ResponseWriter, r *http.Request) (int, error) { |
|
|
|
|
if ch.Media == nil { |
|
|
|
|
http.NotFound(w, r) |
|
|
|
@ -231,41 +306,22 @@ func (ch *CaddyHugo) serveMedia(w http.ResponseWriter, r *http.Request) (int, er |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
segs := strings.Split(r.URL.Path, "/") |
|
|
|
|
name := segs[2] |
|
|
|
|
file := "" |
|
|
|
|
var err error |
|
|
|
|
m := ch.Media.ByName(name) |
|
|
|
|
name := segs[len(segs)-1] // the last segment is the filename
|
|
|
|
|
|
|
|
|
|
switch len(segs) { |
|
|
|
|
case 3: |
|
|
|
|
file = name |
|
|
|
|
case 4: |
|
|
|
|
var max int |
|
|
|
|
max, err = strconv.Atoi(removeExtension(segs[3])) |
|
|
|
|
if err != nil { |
|
|
|
|
http.Error(w, fmt.Sprintf("expected /media/filename, /media/filename/maxDim, or /media/filename/width/height..."), http.StatusBadRequest) |
|
|
|
|
return 400, nil |
|
|
|
|
} |
|
|
|
|
size := image.Rectangle{} |
|
|
|
|
|
|
|
|
|
file, _, _, err = ch.Media.ThumbMax(*m, max) |
|
|
|
|
case 5: |
|
|
|
|
var width, height int |
|
|
|
|
width, err = strconv.Atoi(segs[3]) |
|
|
|
|
if err != nil { |
|
|
|
|
http.Error(w, fmt.Sprintf("expected /media/filename, /media/filename/maxDim, or /media/filename/width/height.."), http.StatusBadRequest) |
|
|
|
|
return 400, nil |
|
|
|
|
} |
|
|
|
|
height, err = strconv.Atoi(removeExtension(segs[4])) |
|
|
|
|
m := ch.Media.ByName(name) |
|
|
|
|
|
|
|
|
|
if len(segs) >= 4 && len(segs) > 2 { |
|
|
|
|
var err error |
|
|
|
|
size, err = parseSizeString(segs[len(segs)-2]) |
|
|
|
|
if err != nil { |
|
|
|
|
http.Error(w, fmt.Sprintf("expected /media/filename, /media/filename/maxDim, or /media/filename/width/height."), http.StatusBadRequest) |
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest) |
|
|
|
|
return 400, nil |
|
|
|
|
} |
|
|
|
|
file, err = ch.Media.Thumb(*m, width, height) |
|
|
|
|
default: |
|
|
|
|
http.Error(w, fmt.Sprintf("expected /media/filename, /media/filename/maxDim, or /media/filename/width/height"), 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 |
|
|
|
@ -275,7 +331,8 @@ func (ch *CaddyHugo) serveMedia(w http.ResponseWriter, r *http.Request) (int, er |
|
|
|
|
file = file[1:] |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
file = path.Join(ch.Media.ThumbDir, file[len("media/"):]) |
|
|
|
|
file = path.Join(ch.Media.ThumbDir, file) |
|
|
|
|
|
|
|
|
|
http.ServeFile(w, r, file) |
|
|
|
|
|
|
|
|
|
return 200, nil |
|
|
|
|