mirror of https://github.com/Xhofe/alist
feat: initial setting items
parent
e4c3ef0262
commit
1a148eee7c
|
@ -27,5 +27,5 @@ bin/*
|
||||||
public/*.html
|
public/*.html
|
||||||
public/assets/
|
public/assets/
|
||||||
public/public/
|
public/public/
|
||||||
data/
|
/data
|
||||||
log/
|
log/
|
11
cmd/alist.go
11
cmd/alist.go
|
@ -3,11 +3,12 @@ package main
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
bootstrap2 "github.com/alist-org/alist/v3/internal/bootstrap"
|
||||||
|
"github.com/alist-org/alist/v3/internal/bootstrap/data"
|
||||||
"github.com/alist-org/alist/v3/internal/conf"
|
"github.com/alist-org/alist/v3/internal/conf"
|
||||||
"github.com/alist-org/alist/v3/server"
|
"github.com/alist-org/alist/v3/server"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/alist-org/alist/v3/bootstrap"
|
|
||||||
"github.com/alist-org/alist/v3/cmd/args"
|
"github.com/alist-org/alist/v3/cmd/args"
|
||||||
_ "github.com/alist-org/alist/v3/drivers"
|
_ "github.com/alist-org/alist/v3/drivers"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
@ -30,10 +31,10 @@ func Init() {
|
||||||
conf.BuiltAt, conf.GoVersion, conf.GitAuthor, conf.GitCommit, conf.Version, conf.WebVersion)
|
conf.BuiltAt, conf.GoVersion, conf.GitAuthor, conf.GitCommit, conf.Version, conf.WebVersion)
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
bootstrap.InitConfig()
|
bootstrap2.InitConfig()
|
||||||
bootstrap.Log()
|
bootstrap2.Log()
|
||||||
bootstrap.InitDB()
|
bootstrap2.InitDB()
|
||||||
bootstrap.InitData()
|
data.InitData()
|
||||||
}
|
}
|
||||||
func main() {
|
func main() {
|
||||||
Init()
|
Init()
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
package data
|
||||||
|
|
||||||
|
func InitData() {
|
||||||
|
initUser()
|
||||||
|
initSettings()
|
||||||
|
}
|
|
@ -0,0 +1,78 @@
|
||||||
|
package data
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/alist-org/alist/v3/internal/conf"
|
||||||
|
"github.com/alist-org/alist/v3/internal/db"
|
||||||
|
"github.com/alist-org/alist/v3/internal/model"
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
var initialSettingItems = []model.SettingItem{
|
||||||
|
// site settings
|
||||||
|
{Key: "version", Value: conf.Version, Type: conf.TypeString, Group: model.SITE, Flag: model.READONLY},
|
||||||
|
{Key: "site_title", Value: "AList", Type: conf.TypeString, Group: model.SITE},
|
||||||
|
{Key: "site_logo", Value: "https://cdn.jsdelivr.net/gh/alist-org/logo@main/logo.svg", Type: conf.TypeString, Group: model.SITE},
|
||||||
|
{Key: "favicon", Value: "https://cdn.jsdelivr.net/gh/alist-org/logo@main/logo.svg", Type: conf.TypeString, Group: model.SITE},
|
||||||
|
{Key: "announcement", Value: "https://github.com/alist-org/alist", Type: conf.TypeString, Group: model.SITE},
|
||||||
|
// style settings
|
||||||
|
{Key: "icon_color", Value: "#1890ff", Type: conf.TypeString, Group: model.STYLE},
|
||||||
|
// preview settings
|
||||||
|
{Key: "text_types", Value: "txt,htm,html,xml,java,properties,sql,js,md,json,conf,ini,vue,php,py,bat,gitignore,yml,go,sh,c,cpp,h,hpp,tsx,vtt,srt,ass,rs,lrc", Type: conf.TypeText, Group: model.PREVIEW, Flag: model.PRIVATE},
|
||||||
|
{Key: "audio_types", Value: "mp3,flac,ogg,m4a,wav,opus", Type: conf.TypeText, Group: model.PREVIEW, Flag: model.PRIVATE},
|
||||||
|
{Key: "video_types", Value: "mp4,mkv,avi,mov,rmvb,webm,flv", Type: conf.TypeText, Group: model.PREVIEW, Flag: model.PRIVATE},
|
||||||
|
{Key: "proxy_types", Value: "m3u8", Type: conf.TypeText, Group: model.PREVIEW, Flag: model.PRIVATE},
|
||||||
|
{Key: "pdf_viewer_url", Value: "https://alist-org.github.io/pdf.js/web/viewer.html?file=$url", Type: conf.TypeString, Group: model.PREVIEW},
|
||||||
|
{Key: "audio_autoplay", Value: "true", Type: conf.TypeBool, Group: model.PREVIEW},
|
||||||
|
{Key: "video_autoplay", Value: "true", Type: conf.TypeBool, Group: model.PREVIEW},
|
||||||
|
// global settings
|
||||||
|
{Key: "hide_files", Value: "/\\/README.md/i", Type: conf.TypeText, Group: model.GLOBAL},
|
||||||
|
{Key: "global_readme", Value: "This is global readme", Type: conf.TypeText, Group: model.GLOBAL},
|
||||||
|
{Key: "customize_head", Type: conf.TypeText, Group: model.GLOBAL, Flag: model.PRIVATE},
|
||||||
|
{Key: "customize_body", Type: conf.TypeText, Group: model.GLOBAL, Flag: model.PRIVATE},
|
||||||
|
}
|
||||||
|
|
||||||
|
func initSettings() {
|
||||||
|
// check deprecated
|
||||||
|
settings, err := db.GetSettings()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("failed get settings: %+v", err)
|
||||||
|
}
|
||||||
|
for i := range settings {
|
||||||
|
if !isActive(settings[i].Key) {
|
||||||
|
settings[i].Flag = model.DEPRECATED
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if settings != nil && len(settings) > 0 {
|
||||||
|
err = db.SaveSettings(settings)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("failed save settings: %+v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// insert new items
|
||||||
|
for i, _ := range initialSettingItems {
|
||||||
|
v := initialSettingItems[i]
|
||||||
|
_, err := db.GetSettingByKey(v.Key)
|
||||||
|
if err == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
err = db.SaveSetting(v)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("failed create setting: %+v", err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
log.Fatalf("failed get setting: %+v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func isActive(key string) bool {
|
||||||
|
for _, item := range initialSettingItems {
|
||||||
|
if item.Key == key {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package bootstrap
|
package data
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/alist-org/alist/v3/cmd/args"
|
"github.com/alist-org/alist/v3/cmd/args"
|
||||||
|
@ -10,10 +10,6 @@ import (
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func InitData() {
|
|
||||||
initUser()
|
|
||||||
}
|
|
||||||
|
|
||||||
func initUser() {
|
func initUser() {
|
||||||
admin, err := db.GetAdmin()
|
admin, err := db.GetAdmin()
|
||||||
adminPassword := random.String(8)
|
adminPassword := random.String(8)
|
|
@ -10,7 +10,7 @@ var db gorm.DB
|
||||||
|
|
||||||
func Init(d *gorm.DB) {
|
func Init(d *gorm.DB) {
|
||||||
db = *d
|
db = *d
|
||||||
err := db.AutoMigrate(new(model.Account), new(model.User), new(model.Meta))
|
err := db.AutoMigrate(new(model.Account), new(model.User), new(model.Meta), new(model.SettingItem))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("failed migrate database: %s", err.Error())
|
log.Fatalf("failed migrate database: %s", err.Error())
|
||||||
}
|
}
|
|
@ -6,12 +6,28 @@ import (
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
func SaveSettings(items []model.SettingItem) error {
|
func GetSettings() ([]model.SettingItem, error) {
|
||||||
return errors.WithStack(db.Save(items).Error)
|
var items []model.SettingItem
|
||||||
|
if err := db.Find(&items).Error; err != nil {
|
||||||
|
return nil, errors.WithStack(err)
|
||||||
|
}
|
||||||
|
return items, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func SaveSetting(item model.SettingItem) error {
|
func GetSettingByKey(key string) (*model.SettingItem, error) {
|
||||||
return errors.WithStack(db.Save(item).Error)
|
var item model.SettingItem
|
||||||
|
if err := db.Where(fmt.Sprintf("%s = ?", columnName("key")), key).First(&item).Error; err != nil {
|
||||||
|
return nil, errors.WithStack(err)
|
||||||
|
}
|
||||||
|
return &item, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetPublicSettings() ([]model.SettingItem, error) {
|
||||||
|
var items []model.SettingItem
|
||||||
|
if err := db.Where(fmt.Sprintf("%s in ?", columnName("flag")), []int{0, 2}).Find(&items).Error; err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return items, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetSettingsByGroup(group int) ([]model.SettingItem, error) {
|
func GetSettingsByGroup(group int) ([]model.SettingItem, error) {
|
||||||
|
@ -22,6 +38,14 @@ func GetSettingsByGroup(group int) ([]model.SettingItem, error) {
|
||||||
return items, nil
|
return items, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func SaveSettings(items []model.SettingItem) error {
|
||||||
|
return errors.WithStack(db.Save(items).Error)
|
||||||
|
}
|
||||||
|
|
||||||
|
func SaveSetting(item model.SettingItem) error {
|
||||||
|
return errors.WithStack(db.Save(item).Error)
|
||||||
|
}
|
||||||
|
|
||||||
func DeleteSettingByKey(key string) error {
|
func DeleteSettingByKey(key string) error {
|
||||||
setting := model.SettingItem{
|
setting := model.SettingItem{
|
||||||
Key: key,
|
Key: key,
|
||||||
|
|
|
@ -1,5 +1,19 @@
|
||||||
package model
|
package model
|
||||||
|
|
||||||
|
const (
|
||||||
|
SITE = iota
|
||||||
|
STYLE
|
||||||
|
PREVIEW
|
||||||
|
GLOBAL
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
PUBLIC = iota
|
||||||
|
PRIVATE
|
||||||
|
READONLY
|
||||||
|
DEPRECATED
|
||||||
|
)
|
||||||
|
|
||||||
type SettingItem struct {
|
type SettingItem struct {
|
||||||
Key string `json:"key" gorm:"primaryKey" binding:"required"` // unique key
|
Key string `json:"key" gorm:"primaryKey" binding:"required"` // unique key
|
||||||
Value string `json:"value"` // value
|
Value string `json:"value"` // value
|
||||||
|
@ -7,5 +21,5 @@ type SettingItem struct {
|
||||||
Type string `json:"type"` // string, number, bool, select
|
Type string `json:"type"` // string, number, bool, select
|
||||||
Values string `json:"values"` // values for select
|
Values string `json:"values"` // values for select
|
||||||
Group int `json:"group"` // use to group setting in frontend
|
Group int `json:"group"` // use to group setting in frontend
|
||||||
Access int `json:"access"` // admin/guest/general
|
Flag int `json:"flag"` // 0 = public, 1 = private, 2 = deprecated, etc.
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue