2020-04-07 12:51:01 +02:00
|
|
|
package workers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
|
|
|
"github.com/photoprism/photoprism/internal/mutex"
|
|
|
|
"github.com/photoprism/photoprism/internal/remote"
|
|
|
|
"github.com/photoprism/photoprism/internal/remote/webdav"
|
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Updates the local list of remote files so that they can be downloaded in batches
|
2020-05-26 15:15:14 +02:00
|
|
|
func (worker *Sync) refresh(a entity.Account) (complete bool, err error) {
|
2020-04-07 12:51:01 +02:00
|
|
|
if a.AccType != remote.ServiceWebDAV {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
client := webdav.New(a.AccURL, a.AccUser, a.AccPass)
|
|
|
|
|
2020-12-05 01:24:33 +01:00
|
|
|
subDirs, err := client.Directories(a.SyncPath, true, webdav.AsyncTimeout)
|
2020-04-07 12:51:01 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
dirs := append(subDirs.Abs(), a.SyncPath)
|
|
|
|
|
|
|
|
for _, dir := range dirs {
|
2020-05-26 15:15:14 +02:00
|
|
|
if mutex.SyncWorker.Canceled() {
|
2020-04-07 12:51:01 +02:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
files, err := client.Files(dir)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, 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
|
|
|
|
}
|
|
|
|
|
|
|
|
f := entity.NewFileSync(a.ID, file.Abs)
|
|
|
|
|
|
|
|
f.Status = entity.FileSyncIgnore
|
|
|
|
f.RemoteDate = file.Date
|
|
|
|
f.RemoteSize = file.Size
|
|
|
|
|
|
|
|
// Select supported types for download
|
|
|
|
mediaType := fs.GetMediaType(file.Name)
|
|
|
|
switch mediaType {
|
2020-12-05 02:18:45 +01:00
|
|
|
case fs.MediaImage, fs.MediaSidecar:
|
2020-04-07 12:51:01 +02:00
|
|
|
f.Status = entity.FileSyncNew
|
2020-12-05 02:18:45 +01:00
|
|
|
case fs.MediaRaw, fs.MediaVideo:
|
2020-04-07 12:51:01 +02:00
|
|
|
if a.SyncRaw {
|
|
|
|
f.Status = entity.FileSyncNew
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-26 11:00:39 +02:00
|
|
|
f = entity.FirstOrCreateFileSync(f)
|
|
|
|
|
|
|
|
if f == nil {
|
2020-12-05 00:13:44 +01:00
|
|
|
log.Errorf("sync: file sync entity should not be nil - bug?")
|
2020-05-26 11:00:39 +02:00
|
|
|
continue
|
|
|
|
}
|
2020-04-07 12:51:01 +02:00
|
|
|
|
2020-12-05 02:18:45 +01:00
|
|
|
if f.Status == entity.FileSyncIgnore && a.SyncRaw && (mediaType == fs.MediaRaw || mediaType == fs.MediaVideo) {
|
2020-05-26 15:15:14 +02:00
|
|
|
worker.logError(f.Update("Status", entity.FileSyncNew))
|
2020-04-07 12:51:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if f.Status == entity.FileSyncDownloaded && !f.RemoteDate.Equal(file.Date) {
|
2020-05-26 15:15:14 +02:00
|
|
|
worker.logError(f.Updates(map[string]interface{}{
|
2020-05-26 11:00:39 +02:00
|
|
|
"Status": entity.FileSyncNew,
|
|
|
|
"RemoteDate": file.Date,
|
|
|
|
"RemoteSize": file.Size,
|
|
|
|
}))
|
2020-04-07 12:51:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|