2020-03-27 18:17:07 +01:00
|
|
|
package entity
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2020-04-04 17:19:34 +02:00
|
|
|
const (
|
|
|
|
FileSyncNew = "new"
|
2020-04-07 10:42:42 +02:00
|
|
|
FileSyncIgnore = "ignore"
|
2020-04-20 07:43:49 +02:00
|
|
|
FileSyncExists = "exists"
|
2020-04-04 17:19:34 +02:00
|
|
|
FileSyncDownloaded = "downloaded"
|
|
|
|
FileSyncUploaded = "uploaded"
|
|
|
|
)
|
|
|
|
|
2020-03-27 18:17:07 +01:00
|
|
|
// FileSync represents a one-to-many relation between File and Account for syncing with remote services.
|
|
|
|
type FileSync struct {
|
2020-09-13 17:51:43 +02:00
|
|
|
RemoteName string `gorm:"primary_key;auto_increment:false;type:VARBINARY(255)"`
|
2020-03-27 18:17:07 +01:00
|
|
|
AccountID uint `gorm:"primary_key;auto_increment:false"`
|
2020-04-07 10:42:42 +02:00
|
|
|
FileID uint `gorm:"index;"`
|
2020-04-02 18:17:07 +02:00
|
|
|
RemoteDate time.Time
|
|
|
|
RemoteSize int64
|
2020-09-13 17:51:43 +02:00
|
|
|
Status string `gorm:"type:VARBINARY(16);"`
|
|
|
|
Error string `gorm:"type:VARBINARY(512);"`
|
2020-04-03 18:08:49 +02:00
|
|
|
Errors int
|
2020-03-27 18:17:07 +01:00
|
|
|
File *File
|
|
|
|
Account *Account
|
|
|
|
CreatedAt time.Time
|
|
|
|
UpdatedAt time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
// TableName returns the entity database table name.
|
|
|
|
func (FileSync) TableName() string {
|
|
|
|
return "files_sync"
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewFileSync creates a new entity.
|
2020-03-29 08:34:25 +02:00
|
|
|
func NewFileSync(accountID uint, remoteName string) *FileSync {
|
2020-03-27 18:17:07 +01:00
|
|
|
result := &FileSync{
|
2020-03-29 12:02:01 +02:00
|
|
|
AccountID: accountID,
|
|
|
|
RemoteName: remoteName,
|
2020-04-04 17:19:34 +02:00
|
|
|
Status: FileSyncNew,
|
2020-03-27 18:17:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-05-26 11:00:39 +02:00
|
|
|
// Updates multiple columns in the database.
|
|
|
|
func (m *FileSync) Updates(values interface{}) error {
|
|
|
|
return UnscopedDb().Model(m).UpdateColumns(values).Error
|
|
|
|
}
|
|
|
|
|
2022-03-27 21:37:11 +02:00
|
|
|
// Update a column in the database.
|
2020-05-26 11:00:39 +02:00
|
|
|
func (m *FileSync) Update(attr string, value interface{}) error {
|
|
|
|
return UnscopedDb().Model(m).UpdateColumn(attr, value).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save updates the existing or inserts a new row.
|
|
|
|
func (m *FileSync) Save() error {
|
|
|
|
return Db().Save(m).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create inserts a new row to the database.
|
|
|
|
func (m *FileSync) Create() error {
|
|
|
|
return Db().Create(m).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// FirstOrCreateFileSync returns the existing row, inserts a new row or nil in case of errors.
|
|
|
|
func FirstOrCreateFileSync(m *FileSync) *FileSync {
|
|
|
|
result := FileSync{}
|
|
|
|
|
|
|
|
if err := Db().Where("account_id = ? AND remote_name = ?", m.AccountID, m.RemoteName).First(&result).Error; err == nil {
|
|
|
|
return &result
|
|
|
|
} else if err := m.Create(); err != nil {
|
|
|
|
log.Errorf("file-sync: %s", err)
|
|
|
|
return nil
|
2020-03-27 18:17:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|