2020-02-02 00:31:09 +01:00
|
|
|
package entity
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2022-03-27 21:37:11 +02:00
|
|
|
"fmt"
|
2020-03-31 17:26:25 +02:00
|
|
|
"sort"
|
2020-02-02 00:31:09 +01:00
|
|
|
"time"
|
2020-03-28 15:29:17 +01:00
|
|
|
|
2022-03-27 21:37:11 +02:00
|
|
|
"github.com/ulule/deepcopier"
|
|
|
|
|
2020-03-28 15:29:17 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/form"
|
2020-04-05 22:26:53 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/remote"
|
|
|
|
"github.com/photoprism/photoprism/internal/remote/webdav"
|
2020-04-02 18:17:07 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
2021-09-23 23:46:17 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
2020-02-02 00:31:09 +01:00
|
|
|
)
|
|
|
|
|
2020-04-04 17:19:34 +02:00
|
|
|
const (
|
|
|
|
AccountSyncStatusRefresh = "refresh"
|
|
|
|
AccountSyncStatusDownload = "download"
|
|
|
|
AccountSyncStatusUpload = "upload"
|
|
|
|
AccountSyncStatusSynced = "synced"
|
|
|
|
)
|
|
|
|
|
2020-06-01 09:45:24 +02:00
|
|
|
type Accounts []Account
|
|
|
|
|
2020-02-02 00:31:09 +01:00
|
|
|
// Account represents a remote service account for uploading, downloading or syncing media files.
|
2022-03-30 20:36:25 +02:00
|
|
|
//
|
|
|
|
// Field Descriptions:
|
|
|
|
// - AccTimeout configures the timeout for requests, options: "", high, medium, low, none.
|
|
|
|
// - AccErrors holds the number of connection errors since the last reset.
|
|
|
|
// - AccShare enables manual upload, see SharePath, ShareSize, and ShareExpires.
|
|
|
|
// - AccSync enables automatic file synchronization, see SyncDownload and SyncUpload.
|
|
|
|
// - RetryLimit specifies the number of retry attempts, a negative value disables the limit.
|
2020-02-02 00:31:09 +01:00
|
|
|
type Account struct {
|
2020-04-04 17:19:34 +02:00
|
|
|
ID uint `gorm:"primary_key"`
|
2021-09-23 23:46:17 +02:00
|
|
|
AccName string `gorm:"type:VARCHAR(160);"`
|
|
|
|
AccOwner string `gorm:"type:VARCHAR(160);"`
|
2022-03-30 20:36:25 +02:00
|
|
|
AccURL string `gorm:"type:VARCHAR(255);"`
|
2020-09-13 17:51:43 +02:00
|
|
|
AccType string `gorm:"type:VARBINARY(255);"`
|
|
|
|
AccKey string `gorm:"type:VARBINARY(255);"`
|
|
|
|
AccUser string `gorm:"type:VARBINARY(255);"`
|
|
|
|
AccPass string `gorm:"type:VARBINARY(255);"`
|
2022-03-30 20:36:25 +02:00
|
|
|
AccTimeout string `gorm:"type:VARBINARY(16);"`
|
2020-09-13 17:51:43 +02:00
|
|
|
AccError string `gorm:"type:VARBINARY(512);"`
|
2022-03-30 20:36:25 +02:00
|
|
|
AccErrors int
|
|
|
|
AccShare bool
|
|
|
|
AccSync bool
|
|
|
|
RetryLimit int
|
2020-12-15 20:14:06 +01:00
|
|
|
SharePath string `gorm:"type:VARBINARY(500);"`
|
2020-09-13 17:51:43 +02:00
|
|
|
ShareSize string `gorm:"type:VARBINARY(16);"`
|
2020-04-04 17:19:34 +02:00
|
|
|
ShareExpires int
|
2020-12-15 20:14:06 +01:00
|
|
|
SyncPath string `gorm:"type:VARBINARY(500);"`
|
2020-09-13 17:51:43 +02:00
|
|
|
SyncStatus string `gorm:"type:VARBINARY(16);"`
|
2020-04-04 17:19:34 +02:00
|
|
|
SyncInterval int
|
|
|
|
SyncDate sql.NullTime `deepcopier:"skip"`
|
|
|
|
SyncUpload bool
|
|
|
|
SyncDownload bool
|
|
|
|
SyncFilenames bool
|
|
|
|
SyncRaw bool
|
|
|
|
CreatedAt time.Time `deepcopier:"skip"`
|
|
|
|
UpdatedAt time.Time `deepcopier:"skip"`
|
|
|
|
DeletedAt *time.Time `deepcopier:"skip" sql:"index"`
|
2020-02-02 00:31:09 +01:00
|
|
|
}
|
2020-03-28 15:29:17 +01:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// TableName returns the entity table name.
|
2022-03-30 20:36:25 +02:00
|
|
|
func (Account) TableName() string {
|
|
|
|
return "accounts"
|
|
|
|
}
|
|
|
|
|
2020-03-28 15:29:17 +01:00
|
|
|
// CreateAccount creates a new account entity in the database.
|
2020-04-30 20:07:03 +02:00
|
|
|
func CreateAccount(form form.Account) (model *Account, err error) {
|
2020-04-04 17:19:34 +02:00
|
|
|
model = &Account{
|
|
|
|
ShareSize: "",
|
|
|
|
ShareExpires: 0,
|
|
|
|
RetryLimit: 3,
|
2022-03-27 21:37:11 +02:00
|
|
|
AccTimeout: string(webdav.TimeoutDefault),
|
2020-04-04 17:19:34 +02:00
|
|
|
SyncStatus: AccountSyncStatusRefresh,
|
|
|
|
}
|
2020-03-28 15:29:17 +01:00
|
|
|
|
2020-05-26 11:00:39 +02:00
|
|
|
err = model.SaveForm(form)
|
2020-03-28 15:29:17 +01:00
|
|
|
|
|
|
|
return model, err
|
|
|
|
}
|
|
|
|
|
2022-03-27 21:37:11 +02:00
|
|
|
// LogError updates the account error count and message.
|
|
|
|
func (m *Account) LogError(err error) error {
|
|
|
|
if err == nil {
|
|
|
|
return m.ResetErrors(true, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update error message and increase count.
|
|
|
|
m.AccError = err.Error()
|
|
|
|
m.AccErrors++
|
|
|
|
|
|
|
|
// Disable sharing when retry limit is reached.
|
|
|
|
if m.RetryLimit > 0 && m.AccErrors > m.RetryLimit {
|
|
|
|
m.AccShare = false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update fields in database.
|
|
|
|
return m.Updates(Account{AccError: m.AccError, AccErrors: m.AccErrors, AccShare: m.AccShare})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResetErrors resets the account and related file error messages and counters.
|
|
|
|
func (m *Account) ResetErrors(share, sync bool) error {
|
2022-04-14 14:26:45 +02:00
|
|
|
if !share && !sync || Db().NewRecord(m) {
|
2022-03-27 21:37:11 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.ID == 0 {
|
|
|
|
return fmt.Errorf("invalid account id")
|
|
|
|
}
|
|
|
|
|
|
|
|
if share {
|
|
|
|
if err := Db().Model(FileShare{}).Where("account_id = ?", m.ID).Updates(Values{"error": "", "errors": 0}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if sync {
|
|
|
|
if err := Db().Model(FileSync{}).Where("account_id = ?", m.ID).Updates(Values{"error": "", "errors": 0}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m.AccError = ""
|
|
|
|
m.AccErrors = 0
|
|
|
|
|
|
|
|
return m.Updates(Values{"acc_error": m.AccError, "acc_errors": m.AccErrors})
|
|
|
|
}
|
|
|
|
|
2021-09-23 23:46:17 +02:00
|
|
|
// SaveForm saves the entity using form data and stores it in the database.
|
2020-05-26 11:00:39 +02:00
|
|
|
func (m *Account) SaveForm(form form.Account) error {
|
2020-04-30 20:07:03 +02:00
|
|
|
db := Db()
|
|
|
|
|
2022-03-27 21:37:11 +02:00
|
|
|
// Copy model values from form.
|
2020-03-28 15:29:17 +01:00
|
|
|
if err := deepcopier.Copy(m).From(form); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-27 12:49:23 +02:00
|
|
|
// TODO: Support for other remote services in addition to WebDAV.
|
|
|
|
if m.AccType != remote.ServiceWebDAV {
|
2022-03-27 21:37:11 +02:00
|
|
|
m.AccShare = false // Disable manual upload.
|
|
|
|
m.AccSync = false // Disable background sync.
|
2020-03-30 09:17:46 +02:00
|
|
|
}
|
|
|
|
|
2022-03-27 12:49:23 +02:00
|
|
|
// Prevent two-way sync, see https://github.com/photoprism/photoprism/issues/1785
|
|
|
|
if m.SyncUpload && m.SyncDownload {
|
|
|
|
m.SyncUpload = false
|
|
|
|
}
|
|
|
|
|
2022-03-27 21:37:11 +02:00
|
|
|
// Set default manual upload folder if empty.
|
2020-03-30 09:17:46 +02:00
|
|
|
if m.SharePath == "" {
|
|
|
|
m.SharePath = "/"
|
|
|
|
}
|
|
|
|
|
2022-03-27 21:37:11 +02:00
|
|
|
// Set default background sync folder if empty.
|
2020-03-30 09:17:46 +02:00
|
|
|
if m.SyncPath == "" {
|
|
|
|
m.SyncPath = "/"
|
|
|
|
}
|
|
|
|
|
2022-03-27 21:37:11 +02:00
|
|
|
// Number of remote request retry attempts.
|
|
|
|
if m.RetryLimit < -1 {
|
|
|
|
m.RetryLimit = -1 // Disabled.
|
|
|
|
} else if m.RetryLimit > 999 {
|
|
|
|
m.RetryLimit = 999 // 999 retries max.
|
|
|
|
}
|
|
|
|
|
2022-03-27 12:49:23 +02:00
|
|
|
// Refresh after performing changes.
|
2020-04-06 22:09:45 +02:00
|
|
|
if m.AccSync && m.SyncStatus == AccountSyncStatusSynced {
|
|
|
|
m.SyncStatus = AccountSyncStatusRefresh
|
|
|
|
}
|
|
|
|
|
2022-04-14 14:26:45 +02:00
|
|
|
// Reset error counters if account already exists.
|
|
|
|
if !Db().NewRecord(m) {
|
|
|
|
Log("account", "reset errors", m.ResetErrors(m.AccShare, m.AccSync))
|
2022-03-27 21:37:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure account name and owner are not too long.
|
2021-09-23 23:46:17 +02:00
|
|
|
m.AccName = txt.Clip(m.AccName, txt.ClipName)
|
|
|
|
m.AccOwner = txt.Clip(m.AccOwner, txt.ClipName)
|
|
|
|
|
2022-03-27 21:37:11 +02:00
|
|
|
// Save changes.
|
2020-03-28 15:29:17 +01:00
|
|
|
return db.Save(m).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete deletes the entity from the database.
|
2020-04-30 20:07:03 +02:00
|
|
|
func (m *Account) Delete() error {
|
|
|
|
return Db().Delete(m).Error
|
2020-03-28 15:29:17 +01:00
|
|
|
}
|
2020-03-31 17:26:25 +02:00
|
|
|
|
2020-04-02 18:17:07 +02:00
|
|
|
// Directories returns a list of directories or albums in an account.
|
|
|
|
func (m *Account) Directories() (result fs.FileInfos, err error) {
|
2020-04-05 22:26:53 +02:00
|
|
|
if m.AccType == remote.ServiceWebDAV {
|
2022-03-27 21:37:11 +02:00
|
|
|
client := webdav.New(m.AccURL, m.AccUser, m.AccPass, webdav.Timeout(m.AccTimeout))
|
|
|
|
result, err = client.Directories("/", true, 0)
|
2020-03-31 17:26:25 +02:00
|
|
|
}
|
|
|
|
|
2022-03-27 21:37:11 +02:00
|
|
|
// Sort directory list.
|
2020-04-02 18:17:07 +02:00
|
|
|
sort.Sort(result)
|
2020-03-31 17:26:25 +02:00
|
|
|
|
2022-03-27 21:37:11 +02:00
|
|
|
// Update error count and message.
|
|
|
|
if err := m.LogError(err); err != nil {
|
|
|
|
log.Warnf("account: %s", err)
|
|
|
|
}
|
|
|
|
|
2020-03-31 17:26:25 +02:00
|
|
|
return result, err
|
|
|
|
}
|
2020-05-26 11:00:39 +02:00
|
|
|
|
|
|
|
// Updates multiple columns in the database.
|
|
|
|
func (m *Account) Updates(values interface{}) error {
|
2022-04-04 08:54:03 +02:00
|
|
|
return UnscopedDb().Model(m).UpdateColumns(values).Error
|
2020-05-26 11:00:39 +02:00
|
|
|
}
|
|
|
|
|
2021-09-23 23:46:17 +02:00
|
|
|
// Update a column in the database.
|
2020-05-26 11:00:39 +02:00
|
|
|
func (m *Account) Update(attr string, value interface{}) error {
|
2022-04-04 08:54:03 +02:00
|
|
|
return UnscopedDb().Model(m).UpdateColumn(attr, value).Error
|
2020-05-26 11:00:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Save updates the existing or inserts a new row.
|
|
|
|
func (m *Account) Save() error {
|
|
|
|
return Db().Save(m).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create inserts a new row to the database.
|
|
|
|
func (m *Account) Create() error {
|
|
|
|
return Db().Create(m).Error
|
|
|
|
}
|
2022-02-27 17:32:54 +01:00
|
|
|
|
|
|
|
// ShareOriginals tests if the unmodified originals should be shared.
|
|
|
|
func (m *Account) ShareOriginals() bool {
|
|
|
|
return m.ShareSize == ""
|
|
|
|
}
|