2020-05-22 16:29:12 +02:00
|
|
|
package entity
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strings"
|
2020-12-14 20:37:54 +01:00
|
|
|
"sync"
|
2020-05-22 16:29:12 +02:00
|
|
|
"time"
|
|
|
|
|
2020-05-30 15:42:04 +02:00
|
|
|
"github.com/gosimple/slug"
|
2020-05-22 16:29:12 +02:00
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
"github.com/photoprism/photoprism/internal/form"
|
|
|
|
"github.com/photoprism/photoprism/pkg/rnd"
|
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
|
|
|
"github.com/ulule/deepcopier"
|
|
|
|
)
|
|
|
|
|
2020-12-14 20:37:54 +01:00
|
|
|
var folderMutex = sync.Mutex{}
|
|
|
|
|
2020-06-01 09:45:24 +02:00
|
|
|
type Folders []Folder
|
|
|
|
|
2020-05-22 16:29:12 +02:00
|
|
|
// Folder represents a file system directory.
|
|
|
|
type Folder struct {
|
2020-12-15 20:14:06 +01:00
|
|
|
Path string `gorm:"type:VARBINARY(500);unique_index:idx_folders_path_root;" json:"Path" yaml:"Path"`
|
2020-09-13 17:51:43 +02:00
|
|
|
Root string `gorm:"type:VARBINARY(16);default:'';unique_index:idx_folders_path_root;" json:"Root" yaml:"Root,omitempty"`
|
|
|
|
FolderUID string `gorm:"type:VARBINARY(42);primary_key;" json:"UID,omitempty" yaml:"UID,omitempty"`
|
|
|
|
FolderType string `gorm:"type:VARBINARY(16);" json:"Type" yaml:"Type,omitempty"`
|
|
|
|
FolderTitle string `gorm:"type:VARCHAR(255);" json:"Title" yaml:"Title,omitempty"`
|
|
|
|
FolderCategory string `gorm:"type:VARCHAR(255);index;" json:"Category" yaml:"Category,omitempty"`
|
|
|
|
FolderDescription string `gorm:"type:TEXT;" json:"Description,omitempty" yaml:"Description,omitempty"`
|
|
|
|
FolderOrder string `gorm:"type:VARBINARY(32);" json:"Order" yaml:"Order,omitempty"`
|
|
|
|
FolderCountry string `gorm:"type:VARBINARY(2);index:idx_folders_country_year_month;default:'zz'" json:"Country" yaml:"Country,omitempty"`
|
2020-05-26 09:02:19 +02:00
|
|
|
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"`
|
2020-07-12 16:36:39 +02:00
|
|
|
FolderDay int `json:"Day" yaml:"Day,omitempty"`
|
2020-05-26 09:02:19 +02:00
|
|
|
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:"-"`
|
2020-05-22 16:29:12 +02:00
|
|
|
CreatedAt time.Time `json:"-" yaml:"-"`
|
|
|
|
UpdatedAt time.Time `json:"-" yaml:"-"`
|
2020-12-09 13:10:21 +01:00
|
|
|
ModifiedAt time.Time `json:"ModifiedAt,omitempty" yaml:"-"`
|
2020-05-22 16:29:12 +02:00
|
|
|
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.
|
2020-12-09 13:10:21 +01:00
|
|
|
func NewFolder(root, pathName string, modTime time.Time) Folder {
|
2021-08-29 13:26:05 +02:00
|
|
|
now := TimeStamp()
|
2020-05-22 16:29:12 +02:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-12-09 13:10:21 +01:00
|
|
|
year := 0
|
|
|
|
month := 0
|
|
|
|
|
|
|
|
if !modTime.IsZero() {
|
|
|
|
year = modTime.Year()
|
|
|
|
month = int(modTime.Month())
|
|
|
|
}
|
|
|
|
|
2020-05-22 16:29:12 +02:00
|
|
|
result := Folder{
|
2020-05-28 15:12:18 +02:00
|
|
|
FolderUID: rnd.PPID('d'),
|
|
|
|
Root: root,
|
|
|
|
Path: pathName,
|
|
|
|
FolderType: TypeDefault,
|
|
|
|
FolderOrder: SortOrderName,
|
|
|
|
FolderCountry: UnknownCountry.ID,
|
2020-12-09 13:10:21 +01:00
|
|
|
FolderYear: year,
|
|
|
|
FolderMonth: month,
|
|
|
|
ModifiedAt: modTime.UTC(),
|
2020-05-28 15:12:18 +02:00
|
|
|
CreatedAt: now,
|
|
|
|
UpdatedAt: now,
|
2020-05-22 16:29:12 +02:00
|
|
|
}
|
|
|
|
|
2020-05-28 15:12:18 +02:00
|
|
|
result.SetValuesFromPath()
|
2020-05-22 16:29:12 +02:00
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-05-28 15:12:18 +02:00
|
|
|
// SetValuesFromPath updates the title and other values based on the path name.
|
|
|
|
func (m *Folder) SetValuesFromPath() {
|
2020-05-22 16:29:12 +02:00
|
|
|
s := m.Path
|
|
|
|
s = strings.TrimSpace(s)
|
|
|
|
|
2020-05-22 19:05:16 +02:00
|
|
|
if s == "" || s == RootPath {
|
2020-06-07 10:09:35 +02:00
|
|
|
if m.Root == RootOriginals {
|
2020-05-27 13:40:21 +02:00
|
|
|
m.FolderTitle = "Originals"
|
2020-05-28 15:12:18 +02:00
|
|
|
|
2020-05-27 13:40:21 +02:00
|
|
|
return
|
|
|
|
} else {
|
|
|
|
s = m.Root
|
|
|
|
}
|
2020-05-22 16:29:12 +02:00
|
|
|
} else {
|
2020-05-28 15:12:18 +02:00
|
|
|
m.FolderCountry = txt.CountryCode(s)
|
2020-12-09 13:10:21 +01:00
|
|
|
|
|
|
|
if year := txt.Year(s); year > 0 {
|
|
|
|
m.FolderYear = year
|
|
|
|
}
|
|
|
|
|
2020-05-22 16:29:12 +02:00
|
|
|
s = path.Base(s)
|
|
|
|
}
|
|
|
|
|
2020-05-28 15:12:18 +02:00
|
|
|
if len(m.Path) >= 6 {
|
2020-05-22 16:29:12 +02:00
|
|
|
if date := txt.Time(m.Path); !date.IsZero() {
|
2020-07-12 16:36:39 +02:00
|
|
|
if txt.IsUInt(s) || txt.IsTime(s) {
|
2020-05-28 15:12:18 +02:00
|
|
|
if date.Day() > 1 {
|
|
|
|
m.FolderTitle = date.Format("January 2, 2006")
|
|
|
|
} else {
|
|
|
|
m.FolderTitle = date.Format("January 2006")
|
|
|
|
}
|
2020-05-22 16:29:12 +02:00
|
|
|
}
|
2020-05-28 15:12:18 +02:00
|
|
|
|
|
|
|
m.FolderYear = date.Year()
|
|
|
|
m.FolderMonth = int(date.Month())
|
2020-07-12 16:36:39 +02:00
|
|
|
m.FolderDay = date.Day()
|
2020-05-22 16:29:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-28 15:12:18 +02:00
|
|
|
if m.FolderTitle == "" {
|
2020-12-25 14:31:06 +01:00
|
|
|
m.FolderTitle = txt.Title(s)
|
2020-05-28 15:12:18 +02:00
|
|
|
}
|
2020-05-22 16:29:12 +02:00
|
|
|
}
|
|
|
|
|
2020-05-30 15:42:04 +02:00
|
|
|
// Slug returns a slug based on the folder title.
|
|
|
|
func (m *Folder) Slug() string {
|
2020-12-08 22:40:13 +01:00
|
|
|
return slug.Make(m.Path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RootPath returns the full folder path including root.
|
|
|
|
func (m *Folder) RootPath() string {
|
|
|
|
return path.Join(m.Root, m.Path)
|
2020-05-30 15:42:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Title returns a human readable folder title.
|
|
|
|
func (m *Folder) Title() string {
|
|
|
|
return m.FolderTitle
|
|
|
|
}
|
|
|
|
|
2020-05-22 19:05:16 +02:00
|
|
|
// Saves the complete entity in the database.
|
|
|
|
func (m *Folder) Create() error {
|
2020-12-14 20:37:54 +01:00
|
|
|
folderMutex.Lock()
|
|
|
|
defer folderMutex.Unlock()
|
|
|
|
|
2020-05-23 20:58:58 +02:00
|
|
|
if err := Db().Create(m).Error; err != nil {
|
|
|
|
return err
|
2020-12-14 20:37:54 +01:00
|
|
|
} else if m.Root != RootOriginals || m.Path == "" {
|
|
|
|
return nil
|
2020-05-23 20:58:58 +02:00
|
|
|
}
|
|
|
|
|
2020-12-14 20:37:54 +01:00
|
|
|
f := form.PhotoSearch{
|
|
|
|
Path: m.Path,
|
|
|
|
Public: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
if a := FindFolderAlbum(m.Path); a != nil {
|
|
|
|
if a.DeletedAt != nil {
|
|
|
|
// Ignore.
|
|
|
|
} else if err := a.UpdateFolder(m.Path, f.Serialize()); err != nil {
|
|
|
|
log.Errorf("folder: %s (update album)", err.Error())
|
|
|
|
}
|
|
|
|
} else if a := NewFolderAlbum(m.Title(), m.Path, f.Serialize()); a != nil {
|
|
|
|
a.AlbumYear = m.FolderYear
|
|
|
|
a.AlbumMonth = m.FolderMonth
|
|
|
|
a.AlbumDay = m.FolderDay
|
|
|
|
a.AlbumCountry = m.FolderCountry
|
|
|
|
|
|
|
|
if err := a.Create(); err != nil {
|
|
|
|
log.Errorf("folder: %s (add album)", err)
|
|
|
|
} else {
|
|
|
|
log.Infof("folder: added album %s (%s)", txt.Quote(a.AlbumTitle), a.AlbumFilter)
|
|
|
|
}
|
|
|
|
}
|
2020-05-23 20:58:58 +02:00
|
|
|
|
|
|
|
return nil
|
2020-05-22 19:05:16 +02:00
|
|
|
}
|
|
|
|
|
2020-05-28 15:12:18 +02:00
|
|
|
// FindFolder returns an existing row if exists.
|
|
|
|
func FindFolder(root, pathName string) *Folder {
|
|
|
|
pathName = strings.Trim(pathName, string(os.PathSeparator))
|
|
|
|
|
2020-12-14 20:37:54 +01:00
|
|
|
if pathName == RootPath {
|
|
|
|
pathName = ""
|
|
|
|
}
|
|
|
|
|
2020-05-28 15:12:18 +02:00
|
|
|
result := Folder{}
|
|
|
|
|
|
|
|
if err := Db().Where("path = ? AND root = ?", pathName, root).First(&result).Error; err == nil {
|
|
|
|
return &result
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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
|
2020-07-07 20:44:33 +02:00
|
|
|
} else if createErr := m.Create(); createErr == nil {
|
|
|
|
return m
|
|
|
|
} else if err := Db().Where("path = ? AND root = ?", m.Path, m.Root).First(&result).Error; err == nil {
|
|
|
|
return &result
|
|
|
|
} else {
|
2020-12-14 13:31:18 +01:00
|
|
|
log.Errorf("folder: %s (find or create %s)", createErr, m.Path)
|
2020-05-24 22:16:06 +02:00
|
|
|
}
|
|
|
|
|
2020-07-07 20:44:33 +02:00
|
|
|
return nil
|
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
|
|
|
}
|