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-04-26 14:31:33 +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-03-29 08:34:25 +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
|
|
|
|
}
|
|
|
|
|
|
|
|
// FirstOrCreate returns the matching entity or creates a new one.
|
2020-04-30 20:07:03 +02:00
|
|
|
func (m *FileSync) FirstOrCreate() *FileSync {
|
|
|
|
if err := Db().FirstOrCreate(m, "account_id = ? AND remote_name = ?", m.AccountID, m.RemoteName).Error; err != nil {
|
2020-03-27 18:17:07 +01:00
|
|
|
log.Errorf("file sync: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|