2021-01-02 18:56:15 +01:00
|
|
|
package auto
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path/filepath"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/photoprism/photoprism/internal/api"
|
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
2022-10-15 21:54:11 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/get"
|
2021-01-02 18:56:15 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/i18n"
|
|
|
|
"github.com/photoprism/photoprism/internal/mutex"
|
|
|
|
"github.com/photoprism/photoprism/internal/photoprism"
|
|
|
|
)
|
|
|
|
|
|
|
|
var autoIndex = time.Time{}
|
|
|
|
var indexMutex = sync.Mutex{}
|
|
|
|
|
|
|
|
// ResetIndex resets the auto index trigger time.
|
|
|
|
func ResetIndex() {
|
|
|
|
indexMutex.Lock()
|
|
|
|
defer indexMutex.Unlock()
|
|
|
|
|
|
|
|
autoIndex = time.Time{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ShouldIndex sets the auto index trigger to the current time.
|
|
|
|
func ShouldIndex() {
|
|
|
|
indexMutex.Lock()
|
|
|
|
defer indexMutex.Unlock()
|
|
|
|
|
|
|
|
autoIndex = time.Now()
|
|
|
|
}
|
|
|
|
|
2021-10-17 14:25:29 +02:00
|
|
|
// mustIndex tests if the index must be updated.
|
2021-01-02 18:56:15 +01:00
|
|
|
func mustIndex(delay time.Duration) bool {
|
|
|
|
if delay.Seconds() <= 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
indexMutex.Lock()
|
|
|
|
defer indexMutex.Unlock()
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
return !autoIndex.IsZero() && autoIndex.Sub(time.Now()) < -1*delay && !mutex.MainWorker.Running()
|
2021-01-02 18:56:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Index starts indexing originals e.g. after WebDAV uploads.
|
|
|
|
func Index() error {
|
2022-09-28 09:01:17 +02:00
|
|
|
if mutex.MainWorker.Running() {
|
2021-01-02 18:56:15 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-15 21:54:11 +02:00
|
|
|
conf := get.Config()
|
2022-04-16 12:39:47 +02:00
|
|
|
settings := conf.Settings()
|
2021-01-02 18:56:15 +01:00
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
path := conf.OriginalsPath()
|
|
|
|
|
2022-10-15 21:54:11 +02:00
|
|
|
ind := get.Index()
|
2021-01-02 18:56:15 +01:00
|
|
|
|
2022-04-16 12:39:47 +02:00
|
|
|
convert := settings.Index.Convert && conf.SidecarWritable()
|
|
|
|
indOpt := photoprism.NewIndexOptions(entity.RootPath, false, convert, true, false, true)
|
2023-03-11 14:09:00 +01:00
|
|
|
indOpt.Action = photoprism.ActionAutoIndex
|
2021-01-02 18:56:15 +01:00
|
|
|
|
2023-02-23 18:33:50 +01:00
|
|
|
lastRun, lastFound := ind.LastRun()
|
|
|
|
found, indexed := ind.Start(indOpt)
|
2021-01-02 18:56:15 +01:00
|
|
|
|
2023-02-23 18:33:50 +01:00
|
|
|
if !lastRun.IsZero() && indexed == 0 && len(found) == lastFound {
|
2021-01-02 18:56:15 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-08 13:29:01 +01:00
|
|
|
api.RemoveFromFolderCache(entity.RootOriginals)
|
2021-01-02 18:56:15 +01:00
|
|
|
|
2022-10-15 21:54:11 +02:00
|
|
|
prg := get.Purge()
|
2021-01-02 18:56:15 +01:00
|
|
|
|
|
|
|
prgOpt := photoprism.PurgeOptions{
|
|
|
|
Path: filepath.Clean(entity.RootPath),
|
2023-02-23 01:06:52 +01:00
|
|
|
Ignore: found,
|
|
|
|
Force: true,
|
2021-01-02 18:56:15 +01:00
|
|
|
}
|
|
|
|
|
2023-02-23 18:33:50 +01:00
|
|
|
if files, photos, updated, err := prg.Start(prgOpt); err != nil {
|
2021-01-02 18:56:15 +01:00
|
|
|
return err
|
2023-02-23 18:33:50 +01:00
|
|
|
} else if updated > 0 {
|
2021-01-02 18:56:15 +01:00
|
|
|
event.InfoMsg(i18n.MsgRemovedFilesAndPhotos, len(files), len(photos))
|
|
|
|
}
|
|
|
|
|
|
|
|
event.Publish("index.updating", event.Data{
|
2023-03-11 14:09:00 +01:00
|
|
|
"uid": indOpt.UID,
|
|
|
|
"action": indOpt.Action,
|
|
|
|
"step": "moments",
|
2021-01-02 18:56:15 +01:00
|
|
|
})
|
|
|
|
|
2022-10-15 21:54:11 +02:00
|
|
|
moments := get.Moments()
|
2021-01-02 18:56:15 +01:00
|
|
|
|
|
|
|
if err := moments.Start(); err != nil {
|
|
|
|
log.Warnf("moments: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
elapsed := int(time.Since(start).Seconds())
|
|
|
|
|
|
|
|
msg := i18n.Msg(i18n.MsgIndexingCompletedIn, elapsed)
|
|
|
|
|
|
|
|
event.Success(msg)
|
2023-03-11 14:09:00 +01:00
|
|
|
|
|
|
|
eventData := event.Data{
|
|
|
|
"uid": indOpt.UID,
|
|
|
|
"action": indOpt.Action,
|
|
|
|
"path": path,
|
|
|
|
"seconds": elapsed,
|
|
|
|
}
|
|
|
|
|
|
|
|
event.Publish("index.completed", eventData)
|
2021-01-02 18:56:15 +01:00
|
|
|
|
|
|
|
api.UpdateClientConfig()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|