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 {
|
2020-05-23 20:58:58 +02:00
|
|
|
ID uint `gorm:"primary_key" json:"ID" yaml:"-"`
|
|
|
|
AlbumUID string `gorm:"type:varbinary(36);unique_index;" json:"UID" yaml:"UID"`
|
|
|
|
CoverUID string `gorm:"type:varbinary(36);" json:"CoverUID" yaml:"CoverUID,omitempty"`
|
|
|
|
FolderUID string `gorm:"type:varbinary(36);index;" json:"FolderUID" yaml:"FolderUID,omitempty"`
|
|
|
|
AlbumSlug string `gorm:"type:varbinary(255);index;" json:"Slug" yaml:"Slug"`
|
2020-05-26 09:02:19 +02:00
|
|
|
AlbumType string `gorm:"type:varbinary(8);" json:"Type" yaml:"Type,omitempty"`
|
|
|
|
AlbumTitle string `gorm:"type:varchar(255);" json:"Title" yaml:"Title"`
|
|
|
|
AlbumCategory string `gorm:"type:varchar(255);index;" json:"Category" yaml:"Category,omitempty"`
|
|
|
|
AlbumCaption string `gorm:"type:text;" json:"Caption" yaml:"Caption,omitempty"`
|
2020-05-23 20:58:58 +02:00
|
|
|
AlbumDescription string `gorm:"type:text;" json:"Description" yaml:"Description,omitempty"`
|
|
|
|
AlbumNotes string `gorm:"type:text;" json:"Notes" yaml:"Notes,omitempty"`
|
2020-05-26 09:02:19 +02:00
|
|
|
AlbumFilter string `gorm:"type:varbinary(1024);" json:"Filter" yaml:"Filter,omitempty"`
|
2020-05-23 20:58:58 +02:00
|
|
|
AlbumOrder string `gorm:"type:varbinary(32);" json:"Order" yaml:"Order,omitempty"`
|
|
|
|
AlbumTemplate string `gorm:"type:varbinary(255);" json:"Template" yaml:"Template,omitempty"`
|
2020-05-26 09:02:19 +02:00
|
|
|
AlbumCountry string `gorm:"type:varbinary(2);index:idx_albums_country_year_month;default:'zz'" json:"Country" yaml:"Country,omitempty"`
|
|
|
|
AlbumYear int `gorm:"index:idx_albums_country_year_month;" json:"Year" yaml:"Year,omitempty"`
|
|
|
|
AlbumMonth int `gorm:"index:idx_albums_country_year_month;" json:"Month" yaml:"Month,omitempty"`
|
2020-05-23 20:58:58 +02:00
|
|
|
AlbumFavorite bool `json:"Favorite" yaml:"Favorite,omitempty"`
|
2020-05-26 09:02:19 +02:00
|
|
|
AlbumPrivate bool `json:"Private" yaml:"Private,omitempty"`
|
2020-05-25 19:10:44 +02:00
|
|
|
Links []Link `gorm:"foreignkey:share_uid;association_foreignkey:album_uid" json:"Links" yaml:"-"`
|
2020-05-23 20:58:58 +02:00
|
|
|
CreatedAt time.Time `json:"CreatedAt" yaml:"-"`
|
|
|
|
UpdatedAt time.Time `json:"UpdatedAt" yaml:"-"`
|
|
|
|
DeletedAt *time.Time `sql:"index" json:"-" yaml:"-"`
|
2018-07-18 15:17:56 +02:00
|
|
|
}
|
2019-06-04 18:26:35 +02:00
|
|
|
|
2020-05-23 20:58:58 +02:00
|
|
|
// BeforeCreate creates a random UID if needed before inserting a new row to the database.
|
2019-06-04 18:26:35 +02:00
|
|
|
func (m *Album) BeforeCreate(scope *gorm.Scope) error {
|
2020-05-23 20:58:58 +02:00
|
|
|
if rnd.IsPPID(m.AlbumUID, 'a') {
|
2020-05-01 12:57:26 +02:00
|
|
|
return nil
|
2019-12-06 10:26:57 +01:00
|
|
|
}
|
|
|
|
|
2020-05-23 20:58:58 +02:00
|
|
|
return scope.SetColumn("AlbumUID", 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-05-26 09:02:19 +02:00
|
|
|
func NewAlbum(albumTitle, albumType string) *Album {
|
2020-04-26 14:31:33 +02:00
|
|
|
now := time.Now().UTC()
|
2019-06-18 06:37:10 +02:00
|
|
|
|
|
|
|
result := &Album{
|
2020-05-23 20:58:58 +02:00
|
|
|
AlbumUID: rnd.PPID('a'),
|
2020-04-20 12:53:58 +02:00
|
|
|
AlbumOrder: SortOrderOldest,
|
2020-05-23 20:58:58 +02:00
|
|
|
AlbumType: albumType,
|
2020-04-26 14:31:33 +02:00
|
|
|
CreatedAt: now,
|
|
|
|
UpdatedAt: now,
|
2019-06-18 06:37:10 +02:00
|
|
|
}
|
|
|
|
|
2020-05-26 09:02:19 +02:00
|
|
|
result.SetTitle(albumTitle)
|
2020-04-26 14:31:33 +02:00
|
|
|
|
2019-06-18 06:37:10 +02:00
|
|
|
return result
|
|
|
|
}
|
2019-12-03 23:55:24 +01:00
|
|
|
|
2020-05-26 09:02:19 +02:00
|
|
|
// SetTitle changes the album name.
|
|
|
|
func (m *Album) SetTitle(title string) {
|
|
|
|
title = strings.TrimSpace(title)
|
2020-04-26 14:31:33 +02:00
|
|
|
|
2020-05-26 09:02:19 +02:00
|
|
|
if title == "" {
|
|
|
|
title = m.CreatedAt.Format("January 2006")
|
2019-12-04 12:11:11 +01:00
|
|
|
}
|
|
|
|
|
2020-05-26 09:02:19 +02:00
|
|
|
m.AlbumTitle = txt.Clip(title, txt.ClipDefault)
|
2020-04-26 14:31:33 +02:00
|
|
|
|
2020-05-26 09:02:19 +02:00
|
|
|
if len(m.AlbumTitle) < txt.ClipSlug {
|
|
|
|
m.AlbumSlug = slug.Make(m.AlbumTitle)
|
2020-04-26 14:31:33 +02:00
|
|
|
} else {
|
2020-05-26 09:02:19 +02:00
|
|
|
m.AlbumSlug = slug.Make(txt.Clip(m.AlbumTitle, txt.ClipSlug)) + "-" + m.AlbumUID
|
2020-04-26 14:31:33 +02:00
|
|
|
}
|
2019-12-03 23:55:24 +01:00
|
|
|
}
|
2020-04-20 10:38:01 +02:00
|
|
|
|
2020-05-22 16:29:12 +02:00
|
|
|
// Saves the entity using form data and stores it in the database.
|
2020-05-26 11:00:39 +02:00
|
|
|
func (m *Album) SaveForm(f form.Album) error {
|
2020-04-20 10:38:01 +02:00
|
|
|
if err := deepcopier.Copy(m).From(f); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-26 09:02:19 +02:00
|
|
|
if f.AlbumTitle != "" {
|
|
|
|
m.SetTitle(f.AlbumTitle)
|
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
|
|
|
}
|
2020-05-26 11:00:39 +02:00
|
|
|
|
|
|
|
// Updates a column in the database.
|
|
|
|
func (m *Album) Update(attr string, value interface{}) error {
|
|
|
|
return UnscopedDb().Model(m).UpdateColumn(attr, value).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save updates the existing or inserts a new row.
|
|
|
|
func (m *Album) Save() error {
|
|
|
|
return Db().Save(m).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create inserts a new row to the database.
|
|
|
|
func (m *Album) Create() error {
|
|
|
|
return Db().Create(m).Error
|
|
|
|
}
|