2020-03-27 18:17:07 +01:00
|
|
|
package entity
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
"github.com/photoprism/photoprism/internal/mutex"
|
|
|
|
)
|
|
|
|
|
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.
|
|
|
|
func (m *FileSync) FirstOrCreate(db *gorm.DB) *FileSync {
|
|
|
|
mutex.Db.Lock()
|
|
|
|
defer mutex.Db.Unlock()
|
|
|
|
|
2020-03-29 08:34:25 +02:00
|
|
|
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
|
|
|
|
}
|