diff options
Diffstat (limited to 'backend/db')
| -rw-r--r-- | backend/db/db.go | 45 |
1 files changed, 30 insertions, 15 deletions
diff --git a/backend/db/db.go b/backend/db/db.go index d40de87..9bfe55a 100644 --- a/backend/db/db.go +++ b/backend/db/db.go @@ -27,22 +27,21 @@ func init() { const TitleLimit = 100 const ContentLimit = 100000 -// creates a new paste with title, content and hash -func New(ip, content, expiry, title, password string) (Paste, error) { +// creates a new paste with title, content and hash, returns the hash of the created paste +func New(ip, content, expiry, title, password string) (string, error) { // generate hash from ip hash := hashing.GenerateURI(ip) // check for size of title and content - errs := "" - if len(title) > TitleLimit { - errs += fmt.Sprintf("title is longer than character limit of %d\n", TitleLimit) - } - if len(content) > ContentLimit { - errs += fmt.Sprintf("content is longer than character limit of %d\n", ContentLimit) + errs := checkLengths(title, content) + if errs != nil { + return "", errs } - // if any errors were found - if errs != "" { - return Paste{}, fmt.Errorf(errs) + + // hash given password + hashedPass, err := hashing.HashPassword(password) + if err != nil { + return "", fmt.Errorf("could not hash password: %s", err.Error()) } // create new struct @@ -50,7 +49,7 @@ func New(ip, content, expiry, title, password string) (Paste, error) { Hash: hash, Content: content, Title: title, - Password: password, + Password: hashedPass, } // check if expiry @@ -59,12 +58,12 @@ func New(ip, content, expiry, title, password string) (Paste, error) { // if time format not current if err != nil { - return Paste{}, err + return "", err } // time is in the past if time.Now().After(t) { - return Paste{}, fmt.Errorf("time %s is in the past", t.String()) + return "", fmt.Errorf("time %s is in the past", t.String()) } new.Expiry = t @@ -77,7 +76,23 @@ func New(ip, content, expiry, title, password string) (Paste, error) { // insert struct log.Infof("create new paste with hash %s", hash) insertErr := insert(new) - return new, insertErr + return hash, insertErr +} + +func checkLengths(title string, content string) error { + errs := "" + if len(title) > TitleLimit { + errs += fmt.Sprintf("title is longer than character limit of %d\n", TitleLimit) + } + if len(content) > ContentLimit { + errs += fmt.Sprintf("content is longer than character limit of %d\n", ContentLimit) + } + // if any errors were found + if errs != "" { + return fmt.Errorf(errs) + } + + return nil } // lookup |