improve dnd; prevent large files early on upload

This commit is contained in:
2026-11-21 19:28:44 -05:00
parent 67849def05
commit 8fc87ab200
8 changed files with 64 additions and 15 deletions
+13 -9
View File
@@ -22,11 +22,13 @@ type Pages struct {
}
type HomePageData struct {
Expiries []config.Expiry
Qt string
PastePlaceholder string
FileMaxSize string
PasteMaxSize string
Expiries []config.Expiry
Qt string
PastePlaceholder string
FileMaxSize string
FileMaxSizeBytes int64
PasteMaxSize string
PasteMaxSizeBytes int64
}
func (pages *Pages) Render(w http.ResponseWriter, name string, data any) {
@@ -108,10 +110,12 @@ 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)),
PasteMaxSize: humanize.Bytes(uint64(cfg.MaxPasteBytes)),
Qt: GetRandomQt(pages.Qts),
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 {
+7 -1
View File
@@ -53,8 +53,14 @@ func UploadHandler(db *gorm.DB, cfg config.Config, p *pages.Pages) http.HandlerF
return
}
maxUploadSize := max(cfg.MaxFileBytes, cfg.MaxPasteBytes)
if r.ContentLength > maxUploadSize {
p.HTTPError(w, r, "upload too large", http.StatusRequestEntityTooLarge)
return
}
// use the largest of the sizes, and then do another size check per type down the line
r.Body = http.MaxBytesReader(w, r.Body, max(cfg.MaxFileBytes, cfg.MaxPasteBytes))
r.Body = http.MaxBytesReader(w, r.Body, maxUploadSize)
var err error
mediaType := "text/plain"