2020-04-06 09:41:42 +02:00
|
|
|
package workers
|
2020-04-03 18:08:49 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-04-04 17:19:34 +02:00
|
|
|
"time"
|
2020-04-03 18:08:49 +02:00
|
|
|
|
|
|
|
"github.com/photoprism/photoprism/internal/config"
|
2020-04-04 17:19:34 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
2020-04-03 18:08:49 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
2020-04-04 17:19:34 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/form"
|
2020-04-03 18:08:49 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/mutex"
|
2020-04-06 16:34:29 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/photoprism"
|
2020-04-04 17:19:34 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/query"
|
2020-04-05 22:26:53 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/remote"
|
|
|
|
"github.com/photoprism/photoprism/internal/remote/webdav"
|
2020-04-06 16:34:29 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/service"
|
2020-04-06 22:09:45 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
2020-04-03 18:08:49 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Sync represents a sync worker.
|
|
|
|
type Sync struct {
|
|
|
|
conf *config.Config
|
2020-04-06 22:09:45 +02:00
|
|
|
q *query.Query
|
2020-04-03 18:08:49 +02:00
|
|
|
}
|
|
|
|
|
2020-04-06 22:09:45 +02:00
|
|
|
type Downloads map[string][]entity.FileSync
|
|
|
|
|
2020-04-03 18:08:49 +02:00
|
|
|
// NewSync returns a new service sync worker.
|
|
|
|
func NewSync(conf *config.Config) *Sync {
|
2020-04-06 22:09:45 +02:00
|
|
|
return &Sync{
|
|
|
|
conf: conf,
|
|
|
|
q: query.New(conf.Db()),
|
|
|
|
}
|
2020-04-03 18:08:49 +02:00
|
|
|
}
|
|
|
|
|
2020-04-06 16:34:29 +02:00
|
|
|
// DownloadPath returns a temporary download path.
|
|
|
|
func (s *Sync) DownloadPath() string {
|
|
|
|
return s.conf.TempPath() + "/sync"
|
|
|
|
}
|
|
|
|
|
2020-04-03 18:08:49 +02:00
|
|
|
// Start starts the sync worker.
|
2020-04-04 17:19:34 +02:00
|
|
|
func (s *Sync) Start() (err error) {
|
2020-04-03 18:08:49 +02:00
|
|
|
if err := mutex.Sync.Start(); err != nil {
|
2020-04-04 17:19:34 +02:00
|
|
|
event.Error(fmt.Sprintf("sync: %s", err.Error()))
|
2020-04-03 18:08:49 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer mutex.Sync.Stop()
|
|
|
|
|
2020-04-04 17:19:34 +02:00
|
|
|
f := form.AccountSearch{
|
|
|
|
Sync: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
db := s.conf.Db()
|
2020-04-06 22:09:45 +02:00
|
|
|
q := s.q
|
2020-04-06 16:34:29 +02:00
|
|
|
|
2020-04-04 17:19:34 +02:00
|
|
|
accounts, err := q.Accounts(f)
|
|
|
|
|
|
|
|
for _, a := range accounts {
|
2020-04-05 22:26:53 +02:00
|
|
|
if a.AccType != remote.ServiceWebDAV {
|
2020-04-04 17:19:34 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if a.AccErrors > a.RetryLimit {
|
|
|
|
log.Warnf("sync: %s failed more than %d times", a.AccName, a.RetryLimit)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
switch a.SyncStatus {
|
|
|
|
case entity.AccountSyncStatusRefresh:
|
|
|
|
if complete, err := s.getRemoteFiles(a); err != nil {
|
|
|
|
a.AccErrors++
|
|
|
|
a.AccError = err.Error()
|
|
|
|
} else if complete {
|
|
|
|
a.AccErrors = 0
|
|
|
|
a.AccError = ""
|
|
|
|
|
|
|
|
if a.SyncDownload {
|
|
|
|
a.SyncStatus = entity.AccountSyncStatusDownload
|
|
|
|
} else if a.SyncUpload {
|
|
|
|
a.SyncStatus = entity.AccountSyncStatusUpload
|
|
|
|
} else {
|
|
|
|
a.SyncStatus = entity.AccountSyncStatusSynced
|
|
|
|
a.SyncDate.Time = time.Now()
|
|
|
|
a.SyncDate.Valid = true
|
|
|
|
}
|
2020-04-06 22:09:45 +02:00
|
|
|
|
|
|
|
event.Publish("sync.refreshed", event.Data{"account": a})
|
2020-04-04 17:19:34 +02:00
|
|
|
}
|
|
|
|
case entity.AccountSyncStatusDownload:
|
|
|
|
if complete, err := s.download(a); err != nil {
|
|
|
|
a.AccErrors++
|
|
|
|
a.AccError = err.Error()
|
|
|
|
} else if complete {
|
2020-04-06 16:34:29 +02:00
|
|
|
if a.SyncUpload {
|
|
|
|
a.SyncStatus = entity.AccountSyncStatusUpload
|
|
|
|
} else {
|
|
|
|
event.Publish("sync.synced", event.Data{"account": a})
|
|
|
|
a.SyncStatus = entity.AccountSyncStatusSynced
|
|
|
|
a.SyncDate.Time = time.Now()
|
|
|
|
a.SyncDate.Valid = true
|
|
|
|
}
|
2020-04-04 17:19:34 +02:00
|
|
|
}
|
|
|
|
case entity.AccountSyncStatusUpload:
|
|
|
|
if complete, err := s.upload(a); err != nil {
|
|
|
|
a.AccErrors++
|
|
|
|
a.AccError = err.Error()
|
|
|
|
} else if complete {
|
2020-04-06 16:34:29 +02:00
|
|
|
event.Publish("sync.synced", event.Data{"account": a})
|
2020-04-04 17:19:34 +02:00
|
|
|
a.SyncStatus = entity.AccountSyncStatusSynced
|
|
|
|
a.SyncDate.Time = time.Now()
|
|
|
|
a.SyncDate.Valid = true
|
|
|
|
}
|
|
|
|
case entity.AccountSyncStatusSynced:
|
|
|
|
if a.SyncDate.Valid && a.SyncDate.Time.Before(time.Now().Add(time.Duration(-1*a.SyncInterval)*time.Second)) {
|
|
|
|
a.SyncStatus = entity.AccountSyncStatusRefresh
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
a.SyncStatus = entity.AccountSyncStatusRefresh
|
|
|
|
}
|
|
|
|
|
|
|
|
if mutex.Sync.Canceled() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := db.Save(&a).Error; err != nil {
|
|
|
|
log.Errorf("sync: %s", err.Error())
|
|
|
|
}
|
|
|
|
}
|
2020-04-03 18:08:49 +02:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
2020-04-04 17:19:34 +02:00
|
|
|
|
|
|
|
func (s *Sync) getRemoteFiles(a entity.Account) (complete bool, err error) {
|
2020-04-05 22:26:53 +02:00
|
|
|
if a.AccType != remote.ServiceWebDAV {
|
2020-04-04 17:19:34 +02:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
db := s.conf.Db()
|
|
|
|
client := webdav.New(a.AccURL, a.AccUser, a.AccPass)
|
|
|
|
|
|
|
|
subDirs, err := client.Directories(a.SyncPath, true)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
dirs := append(subDirs.Abs(), a.SyncPath)
|
|
|
|
|
|
|
|
for _, dir := range dirs {
|
|
|
|
if mutex.Sync.Canceled() {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
files, err := client.Files(dir)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
if mutex.Sync.Canceled() {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
f := entity.NewFileSync(a.ID, file.Abs)
|
2020-04-07 10:42:42 +02:00
|
|
|
|
|
|
|
f.Status = entity.FileSyncIgnore
|
2020-04-04 17:19:34 +02:00
|
|
|
f.RemoteDate = file.Date
|
|
|
|
f.RemoteSize = file.Size
|
2020-04-07 10:42:42 +02:00
|
|
|
|
|
|
|
// Select supported types for download
|
|
|
|
mediaType := fs.GetMediaType(file.Name)
|
|
|
|
switch mediaType {
|
|
|
|
case fs.MediaImage:
|
|
|
|
f.Status = entity.FileSyncNew
|
|
|
|
case fs.MediaSidecar:
|
|
|
|
f.Status = entity.FileSyncNew
|
|
|
|
case fs.MediaRaw:
|
|
|
|
if a.SyncRaw {
|
|
|
|
f.Status = entity.FileSyncNew
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-04 17:19:34 +02:00
|
|
|
f.FirstOrCreate(db)
|
|
|
|
|
2020-04-07 10:42:42 +02:00
|
|
|
if f.Status == entity.FileSyncIgnore && mediaType == fs.MediaRaw && a.SyncRaw {
|
|
|
|
f.Status = entity.FileSyncNew
|
|
|
|
db.Save(&f)
|
|
|
|
}
|
|
|
|
|
2020-04-04 17:19:34 +02:00
|
|
|
if f.Status == entity.FileSyncDownloaded && !f.RemoteDate.Equal(file.Date) {
|
|
|
|
f.Status = entity.FileSyncNew
|
|
|
|
f.RemoteDate = file.Date
|
|
|
|
f.RemoteSize = file.Size
|
|
|
|
db.Save(&f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2020-04-06 22:09:45 +02:00
|
|
|
func (s *Sync) relatedDownloads(a entity.Account) (result Downloads, err error) {
|
|
|
|
result = make(Downloads)
|
2020-04-06 22:27:05 +02:00
|
|
|
maxResults := 1000
|
2020-04-06 22:09:45 +02:00
|
|
|
|
2020-04-06 22:27:05 +02:00
|
|
|
// Get remote files from database
|
|
|
|
files, err := s.q.FileSyncs(a.ID, entity.FileSyncNew, maxResults)
|
2020-04-06 22:09:45 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
2020-04-06 22:27:05 +02:00
|
|
|
// Group results by directory and base name
|
2020-04-06 22:09:45 +02:00
|
|
|
for i, file := range files {
|
|
|
|
k := fs.AbsBase(file.RemoteName)
|
|
|
|
|
|
|
|
result[k] = append(result[k], file)
|
|
|
|
|
2020-04-06 22:27:05 +02:00
|
|
|
// Skip last 50 to make sure we see all related files
|
|
|
|
if i > (maxResults - 50) {
|
2020-04-06 22:09:45 +02:00
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2020-04-04 17:19:34 +02:00
|
|
|
func (s *Sync) download(a entity.Account) (complete bool, err error) {
|
|
|
|
db := s.conf.Db()
|
|
|
|
|
2020-04-06 22:09:45 +02:00
|
|
|
// Set up index worker
|
|
|
|
indexJobs := make(chan photoprism.IndexJob)
|
|
|
|
go photoprism.IndexWorker(indexJobs)
|
|
|
|
defer close(indexJobs)
|
|
|
|
|
|
|
|
// Set up import worker
|
|
|
|
importJobs := make(chan photoprism.ImportJob)
|
|
|
|
go photoprism.ImportWorker(importJobs)
|
|
|
|
defer close(importJobs)
|
|
|
|
|
|
|
|
relatedFiles, err := s.relatedDownloads(a)
|
2020-04-04 17:19:34 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("sync: %s", err.Error())
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2020-04-06 22:09:45 +02:00
|
|
|
if len(relatedFiles) == 0 {
|
|
|
|
log.Infof("sync: download complete for %s", a.AccName)
|
2020-04-04 17:19:34 +02:00
|
|
|
event.Publish("sync.downloaded", event.Data{"account": a})
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2020-04-06 22:09:45 +02:00
|
|
|
log.Infof("sync: downloading from %s", a.AccName)
|
|
|
|
|
2020-04-04 17:19:34 +02:00
|
|
|
client := webdav.New(a.AccURL, a.AccUser, a.AccPass)
|
|
|
|
|
|
|
|
var baseDir string
|
|
|
|
|
|
|
|
if a.SyncFilenames {
|
|
|
|
baseDir = s.conf.OriginalsPath()
|
|
|
|
} else {
|
2020-04-06 16:34:29 +02:00
|
|
|
baseDir = fmt.Sprintf("%s/%d", s.DownloadPath(), a.ID)
|
2020-04-04 17:19:34 +02:00
|
|
|
}
|
|
|
|
|
2020-04-06 22:09:45 +02:00
|
|
|
done := make(map[string]bool)
|
2020-04-04 17:19:34 +02:00
|
|
|
|
2020-04-06 22:09:45 +02:00
|
|
|
for _, files := range relatedFiles {
|
|
|
|
for _, file := range files {
|
|
|
|
if mutex.Sync.Canceled() {
|
|
|
|
return false, nil
|
|
|
|
}
|
2020-04-04 17:19:34 +02:00
|
|
|
|
2020-04-06 22:09:45 +02:00
|
|
|
if file.Errors > a.RetryLimit {
|
|
|
|
log.Warnf("sync: downloading %s failed more than %d times", file.RemoteName, a.RetryLimit)
|
|
|
|
continue
|
|
|
|
}
|
2020-04-04 17:19:34 +02:00
|
|
|
|
2020-04-06 22:09:45 +02:00
|
|
|
localName := baseDir + file.RemoteName
|
2020-04-04 17:19:34 +02:00
|
|
|
|
2020-04-06 22:09:45 +02:00
|
|
|
if err := client.Download(file.RemoteName, localName, false); err != nil {
|
|
|
|
log.Errorf("sync: %s", err.Error())
|
|
|
|
file.Errors++
|
|
|
|
file.Error = err.Error()
|
|
|
|
} else {
|
|
|
|
log.Infof("sync: downloaded %s from %s", file.RemoteName, a.AccName)
|
|
|
|
file.Status = entity.FileSyncDownloaded
|
|
|
|
}
|
|
|
|
|
|
|
|
if mutex.Sync.Canceled() {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := db.Save(&file).Error; err != nil {
|
|
|
|
log.Errorf("sync: %s", err.Error())
|
|
|
|
}
|
2020-04-04 17:19:34 +02:00
|
|
|
}
|
|
|
|
|
2020-04-06 22:09:45 +02:00
|
|
|
for _, file := range files {
|
|
|
|
mf, err := photoprism.NewMediaFile(baseDir + file.RemoteName)
|
|
|
|
|
|
|
|
if err != nil || !mf.IsPhoto() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
related, err := mf.RelatedFiles()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("sync: %s", err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var rf photoprism.MediaFiles
|
|
|
|
|
|
|
|
for _, f := range related.Files {
|
|
|
|
if done[f.FileName()] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
rf = append(rf, f)
|
|
|
|
done[f.FileName()] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
done[mf.FileName()] = true
|
|
|
|
related.Files = rf
|
|
|
|
|
|
|
|
if a.SyncFilenames {
|
|
|
|
log.Infof("sync: indexing %s and related files", file.RemoteName)
|
|
|
|
indexJobs <- photoprism.IndexJob{
|
|
|
|
FileName: mf.FileName(),
|
|
|
|
Related: related,
|
|
|
|
IndexOpt: photoprism.IndexOptionsAll(),
|
|
|
|
Ind: service.Index(),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Infof("sync: importing %s and related files", file.RemoteName)
|
|
|
|
importJobs <- photoprism.ImportJob{
|
2020-04-06 22:27:05 +02:00
|
|
|
FileName: mf.FileName(),
|
|
|
|
Related: related,
|
|
|
|
IndexOpt: photoprism.IndexOptionsAll(),
|
2020-04-06 22:09:45 +02:00
|
|
|
ImportOpt: photoprism.ImportOptionsMove(baseDir),
|
2020-04-06 22:27:05 +02:00
|
|
|
Imp: service.Import(),
|
2020-04-06 22:09:45 +02:00
|
|
|
}
|
|
|
|
}
|
2020-04-04 17:19:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Sync) upload(a entity.Account) (complete bool, err error) {
|
2020-04-05 22:26:53 +02:00
|
|
|
// TODO: Not implemented yet
|
2020-04-04 17:19:34 +02:00
|
|
|
return false, nil
|
|
|
|
}
|