package caddyhugo import ( "fmt" "image" "io" "net/http" "path" "strings" "git.stephensearles.com/stephen/caddy-hugo2/media" ) func (ch *CaddyHugo) uploadMedia(w http.ResponseWriter, r *http.Request) (int, error) { if ch.Media == nil { http.NotFound(w, r) return 404, nil } mr, err := r.MultipartReader() if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return 400, nil } for { part, err := mr.NextPart() if err == io.EOF { break } if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return 400, nil } name := part.FileName() if name != "" { err = ch.Media.ReceiveNewMedia(name, part) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return 500, nil } } } return 200, nil } func (ch *CaddyHugo) serveMediaPage(w http.ResponseWriter, r *http.Request) (int, error) { if ch.Media == nil { http.NotFound(w, r) return 404, nil } io.WriteString(w, `
`) io.WriteString(w, UploadPage("media")) io.WriteString(w, `
`) if ch.Media != nil { mm, err := ch.Media.Walk() if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return 500, nil } for _, m := range media.Set(mm).ByDate() { src, size, err := ch.Media.ThumbMax(*m, 100) if err != nil { fmt.Fprintf(w, `
error rendering %q: %v
`, m.Name, err) continue } fmt.Fprintf(w, `

📋
`, size.Dx(), size.Dy(), src, m.Name, src) } } io.WriteString(w, ``) return 200, nil } func (ch *CaddyHugo) serveMedia(w http.ResponseWriter, r *http.Request) (int, error) { if ch.Media == nil { http.NotFound(w, r) 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) return 200, nil }