2020-02-02 00:31:09 +01:00
|
|
|
package entity
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
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
|
|
|
|
|
|
|
"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"
|
2020-03-28 15:29:17 +01:00
|
|
|
"github.com/ulule/deepcopier"
|
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-02-02 00:31:09 +01:00
|
|
|
// Account represents a remote service account for uploading, downloading or syncing media files.
|
|
|
|
type Account struct {
|
2020-04-04 17:19:34 +02:00
|
|
|
ID uint `gorm:"primary_key"`
|
2020-04-26 14:31:33 +02:00
|
|
|
AccName string `gorm:"type:varchar(255);"`
|
|
|
|
AccOwner string `gorm:"type:varchar(255);"`
|
2020-04-04 17:19:34 +02:00
|
|
|
AccURL string `gorm:"type:varbinary(512);"`
|
2020-04-26 14:31:33 +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);"`
|
2020-04-04 17:19:34 +02:00
|
|
|
AccError string `gorm:"type:varbinary(512);"`
|
|
|
|
AccErrors int
|
|
|
|
AccShare bool
|
|
|
|
AccSync bool
|
|
|
|
RetryLimit int
|
2020-04-26 14:31:33 +02:00
|
|
|
SharePath string `gorm:"type:varbinary(255);"`
|
2020-04-04 17:19:34 +02:00
|
|
|
ShareSize string `gorm:"type:varbinary(16);"`
|
|
|
|
ShareExpires int
|
2020-04-26 14:31:33 +02:00
|
|
|
SyncPath string `gorm:"type:varbinary(255);"`
|
2020-04-04 17:19:34 +02:00
|
|
|
SyncStatus string `gorm:"type:varbinary(16);"`
|
|
|
|
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
|
|
|
|
|
|
|
// 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,
|
|
|
|
SyncStatus: AccountSyncStatusRefresh,
|
|
|
|
}
|
2020-03-28 15:29:17 +01:00
|
|
|
|
2020-04-30 20:07:03 +02:00
|
|
|
err = model.Save(form)
|
2020-03-28 15:29:17 +01:00
|
|
|
|
|
|
|
return model, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save updates the entity using form data and stores it in the database.
|
2020-04-30 20:07:03 +02:00
|
|
|
func (m *Account) Save(form form.Account) error {
|
|
|
|
db := Db()
|
|
|
|
|
2020-03-28 15:29:17 +01:00
|
|
|
if err := deepcopier.Copy(m).From(form); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-05 22:26:53 +02:00
|
|
|
if m.AccType != string(remote.ServiceWebDAV) {
|
2020-03-30 09:17:46 +02:00
|
|
|
// TODO: Only WebDAV supported at the moment
|
|
|
|
m.AccShare = false
|
|
|
|
m.AccSync = false
|
|
|
|
}
|
|
|
|
|
2020-04-06 22:09:45 +02:00
|
|
|
// Set defaults
|
2020-03-30 09:17:46 +02:00
|
|
|
if m.SharePath == "" {
|
|
|
|
m.SharePath = "/"
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.SyncPath == "" {
|
|
|
|
m.SyncPath = "/"
|
|
|
|
}
|
|
|
|
|
2020-04-06 22:09:45 +02:00
|
|
|
// Refresh after performing changes
|
|
|
|
if m.AccSync && m.SyncStatus == AccountSyncStatusSynced {
|
|
|
|
m.SyncStatus = AccountSyncStatusRefresh
|
|
|
|
}
|
|
|
|
|
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 {
|
2020-04-03 18:08:49 +02:00
|
|
|
c := webdav.New(m.AccURL, m.AccUser, m.AccPass)
|
2020-03-31 17:26:25 +02:00
|
|
|
result, err = c.Directories("/", true)
|
|
|
|
}
|
|
|
|
|
2020-04-02 18:17:07 +02:00
|
|
|
sort.Sort(result)
|
2020-03-31 17:26:25 +02:00
|
|
|
|
|
|
|
return result, err
|
|
|
|
}
|