diff --git a/collections.go b/collections.go index 8368447..60536ed 100644 --- a/collections.go +++ b/collections.go @@ -39,7 +39,12 @@ import ( "golang.org/x/net/idna" ) -const collAttrLetterReplyTo = "letter_reply_to" +const ( + collAttrLetterReplyTo = "letter_reply_to" + + collMaxLengthTitle = 255 + collMaxLengthDescription = 160 +) type ( // TODO: add Direction to db diff --git a/database.go b/database.go index 85cce90..ef67130 100644 --- a/database.go +++ b/database.go @@ -17,6 +17,7 @@ import ( "github.com/go-sql-driver/mysql" "github.com/writeas/web-core/silobridge" wf_db "github.com/writefreely/writefreely/db" + "github.com/writefreely/writefreely/parse" "net/http" "net/url" "strings" @@ -862,6 +863,14 @@ func (db *datastore) GetCollectionFromDomain(host string) (*Collection, 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(). SetStringPtr(c.Title, "title"). SetStringPtr(c.Description, "description"). diff --git a/parse/posts.go b/parse/posts.go index 6a9a910..5a39a8d 100644 --- a/parse/posts.go +++ b/parse/posts.go @@ -80,3 +80,12 @@ func TruncToWord(s string, l int) (string, bool) { } 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 +}