From bc35ef6428800f7cc96bcea42438050c5e6d276e Mon Sep 17 00:00:00 2001 From: Ada Date: Thu, 28 Dec 2023 00:02:50 +0100 Subject: [PATCH] add: Plak expiration support --- db.go | 2 +- main.go | 12 +++++++++++- static/index.html | 2 +- utils.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ utils_test.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 100 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..c72ed78 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "log" "net/http" "strings" + "time" ) var currentConfig Config @@ -60,7 +61,16 @@ 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 := parseExpiration(rawExpiration) + 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..a19eac5 100644 --- a/static/index.html +++ b/static/index.html @@ -27,7 +27,7 @@
  • -
  • +