some ui features for uploading
This commit is contained in:
@@ -3,7 +3,9 @@ package caddyhugo
|
||||
import (
|
||||
"fmt"
|
||||
"image"
|
||||
_ "image/gif"
|
||||
"image/jpeg"
|
||||
_ "image/png"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
@@ -24,6 +26,21 @@ func (ms *MediaSource) LocationOrig(m Media) string {
|
||||
return path.Join(ms.StorageDir, m.Name)
|
||||
}
|
||||
|
||||
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)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = io.Copy(f, r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return f.Close()
|
||||
}
|
||||
|
||||
type Media struct {
|
||||
Type string
|
||||
Name string
|
||||
@@ -122,6 +139,38 @@ func (ms *MediaSource) Walk() ([]*Media, error) {
|
||||
return media, err
|
||||
}
|
||||
|
||||
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 != 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)
|
||||
@@ -131,7 +180,11 @@ func (ch *CaddyHugo) serveMediaPage(w http.ResponseWriter, r *http.Request) (int
|
||||
io.WriteString(w, `<html>
|
||||
<head><style>.img { float: left; text-align: center; }</style></head>
|
||||
<body>
|
||||
<div style="position: fixed; top: 0; height: 10vh; width: 100%;">
|
||||
<div style="float: left; width: 100%; height: 10vh;">
|
||||
`)
|
||||
io.WriteString(w, UploadPage("media"))
|
||||
io.WriteString(w, "</div>")
|
||||
if ch.Media != nil {
|
||||
media, err := ch.Media.Walk()
|
||||
if err != nil {
|
||||
@@ -140,10 +193,12 @@ func (ch *CaddyHugo) serveMediaPage(w http.ResponseWriter, r *http.Request) (int
|
||||
}
|
||||
|
||||
for _, m := range media {
|
||||
fmt.Fprintf(w, `<div class="img">`)
|
||||
|
||||
src, width, height, err := ch.Media.ThumbMax(*m, 100)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return 500, 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" disabled value=%q /></div>`, width, height, src, src)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user