2020-02-05 16:49:51 +01:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2021-01-17 13:08:44 +01:00
|
|
|
db_drivers "github.com/photoview/photoview/api/database/drivers"
|
2020-09-24 17:52:53 +02:00
|
|
|
"github.com/pkg/errors"
|
2020-11-23 19:39:44 +01:00
|
|
|
"gorm.io/gorm"
|
2020-02-05 16:49:51 +01:00
|
|
|
)
|
|
|
|
|
2020-11-23 19:59:01 +01:00
|
|
|
type SiteInfo struct {
|
2020-11-28 21:49:33 +01:00
|
|
|
InitialSetup bool `gorm:"not null"`
|
|
|
|
PeriodicScanInterval int `gorm:"not null"`
|
|
|
|
ConcurrentWorkers int `gorm:"not null"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (SiteInfo) TableName() string {
|
|
|
|
return "site_info"
|
2020-11-23 19:59:01 +01:00
|
|
|
}
|
2020-09-20 22:19:25 +02:00
|
|
|
|
2021-04-23 15:10:23 +02:00
|
|
|
func DefaultSiteInfo() SiteInfo {
|
|
|
|
defaultConcurrentWorkers := 3
|
|
|
|
if db_drivers.DatabaseDriver() == db_drivers.DatabaseDriverSqlite {
|
|
|
|
defaultConcurrentWorkers = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
return SiteInfo{
|
|
|
|
InitialSetup: true,
|
|
|
|
PeriodicScanInterval: 0,
|
|
|
|
ConcurrentWorkers: defaultConcurrentWorkers,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-24 17:52:53 +02:00
|
|
|
// GetSiteInfo gets the site info row from the database, and creates it if it does not exist
|
2020-11-23 19:39:44 +01:00
|
|
|
func GetSiteInfo(db *gorm.DB) (*SiteInfo, error) {
|
|
|
|
|
2021-04-23 15:10:23 +02:00
|
|
|
var siteInfo []*SiteInfo
|
2020-02-05 16:49:51 +01:00
|
|
|
|
2021-04-23 15:10:23 +02:00
|
|
|
if err := db.Limit(1).Find(&siteInfo).Error; err != nil {
|
|
|
|
return nil, errors.Wrap(err, "get site info from database")
|
|
|
|
}
|
2021-01-17 13:08:44 +01:00
|
|
|
|
2021-04-23 15:10:23 +02:00
|
|
|
if len(siteInfo) == 0 {
|
|
|
|
newSiteInfo := DefaultSiteInfo()
|
2020-11-30 16:42:13 +01:00
|
|
|
|
2021-04-23 15:10:23 +02:00
|
|
|
if err := db.Create(&newSiteInfo).Error; err != nil {
|
|
|
|
return nil, errors.Wrap(err, "initialize site_info")
|
2020-11-30 16:42:13 +01:00
|
|
|
}
|
2020-02-05 16:49:51 +01:00
|
|
|
|
2021-04-23 15:10:23 +02:00
|
|
|
return &newSiteInfo, nil
|
|
|
|
} else {
|
|
|
|
return siteInfo[0], nil
|
|
|
|
}
|
2020-02-05 16:49:51 +01:00
|
|
|
}
|