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:
2026-12-05 16:24:57 -05:00
parent a1ebd905df
commit c5636c9160
8 changed files with 269 additions and 87 deletions
+59 -36
View File
@@ -15,6 +15,7 @@ import (
"os"
"path/filepath"
"slices"
"strconv"
"strings"
"time"
"unicode/utf8"
@@ -31,6 +32,7 @@ type UploadInput struct {
Filename string
MIMEType string
Type database.ContentType
Size int64
}
type UploadResponse struct {
database.Upload
@@ -109,6 +111,7 @@ func UploadHandler(db *gorm.DB, cfg config.Config, p *pages.Pages) http.HandlerF
input.Reader = file
input.Filename = header.Filename
input.Type = database.ContentFile
input.Size = header.Size
case "text/plain", "application/x-www-form-urlencoded": // paste uploads
r.Body = http.MaxBytesReader(w, r.Body, cfg.MaxPasteBytes)
var content string
@@ -145,6 +148,7 @@ func UploadHandler(db *gorm.DB, cfg config.Config, p *pages.Pages) http.HandlerF
Filename: "paste.txt",
MIMEType: "text/plain; charset=utf-8",
Type: database.ContentPaste,
Size: int64(len(content)),
}
default:
p.HTTPError(w, r, "unsupported Content-Type", http.StatusUnsupportedMediaType)
@@ -181,39 +185,6 @@ func UploadHandler(db *gorm.DB, cfg config.Config, p *pages.Pages) http.HandlerF
return
}
dest, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0600)
if err != nil {
log.Println("create file:", err)
p.HTTPError(w, r, "faield to save upload", http.StatusInternalServerError)
return
}
saved := false
defer func() {
dest.Close()
if !saved {
if err := os.Remove(path); err != nil {
log.Printf("remove incomplete upload %s: %v", id, err)
}
}
}()
hasher := sha256.New()
size, err := io.Copy(io.MultiWriter(dest, hasher), input.Reader)
if err != nil {
if _, ok := errors.AsType[*http.MaxBytesError](err); ok {
p.HTTPError(w, r, "upload too large", http.StatusRequestEntityTooLarge)
return
}
log.Println("write file:", err)
p.HTTPError(w, r, "faield to write upload", http.StatusInternalServerError)
return
}
if err := dest.Close(); err != nil {
log.Println("close file:", err)
p.HTTPError(w, r, "failed to save upload", http.StatusInternalServerError)
return
}
createdAt := time.Now().UTC().Unix()
expires, err := selected.ExpiresFrom(time.Now().UTC())
if err != nil {
@@ -243,14 +214,59 @@ func UploadHandler(db *gorm.DB, cfg config.Config, p *pages.Pages) http.HandlerF
Type: input.Type,
Filename: input.Filename,
MIMEType: input.MIMEType,
Size: size,
SHA256: hasher.Sum(nil),
Size: input.Size,
CreatedAt: createdAt,
ExpiresAt: expiresAt,
LastAccessedAt: createdAt,
DeleteToken: rand.Text(),
}
err = database.InsertUpload(db, &upload)
usedBytes, err := database.ReserveUpload(db, &upload, cfg.MaxStorageUsage)
if err != nil {
if errors.Is(err, database.ErrStorageQuotaExceeded) {
p.HTTPError(w, r, "storage capacity reached", http.StatusInsufficientStorage)
return
}
log.Println("reserve upload:", err)
p.HTTPError(w, r, "failed to reserve upload", http.StatusInternalServerError)
return
}
dest, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0600)
if err != nil {
log.Println("create file:", err)
p.HTTPError(w, r, "failed to save upload", http.StatusInternalServerError)
return
}
saved := false
defer func() {
dest.Close()
if !saved {
if err := os.Remove(path); err != nil {
log.Printf("remove incomplete upload %s: %v", id, err)
}
}
}()
hasher := sha256.New()
_, err = io.Copy(io.MultiWriter(dest, hasher), input.Reader)
if err != nil {
if _, ok := errors.AsType[*http.MaxBytesError](err); ok {
p.HTTPError(w, r, "upload too large", http.StatusRequestEntityTooLarge)
return
}
log.Println("write file:", err)
p.HTTPError(w, r, "faield to write upload", http.StatusInternalServerError)
return
}
if err := dest.Close(); err != nil {
log.Println("close file:", err)
p.HTTPError(w, r, "failed to save upload", http.StatusInternalServerError)
return
}
upload.SHA256 = hasher.Sum(nil)
err = database.CompleteUpload(db, &upload)
if err != nil {
log.Println("save upload:", err)
p.HTTPError(w, r, "failed to save upload", http.StatusInternalServerError)
@@ -266,6 +282,13 @@ func UploadHandler(db *gorm.DB, cfg config.Config, p *pages.Pages) http.HandlerF
Qt: pages.GetRandomQt(p.Qts),
}
if cfg.DisplayStorageUsage {
w.Header().Set("X-Storage-Usage", strconv.FormatInt(usedBytes, 10))
}
if cfg.DisplayStorageLimit {
w.Header().Set("X-Storage-Limit", strconv.FormatInt(cfg.MaxStorageUsage, 10))
}
switch returnType {
// html & maybe xml? cant think of other return types
case "html":