2019-12-11 16:55:18 +01:00
|
|
|
package entity
|
2018-07-18 15:17:56 +02:00
|
|
|
|
|
|
|
import (
|
2019-06-18 06:37:10 +02:00
|
|
|
"strings"
|
2019-12-04 12:11:11 +01:00
|
|
|
"time"
|
2019-06-18 06:37:10 +02:00
|
|
|
|
|
|
|
"github.com/gosimple/slug"
|
2018-07-18 15:17:56 +02:00
|
|
|
"github.com/jinzhu/gorm"
|
2020-04-20 10:38:01 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/form"
|
2020-01-12 14:00:56 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/rnd"
|
2020-04-26 14:31:33 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
2020-04-20 10:38:01 +02:00
|
|
|
"github.com/ulule/deepcopier"
|
2018-07-18 15:17:56 +02:00
|
|
|
)
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// Album represents a photo album
|
2018-07-18 15:17:56 +02:00
|
|
|
type Album struct {
|
2019-12-27 05:18:52 +01:00
|
|
|
ID uint `gorm:"primary_key"`
|
|
|
|
CoverUUID string `gorm:"type:varbinary(36);"`
|
|
|
|
AlbumUUID string `gorm:"type:varbinary(36);unique_index;"`
|
2020-04-26 14:31:33 +02:00
|
|
|
AlbumSlug string `gorm:"type:varbinary(255);index;"`
|
|
|
|
AlbumName string `gorm:"type:varchar(255);"`
|
2018-09-27 08:59:53 +02:00
|
|
|
AlbumDescription string `gorm:"type:text;"`
|
|
|
|
AlbumNotes string `gorm:"type:text;"`
|
2020-02-02 00:31:09 +01:00
|
|
|
AlbumOrder string `gorm:"type:varbinary(32);"`
|
2020-04-26 14:31:33 +02:00
|
|
|
AlbumTemplate string `gorm:"type:varbinary(255);"`
|
2020-04-08 13:24:06 +02:00
|
|
|
AlbumFavorite bool
|
|
|
|
Links []Link `gorm:"foreignkey:ShareUUID;association_foreignkey:AlbumUUID"`
|
2019-12-27 05:18:52 +01:00
|
|
|
CreatedAt time.Time
|
|
|
|
UpdatedAt time.Time
|
|
|
|
DeletedAt *time.Time `sql:"index"`
|
2018-07-18 15:17:56 +02:00
|
|
|
}
|
2019-06-04 18:26:35 +02:00
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// BeforeCreate computes a random UUID when a new album is created in database
|
2019-06-04 18:26:35 +02:00
|
|
|
func (m *Album) BeforeCreate(scope *gorm.Scope) error {
|
2020-05-01 12:57:26 +02:00
|
|
|
if rnd.IsPPID(m.AlbumUUID, 'a') {
|
|
|
|
return nil
|
2019-12-06 10:26:57 +01:00
|
|
|
}
|
|
|
|
|
2020-05-01 12:57:26 +02:00
|
|
|
return scope.SetColumn("AlbumUUID", rnd.PPID('a'))
|
2019-06-04 18:26:35 +02:00
|
|
|
}
|
2019-06-18 06:37:10 +02:00
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// NewAlbum creates a new album; default name is current month and year
|
2020-04-26 14:31:33 +02:00
|
|
|
func NewAlbum(name string) *Album {
|
|
|
|
now := time.Now().UTC()
|
2019-06-18 06:37:10 +02:00
|
|
|
|
|
|
|
result := &Album{
|
2020-04-26 14:31:33 +02:00
|
|
|
AlbumUUID: rnd.PPID('a'),
|
2020-04-20 12:53:58 +02:00
|
|
|
AlbumOrder: SortOrderOldest,
|
2020-04-26 14:31:33 +02:00
|
|
|
CreatedAt: now,
|
|
|
|
UpdatedAt: now,
|
2019-06-18 06:37:10 +02:00
|
|
|
}
|
|
|
|
|
2020-04-26 14:31:33 +02:00
|
|
|
result.SetName(name)
|
|
|
|
|
2019-06-18 06:37:10 +02:00
|
|
|
return result
|
|
|
|
}
|
2019-12-03 23:55:24 +01:00
|
|
|
|
2020-04-26 14:31:33 +02:00
|
|
|
// SetName changes the album name.
|
|
|
|
func (m *Album) SetName(name string) {
|
|
|
|
name = strings.TrimSpace(name)
|
|
|
|
|
|
|
|
if name == "" {
|
|
|
|
name = m.CreatedAt.Format("January 2006")
|
2019-12-04 12:11:11 +01:00
|
|
|
}
|
|
|
|
|
2020-04-26 14:31:33 +02:00
|
|
|
m.AlbumName = txt.Clip(name, txt.ClipDefault)
|
|
|
|
|
|
|
|
if len(m.AlbumName) < txt.ClipSlug {
|
|
|
|
m.AlbumSlug = slug.Make(m.AlbumName)
|
|
|
|
} else {
|
|
|
|
m.AlbumSlug = slug.Make(txt.Clip(m.AlbumName, txt.ClipSlug)) + "-" + m.AlbumUUID
|
|
|
|
}
|
2019-12-03 23:55:24 +01:00
|
|
|
}
|
2020-04-20 10:38:01 +02:00
|
|
|
|
|
|
|
// Save updates the entity using form data and stores it in the database.
|
2020-04-30 20:07:03 +02:00
|
|
|
func (m *Album) Save(f form.Album) error {
|
2020-04-20 10:38:01 +02:00
|
|
|
if err := deepcopier.Copy(m).From(f); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.AlbumName != "" {
|
2020-04-26 14:31:33 +02:00
|
|
|
m.SetName(f.AlbumName)
|
2020-04-20 10:38:01 +02:00
|
|
|
}
|
|
|
|
|
2020-04-30 20:07:03 +02:00
|
|
|
return Db().Save(m).Error
|
2020-04-20 10:38:01 +02:00
|
|
|
}
|