diff --git a/go.mod b/go.mod index ce86e98..06fc60c 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module git.whoswhip.dev/whoswhip/minibin go 1.27.1 require ( + github.com/dustin/go-humanize v1.0.1 github.com/gabriel-vasile/mimetype v1.4.16 github.com/glebarez/sqlite v1.11.0 github.com/joho/godotenv v1.5.1 @@ -10,7 +11,6 @@ require ( ) require ( - github.com/dustin/go-humanize v1.0.1 // indirect github.com/glebarez/go-sqlite v1.21.2 // indirect github.com/google/uuid v1.3.0 // indirect github.com/jinzhu/inflection v1.0.0 // indirect diff --git a/handlers.go b/handlers.go index 24b0ee1..86f236c 100644 --- a/handlers.go +++ b/handlers.go @@ -27,6 +27,8 @@ type HomePageData struct { Expiries []Expiry Qt string PastePlaceholder string + FileMaxSize string + PasteMaxSize string } func health(w http.ResponseWriter, r *http.Request) { @@ -45,6 +47,7 @@ type UploadResponse struct { SHA256 string `json:"sha256"` DeleteToken string `json:"deleteToken"` FileExtension string `json:"extension"` // useful for things like sharex to append the extension + Qt string `json:"-"` } type ContentInfoResponse struct { Upload @@ -59,13 +62,13 @@ func render(pages *template.Template, w http.ResponseWriter, name string, data a } } -func staticPage(pages *template.Template, name string) http.HandlerFunc { +func staticPage(pages *template.Template, name string, qts []string) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - render(pages, w, name, nil) + render(pages, w, name, getRandomQt(qts)) } } -func uploadHandler(db *gorm.DB, cfg Config, pages *template.Template) http.HandlerFunc { +func uploadHandler(db *gorm.DB, cfg Config, pages *template.Template, qts []string) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost && r.Method != http.MethodPut { w.Header().Set("Allow", "POST,PUT") @@ -273,6 +276,7 @@ func uploadHandler(db *gorm.DB, cfg Config, pages *template.Template) http.Handl SHA256: hex.EncodeToString(upload.SHA256), DeleteToken: upload.DeleteToken, FileExtension: filepath.Ext(upload.Filename), + Qt: getRandomQt(qts), } switch returnType { diff --git a/main.go b/main.go index fcd269d..3d869b3 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,8 @@ import ( "os" "path/filepath" "strings" + + "github.com/dustin/go-humanize" ) func main() { @@ -97,11 +99,10 @@ func main() { )) mux.HandleFunc("GET /{$}", func(w http.ResponseWriter, r *http.Request) { data := HomePageData{ - Expiries: config.Expiries, - } - - if len(qts) > 0 { - data.Qt = qts[rand.IntN(len(qts))] + Expiries: config.Expiries, + FileMaxSize: humanize.Bytes(uint64(config.MaxFileBytes)), + PasteMaxSize: humanize.Bytes(uint64(config.MaxPasteBytes)), + Qt: getRandomQt(qts), } if len(placeholders) > 0 { @@ -110,21 +111,22 @@ func main() { render(pages, w, "home.html", data) }) - mux.HandleFunc("GET /tos", staticPage(pages, "tos.html")) - mux.HandleFunc("GET /privacy", staticPage(pages, "privacy.html")) + mux.HandleFunc("GET /tos", staticPage(pages, "tos.html", qts)) + mux.HandleFunc("GET /privacy", staticPage(pages, "privacy.html", qts)) mux.HandleFunc("GET /faq", func(w http.ResponseWriter, r *http.Request) { render(pages, w, "faq.html", map[string]any{ "Expiries": config.Expiries, "Host": config.Host, "NormalizeMIMETypes": config.NormalizeMIMETypes, + "Qt": getRandomQt(qts), }) }) uploads := http.NewServeMux() uploads.HandleFunc("GET /{id}", contentHandler(db, config, tracker)) uploads.HandleFunc("GET /{id}/info", contentInfoHandler(db, tracker)) mux.Handle("/", uploads) - mux.HandleFunc("POST /upload", uploadHandler(db, config, pages)) - mux.HandleFunc("PUT /upload", uploadHandler(db, config, pages)) + mux.HandleFunc("POST /upload", uploadHandler(db, config, pages, qts)) + mux.HandleFunc("PUT /upload", uploadHandler(db, config, pages, qts)) mux.HandleFunc("DELETE /{id}", deleteHandler(db, config)) mux.HandleFunc("POST /{id}/delete", deleteHandler(db, config)) mux.HandleFunc("GET /health", health) @@ -136,3 +138,10 @@ func main() { log.Fatal(err) } } + +func getRandomQt(qts []string) string { + if len(qts) > 0 { + return qts[rand.IntN(len(qts))] + } + return "" +} diff --git a/static/css/main.css b/static/css/main.css index 29e2ed5..c4dd7c3 100644 --- a/static/css/main.css +++ b/static/css/main.css @@ -24,6 +24,7 @@ body { background-repeat: no-repeat; background-position: bottom right; background-size: auto 256px; + background-attachment: fixed; } b { diff --git a/templates/faq.html b/templates/faq.html index a59aa13..670bb39 100644 --- a/templates/faq.html +++ b/templates/faq.html @@ -5,6 +5,13 @@ faq | minibin + {{if .Qt}} + + {{end}} {{template "site-header"}} diff --git a/templates/home.html b/templates/home.html index 024b138..38d0985 100644 --- a/templates/home.html +++ b/templates/home.html @@ -26,17 +26,21 @@
- - - - +
+ + + + + max {{.FileMaxSize}} +
+
diff --git a/templates/privacy.html b/templates/privacy.html index 6e66e1c..bb5ca9a 100644 --- a/templates/privacy.html +++ b/templates/privacy.html @@ -5,6 +5,13 @@ privacy | minibin + {{if .}} + + {{end}} {{template "site-header"}} diff --git a/templates/tos.html b/templates/tos.html index 4d177fc..9eff901 100644 --- a/templates/tos.html +++ b/templates/tos.html @@ -5,6 +5,13 @@ tos | minibin + {{if .}} + + {{end}} {{template "site-header"}} diff --git a/templates/upload.html b/templates/upload.html index 3154973..15ef924 100644 --- a/templates/upload.html +++ b/templates/upload.html @@ -5,6 +5,13 @@ {{.ID}} | minibin + {{if .Qt}} + + {{end}}