implement expiry!!!
oh my fucking GOD bro, i've seen expiry SO SO SO much it idek if i've been spelling it right at this point, please let me be done with this
This commit is contained in:
@@ -14,6 +14,7 @@ type Config struct {
|
||||
UploadDir string
|
||||
MaxPasteBytes int64
|
||||
MaxFileBytes int64
|
||||
Expiries []Expiry
|
||||
|
||||
DatabasePath string
|
||||
}
|
||||
@@ -45,10 +46,21 @@ func loadConfig() (Config, error) {
|
||||
|
||||
databasePath := envOrDefault("MINIBIN_DATABASE_PATH", "minibin.sqlite")
|
||||
|
||||
expiriesSetting := envOrDefault("MINIBIN_EXPIRIES", "1h,12h,1d,7d,2w,1mo,6mo,1y,5y, 0")
|
||||
var expiries []Expiry
|
||||
for entry := range strings.SplitSeq(expiriesSetting, ",") {
|
||||
expiry, err := parseExpiry(entry)
|
||||
if err != nil {
|
||||
return Config{}, fmt.Errorf("MINIBIN_EXPIRIES: %w", err)
|
||||
}
|
||||
expiries = append(expiries, expiry)
|
||||
}
|
||||
|
||||
return Config{
|
||||
UploadDir: uploadDir,
|
||||
MaxPasteBytes: maxPasteKiB * 1024,
|
||||
MaxFileBytes: maxFileKiB * 1024,
|
||||
Expiries: expiries,
|
||||
|
||||
DatabasePath: databasePath,
|
||||
}, nil
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Expiry struct {
|
||||
Label string
|
||||
Amount int
|
||||
Unit string
|
||||
}
|
||||
|
||||
// this will parse something like "1d" into 2 groups: the amount, and the unit
|
||||
var expiryPattern = regexp.MustCompile(`^([1-9][0-9]*)(h|d|w|mo|y)$`)
|
||||
|
||||
func parseExpiry(expiry string) (Expiry, error) {
|
||||
expiry = strings.TrimSpace(expiry)
|
||||
if expiry == "0" {
|
||||
return Expiry{
|
||||
Label: "indefinite",
|
||||
Unit: "indefinite",
|
||||
}, nil
|
||||
}
|
||||
matches := expiryPattern.FindStringSubmatch(expiry)
|
||||
if matches == nil {
|
||||
return Expiry{}, fmt.Errorf("invalid expiry %q", expiry)
|
||||
}
|
||||
|
||||
// parses the string amount to an int
|
||||
// the amount is 2nd (1) in the index because the first (0) is the entire string
|
||||
amount, err := strconv.Atoi(matches[1])
|
||||
if err != nil {
|
||||
return Expiry{}, fmt.Errorf("invalid expiry %q: %w", expiry, err)
|
||||
}
|
||||
|
||||
return Expiry{
|
||||
Label: expiry,
|
||||
Amount: amount,
|
||||
Unit: matches[2],
|
||||
}, nil
|
||||
}
|
||||
|
||||
func findExpiry(expiries []Expiry, label string) (Expiry, bool) {
|
||||
index := slices.IndexFunc(expiries, func(expiry Expiry) bool {
|
||||
return expiry.Label == label
|
||||
})
|
||||
if index == -1 {
|
||||
return Expiry{}, false
|
||||
}
|
||||
return expiries[index], true
|
||||
}
|
||||
|
||||
func (expiry Expiry) ExpiresFrom(now time.Time) (*time.Time, error) {
|
||||
var expires time.Time
|
||||
switch expiry.Unit {
|
||||
case "h":
|
||||
expires = now.Add(time.Duration(expiry.Amount) * time.Hour)
|
||||
case "d":
|
||||
expires = now.AddDate(0, 0, expiry.Amount)
|
||||
case "w":
|
||||
expires = now.AddDate(0, 0, expiry.Amount*7)
|
||||
case "mo":
|
||||
expires = now.AddDate(0, expiry.Amount, 0)
|
||||
case "y":
|
||||
expires = now.AddDate(expiry.Amount, 0, 0)
|
||||
case "indefinite":
|
||||
return nil, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported expiry unit %q", expiry.Unit)
|
||||
}
|
||||
|
||||
return &expires, nil
|
||||
}
|
||||
+20
@@ -70,6 +70,17 @@ func uploadHandler(db *gorm.DB, cfg Config) http.HandlerFunc {
|
||||
redirect = true
|
||||
}
|
||||
|
||||
expLabel := r.URL.Query().Get("expiry")
|
||||
if expLabel == "" {
|
||||
expLabel = cfg.Expiries[0].Label
|
||||
}
|
||||
|
||||
selected, found := findExpiry(cfg.Expiries, expLabel)
|
||||
if !found {
|
||||
http.Error(w, "invalid expiry", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var input UploadInput
|
||||
switch mediaType {
|
||||
case "multipart/form-data": // file uploads
|
||||
@@ -182,6 +193,14 @@ func uploadHandler(db *gorm.DB, cfg Config) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
createdAt := time.Now().UTC().Unix()
|
||||
expires, err := selected.ExpiresFrom(time.Now().UTC())
|
||||
if err != nil {
|
||||
log.Println("calc expiry:", err) // im just using slang
|
||||
http.Error(w, "internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
expiresAt := expires.Unix()
|
||||
|
||||
if utf8.RuneCountInString(input.Filename) > 255 {
|
||||
name := []rune(input.Filename)
|
||||
ext := []rune(filepath.Ext(input.Filename))
|
||||
@@ -199,6 +218,7 @@ func uploadHandler(db *gorm.DB, cfg Config) http.HandlerFunc {
|
||||
Size: size,
|
||||
SHA256: hasher.Sum(nil),
|
||||
CreatedAt: createdAt,
|
||||
ExpiresAt: &expiresAt,
|
||||
LastAccessedAt: createdAt,
|
||||
DeleteToken: rand.Text(),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user