Compare commits

..

No commits in common. "3308d5419c214dfc3bc67652ce596f5d7bc9d45b" and "1731efcc289a40747fd178aa6feffaca7e78dca2" have entirely different histories.

5 changed files with 3 additions and 114 deletions

2
db.go
View file

@ -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.Expire(ctx, key, ttl)
db.Do(ctx, key, ttl)
}
}

12
main.go
View file

@ -8,7 +8,6 @@ import (
"log"
"net/http"
"strings"
"time"
)
var currentConfig Config
@ -61,18 +60,7 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
secret := GenerateSecret()
url := "/" + GenerateUrl()
content := r.FormValue("content")
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)

View file

@ -27,7 +27,7 @@
<input id="password" type="text">
</li>
<li><label for="exp">Expiration?</label>
<input id="exp" name="exp" type="text" placeholder="vide, 0 ou 1d1h1m1s"></li>
<input id="exp" type="date"></li>
<li>
<label for="type">Type</label>
<select id="type" name="type">

View file

@ -3,11 +3,8 @@ package main
import (
"crypto/rand"
"encoding/hex"
"fmt"
"log"
mathrand "math/rand"
"strconv"
"strings"
)
func GenerateUrl() string {
@ -37,56 +34,3 @@ func UrlExist(url string) bool {
func VerifySecret(url string, secret string) bool {
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
}

View file

@ -1,43 +0,0 @@
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)
}
}