add: Plak expiration support
This commit is contained in:
parent
1731efcc28
commit
bc35ef6428
5 changed files with 100 additions and 3 deletions
2
db.go
2
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)
|
err = db.HSet(ctx, key, "secret", hash.secret)
|
||||||
if ttl > -1 {
|
if ttl > -1 {
|
||||||
db.Do(ctx, key, ttl)
|
db.Expire(ctx, key, ttl)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
12
main.go
12
main.go
|
@ -8,6 +8,7 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var currentConfig Config
|
var currentConfig Config
|
||||||
|
@ -60,7 +61,16 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
|
||||||
secret := GenerateSecret()
|
secret := GenerateSecret()
|
||||||
url := "/" + GenerateUrl()
|
url := "/" + GenerateUrl()
|
||||||
content := r.FormValue("content")
|
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)
|
http.Redirect(w, r, url, http.StatusSeeOther)
|
||||||
} else {
|
} else {
|
||||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||||
|
|
|
@ -27,7 +27,7 @@
|
||||||
<input id="password" type="text">
|
<input id="password" type="text">
|
||||||
</li>
|
</li>
|
||||||
<li><label for="exp">Expiration?</label>
|
<li><label for="exp">Expiration?</label>
|
||||||
<input id="exp" type="date"></li>
|
<input id="exp" name="exp" type="text" placeholder="vide, 0 ou 1d1h1m1"></li>
|
||||||
<li>
|
<li>
|
||||||
<label for="type">Type</label>
|
<label for="type">Type</label>
|
||||||
<select id="type" name="type">
|
<select id="type" name="type">
|
||||||
|
|
44
utils.go
44
utils.go
|
@ -5,6 +5,8 @@ import (
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"log"
|
"log"
|
||||||
mathrand "math/rand"
|
mathrand "math/rand"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GenerateUrl() string {
|
func GenerateUrl() string {
|
||||||
|
@ -34,3 +36,45 @@ func UrlExist(url string) bool {
|
||||||
func VerifySecret(url string, secret string) bool {
|
func VerifySecret(url string, secret string) bool {
|
||||||
return secret == db.HGet(ctx, url, "secret").Val()
|
return secret == db.HGet(ctx, url, "secret").Val()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func parseIntBeforeSeparator(source *string, sep string) int { // return -1 if error, only accept positive number
|
||||||
|
var value int
|
||||||
|
var err error
|
||||||
|
if strings.Contains(*source, sep) {
|
||||||
|
value, err = strconv.Atoi(strings.Split(*source, sep)[0])
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
if value < 0 { // Only positive value is correct
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
*source = strings.Join(strings.Split(*source, sep)[1:], "")
|
||||||
|
}
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseExpiration(source string) int { // return -1 if error
|
||||||
|
var expiration int
|
||||||
|
if source == "0" {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
expiration = 86400 * parseIntBeforeSeparator(&source, "d")
|
||||||
|
if expiration < 0 {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
expiration += 3600 * parseIntBeforeSeparator(&source, "h")
|
||||||
|
if expiration < 0 {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
expiration += 60 * parseIntBeforeSeparator(&source, "m")
|
||||||
|
if expiration < 0 {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
expiration += parseIntBeforeSeparator(&source, "s")
|
||||||
|
if expiration < 0 {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
|
return expiration
|
||||||
|
}
|
||||||
|
|
43
utils_test.go
Normal file
43
utils_test.go
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestParseExpirationFull(t *testing.T) { // test parseExpirationFull with all valid separator
|
||||||
|
result := parseExpiration("2d1h3m47s")
|
||||||
|
correctValue := 176627
|
||||||
|
if result != correctValue {
|
||||||
|
t.Fatal("Error in parseExpirationFull, want : ", correctValue, "got : ", result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseExpirationMissing(t *testing.T) { // test parseExpirationFull with all valid separator
|
||||||
|
result := parseExpiration("1h47s")
|
||||||
|
correctValue := 3647
|
||||||
|
if result != correctValue {
|
||||||
|
t.Fatal("Error in parseExpirationFull, want : ", correctValue, "got : ", result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseExpirationNull(t *testing.T) { // test parseExpirationFull with all valid separator
|
||||||
|
result := parseExpiration("0")
|
||||||
|
correctValue := 0
|
||||||
|
if result != correctValue {
|
||||||
|
t.Fatal("Error in parseExpirationFull, want : ", correctValue, "got : ", result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseExpirationNegative(t *testing.T) { // test parseExpirationFull with all valid separator
|
||||||
|
result := parseExpiration("-42h1m1d4s")
|
||||||
|
correctValue := -1
|
||||||
|
if result != correctValue {
|
||||||
|
t.Fatal("Error in parseExpirationFull, want : ", correctValue, "got : ", result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseExpirationInvalid(t *testing.T) { // test parseExpirationFull with all valid separator
|
||||||
|
result := parseExpiration("8h42h1m1d4s")
|
||||||
|
correctValue := -1
|
||||||
|
if result != correctValue {
|
||||||
|
t.Fatal("Error in parseExpirationFull, want : ", correctValue, "got : ", result)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue