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"
|
2022-04-15 09:42:07 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/media"
|
2020-04-07 12:51:01 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Updates the local list of remote files so that they can be downloaded in batches
|
2022-10-15 21:54:11 +02:00
|
|
|
func (w *Sync) refresh(a entity.Service) (complete bool, err error) {
|
2020-04-07 12:51:01 +02:00
|
|
|
if a.AccType != remote.ServiceWebDAV {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2022-03-27 21:37:11 +02:00
|
|
|
subDirs, err := client.Directories(a.SyncPath, true, webdav.MaxRequestDuration)
|
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
|
2022-04-15 09:42:07 +02:00
|
|
|
content := media.FromName(file.Name)
|
|
|
|
switch content {
|
|
|
|
case media.Image, media.Sidecar:
|
2020-04-07 12:51:01 +02:00
|
|
|
f.Status = entity.FileSyncNew
|
2022-04-15 09:42:07 +02:00
|
|
|
case media.Raw, media.Video:
|
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 {
|
2022-04-06 17:46:41 +02:00
|
|
|
log.Errorf("sync: file sync entity should not be nil - possible bug")
|
2020-05-26 11:00:39 +02:00
|
|
|
continue
|
|
|
|
}
|
2020-04-07 12:51:01 +02:00
|
|
|
|
2022-04-15 09:42:07 +02:00
|
|
|
if f.Status == entity.FileSyncIgnore && a.SyncRaw && (content == media.Raw || content == media.Video) {
|
2022-10-03 22:59:29 +02:00
|
|
|
w.logError(f.Update("Status", entity.FileSyncNew))
|
2020-04-07 12:51:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if f.Status == entity.FileSyncDownloaded && !f.RemoteDate.Equal(file.Date) {
|
2022-10-03 22:59:29 +02:00
|
|
|
w.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
|
|
|
|
}
|