Prevent 500 errors on too-long collection title or description

This truncates long titles and descriptions to the maximum column length, so
we don't get errors back from MySQL.

Fixes #600
pull/782/head
Matt Baer 1 year ago
parent 7db4b699e2
commit 530a36fc53
  1. 7
      collections.go
  2. 9
      database.go
  3. 9
      parse/posts.go

@ -39,7 +39,12 @@ import (
"golang.org/x/net/idna" "golang.org/x/net/idna"
) )
const collAttrLetterReplyTo = "letter_reply_to" const (
collAttrLetterReplyTo = "letter_reply_to"
collMaxLengthTitle = 255
collMaxLengthDescription = 160
)
type ( type (
// TODO: add Direction to db // TODO: add Direction to db

@ -17,6 +17,7 @@ import (
"github.com/go-sql-driver/mysql" "github.com/go-sql-driver/mysql"
"github.com/writeas/web-core/silobridge" "github.com/writeas/web-core/silobridge"
wf_db "github.com/writefreely/writefreely/db" wf_db "github.com/writefreely/writefreely/db"
"github.com/writefreely/writefreely/parse"
"net/http" "net/http"
"net/url" "net/url"
"strings" "strings"
@ -862,6 +863,14 @@ func (db *datastore) GetCollectionFromDomain(host string) (*Collection, error) {
} }
func (db *datastore) UpdateCollection(app *App, c *SubmittedCollection, alias string) error { func (db *datastore) UpdateCollection(app *App, c *SubmittedCollection, alias string) error {
// Truncate fields correctly, so we don't get "Data too long for column" errors in MySQL (writefreely#600)
if c.Title != nil {
*c.Title = parse.Truncate(*c.Title, collMaxLengthTitle)
}
if c.Description != nil {
*c.Description = parse.Truncate(*c.Description, collMaxLengthDescription)
}
q := query.NewUpdate(). q := query.NewUpdate().
SetStringPtr(c.Title, "title"). SetStringPtr(c.Title, "title").
SetStringPtr(c.Description, "description"). SetStringPtr(c.Description, "description").

@ -80,3 +80,12 @@ func TruncToWord(s string, l int) (string, bool) {
} }
return s, truncated return s, truncated
} }
// Truncate truncates the given text to the provided limit, returning the original string if it's shorter than the limit.
func Truncate(s string, l int) string {
c := []rune(s)
if len(c) > l {
s = string(c[:l])
}
return s
}

Loading…
Cancel
Save