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"
|
|
|
|
|
|
|
|
"github.com/jinzhu/gorm"
|
2022-04-15 09:42:07 +02:00
|
|
|
"github.com/ulule/deepcopier"
|
|
|
|
|
2020-05-22 16:29:12 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/form"
|
2022-04-15 09:42:07 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
2020-05-22 16:29:12 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/rnd"
|
2023-01-30 12:27:34 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/sortby"
|
2020-05-22 16:29:12 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
|
|
|
)
|
|
|
|
|
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 {
|
2022-09-30 00:42:19 +02:00
|
|
|
Path string `gorm:"type:VARBINARY(1024);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"`
|
2022-10-02 11:38:30 +02:00
|
|
|
FolderUID string `gorm:"type:VARBINARY(42);primary_key;" json:"UID,omitempty" yaml:"UID,omitempty"`
|
2020-09-13 17:51:43 +02:00
|
|
|
FolderType string `gorm:"type:VARBINARY(16);" json:"Type" yaml:"Type,omitempty"`
|
2021-09-23 23:46:17 +02:00
|
|
|
FolderTitle string `gorm:"type:VARCHAR(200);" json:"Title" yaml:"Title,omitempty"`
|
|
|
|
FolderCategory string `gorm:"type:VARCHAR(100);index;" json:"Category" yaml:"Category,omitempty"`
|
2022-03-30 20:36:25 +02:00
|
|
|
FolderDescription string `gorm:"type:VARCHAR(2048);" json:"Description,omitempty" yaml:"Description,omitempty"`
|
2020-09-13 17:51:43 +02:00
|
|
|
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:"-"`
|
2022-10-02 11:38:30 +02:00
|
|
|
PublishedAt *time.Time `sql:"index" json:"PublishedAt,omitempty" yaml:"PublishedAt,omitempty"`
|
2020-05-22 16:29:12 +02:00
|
|
|
DeletedAt *time.Time `sql:"index" json:"-"`
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// TableName returns the entity table name.
|
2022-03-30 20:36:25 +02:00
|
|
|
func (Folder) TableName() string {
|
|
|
|
return "folders"
|
|
|
|
}
|
|
|
|
|
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 {
|
2022-09-28 09:01:17 +02:00
|
|
|
if rnd.IsUnique(m.FolderUID, 'd') {
|
2020-05-22 16:29:12 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-15 09:42:07 +02:00
|
|
|
return scope.SetColumn("FolderUID", rnd.GenerateUID('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{
|
2022-04-15 09:42:07 +02:00
|
|
|
FolderUID: rnd.GenerateUID('d'),
|
2020-05-28 15:12:18 +02:00
|
|
|
Root: root,
|
|
|
|
Path: pathName,
|
2022-04-13 22:17:59 +02:00
|
|
|
FolderType: MediaUnknown,
|
2023-01-30 12:27:34 +01:00
|
|
|
FolderOrder: sortby.Name,
|
2020-05-28 15:12:18 +02:00
|
|
|
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 {
|
2022-03-25 16:31:09 +01:00
|
|
|
if date := txt.DateFromFilePath(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 == "" {
|
2022-09-28 09:01:17 +02:00
|
|
|
m.FolderTitle = txt.Clip(txt.Title(s), txt.ClipLongName)
|
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 {
|
2021-09-23 23:46:17 +02:00
|
|
|
return txt.Slug(m.Path)
|
2020-12-08 22:40:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2021-09-23 23:46:17 +02:00
|
|
|
// Title returns the human-readable folder title.
|
2020-05-30 15:42:04 +02:00
|
|
|
func (m *Folder) Title() string {
|
|
|
|
return m.FolderTitle
|
|
|
|
}
|
|
|
|
|
2021-09-23 23:46:17 +02:00
|
|
|
// Create inserts the entity to the index.
|
2020-05-22 19:05:16 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-11-26 13:59:10 +01:00
|
|
|
f := form.SearchPhotos{
|
2020-12-14 20:37:54 +01:00
|
|
|
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 {
|
2022-04-15 09:42:07 +02:00
|
|
|
log.Infof("folder: added album %s (%s)", clean.Log(a.AlbumTitle), a.AlbumFilter)
|
2020-12-14 20:37:54 +01:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
m.FolderTitle = txt.Clip(m.FolderTitle, txt.ClipLongName)
|
2021-09-23 23:46:17 +02:00
|
|
|
m.FolderCategory = txt.Clip(m.FolderCategory, txt.ClipCategory)
|
|
|
|
|
2020-05-22 19:05:16 +02:00
|
|
|
return nil
|
2020-05-22 16:29:12 +02:00
|
|
|
}
|