/
sync_refresh.go
93 lines (73 loc) Β· 2.2 KB
/
sync_refresh.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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/media"
)
// Updates the local list of remote files so that they can be downloaded in batches
func (w *Sync) refresh(a entity.Service) (complete bool, err error) {
if a.AccType != remote.ServiceWebDAV {
return false, nil
}
client, err := webdav.NewClient(a.AccURL, a.AccUser, a.AccPass, webdav.Timeout(a.AccTimeout))
if err != nil {
return false, err
}
// Ensure remote folder exists.
if err = client.MkdirAll(a.SyncPath); err != nil {
log.Debugf("sync: %s", err)
}
subDirs, err := client.Directories(a.SyncPath, true, webdav.MaxRequestDuration)
if err != nil {
log.Errorf("sync: %s", err)
return false, err
}
dirs := append(subDirs.Abs(), a.SyncPath)
for _, dir := range dirs {
if mutex.SyncWorker.Canceled() {
return false, nil
}
files, err := client.Files(dir, false)
if err != nil {
log.Error(err)
return false, err
}
for _, file := range files {
if mutex.SyncWorker.Canceled() {
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
content := media.FromName(file.Name)
switch content {
case media.Image, media.Sidecar:
f.Status = entity.FileSyncNew
case media.Raw, media.Video:
if a.SyncRaw {
f.Status = entity.FileSyncNew
}
}
f = entity.FirstOrCreateFileSync(f)
if f == nil {
log.Errorf("sync: file sync entity should not be nil - you may have found a bug")
continue
}
if f.Status == entity.FileSyncIgnore && a.SyncRaw && (content == media.Raw || content == media.Video) {
w.logErr(f.Update("Status", entity.FileSyncNew))
}
if f.Status == entity.FileSyncDownloaded && !f.RemoteDate.Equal(file.Date) {
w.logErr(f.Updates(map[string]interface{}{
"Status": entity.FileSyncNew,
"RemoteDate": file.Date,
"RemoteSize": file.Size,
}))
}
}
}
return true, nil
}