Compare commits
2 commits
1731efcc28
...
3308d5419c
Author | SHA1 | Date | |
---|---|---|---|
3308d5419c | |||
d5610eb24c |
5 changed files with 114 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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
14
main.go
14
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,18 @@ 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, 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)
|
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 1d1h1m1s"></li>
|
||||||
<li>
|
<li>
|
||||||
<label for="type">Type</label>
|
<label for="type">Type</label>
|
||||||
<select id="type" name="type">
|
<select id="type" name="type">
|
||||||
|
|
56
utils.go
56
utils.go
|
@ -3,8 +3,11 @@ package main
|
||||||
import (
|
import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
mathrand "math/rand"
|
mathrand "math/rand"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GenerateUrl() string {
|
func GenerateUrl() string {
|
||||||
|
@ -34,3 +37,56 @@ 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, error) { // 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, fmt.Errorf("parseIntBeforeSeparator : \"%s\" : cannot parse value as int", *source)
|
||||||
|
}
|
||||||
|
if value < 0 { // Only positive value is correct
|
||||||
|
return -1, fmt.Errorf("parseIntBeforeSeparator : \"%s\" : format only take positive value", *source)
|
||||||
|
}
|
||||||
|
*source = strings.Join(strings.Split(*source, sep)[1:], "")
|
||||||
|
}
|
||||||
|
return value, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func ParseExpiration(source string) (int, error) { // return -1 if error
|
||||||
|
var expiration int
|
||||||
|
var tempOutput int
|
||||||
|
var err error
|
||||||
|
errMessage := "ParseExpiration : \"%s\" : invalid syntax"
|
||||||
|
if source == "0" {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
tempOutput, err = parseIntBeforeSeparator(&source, "d")
|
||||||
|
expiration = tempOutput * 86400
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
return -1, fmt.Errorf(errMessage, source)
|
||||||
|
}
|
||||||
|
tempOutput, err = parseIntBeforeSeparator(&source, "h")
|
||||||
|
expiration += tempOutput * 3600
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
return -1, fmt.Errorf(errMessage, source)
|
||||||
|
}
|
||||||
|
tempOutput, err = parseIntBeforeSeparator(&source, "m")
|
||||||
|
expiration += tempOutput * 60
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
return -1, fmt.Errorf(errMessage, source)
|
||||||
|
}
|
||||||
|
tempOutput, err = parseIntBeforeSeparator(&source, "s")
|
||||||
|
expiration += tempOutput * 1
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
return -1, fmt.Errorf(errMessage, source)
|
||||||
|
}
|
||||||
|
|
||||||
|
return expiration, nil
|
||||||
|
}
|
||||||
|
|
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("-42h1m4s")
|
||||||
|
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