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"
|
|
|
|
"github.com/photoprism/photoprism/internal/i18n"
|
|
|
|
"github.com/photoprism/photoprism/internal/mutex"
|
|
|
|
"github.com/photoprism/photoprism/internal/photoprism"
|
|
|
|
"github.com/photoprism/photoprism/internal/service"
|
2022-04-15 09:42:07 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
2021-01-02 18:56:15 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var autoImport = time.Time{}
|
|
|
|
var importMutex = sync.Mutex{}
|
|
|
|
|
|
|
|
// ResetImport resets the auto import trigger time.
|
|
|
|
func ResetImport() {
|
|
|
|
importMutex.Lock()
|
|
|
|
defer importMutex.Unlock()
|
|
|
|
|
|
|
|
autoImport = time.Time{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ShouldImport sets the auto import trigger to the current time.
|
|
|
|
func ShouldImport() {
|
|
|
|
importMutex.Lock()
|
|
|
|
defer importMutex.Unlock()
|
|
|
|
|
|
|
|
autoImport = time.Now()
|
|
|
|
}
|
|
|
|
|
|
|
|
// mustImport tests if auto import must be started.
|
|
|
|
func mustImport(delay time.Duration) bool {
|
|
|
|
if delay.Seconds() <= 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
importMutex.Lock()
|
|
|
|
defer importMutex.Unlock()
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
return !autoImport.IsZero() && autoImport.Sub(time.Now()) < -1*delay && !mutex.MainWorker.Running()
|
2021-01-02 18:56:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Import starts importing originals e.g. after WebDAV uploads.
|
|
|
|
func Import() error {
|
2022-09-28 09:01:17 +02:00
|
|
|
if mutex.MainWorker.Running() {
|
2021-01-02 18:56:15 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
conf := service.Config()
|
|
|
|
|
|
|
|
if conf.ReadOnly() || !conf.Settings().Features.Import {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
path := filepath.Clean(conf.ImportPath())
|
|
|
|
|
|
|
|
imp := service.Import()
|
|
|
|
|
2021-01-08 13:29:01 +01:00
|
|
|
api.RemoveFromFolderCache(entity.RootImport)
|
2021-01-02 18:56:15 +01:00
|
|
|
|
2022-04-15 09:42:07 +02:00
|
|
|
event.InfoMsg(i18n.MsgCopyingFilesFrom, clean.Log(filepath.Base(path)))
|
2021-01-11 17:09:19 +01:00
|
|
|
|
|
|
|
var opt photoprism.ImportOptions
|
|
|
|
|
|
|
|
if conf.Settings().Import.Move {
|
2022-09-30 00:42:19 +02:00
|
|
|
opt = photoprism.ImportOptionsMove(path, conf.ImportDest())
|
2021-01-11 17:09:19 +01:00
|
|
|
} else {
|
2022-09-30 00:42:19 +02:00
|
|
|
opt = photoprism.ImportOptionsCopy(path, conf.ImportDest())
|
2021-01-11 17:09:19 +01:00
|
|
|
}
|
2021-01-02 18:56:15 +01:00
|
|
|
|
|
|
|
imported := imp.Start(opt)
|
|
|
|
|
|
|
|
if len(imported) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
moments := service.Moments()
|
|
|
|
|
|
|
|
if err := moments.Start(); err != nil {
|
|
|
|
log.Warnf("moments: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
elapsed := int(time.Since(start).Seconds())
|
|
|
|
|
|
|
|
msg := i18n.Msg(i18n.MsgImportCompletedIn, elapsed)
|
|
|
|
|
|
|
|
event.Success(msg)
|
|
|
|
event.Publish("import.completed", event.Data{"path": path, "seconds": elapsed})
|
|
|
|
event.Publish("index.completed", event.Data{"path": path, "seconds": elapsed})
|
|
|
|
|
|
|
|
api.UpdateClientConfig()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|