files can now be uploaded, added style to confirmation page

This commit is contained in:
TheLastBilly 2023-05-21 22:12:56 -04:00
parent 9d42264db5
commit abed4eed41
5 changed files with 106 additions and 8 deletions

View File

@ -42,6 +42,7 @@ type Options struct {
Title string
ImagePath string
RunManager bool
UploadSize int64
}
type Comic struct {
@ -85,6 +86,7 @@ func (o * Options) Parse() error {
flag.StringVar(&o.Title, "l", "", "Title for new comic")
flag.StringVar(&o.ImagePath, "i", "", "Image path for new comic")
flag.BoolVar(&o.RunManager, "r", false, "Runs the content manager instead of the site")
flag.Int64Var(&o.UploadSize, "z", 10, "Max upload size for posts")
flag.Parse()

View File

@ -1,13 +1,21 @@
package main
import (
"io"
"os"
"log"
"fmt"
"path"
"time"
"strconv"
"strings"
"net/http"
"math/rand"
"path/filepath"
)
var seededRand *rand.Rand = nil
type ConfirmContext struct {
Action string
Message string
@ -15,6 +23,15 @@ type ConfirmContext struct {
OnNo string
}
func randomString(length int) string {
charset := "aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ0123456789"
b := make([]byte, length)
for i := range b {
b[i] = charset[seededRand.Intn(len(charset)-1)]
}
return string(b)
}
func managerIndexView(w http.ResponseWriter, r * http.Request) {
var err error
@ -101,17 +118,63 @@ func managerRemoveView(w http.ResponseWriter, r * http.Request) {
}
func managerPublishView(w http.ResponseWriter, r * http.Request) {
return500(w, r)
var err error
if r.Method == "GET" {
http.Redirect(w, r, fmt.Sprintf("http://%s:%d", options.Address, options.Port), http.StatusSeeOther)
return
}
if r.Method != "POST" {
return404(w, r)
return
}
defer func() {
if err != nil {
errlog.Println(err)
return500(w, r)
}
} ()
r.ParseMultipartForm(options.UploadSize * (1 << 20))
file, handler, err := r.FormFile("image")
if err != nil {
return
}
defer file.Close()
title := r.FormValue("title")
if len(title) > 80 {
title = title[:80]
}
ext := path.Ext(handler.Filename)
filename := randomString(10)+ext
f, err := os.OpenFile(filepath.Join(options.MediaPath, filename), os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
return
}
defer f.Close()
io.Copy(f, file)
err = newComic(title, filename, "", "")
if err != nil {
return
}
http.Redirect(w, r, fmt.Sprintf("http://%s:%d", options.Address, options.Port), http.StatusSeeOther)
}
func managerEditView(w http.ResponseWriter, r * http.Request) {
}
func startManager(address string, port int, dbPath string, mediaPath string) error {
seededRand = rand.New(rand.NewSource(time.Now().UnixNano()))
http.HandleFunc("/", managerIndexView)
http.HandleFunc("/edit/", managerEditView)
http.HandleFunc("/remove/", managerRemoveView)
http.HandleFunc("/publish", managerPublishView)
http.HandleFunc("/edit/", managerEditView)
uri := fmt.Sprintf("%s:%d", address, port)
log.Println("listening to http://" + uri)

View File

@ -11,8 +11,8 @@
<div class="confirm">
<h1>{{ .Message }}</h1>
<div class="options">
<a href="{{ .OnYes }}"><p>Yes</p></a>
<a href="{{ .OnNo }}"><p>No</p></a>
<a href="{{ .OnYes }}"><p>&ltYes&gt</p></a>
<a href="{{ .OnNo }}"><p>&ltNo&gt</p></a>
</div>
</div>
</body>

View File

@ -23,12 +23,11 @@
{{ template "top" . }}
<div class="manager-publish">
<form action="/publish" method="post" enctype="multipart/form-data">
<form enctype="multipart/form-data" action="/publish" method="post">
<label for="title">&ltTitle&gt</label>
<input type="text" class="title" name="title" onkeypress="return event.keyCode != 13;">
<input type="file" name="image" style="display:none;" id="browse">
<label type="submit" class="browse fake-button" for="browse" class="fake-button"
accept=".jpg, .jpeg, .png">Browse</label>
<input type="file" name="image" style="display:none;" id="browse" accept=".jpg, .jpeg, .png">
<label type="submit" class="browse fake-button" for="browse" class="fake-button">Browse</label>
<input type="submit" style="display:none;" id="publish">
<label type="submit" class="publish fake-button" for="publish">Publish</label>
</form>

View File

@ -0,0 +1,34 @@
.confirm {
margin-top: 5em;
width: 100%
}
.confirm h1 {
font-size: 3em;
border-bottom: 10px solid #ebdbb2;
}
.confirm .options {
display: flex;
}
.confirm .options a {
display: block;
margin: auto;
background-color: #ebdbb2;
color: #282828;
}
.confirm .options a:hover {
color: #ebdbb2;
background-color: #282828;
}
.confirm .options p {
margin-top: 0.2em;
margin-bottom: 0.2em;
font-size: 2em;
text-transform: uppercase;
width: 5em;
text-align: center;
}