From a9d3b089553b5e57eb9aaa816114ac6c40d084f6 Mon Sep 17 00:00:00 2001 From: Ada Date: Thu, 28 Dec 2023 00:02:50 +0100 Subject: [PATCH] :sparkles: Plak expiration support --- db.go | 2 +- main.go | 14 +++++++++++- static/index.html | 2 +- utils.go | 56 +++++++++++++++++++++++++++++++++++++++++++++++ utils_test.go | 43 ++++++++++++++++++++++++++++++++++++ 5 files changed, 114 insertions(+), 3 deletions(-) create mode 100644 utils_test.go diff --git a/db.go b/db.go index d6b60b7..a156541 100644 --- a/db.go +++ b/db.go @@ -35,7 +35,7 @@ func insertPaste(key string, content string, secret string, ttl time.Duration) { } err = db.HSet(ctx, key, "secret", hash.secret) if ttl > -1 { - db.Do(ctx, key, ttl) + db.Expire(ctx, key, ttl) } } diff --git a/main.go b/main.go index 2fdec8a..3064b46 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "log" "net/http" "strings" + "time" ) var currentConfig Config @@ -60,7 +61,18 @@ func handleRequest(w http.ResponseWriter, r *http.Request) { secret := GenerateSecret() url := "/" + GenerateUrl() content := r.FormValue("content") - insertPaste(url, content, secret, -1) + rawExpiration := r.FormValue("exp") + expiration, err := ParseExpiration(rawExpiration) + if err != nil { + log.Println(err) + } else if expiration == 0 { + insertPaste(url, content, secret, -1) + } else if expiration == -1 { + w.WriteHeader(http.StatusBadRequest) + } else { + insertPaste(url, content, secret, time.Duration(expiration*int(time.Second))) + } + http.Redirect(w, r, url, http.StatusSeeOther) } else { w.WriteHeader(http.StatusMethodNotAllowed) diff --git a/static/index.html b/static/index.html index 40eb943..dfaa289 100644 --- a/static/index.html +++ b/static/index.html @@ -27,7 +27,7 @@
  • -
  • +