Version 1.0.2
This commit is contained in:
commit
f2d281dc1f
|
@ -2,7 +2,7 @@ package routes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/sha512"
|
"crypto/sha512"
|
||||||
"encoding/base64"
|
"encoding/hex"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"git.1750studios.com/ToddShepard/ShortDragon/database"
|
"git.1750studios.com/ToddShepard/ShortDragon/database"
|
||||||
|
@ -47,17 +47,17 @@ func Encode(c *gin.Context) {
|
||||||
if URL.Short.String == "" {
|
if URL.Short.String == "" {
|
||||||
hasher := sha512.New()
|
hasher := sha512.New()
|
||||||
hasher.Write([]byte(URL.Long.String))
|
hasher.Write([]byte(URL.Long.String))
|
||||||
base := base64.StdEncoding.EncodeToString(hasher.Sum(nil))
|
hash := hex.EncodeToString(hasher.Sum(nil))
|
||||||
i := 2
|
i := 2
|
||||||
for {
|
for {
|
||||||
database.Db.Model(&database.URL{}).Where("short = ?", base[0:i]).Count(&count)
|
database.Db.Model(&database.URL{}).Where("short = ?", hash[0:i]).Count(&count)
|
||||||
if count > 0 && i < len(base) {
|
if count > 0 && i < len(hash) {
|
||||||
i = i + 1
|
i = i + 1
|
||||||
} else if count > 0 {
|
} else if count > 0 {
|
||||||
c.AbortWithStatus(http.StatusConflict)
|
c.AbortWithStatus(http.StatusConflict)
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
URL.Short.String = base[0:i]
|
URL.Short.String = hash[0:i]
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue