2020-04-06 09:41:42 +02:00
|
|
|
package workers
|
2020-04-03 18:08:49 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-04-07 12:51:01 +02:00
|
|
|
"path"
|
2020-04-04 13:53:52 +02:00
|
|
|
"path/filepath"
|
2020-04-03 18:08:49 +02:00
|
|
|
|
|
|
|
"github.com/photoprism/photoprism/internal/config"
|
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
|
|
|
"github.com/photoprism/photoprism/internal/form"
|
|
|
|
"github.com/photoprism/photoprism/internal/mutex"
|
|
|
|
"github.com/photoprism/photoprism/internal/query"
|
2020-04-05 22:26:53 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/remote"
|
|
|
|
"github.com/photoprism/photoprism/internal/remote/webdav"
|
2020-04-03 18:08:49 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/thumb"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Share represents a share worker.
|
|
|
|
type Share struct {
|
|
|
|
conf *config.Config
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewShare returns a new service share worker.
|
|
|
|
func NewShare(conf *config.Config) *Share {
|
|
|
|
return &Share{conf: conf}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start starts the share worker.
|
|
|
|
func (s *Share) Start() (err error) {
|
|
|
|
if err := mutex.Share.Start(); err != nil {
|
|
|
|
event.Error(fmt.Sprintf("share: %s", err.Error()))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer mutex.Share.Stop()
|
|
|
|
|
|
|
|
f := form.AccountSearch{
|
|
|
|
Share: true,
|
|
|
|
}
|
|
|
|
|
2020-04-06 16:34:29 +02:00
|
|
|
// Find accounts for which sharing is enabled
|
2020-05-08 15:41:01 +02:00
|
|
|
accounts, err := query.Accounts(f)
|
2020-04-03 18:08:49 +02:00
|
|
|
|
2020-04-06 16:34:29 +02:00
|
|
|
// Upload newly shared files
|
2020-04-03 18:08:49 +02:00
|
|
|
for _, a := range accounts {
|
2020-04-04 17:19:34 +02:00
|
|
|
if mutex.Share.Canceled() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-05 22:26:53 +02:00
|
|
|
if a.AccType != remote.ServiceWebDAV {
|
2020-04-03 18:08:49 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-05-08 15:41:01 +02:00
|
|
|
files, err := query.FileShares(a.ID, entity.FileShareNew)
|
2020-04-03 18:08:49 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("share: %s", err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(files) == 0 {
|
2020-04-06 16:34:29 +02:00
|
|
|
// No files to upload for this account
|
2020-04-03 18:08:49 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
client := webdav.New(a.AccURL, a.AccUser, a.AccPass)
|
2020-04-04 13:53:52 +02:00
|
|
|
existingDirs := make(map[string]string)
|
2020-04-03 18:08:49 +02:00
|
|
|
|
|
|
|
for _, file := range files {
|
2020-04-04 17:19:34 +02:00
|
|
|
if mutex.Share.Canceled() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-04 13:53:52 +02:00
|
|
|
dir := filepath.Dir(file.RemoteName)
|
|
|
|
|
2020-04-07 12:51:01 +02:00
|
|
|
if _, ok := existingDirs[dir]; !ok {
|
2020-04-04 13:53:52 +02:00
|
|
|
if err := client.CreateDir(dir); err != nil {
|
2020-05-07 12:33:09 +02:00
|
|
|
log.Errorf("share: could not create folder %s", dir)
|
2020-04-04 13:53:52 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-07 12:51:01 +02:00
|
|
|
srcFileName := path.Join(s.conf.OriginalsPath(), file.File.FileName)
|
2020-04-03 18:08:49 +02:00
|
|
|
|
|
|
|
if a.ShareSize != "" {
|
|
|
|
thumbType, ok := thumb.Types[a.ShareSize]
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
log.Errorf("share: invalid size %s", a.ShareSize)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-05-05 17:04:13 +02:00
|
|
|
srcFileName, err = thumb.FromFile(srcFileName, file.File.FileHash, s.conf.ThumbPath(), thumbType.Width, thumbType.Height, thumbType.Options...)
|
2020-04-03 18:08:49 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("share: %s", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := client.Upload(srcFileName, file.RemoteName); err != nil {
|
|
|
|
log.Errorf("share: %s", err.Error())
|
|
|
|
file.Errors++
|
|
|
|
file.Error = err.Error()
|
|
|
|
} else {
|
2020-04-06 10:26:26 +02:00
|
|
|
log.Infof("share: uploaded %s to %s", file.RemoteName, a.AccName)
|
2020-04-03 18:08:49 +02:00
|
|
|
file.Errors = 0
|
|
|
|
file.Error = ""
|
|
|
|
file.Status = entity.FileShareShared
|
|
|
|
}
|
|
|
|
|
|
|
|
if a.RetryLimit >= 0 && file.Errors > a.RetryLimit {
|
|
|
|
file.Status = entity.FileShareError
|
|
|
|
}
|
|
|
|
|
2020-04-04 17:19:34 +02:00
|
|
|
if mutex.Share.Canceled() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-08 15:41:01 +02:00
|
|
|
if err := entity.Db().Save(&file).Error; err != nil {
|
2020-04-03 18:08:49 +02:00
|
|
|
log.Errorf("share: %s", err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-06 16:34:29 +02:00
|
|
|
// Remove previously shared files if expired
|
2020-04-04 13:53:52 +02:00
|
|
|
for _, a := range accounts {
|
2020-04-04 17:19:34 +02:00
|
|
|
if mutex.Share.Canceled() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-05 22:26:53 +02:00
|
|
|
if a.AccType != remote.ServiceWebDAV {
|
2020-04-04 13:53:52 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-05-08 15:41:01 +02:00
|
|
|
files, err := query.ExpiredFileShares(a)
|
2020-04-04 13:53:52 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("share: %s", err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(files) == 0 {
|
2020-04-06 16:34:29 +02:00
|
|
|
// No files to remove for this account
|
2020-04-04 13:53:52 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
client := webdav.New(a.AccURL, a.AccUser, a.AccPass)
|
|
|
|
|
|
|
|
for _, file := range files {
|
2020-04-04 17:19:34 +02:00
|
|
|
if mutex.Share.Canceled() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-04 13:53:52 +02:00
|
|
|
if err := client.Delete(file.RemoteName); err != nil {
|
|
|
|
file.Errors++
|
|
|
|
file.Error = err.Error()
|
|
|
|
} else {
|
2020-04-06 10:26:26 +02:00
|
|
|
log.Infof("share: removed %s from %s", file.RemoteName, a.AccName)
|
2020-04-04 13:53:52 +02:00
|
|
|
file.Errors = 0
|
|
|
|
file.Error = ""
|
|
|
|
file.Status = entity.FileShareRemoved
|
|
|
|
}
|
|
|
|
|
2020-05-08 15:41:01 +02:00
|
|
|
if err := entity.Db().Save(&file).Error; err != nil {
|
2020-04-04 13:53:52 +02:00
|
|
|
log.Errorf("share: %s", err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-03 18:08:49 +02:00
|
|
|
return err
|
|
|
|
}
|