2020-05-22 16:29:12 +02:00
|
|
|
package entity
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/jinzhu/gorm"
|
2020-05-23 20:58:58 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
2020-05-22 16:29:12 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/form"
|
|
|
|
"github.com/photoprism/photoprism/pkg/rnd"
|
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
|
|
|
"github.com/ulule/deepcopier"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Folder represents a file system directory.
|
|
|
|
type Folder struct {
|
2020-05-24 22:16:06 +02:00
|
|
|
Path string `gorm:"type:varbinary(255);unique_index:idx_folders_path_root;" json:"Path" yaml:"Path"`
|
2020-05-27 13:40:21 +02:00
|
|
|
Root string `gorm:"type:varbinary(16);default:'';unique_index:idx_folders_path_root;" json:"Root" yaml:"Root,omitempty"`
|
2020-05-23 20:58:58 +02:00
|
|
|
FolderUID string `gorm:"type:varbinary(36);primary_key;" json:"UID,omitempty" yaml:"UID,omitempty"`
|
2020-05-26 09:02:19 +02:00
|
|
|
FolderType string `gorm:"type:varbinary(16);" json:"Type" yaml:"Type,omitempty"`
|
2020-05-22 16:29:12 +02:00
|
|
|
FolderTitle string `gorm:"type:varchar(255);" json:"Title" yaml:"Title,omitempty"`
|
2020-05-26 09:02:19 +02:00
|
|
|
FolderCategory string `gorm:"type:varchar(255);index;" json:"Category" yaml:"Category,omitempty"`
|
2020-05-22 16:29:12 +02:00
|
|
|
FolderDescription string `gorm:"type:text;" json:"Description,omitempty" yaml:"Description,omitempty"`
|
|
|
|
FolderOrder string `gorm:"type:varbinary(32);" json:"Order" yaml:"Order,omitempty"`
|
2020-05-26 09:02:19 +02:00
|
|
|
FolderCountry string `gorm:"type:varbinary(2);index:idx_folders_country_year_month;default:'zz'" json:"Country" yaml:"Country,omitempty"`
|
|
|
|
FolderYear int `gorm:"index:idx_folders_country_year_month;" json:"Year" yaml:"Year,omitempty"`
|
|
|
|
FolderMonth int `gorm:"index:idx_folders_country_year_month;" json:"Month" yaml:"Month,omitempty"`
|
|
|
|
FolderFavorite bool `json:"Favorite" yaml:"Favorite,omitempty"`
|
|
|
|
FolderPrivate bool `json:"Private" yaml:"Private,omitempty"`
|
|
|
|
FolderIgnore bool `json:"Ignore" yaml:"Ignore,omitempty"`
|
|
|
|
FolderWatch bool `json:"Watch" yaml:"Watch,omitempty"`
|
|
|
|
FileCount int `gorm:"-" json:"FileCount" yaml:"-"`
|
|
|
|
Links []Link `gorm:"foreignkey:share_uid;association_foreignkey:folder_uid" json:"Links" yaml:"-"`
|
2020-05-22 16:29:12 +02:00
|
|
|
CreatedAt time.Time `json:"-" yaml:"-"`
|
|
|
|
UpdatedAt time.Time `json:"-" yaml:"-"`
|
|
|
|
ModifiedAt *time.Time `json:"ModifiedAt,omitempty" yaml:"-"`
|
|
|
|
DeletedAt *time.Time `sql:"index" json:"-"`
|
|
|
|
}
|
|
|
|
|
2020-05-23 20:58:58 +02:00
|
|
|
// BeforeCreate creates a random UID if needed before inserting a new row to the database.
|
2020-05-22 16:29:12 +02:00
|
|
|
func (m *Folder) BeforeCreate(scope *gorm.Scope) error {
|
2020-05-27 13:40:21 +02:00
|
|
|
if rnd.IsUID(m.FolderUID, 'd') {
|
2020-05-22 16:29:12 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-23 20:58:58 +02:00
|
|
|
return scope.SetColumn("FolderUID", rnd.PPID('d'))
|
2020-05-22 16:29:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewFolder creates a new file system directory entity.
|
|
|
|
func NewFolder(root, pathName string, modTime *time.Time) Folder {
|
|
|
|
now := time.Now().UTC()
|
|
|
|
|
|
|
|
pathName = strings.Trim(pathName, string(os.PathSeparator))
|
|
|
|
|
2020-05-22 19:05:16 +02:00
|
|
|
if pathName == RootPath {
|
|
|
|
pathName = ""
|
2020-05-22 16:29:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
result := Folder{
|
2020-05-23 20:58:58 +02:00
|
|
|
FolderUID: rnd.PPID('d'),
|
2020-05-22 16:29:12 +02:00
|
|
|
Root: root,
|
|
|
|
Path: pathName,
|
|
|
|
FolderType: TypeDefault,
|
|
|
|
FolderOrder: SortOrderName,
|
|
|
|
ModifiedAt: modTime,
|
|
|
|
CreatedAt: now,
|
|
|
|
UpdatedAt: now,
|
|
|
|
}
|
|
|
|
|
|
|
|
result.SetTitleFromPath()
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetTitleFromPath updates the title based on the path name (e.g. when displaying it as an album).
|
|
|
|
func (m *Folder) SetTitleFromPath() {
|
|
|
|
s := m.Path
|
|
|
|
s = strings.TrimSpace(s)
|
|
|
|
|
2020-05-22 19:05:16 +02:00
|
|
|
if s == "" || s == RootPath {
|
2020-05-27 13:40:21 +02:00
|
|
|
if m.Root == RootDefault {
|
|
|
|
m.FolderTitle = "Originals"
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
s = m.Root
|
|
|
|
}
|
2020-05-22 16:29:12 +02:00
|
|
|
} else {
|
|
|
|
s = path.Base(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(m.Path) >= 6 && txt.IsUInt(s) {
|
|
|
|
if date := txt.Time(m.Path); !date.IsZero() {
|
|
|
|
if date.Day() > 1 {
|
|
|
|
m.FolderTitle = date.Format("January 2, 2006")
|
|
|
|
} else {
|
|
|
|
m.FolderTitle = date.Format("January 2006")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s = strings.ReplaceAll(s, "_", " ")
|
|
|
|
s = strings.ReplaceAll(s, "-", " ")
|
|
|
|
s = strings.Title(s)
|
|
|
|
|
|
|
|
m.FolderTitle = txt.Clip(s, txt.ClipDefault)
|
|
|
|
}
|
|
|
|
|
2020-05-22 19:05:16 +02:00
|
|
|
// Saves the complete entity in the database.
|
|
|
|
func (m *Folder) Create() error {
|
2020-05-23 20:58:58 +02:00
|
|
|
if err := Db().Create(m).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
event.Publish("count.folders", event.Data{
|
|
|
|
"count": 1,
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
2020-05-22 19:05:16 +02:00
|
|
|
}
|
|
|
|
|
2020-05-26 11:00:39 +02:00
|
|
|
// FirstOrCreateFolder returns the existing row, inserts a new row or nil in case of errors.
|
2020-05-25 19:10:44 +02:00
|
|
|
func FirstOrCreateFolder(m *Folder) *Folder {
|
|
|
|
result := Folder{}
|
2020-05-24 22:16:06 +02:00
|
|
|
|
2020-05-25 19:10:44 +02:00
|
|
|
if err := Db().Where("path = ? AND root = ?", m.Path, m.Root).First(&result).Error; err == nil {
|
|
|
|
return &result
|
|
|
|
} else if err := m.Create(); err != nil {
|
|
|
|
log.Errorf("folder: %s", err)
|
2020-05-24 22:16:06 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-25 19:10:44 +02:00
|
|
|
return m
|
2020-05-24 22:16:06 +02:00
|
|
|
}
|
|
|
|
|
2020-05-22 19:05:16 +02:00
|
|
|
// Updates selected properties in the database.
|
|
|
|
func (m *Folder) Updates(values interface{}) error {
|
|
|
|
return Db().Model(m).Updates(values).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetForm updates the entity properties based on form values.
|
|
|
|
func (m *Folder) SetForm(f form.Folder) error {
|
2020-05-22 16:29:12 +02:00
|
|
|
if err := deepcopier.Copy(m).From(f); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-22 19:05:16 +02:00
|
|
|
return nil
|
2020-05-22 16:29:12 +02:00
|
|
|
}
|