/
share.go
206 lines (165 loc) Β· 4.53 KB
/
share.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package workers
import (
"fmt"
"path"
"runtime/debug"
"github.com/photoprism/photoprism/internal/config"
"github.com/photoprism/photoprism/internal/entity"
"github.com/photoprism/photoprism/internal/form"
"github.com/photoprism/photoprism/internal/mutex"
"github.com/photoprism/photoprism/internal/photoprism"
"github.com/photoprism/photoprism/internal/query"
"github.com/photoprism/photoprism/internal/remote"
"github.com/photoprism/photoprism/internal/remote/webdav"
"github.com/photoprism/photoprism/internal/search"
"github.com/photoprism/photoprism/internal/thumb"
"github.com/photoprism/photoprism/pkg/clean"
"github.com/photoprism/photoprism/pkg/fs"
)
// Share represents a share worker.
type Share struct {
conf *config.Config
}
// NewShare returns a new share worker.
func NewShare(conf *config.Config) *Share {
return &Share{conf: conf}
}
// logErr logs an error message if err is not nil.
func (w *Share) logErr(err error) {
if err != nil {
log.Errorf("share: %s", err.Error())
}
}
// Start starts the share worker.
func (w *Share) Start() (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("share: %s (worker panic)\nstack: %s", r, debug.Stack())
log.Error(err)
}
}()
if err := mutex.ShareWorker.Start(); err != nil {
return err
}
defer mutex.ShareWorker.Stop()
f := form.SearchServices{
Share: true,
}
// Find accounts for which sharing is enabled
accounts, err := search.Accounts(f)
// Upload newly shared files
for _, a := range accounts {
if mutex.ShareWorker.Canceled() {
return nil
}
if a.AccType != remote.ServiceWebDAV {
continue
}
files, err := query.FileShares(a.ID, entity.FileShareNew)
if err != nil {
w.logErr(err)
continue
}
if len(files) == 0 {
// No files to upload for this account
continue
}
size := thumb.Size{}
if a.ShareSize != "" {
if s, ok := thumb.Sizes[thumb.Name(a.ShareSize)]; ok {
size = s
} else {
size = thumb.Sizes[thumb.Fit2048]
}
}
client, err := webdav.NewClient(a.AccURL, a.AccUser, a.AccPass, webdav.Timeout(a.AccTimeout))
if err != nil {
return err
}
for _, file := range files {
if mutex.ShareWorker.Canceled() {
return nil
}
// Skip deleted files.
if file.File == nil || file.FileID <= 0 {
log.Warnf("share: %s cannot be uploaded because it has been deleted", clean.Log(file.RemoteName))
file.Status = entity.FileShareError
file.Error = "file not found"
file.Errors++
w.logErr(entity.Db().Save(&file).Error)
continue
}
dir := path.Dir(file.RemoteName)
// Ensure remote folder exists.
if err := client.MkdirAll(dir); err != nil {
log.Debugf("share: %s", err)
}
srcFileName := photoprism.FileName(file.File.FileRoot, file.File.FileName)
if fs.ImageJPEG.Equal(file.File.FileType) && size.Width > 0 && size.Height > 0 {
srcFileName, err = thumb.FromFile(srcFileName, file.File.FileHash, w.conf.ThumbCachePath(), size.Width, size.Height, file.File.FileOrientation, size.Options...)
if err != nil {
w.logErr(err)
continue
}
}
if err := client.Upload(srcFileName, file.RemoteName); err != nil {
w.logErr(err)
file.Errors++
file.Error = err.Error()
} else {
log.Infof("share: uploaded %s to %s", file.RemoteName, a.AccName)
file.Errors = 0
file.Error = ""
file.Status = entity.FileShareShared
}
// Failed too often?
if a.RetryLimit > 0 && file.Errors > a.RetryLimit {
file.Status = entity.FileShareError
}
if mutex.ShareWorker.Canceled() {
return nil
}
w.logErr(entity.Db().Save(&file).Error)
}
}
// Remove previously shared files if expired
for _, a := range accounts {
if mutex.ShareWorker.Canceled() {
return nil
}
if a.AccType != remote.ServiceWebDAV {
continue
}
files, err := query.ExpiredFileShares(a)
if err != nil {
w.logErr(err)
continue
}
if len(files) == 0 {
// No files to remove for this account
continue
}
client, err := webdav.NewClient(a.AccURL, a.AccUser, a.AccPass, webdav.Timeout(a.AccTimeout))
if err != nil {
return err
}
for _, file := range files {
if mutex.ShareWorker.Canceled() {
return nil
}
if err := client.Delete(file.RemoteName); err != nil {
file.Errors++
file.Error = err.Error()
} else {
log.Infof("share: removed %s from %s", file.RemoteName, a.AccName)
file.Errors = 0
file.Error = ""
file.Status = entity.FileShareRemoved
}
if err := entity.Db().Save(&file).Error; err != nil {
w.logErr(err)
}
}
}
return err
}