Albums: Shorten title and slug to avoid "data too long" error #2181

Related GitHub Discussions:
 - https://github.com/photoprism/photoprism/discussions/2151
 - https://github.com/photoprism/photoprism/discussions/2179
This commit is contained in:
Michael Mayer 2022-03-24 21:46:25 +01:00
parent 9d110e8b80
commit 0e7fec1cec
6 changed files with 25 additions and 27 deletions

View file

@ -7,7 +7,6 @@ import (
"strings"
"time"
"github.com/gosimple/slug"
"github.com/jinzhu/gorm"
"github.com/ulule/deepcopier"
@ -133,7 +132,7 @@ func NewAlbum(albumTitle, albumType string) *Album {
// NewFolderAlbum creates a new folder album.
func NewFolderAlbum(albumTitle, albumPath, albumFilter string) *Album {
albumSlug := slug.Make(albumPath)
albumSlug := txt.Slug(albumPath)
if albumTitle == "" || albumSlug == "" || albumPath == "" || albumFilter == "" {
return nil
@ -144,9 +143,9 @@ func NewFolderAlbum(albumTitle, albumPath, albumFilter string) *Album {
result := &Album{
AlbumOrder: SortOrderAdded,
AlbumType: AlbumFolder,
AlbumTitle: albumTitle,
AlbumSlug: albumSlug,
AlbumPath: albumPath,
AlbumTitle: txt.Clip(albumTitle, txt.ClipDefault),
AlbumSlug: txt.Clip(albumSlug, txt.ClipSlug),
AlbumPath: txt.Clip(albumPath, txt.ClipPath),
AlbumFilter: albumFilter,
CreatedAt: now,
UpdatedAt: now,
@ -166,8 +165,8 @@ func NewMomentsAlbum(albumTitle, albumSlug, albumFilter string) *Album {
result := &Album{
AlbumOrder: SortOrderOldest,
AlbumType: AlbumMoment,
AlbumTitle: albumTitle,
AlbumSlug: albumSlug,
AlbumTitle: txt.Clip(albumTitle, txt.ClipDefault),
AlbumSlug: txt.Clip(albumSlug, txt.ClipSlug),
AlbumFilter: albumFilter,
CreatedAt: now,
UpdatedAt: now,
@ -190,8 +189,8 @@ func NewStateAlbum(albumTitle, albumSlug, albumFilter string) *Album {
result := &Album{
AlbumOrder: SortOrderNewest,
AlbumType: AlbumState,
AlbumTitle: albumTitle,
AlbumSlug: albumSlug,
AlbumTitle: txt.Clip(albumTitle, txt.ClipDefault),
AlbumSlug: txt.Clip(albumSlug, txt.ClipSlug),
AlbumFilter: albumFilter,
CreatedAt: now,
UpdatedAt: now,
@ -276,7 +275,7 @@ func FindAlbumByAttr(slugs, filters []string, albumType string) *Album {
// FindFolderAlbum finds a matching folder album or returns nil.
func FindFolderAlbum(albumPath string) *Album {
albumPath = strings.Trim(albumPath, string(os.PathSeparator))
albumSlug := slug.Make(albumPath)
albumSlug := txt.Slug(albumPath)
if albumSlug == "" {
return nil
@ -391,8 +390,8 @@ func (m *Album) SetTitle(title string) {
// UpdateSlug updates title and slug of generated albums if needed.
func (m *Album) UpdateSlug(title, slug string) error {
title = strings.TrimSpace(title)
slug = strings.TrimSpace(slug)
title = txt.Clip(title, txt.ClipDefault)
slug = txt.Clip(slug, txt.ClipSlug)
if title == "" || slug == "" {
return nil
@ -416,6 +415,9 @@ func (m *Album) UpdateSlug(title, slug string) error {
// UpdateState updates the album location.
func (m *Album) UpdateState(title, slug, stateName, countryCode string) error {
title = txt.Clip(title, txt.ClipDefault)
slug = txt.Clip(slug, txt.ClipSlug)
if title == "" || slug == "" || stateName == "" || countryCode == "" {
return nil
}
@ -482,7 +484,7 @@ func (m *Album) Updates(values interface{}) error {
// UpdateFolder updates the path, filter and slug for a folder album.
func (m *Album) UpdateFolder(albumPath, albumFilter string) error {
albumPath = strings.Trim(albumPath, string(os.PathSeparator))
albumSlug := slug.Make(albumPath)
albumSlug := txt.Slug(albumPath)
if albumSlug == "" {
return nil
@ -592,7 +594,7 @@ func (m *Album) Title() string {
// ZipName returns the zip download filename.
func (m *Album) ZipName() string {
s := slug.Make(m.AlbumTitle)
s := txt.Slug(m.AlbumTitle)
if len(s) < 2 {
s = fmt.Sprintf("photoprism-album-%s", m.AlbumUID)

View file

@ -4,7 +4,6 @@ import (
"testing"
"time"
"github.com/gosimple/slug"
"github.com/stretchr/testify/assert"
"github.com/photoprism/photoprism/internal/form"
@ -21,7 +20,7 @@ func TestNewAlbum(t *testing.T) {
album := NewAlbum("", AlbumDefault)
defaultName := time.Now().Format("January 2006")
defaultSlug := slug.Make(defaultName)
defaultSlug := txt.Slug(defaultName)
assert.Equal(t, defaultName, album.AlbumTitle)
assert.Equal(t, defaultSlug, album.AlbumSlug)
@ -50,7 +49,7 @@ func TestAlbum_SetName(t *testing.T) {
album.SetTitle("")
expected := album.CreatedAt.Format("January 2006")
assert.Equal(t, expected, album.AlbumTitle)
assert.Equal(t, slug.Make(expected), album.AlbumSlug)
assert.Equal(t, txt.Slug(expected), album.AlbumSlug)
})
t.Run("long name", func(t *testing.T) {
longName := `A value in decimal degrees to a precision of 4 decimal places is precise to 11.132 meters at the
@ -65,7 +64,7 @@ is an oblate spheroid.`
slugExpected := txt.Clip(longName, txt.ClipSlug)
album := NewAlbum(longName, AlbumDefault)
assert.Equal(t, expected, album.AlbumTitle)
assert.Contains(t, album.AlbumSlug, slug.Make(slugExpected))
assert.Contains(t, album.AlbumSlug, txt.Slug(slugExpected))
})
}

View file

@ -5,8 +5,6 @@ import (
"strings"
"time"
"github.com/gosimple/slug"
"github.com/photoprism/photoprism/internal/entity"
"github.com/photoprism/photoprism/internal/maps"
"github.com/photoprism/photoprism/pkg/sanitize"
@ -123,12 +121,12 @@ func (m Moment) Slug() (s string) {
s = fmt.Sprintf("%s-%s", country, state)
}
return slug.Make(s)
return txt.Slug(s)
}
// TitleSlug returns an identifier string based on the title.
func (m Moment) TitleSlug() string {
return slug.Make(m.Title())
return txt.Slug(m.Title())
}
// Title returns an english title for the moment.

View file

@ -3,7 +3,6 @@ package search
import (
"strings"
"github.com/gosimple/slug"
"github.com/photoprism/photoprism/internal/entity"
"github.com/photoprism/photoprism/internal/form"
"github.com/photoprism/photoprism/pkg/sanitize"
@ -56,7 +55,7 @@ func Labels(f form.SearchLabels) (results []Label, err error) {
var categories []entity.Category
var label entity.Label
slugString := slug.Make(f.Query)
slugString := txt.Slug(f.Query)
likeString := "%" + f.Query + "%"
if result := Db().First(&label, "label_slug = ? OR custom_slug = ?", slugString, slugString); result.Error != nil {

View file

@ -7,7 +7,6 @@ import (
"github.com/photoprism/photoprism/pkg/sanitize"
"github.com/photoprism/photoprism/pkg/txt"
"github.com/gosimple/slug"
"github.com/jinzhu/inflection"
)
@ -172,7 +171,7 @@ func AnySlug(col, search, sep string) (where string) {
for _, w := range strings.Split(search, sep) {
w = strings.TrimSpace(w)
words = append(words, slug.Make(w))
words = append(words, txt.Slug(w))
if !txt.ContainsASCIILetters(w) {
continue
@ -181,7 +180,7 @@ func AnySlug(col, search, sep string) (where string) {
singular := inflection.Singular(w)
if singular != w {
words = append(words, slug.Make(singular))
words = append(words, txt.Slug(singular))
}
}

View file

@ -16,6 +16,7 @@ const (
ClipName = 160
ClipTitle = 200
ClipVarchar = 255
ClipPath = 500
ClipLabel = 500
ClipQuery = 1000
ClipDescription = 16000