aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore2
-rw-r--r--backend/api/routes.go4
-rw-r--r--backend/db/db.go25
-rw-r--r--backend/db/schemas.go1
4 files changed, 26 insertions, 6 deletions
diff --git a/.gitignore b/.gitignore
index eae8e6c..c508331 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,4 @@
-.env
+backend/.env
.idea
go.sum
.DS_Store
diff --git a/backend/api/routes.go b/backend/api/routes.go
index 760ee35..a65c886 100644
--- a/backend/api/routes.go
+++ b/backend/api/routes.go
@@ -22,6 +22,7 @@ func insertFunc(w http.ResponseWriter, r *http.Request) {
_ = r.ParseMultipartForm(0)
expiry := r.FormValue("expiry")
content := r.FormValue("content")
+ title := r.FormValue("title")
// get ip
ip := getIP(r)
@@ -29,8 +30,9 @@ func insertFunc(w http.ResponseWriter, r *http.Request) {
log.Infof("got content '%s' and ip '%s'", content, ip)
// insert content
- err := db.New(ip, content, expiry)
+ err := db.New(ip, content, expiry, title)
if err != nil {
+ w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "got err: %s", err.Error())
}
}
diff --git a/backend/db/db.go b/backend/db/db.go
index 053ba87..4451f34 100644
--- a/backend/db/db.go
+++ b/backend/db/db.go
@@ -24,15 +24,32 @@ func init() {
initSessions(mUser, mPass, mIP)
}
-// creates a new paste with content and hash
-func New(ip, content, expiry string) error {
+const TitleLimit = 100
+const ContentLimit = 100000
+
+// creates a new paste with title, content and hash
+func New(ip, content, expiry, title 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)
+ }
+ // if any errors were found
+ if errs != "" {
+ return fmt.Errorf(errs)
+ }
+
// create new struct
new := Paste{
Hash: hash,
Content: content,
+ Title: title,
}
// check if expiry
@@ -45,8 +62,8 @@ func New(ip, content, expiry string) error {
}
// time is in the past
- if t.After(time.Now()) {
- return fmt.Errorf("err: time %s is in the past", t.String())
+ if time.Now().After(t) {
+ return fmt.Errorf("time %s is in the past", t.String())
}
new.Expiry = t
diff --git a/backend/db/schemas.go b/backend/db/schemas.go
index bdfa60c..62c5a11 100644
--- a/backend/db/schemas.go
+++ b/backend/db/schemas.go
@@ -12,4 +12,5 @@ type Paste struct {
Hash string
Content string
Expiry time.Time `bson:"expiry"`
+ Title string
}