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
+42 -21
View File
@@ -2,7 +2,6 @@ package config
import (
"fmt"
"log"
"net/netip"
"os"
"slices"
@@ -16,12 +15,16 @@ type Config struct {
UploadDir string
MaxPasteBytes int64
MaxFileBytes int64
MaxStorageUsage int64
Expiries []Expiry
NormalizeMIMETypes bool
BannedMIMETypes []string
PoWDifficulty int
ReportOptions []string
DisplayStorageUsage bool
DisplayStorageLimit bool
DatabasePath string
// public host like "https://bin.zip", NOT the address minbin listens on
Host string
@@ -34,25 +37,21 @@ type Config struct {
func LoadConfig() (Config, error) {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
return Config{}, fmt.Errorf("failed to load .env: %w", err)
}
pasteSizeSetting := envOrDefault("MINIBIN_MAX_PASTE_KIB", "1024")
maxPasteKiB, err := strconv.ParseInt(pasteSizeSetting, 10, 64)
maxPasteKiB, err := envKibAsBytes("MINIBIN_MAX_PASTE_KIB", "1024", 1)
if err != nil {
return Config{}, fmt.Errorf("failed to parse MINIBIN_MAX_PASTE_KIB: %w", err)
}
if maxPasteKiB <= 0 {
return Config{}, fmt.Errorf("MINIBIN_MAX_PASTE_KIB must be greater than 0")
return Config{}, err
}
fileSizeSetting := envOrDefault("MINIBIN_MAX_FILE_KIB", "1024")
maxFileKiB, err := strconv.ParseInt(fileSizeSetting, 10, 64)
maxFileKiB, err := envKibAsBytes("MINIBIN_MAX_FILE_KIB", "1024", 1)
if err != nil {
return Config{}, fmt.Errorf("failed to parse MINIBIN_MAX_FILE_KIB: %w", err)
return Config{}, err
}
if maxFileKiB <= 0 {
return Config{}, fmt.Errorf("MINIBIN_MAX_FILE_KIB must be greater than 0")
maxStorageUsageKiB, err := envKibAsBytes("MINIBIN_MAX_STORAGE_USAGE_KIB", "0", 0)
if err != nil {
return Config{}, err
}
expiriesSetting := envOrDefault("MINIBIN_EXPIRIES", "1h,12h,1d,7d,2w,1mo,6mo,1y,5y,0")
@@ -72,9 +71,6 @@ func LoadConfig() (Config, error) {
}
reportOptionsSetting := envOrDefault("MINIBIN_REPORT_OPTIONS", "Malware (including links),CSAM/CSEM,Copyright Infringement,Fraud")
reportOptions := strings.Split(reportOptionsSetting, ",")
if len(reportOptions) == 0 {
return Config{}, fmt.Errorf("no valid report options set")
}
ipAddressSource := envOrDefault("MINIBIN_IP_ADDRESS_SOURCE", "remote")
if !slices.Contains([]string{"remote", "cloudflare", "xff"}, ipAddressSource) {
return Config{}, fmt.Errorf("%s is not a valid MINIBIN_IP_ADDRESS_SOURCE option", ipAddressSource)
@@ -105,23 +101,32 @@ func LoadConfig() (Config, error) {
}
}
return Config{
config := Config{
UploadDir: envOrDefault("MINIBIN_UPLOAD_DIR", "uploads"),
MaxPasteBytes: maxPasteKiB * 1024,
MaxFileBytes: maxFileKiB * 1024,
MaxPasteBytes: maxPasteKiB,
MaxFileBytes: maxFileKiB,
MaxStorageUsage: maxStorageUsageKiB,
Expiries: expiries,
NormalizeMIMETypes: envOrDefault("MINIBIN_NORMALIZE_MIME_TYPES", "true") == "true",
NormalizeMIMETypes: envAsBool("MINIBIN_NORMALIZE_MIME_TYPES", "true"),
BannedMIMETypes: strings.Split(envOrDefault("MINIBIN_BANNED_MIME_TYPES", ""), ","),
PoWDifficulty: powDifficulty,
ReportOptions: reportOptions,
DisplayStorageUsage: envAsBool("MINIBIN_STATS_DISPLAY_STORAGE_USAGE", "false"),
DisplayStorageLimit: envAsBool("MINIBIN_STATS_DISPLAY_STORAGE_LIMIT", "false"),
DatabasePath: envOrDefault("MINIBIN_DATABASE_PATH", "minibin.sqlite"),
Host: envOrDefault("MINIBIN_HOST", "http://localhost:8080"),
Addr: envOrDefault("MINIBIN_ADDR", "localhost:8080"),
IPAdressSource: ipAddressSource,
TrustedProxies: trustedProxies,
BannedClientASNs: bannedAsns,
}, nil
}
if !config.DisplayStorageUsage && config.DisplayStorageLimit {
return Config{}, fmt.Errorf("MINIBIN_STATS_DISPLAY_STORAGE_LIMIT must be enabled when MINIBIN_STATS_DISPLAY_STORAGE_USAGE is enabled")
}
return config, nil
}
func envOrDefault(key, fallback string) string {
@@ -131,3 +136,19 @@ func envOrDefault(key, fallback string) string {
}
return value
}
func envKibAsBytes(key, fallback string, minKiB int64) (int64, error) {
setting := envOrDefault(key, fallback)
value, err := strconv.ParseInt(setting, 10, 64)
if err != nil {
return 0, fmt.Errorf("parse %s: %w", key, err)
}
if value < minKiB {
return 0, fmt.Errorf("%s cannot be less than %v", key, minKiB)
}
return value * 1024, nil
}
func envAsBool(key, fallback string) bool {
return strings.ToLower(envOrDefault(key, fallback)) == "true"
}