Create paste duratation function

This commit is contained in:
Eliot Whalan 2016-07-04 09:59:31 +10:00
parent b5ece561b5
commit d87b462b1d
1 changed files with 21 additions and 36 deletions

57
main.go
View File

@ -78,6 +78,25 @@ func hash(paste string) string {
sha := base64.URLEncoding.EncodeToString(hasher.Sum(nil)) sha := base64.URLEncoding.EncodeToString(hasher.Sum(nil))
return sha return sha
} }
func durationFromExpiry(expiry string) time.Duration {
switch expiry {
case "5 minutes":
return time.Minute * 5
case "1 hour":
return time.Hour + 1 // XXX: did you mean '*'?
case "1 day":
return time.Hour * 24
case "1 week":
return time.Hour * 24 * 7
case "1 month":
return time.Hour * 24 * 30
case "1 year":
return time.Hour * 24 * 365
case "forever":
return time.Hour * 24 * (365 * 20)
}
return time.Hour * 24 * (365 * 20)
}
func save(raw string, lang string, title string, expiry string) []string { func save(raw string, lang string, title string, expiry string) []string {
db, err := sql.Open("mysql", DATABASE) db, err := sql.Open("mysql", DATABASE)
@ -101,43 +120,9 @@ func save(raw string, lang string, title string, expiry string) []string {
} else { } else {
url = ADDRESS + "/p/" + id + "/" + lang url = ADDRESS + "/p/" + id + "/" + lang
} }
now := time.Now() const timeFormat = "2006-01-02 15:04:05"
var expiryTime string expiryTime := time.Now().Add(durationFromExpiry(expiry)).Format(timeFormat)
switch expiry {
case "5 minutes":
expiryTime = now.Add(time.Minute * 5).Format("2006-01-02 15:04:05")
break
case "1 hour":
expiryTime = now.Add(time.Hour + 1).Format("2006-01-02 15:04:05")
break
case "1 day":
expiryTime = now.Add(time.Hour * 24 * 1).Format("2006-01-02 15:04:05")
break
case "1 week":
expiryTime = now.Add(time.Hour * 24 * 7).Format("2006-01-02 15:04:05")
break
case "1 month":
expiryTime = now.Add(time.Hour * 24 * 30).Format("2006-01-02 15:04:05")
break
case "1 year":
expiryTime = now.Add(time.Hour * 24 * 365).Format("2006-01-02 15:04:05")
break
case "forever":
expiryTime = now.Add(time.Hour * 24 * (365 * 20)).Format("2006-01-02 15:04:05")
break
default:
expiryTime = now.Add(time.Hour * 24 * (365 * 20)).Format("2006-01-02 15:04:05")
break
}
delKey := uniuri.NewLen(40) delKey := uniuri.NewLen(40)
dataEscaped := html.EscapeString(raw) dataEscaped := html.EscapeString(raw)