2020-04-07 12:51:01 +02:00
|
|
|
package workers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-04-20 07:43:49 +02:00
|
|
|
"os"
|
2020-04-07 12:51:01 +02:00
|
|
|
|
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
|
|
|
"github.com/photoprism/photoprism/internal/mutex"
|
|
|
|
"github.com/photoprism/photoprism/internal/photoprism"
|
2020-05-08 15:41:01 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/query"
|
2020-04-07 12:51:01 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/remote/webdav"
|
|
|
|
"github.com/photoprism/photoprism/internal/service"
|
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Downloads map[string][]entity.FileSync
|
|
|
|
|
|
|
|
// downloadPath returns a temporary download path.
|
2020-05-26 15:15:14 +02:00
|
|
|
func (worker *Sync) downloadPath() string {
|
|
|
|
return worker.conf.TempPath() + "/sync"
|
2020-04-07 12:51:01 +02:00
|
|
|
}
|
|
|
|
|
2020-05-08 15:41:01 +02:00
|
|
|
// relatedDownloads returns files to be downloaded grouped by prefix.
|
2020-05-26 15:15:14 +02:00
|
|
|
func (worker *Sync) relatedDownloads(a entity.Account) (result Downloads, err error) {
|
2020-04-07 12:51:01 +02:00
|
|
|
result = make(Downloads)
|
|
|
|
maxResults := 1000
|
|
|
|
|
|
|
|
// Get remote files from database
|
2020-05-08 15:41:01 +02:00
|
|
|
files, err := query.FileSyncs(a.ID, entity.FileSyncNew, maxResults)
|
2020-04-07 12:51:01 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Group results by directory and base name
|
|
|
|
for i, file := range files {
|
2020-12-07 17:03:06 +01:00
|
|
|
k := fs.AbsPrefix(file.RemoteName, worker.conf.Settings().StackSequences())
|
2020-04-07 12:51:01 +02:00
|
|
|
|
|
|
|
result[k] = append(result[k], file)
|
|
|
|
|
|
|
|
// Skip last 50 to make sure we see all related files
|
|
|
|
if i > (maxResults - 50) {
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Downloads remote files in batches and imports / indexes them
|
2020-05-26 15:15:14 +02:00
|
|
|
func (worker *Sync) download(a entity.Account) (complete bool, err error) {
|
2020-04-07 12:51:01 +02:00
|
|
|
// Set up index worker
|
|
|
|
indexJobs := make(chan photoprism.IndexJob)
|
2020-05-08 15:41:01 +02:00
|
|
|
|
2020-04-07 12:51:01 +02:00
|
|
|
go photoprism.IndexWorker(indexJobs)
|
|
|
|
defer close(indexJobs)
|
|
|
|
|
|
|
|
// Set up import worker
|
|
|
|
importJobs := make(chan photoprism.ImportJob)
|
|
|
|
go photoprism.ImportWorker(importJobs)
|
|
|
|
defer close(importJobs)
|
|
|
|
|
2020-05-26 15:15:14 +02:00
|
|
|
relatedFiles, err := worker.relatedDownloads(a)
|
2020-04-07 12:51:01 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2020-05-26 15:15:14 +02:00
|
|
|
worker.logError(err)
|
2020-04-07 12:51:01 +02:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(relatedFiles) == 0 {
|
2020-12-05 00:13:44 +01:00
|
|
|
log.Infof("sync: download complete for %s", a.AccName)
|
2020-04-07 12:51:01 +02:00
|
|
|
event.Publish("sync.downloaded", event.Data{"account": a})
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2020-12-05 00:13:44 +01:00
|
|
|
log.Infof("sync: downloading from %s", a.AccName)
|
2020-04-07 12:51:01 +02:00
|
|
|
|
2022-03-27 21:37:11 +02:00
|
|
|
client := webdav.New(a.AccURL, a.AccUser, a.AccPass, webdav.Timeout(a.AccTimeout))
|
2020-04-07 12:51:01 +02:00
|
|
|
|
|
|
|
var baseDir string
|
|
|
|
|
|
|
|
if a.SyncFilenames {
|
2020-05-26 15:15:14 +02:00
|
|
|
baseDir = worker.conf.OriginalsPath()
|
2020-04-07 12:51:01 +02:00
|
|
|
} else {
|
2020-05-26 15:15:14 +02:00
|
|
|
baseDir = fmt.Sprintf("%s/%d", worker.downloadPath(), a.ID)
|
2020-04-07 12:51:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
done := make(map[string]bool)
|
|
|
|
|
|
|
|
for _, files := range relatedFiles {
|
2020-04-20 07:43:49 +02:00
|
|
|
for i, file := range files {
|
2020-05-26 15:15:14 +02:00
|
|
|
if mutex.SyncWorker.Canceled() {
|
2020-04-07 12:51:01 +02:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2022-03-27 21:37:11 +02:00
|
|
|
// Failed too often?
|
|
|
|
if a.RetryLimit > 0 && file.Errors > a.RetryLimit {
|
2020-12-05 00:13:44 +01:00
|
|
|
log.Debugf("sync: downloading %s failed more than %d times", file.RemoteName, a.RetryLimit)
|
2020-04-07 12:51:01 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
localName := baseDir + file.RemoteName
|
|
|
|
|
2020-04-20 07:43:49 +02:00
|
|
|
if _, err := os.Stat(localName); err == nil {
|
2020-12-05 00:13:44 +01:00
|
|
|
log.Warnf("sync: download skipped, %s already exists", localName)
|
2020-04-20 07:43:49 +02:00
|
|
|
file.Status = entity.FileSyncExists
|
2022-03-27 21:37:11 +02:00
|
|
|
file.Error = ""
|
|
|
|
file.Errors = 0
|
2020-04-07 12:51:01 +02:00
|
|
|
} else {
|
2020-04-20 07:43:49 +02:00
|
|
|
if err := client.Download(file.RemoteName, localName, false); err != nil {
|
|
|
|
file.Errors++
|
|
|
|
file.Error = err.Error()
|
|
|
|
} else {
|
2020-12-05 00:13:44 +01:00
|
|
|
log.Infof("sync: downloaded %s from %s", file.RemoteName, a.AccName)
|
2020-04-20 07:43:49 +02:00
|
|
|
file.Status = entity.FileSyncDownloaded
|
2022-03-27 21:37:11 +02:00
|
|
|
file.Error = ""
|
|
|
|
file.Errors = 0
|
2020-04-20 07:43:49 +02:00
|
|
|
}
|
|
|
|
|
2020-05-26 15:15:14 +02:00
|
|
|
if mutex.SyncWorker.Canceled() {
|
2020-04-20 07:43:49 +02:00
|
|
|
return false, nil
|
|
|
|
}
|
2020-04-07 12:51:01 +02:00
|
|
|
}
|
|
|
|
|
2020-05-08 15:41:01 +02:00
|
|
|
if err := entity.Db().Save(&file).Error; err != nil {
|
2020-05-26 15:15:14 +02:00
|
|
|
worker.logError(err)
|
2020-04-20 07:43:49 +02:00
|
|
|
} else {
|
|
|
|
files[i] = file
|
2020-04-07 12:51:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range files {
|
2020-04-20 07:43:49 +02:00
|
|
|
if file.Status != entity.FileSyncDownloaded {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-04-07 12:51:01 +02:00
|
|
|
mf, err := photoprism.NewMediaFile(baseDir + file.RemoteName)
|
|
|
|
|
2020-05-11 18:29:17 +02:00
|
|
|
if err != nil || !mf.IsMedia() {
|
2020-04-07 12:51:01 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-12-07 17:03:06 +01:00
|
|
|
related, err := mf.RelatedFiles(worker.conf.Settings().StackSequences())
|
2020-04-07 12:51:01 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2020-05-26 15:15:14 +02:00
|
|
|
worker.logWarn(err)
|
2020-04-07 12:51:01 +02:00
|
|
|
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 {
|
2020-12-05 00:13:44 +01:00
|
|
|
log.Infof("sync: indexing %s and related files", file.RemoteName)
|
2020-04-07 12:51:01 +02:00
|
|
|
indexJobs <- photoprism.IndexJob{
|
|
|
|
FileName: mf.FileName(),
|
|
|
|
Related: related,
|
|
|
|
IndexOpt: photoprism.IndexOptionsAll(),
|
|
|
|
Ind: service.Index(),
|
|
|
|
}
|
|
|
|
} else {
|
2020-12-05 00:13:44 +01:00
|
|
|
log.Infof("sync: importing %s and related files", file.RemoteName)
|
2020-04-07 12:51:01 +02:00
|
|
|
importJobs <- photoprism.ImportJob{
|
|
|
|
FileName: mf.FileName(),
|
|
|
|
Related: related,
|
|
|
|
IndexOpt: photoprism.IndexOptionsAll(),
|
|
|
|
ImportOpt: photoprism.ImportOptionsMove(baseDir),
|
|
|
|
Imp: service.Import(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-23 16:06:59 +02:00
|
|
|
// Any files downloaded?
|
2020-05-11 10:48:18 +02:00
|
|
|
if len(done) > 0 {
|
2021-09-23 16:06:59 +02:00
|
|
|
// Update precalculated photo and file counts.
|
2021-10-02 14:24:44 +02:00
|
|
|
worker.logWarn(entity.UpdateCounts())
|
2021-09-23 16:06:59 +02:00
|
|
|
|
2021-10-01 16:44:50 +02:00
|
|
|
// Update album, subject, and label cover thumbs.
|
2021-10-02 14:24:44 +02:00
|
|
|
worker.logWarn(query.UpdateCovers())
|
2020-05-11 10:48:18 +02:00
|
|
|
}
|
|
|
|
|
2020-04-07 12:51:01 +02:00
|
|
|
return false, nil
|
|
|
|
}
|