max total storage usage implemented!
code may be a bit messy but.... also implemented a storage usage stat (that can be toggled in .env) that shows on the home page, you can also enable the total limit to show as well eventually i need to implement clearing old unfinished uploads both on startup, admin actions, and cleaning up on upload failure
This commit is contained in:
+19
-19
@@ -11,6 +11,7 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
|
||||
"git.whoswhip.dev/whoswhip/minibin/internal/config"
|
||||
"github.com/dustin/go-humanize"
|
||||
@@ -20,16 +21,7 @@ type Pages struct {
|
||||
Templates *template.Template
|
||||
Qts []string
|
||||
Placeholders []string
|
||||
}
|
||||
|
||||
type HomePageData struct {
|
||||
Expiries []config.Expiry
|
||||
Qt string
|
||||
PastePlaceholder string
|
||||
FileMaxSize string
|
||||
FileMaxSizeBytes int64
|
||||
PasteMaxSize string
|
||||
PasteMaxSizeBytes int64
|
||||
QuotaUsage *atomic.Int64
|
||||
}
|
||||
|
||||
func (pages *Pages) Render(w http.ResponseWriter, name string, data any) {
|
||||
@@ -68,7 +60,7 @@ func GetRandomQt(qts []string) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func LoadPages() (*Pages, error) {
|
||||
func LoadPages(usage *atomic.Int64) (*Pages, error) {
|
||||
templates, err := template.New("").Funcs(template.FuncMap{
|
||||
"hasPrefix": strings.HasPrefix,
|
||||
}).ParseGlob("web/templates/*.html")
|
||||
@@ -79,6 +71,7 @@ func LoadPages() (*Pages, error) {
|
||||
pages := &Pages{
|
||||
Templates: templates,
|
||||
Placeholders: []string{"cmon, paste something already!"},
|
||||
QuotaUsage: usage,
|
||||
}
|
||||
|
||||
qtFiles, err := os.ReadDir("qts")
|
||||
@@ -110,17 +103,24 @@ func LoadPages() (*Pages, error) {
|
||||
|
||||
func HomeHandler(pages *Pages, cfg config.Config) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
data := HomePageData{
|
||||
Expiries: cfg.Expiries,
|
||||
FileMaxSize: humanize.Bytes(uint64(cfg.MaxFileBytes)),
|
||||
FileMaxSizeBytes: cfg.MaxFileBytes,
|
||||
PasteMaxSize: humanize.Bytes(uint64(cfg.MaxPasteBytes)),
|
||||
PasteMaxSizeBytes: cfg.MaxPasteBytes,
|
||||
Qt: GetRandomQt(pages.Qts),
|
||||
data := map[string]any{
|
||||
"Expiries": cfg.Expiries,
|
||||
"FileMaxSize": humanize.Bytes(uint64(cfg.MaxFileBytes)),
|
||||
"FileMaxSizeBytes": cfg.MaxFileBytes,
|
||||
"PasteMaxSize": humanize.Bytes(uint64(cfg.MaxPasteBytes)),
|
||||
"PasteMaxSizeBytes": cfg.MaxPasteBytes,
|
||||
"Qt": GetRandomQt(pages.Qts),
|
||||
}
|
||||
|
||||
if len(pages.Placeholders) > 0 {
|
||||
data.PastePlaceholder = pages.Placeholders[rand.IntN(len(pages.Placeholders))]
|
||||
data["PastePlaceholder"] = pages.Placeholders[rand.IntN(len(pages.Placeholders))]
|
||||
}
|
||||
|
||||
if cfg.DisplayStorageLimit {
|
||||
data["StorageLimit"] = humanize.Bytes(uint64(cfg.MaxStorageUsage))
|
||||
}
|
||||
if cfg.DisplayStorageUsage {
|
||||
data["StorageUsage"] = humanize.Bytes(uint64(pages.QuotaUsage.Load()))
|
||||
}
|
||||
|
||||
pages.Render(w, "home.html", data)
|
||||
|
||||
Reference in New Issue
Block a user