2020-04-07 12:51:01 +02:00
|
|
|
package workers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
|
|
|
"github.com/photoprism/photoprism/internal/mutex"
|
2020-06-07 10:09:35 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/photoprism"
|
2020-05-08 15:41:01 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/query"
|
2020-04-07 12:51:01 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/remote/webdav"
|
2022-04-15 09:42:07 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
2020-04-07 12:51:01 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Uploads local files to a remote account
|
2022-10-15 21:54:11 +02:00
|
|
|
func (w *Sync) upload(a entity.Service) (complete bool, err error) {
|
2020-04-07 12:51:01 +02:00
|
|
|
maxResults := 250
|
|
|
|
|
|
|
|
// Get upload file list from database
|
2020-05-08 15:41:01 +02:00
|
|
|
files, err := query.AccountUploads(a, maxResults)
|
2020-04-07 12:51:01 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(files) == 0 {
|
2020-12-05 00:13:44 +01:00
|
|
|
log.Infof("sync: upload complete for %s", a.AccName)
|
2020-04-07 12:51:01 +02:00
|
|
|
event.Publish("sync.uploaded", event.Data{"account": a})
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2023-03-28 20:37:42 +02:00
|
|
|
client, err := webdav.NewClient(a.AccURL, a.AccUser, a.AccPass, webdav.Timeout(a.AccTimeout))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2020-04-07 12:51:01 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-06-07 10:09:35 +02:00
|
|
|
fileName := photoprism.FileName(file.FileRoot, file.FileName)
|
2020-04-07 12:51:01 +02:00
|
|
|
remoteName := path.Join(a.SyncPath, file.FileName)
|
2023-03-28 20:37:42 +02:00
|
|
|
remoteDir := path.Dir(remoteName)
|
2020-04-07 12:51:01 +02:00
|
|
|
|
2023-03-30 14:50:16 +02:00
|
|
|
// Ensure remote folder exists.
|
2024-01-18 11:23:59 +01:00
|
|
|
if err = client.MkdirAll(remoteDir); err != nil {
|
2023-03-30 14:50:16 +02:00
|
|
|
log.Debugf("sync: %s", err)
|
|
|
|
}
|
2020-04-07 12:51:01 +02:00
|
|
|
|
2024-01-18 11:23:59 +01:00
|
|
|
if err = client.Upload(fileName, remoteName); err != nil {
|
|
|
|
w.logErr(err)
|
2020-04-07 12:51:01 +02:00
|
|
|
continue // try again next time
|
|
|
|
}
|
|
|
|
|
2022-04-15 09:42:07 +02:00
|
|
|
log.Infof("sync: uploaded %s to %s (%s)", clean.Log(file.FileName), clean.Log(remoteName), a.AccName)
|
2020-04-07 12:51:01 +02:00
|
|
|
|
|
|
|
fileSync := entity.NewFileSync(a.ID, remoteName)
|
|
|
|
fileSync.Status = entity.FileSyncUploaded
|
|
|
|
fileSync.RemoteDate = time.Now()
|
|
|
|
fileSync.RemoteSize = file.FileSize
|
|
|
|
fileSync.FileID = file.ID
|
|
|
|
fileSync.Error = ""
|
|
|
|
fileSync.Errors = 0
|
|
|
|
|
2020-05-26 15:15:14 +02:00
|
|
|
if mutex.SyncWorker.Canceled() {
|
2020-04-07 12:51:01 +02:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2024-01-18 11:23:59 +01:00
|
|
|
w.logErr(entity.Db().Save(&fileSync).Error)
|
2020-04-07 12:51:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|