alist/internal/bootstrap/data/setting.go

178 lines
9.7 KiB
Go
Raw Normal View History

2022-06-27 07:51:02 +00:00
package data
import (
2022-08-07 05:09:59 +00:00
"github.com/alist-org/alist/v3/cmd/flags"
2022-06-27 07:51:02 +00:00
"github.com/alist-org/alist/v3/internal/conf"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/op"
2022-12-29 16:24:10 +00:00
"github.com/alist-org/alist/v3/pkg/utils"
2022-06-28 06:18:10 +00:00
"github.com/alist-org/alist/v3/pkg/utils/random"
2022-06-27 07:51:02 +00:00
"github.com/pkg/errors"
"gorm.io/gorm"
)
2022-06-28 06:18:10 +00:00
var initialSettingItems []model.SettingItem
2022-06-27 07:51:02 +00:00
func initSettings() {
2022-08-27 10:35:05 +00:00
InitialSettings()
2022-06-27 07:51:02 +00:00
// check deprecated
settings, err := op.GetSettingItems()
2022-06-27 07:51:02 +00:00
if err != nil {
2022-12-29 16:24:10 +00:00
utils.Log.Fatalf("failed get settings: %+v", err)
2022-06-27 07:51:02 +00:00
}
2022-06-27 07:51:02 +00:00
for i := range settings {
if !isActive(settings[i].Key) && settings[i].Flag != model.DEPRECATED {
2022-06-27 07:51:02 +00:00
settings[i].Flag = model.DEPRECATED
err = op.SaveSettingItem(&settings[i])
if err != nil {
2022-12-29 16:24:10 +00:00
utils.Log.Fatalf("failed save setting: %+v", err)
}
2022-06-27 07:51:02 +00:00
}
}
// create or save setting
2022-07-23 12:42:12 +00:00
for i := range initialSettingItems {
item := &initialSettingItems[i]
// err
stored, err := op.GetSettingItemByKey(item.Key)
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
2022-12-29 16:24:10 +00:00
utils.Log.Fatalf("failed get setting: %+v", err)
continue
}
// save
2022-12-29 16:24:10 +00:00
if stored != nil && item.Key != conf.VERSION {
item.Value = stored.Value
}
if stored == nil || *item != *stored {
err = op.SaveSettingItem(item)
if err != nil {
2022-12-29 16:24:10 +00:00
utils.Log.Fatalf("failed save setting: %+v", err)
}
} else {
// Not save so needs to execute hook
_, err = op.HandleSettingItemHook(item)
if err != nil {
utils.Log.Errorf("failed to execute hook on %s: %+v", item.Key, err)
}
2022-06-27 07:51:02 +00:00
}
}
}
func isActive(key string) bool {
for _, item := range initialSettingItems {
if item.Key == key {
return true
}
}
return false
}
2022-06-28 06:18:10 +00:00
2022-08-27 10:35:05 +00:00
func InitialSettings() []model.SettingItem {
2022-06-28 06:18:10 +00:00
var token string
2022-08-07 05:09:59 +00:00
if flags.Dev {
2022-06-28 06:18:10 +00:00
token = "dev_token"
} else {
token = random.Token()
}
initialSettingItems = []model.SettingItem{
// site settings
2022-06-29 08:08:55 +00:00
{Key: conf.VERSION, Value: conf.Version, Type: conf.TypeString, Group: model.SITE, Flag: model.READONLY},
//{Key: conf.ApiUrl, Value: "", Type: conf.TypeString, Group: model.SITE},
//{Key: conf.BasePath, Value: "", Type: conf.TypeString, Group: model.SITE},
2022-06-29 08:08:55 +00:00
{Key: conf.SiteTitle, Value: "AList", Type: conf.TypeString, Group: model.SITE},
2022-08-30 08:13:01 +00:00
{Key: conf.Announcement, Value: "### repo\nhttps://github.com/alist-org/alist", Type: conf.TypeText, Group: model.SITE},
2022-08-27 15:07:48 +00:00
{Key: "pagination_type", Value: "all", Type: conf.TypeSelect, Options: "all,pagination,load_more,auto_load_more", Group: model.SITE},
{Key: "default_page_size", Value: "30", Type: conf.TypeNumber, Group: model.SITE},
{Key: conf.AllowIndexed, Value: "false", Type: conf.TypeBool, Group: model.SITE},
{Key: conf.AllowMounted, Value: "true", Type: conf.TypeBool, Group: model.SITE},
{Key: conf.RobotsTxt, Value: "User-agent: *\nAllow: /", Type: conf.TypeText, Group: model.SITE},
2022-06-28 06:18:10 +00:00
// style settings
{Key: conf.Logo, Value: "https://cdn.jsdelivr.net/gh/alist-org/logo@main/logo.svg", Type: conf.TypeText, Group: model.STYLE},
2022-07-12 06:03:03 +00:00
{Key: conf.Favicon, Value: "https://cdn.jsdelivr.net/gh/alist-org/logo@main/logo.svg", Type: conf.TypeString, Group: model.STYLE},
{Key: conf.MainColor, Value: "#1890ff", Type: conf.TypeString, Group: model.STYLE},
2022-08-09 10:06:04 +00:00
{Key: "home_icon", Value: "🏠", Type: conf.TypeString, Group: model.STYLE},
2022-08-30 11:34:11 +00:00
{Key: "home_container", Value: "max_980px", Type: conf.TypeSelect, Options: "max_980px,hope_container", Group: model.STYLE},
{Key: "settings_layout", Value: "list", Type: conf.TypeSelect, Options: "list,responsive", Group: model.STYLE},
2022-06-28 06:18:10 +00:00
// preview settings
2022-06-29 08:08:55 +00:00
{Key: conf.TextTypes, 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: conf.AudioTypes, Value: "mp3,flac,ogg,m4a,wav,opus,wma", Type: conf.TypeText, Group: model.PREVIEW, Flag: model.PRIVATE},
2022-06-29 08:08:55 +00:00
{Key: conf.VideoTypes, Value: "mp4,mkv,avi,mov,rmvb,webm,flv", Type: conf.TypeText, Group: model.PREVIEW, Flag: model.PRIVATE},
2022-08-07 16:51:05 +00:00
{Key: conf.ImageTypes, Value: "jpg,tiff,jpeg,png,gif,bmp,svg,ico,swf,webp", Type: conf.TypeText, Group: model.PREVIEW, Flag: model.PRIVATE},
2022-08-23 08:50:54 +00:00
//{Key: conf.OfficeTypes, Value: "doc,docx,xls,xlsx,ppt,pptx", Type: conf.TypeText, Group: model.PREVIEW, Flag: model.PRIVATE},
2022-06-29 08:08:55 +00:00
{Key: conf.ProxyTypes, Value: "m3u8", Type: conf.TypeText, Group: model.PREVIEW, Flag: model.PRIVATE},
2022-12-20 12:07:19 +00:00
{Key: conf.ProxyIgnoreHeaders, Value: "authorization,referer", Type: conf.TypeText, Group: model.PREVIEW, Flag: model.PRIVATE},
2022-08-18 03:18:11 +00:00
{Key: "external_previews", Value: `{}`, Type: conf.TypeText, Group: model.PREVIEW},
2022-08-23 08:50:54 +00:00
{Key: "iframe_previews", Value: `{
"doc,docx,xls,xlsx,ppt,pptx": {
2022-08-31 09:32:57 +00:00
"Microsoft":"https://view.officeapps.live.com/op/view.aspx?src=$e_url",
"Google":"https://docs.google.com/gview?url=$e_url&embedded=true"
2022-08-23 08:50:54 +00:00
},
"pdf": {
"PDF.js":"https://alist-org.github.io/pdf.js/web/viewer.html?file=$e_url"
},
"epub": {
"EPUB.js":"https://alist-org.github.io/static/epub.js/viewer.html?url=$e_url"
2022-08-23 08:50:54 +00:00
}
2022-08-08 05:03:34 +00:00
}`, Type: conf.TypeText, Group: model.PREVIEW},
2022-08-23 08:50:54 +00:00
// {Key: conf.OfficeViewers, Value: `{
// "Microsoft":"https://view.officeapps.live.com/op/view.aspx?src=$url",
// "Google":"https://docs.google.com/gview?url=$url&embedded=true",
//}`, Type: conf.TypeText, Group: model.PREVIEW},
// {Key: conf.PdfViewers, Value: `{
// "pdf.js":"https://alist-org.github.io/pdf.js/web/viewer.html?file=$url"
//}`, Type: conf.TypeText, Group: model.PREVIEW},
2022-09-07 11:18:19 +00:00
{Key: "audio_cover", Value: "https://jsd.nn.ci/gh/alist-org/logo@main/logo.svg", Type: conf.TypeString, Group: model.PREVIEW},
2022-06-29 08:08:55 +00:00
{Key: conf.AudioAutoplay, Value: "true", Type: conf.TypeBool, Group: model.PREVIEW},
{Key: conf.VideoAutoplay, Value: "true", Type: conf.TypeBool, Group: model.PREVIEW},
2022-06-28 06:18:10 +00:00
// global settings
2022-06-29 08:08:55 +00:00
{Key: conf.HideFiles, Value: "/\\/README.md/i", Type: conf.TypeText, Group: model.GLOBAL},
2022-08-27 10:35:05 +00:00
{Key: "package_download", Value: "true", Type: conf.TypeBool, Group: model.GLOBAL},
{Key: conf.CustomizeHead, Value: `<script src="https://polyfill.io/v3/polyfill.min.js?features=String.prototype.replaceAll"></script>`, Type: conf.TypeText, Group: model.GLOBAL, Flag: model.PRIVATE},
2022-06-29 08:08:55 +00:00
{Key: conf.CustomizeBody, Type: conf.TypeText, Group: model.GLOBAL, Flag: model.PRIVATE},
{Key: conf.LinkExpiration, Value: "0", Type: conf.TypeNumber, Group: model.GLOBAL, Flag: model.PRIVATE},
2023-02-14 11:20:15 +00:00
{Key: conf.SignAll, Value: "false", Type: conf.TypeBool, Group: model.GLOBAL, Flag: model.PRIVATE},
2022-08-08 08:29:56 +00:00
{Key: conf.PrivacyRegs, Value: `(?:(?:\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.){3}(?:\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])
([[:xdigit:]]{1,4}(?::[[:xdigit:]]{1,4}){7}|::|:(?::[[:xdigit:]]{1,4}){1,6}|[[:xdigit:]]{1,4}:(?::[[:xdigit:]]{1,4}){1,5}|(?:[[:xdigit:]]{1,4}:){2}(?::[[:xdigit:]]{1,4}){1,4}|(?:[[:xdigit:]]{1,4}:){3}(?::[[:xdigit:]]{1,4}){1,3}|(?:[[:xdigit:]]{1,4}:){4}(?::[[:xdigit:]]{1,4}){1,2}|(?:[[:xdigit:]]{1,4}:){5}:[[:xdigit:]]{1,4}|(?:[[:xdigit:]]{1,4}:){1,6}:)
(?U)access_token=(.*)&`,
Type: conf.TypeText, Group: model.GLOBAL, Flag: model.PRIVATE},
2022-09-06 06:39:21 +00:00
{Key: conf.OcrApi, Value: "https://api.nn.ci/ocr/file/json", Type: conf.TypeString, Group: model.GLOBAL},
{Key: conf.FilenameCharMapping, Value: `{"/": "|"}`, Type: conf.TypeText, Group: model.GLOBAL},
{Key: conf.ForwardDirectLinkParams, Value: "false", Type: conf.TypeBool, Group: model.GLOBAL},
2022-11-30 14:10:07 +00:00
// aria2 settings
{Key: conf.Aria2Uri, Value: "http://localhost:6800/jsonrpc", Type: conf.TypeString, Group: model.ARIA2, Flag: model.PRIVATE},
{Key: conf.Aria2Secret, Value: "", Type: conf.TypeString, Group: model.ARIA2, Flag: model.PRIVATE},
2022-11-30 14:10:07 +00:00
2022-06-28 06:18:10 +00:00
// single settings
2022-06-29 08:08:55 +00:00
{Key: conf.Token, Value: token, Type: conf.TypeString, Group: model.SINGLE, Flag: model.PRIVATE},
{Key: conf.SearchIndex, Value: "none", Type: conf.TypeSelect, Options: "database,database_non_full_text,bleve,none", Group: model.INDEX},
{Key: conf.AutoUpdateIndex, Value: "false", Type: conf.TypeBool, Group: model.INDEX},
{Key: conf.IgnorePaths, Value: "", Type: conf.TypeText, Group: model.INDEX, Flag: model.PRIVATE, Help: `one path per line`},
{Key: conf.MaxIndexDepth, Value: "20", Type: conf.TypeNumber, Group: model.INDEX, Flag: model.PRIVATE, Help: `max depth of index`},
{Key: conf.IndexProgress, Value: "{}", Type: conf.TypeText, Group: model.SINGLE, Flag: model.PRIVATE},
// SSO settings
{Key: conf.SSOLoginEnabled, Value: "false", Type: conf.TypeBool, Group: model.SSO, Flag: model.PUBLIC},
{Key: conf.SSOLoginplatform, Type: conf.TypeSelect, Options: "Casdoor,Github,Microsoft,Google,Dingtalk", Group: model.SSO, Flag: model.PUBLIC},
{Key: conf.SSOClientId, Value: "", Type: conf.TypeString, Group: model.SSO, Flag: model.PRIVATE},
{Key: conf.SSOClientSecret, Value: "", Type: conf.TypeString, Group: model.SSO, Flag: model.PRIVATE},
{Key: conf.SSOOrganizationName, Value: "", Type: conf.TypeString, Group: model.SSO, Flag: model.PRIVATE},
{Key: conf.SSOApplicationName, Value: "", Type: conf.TypeString, Group: model.SSO, Flag: model.PRIVATE},
{Key: conf.SSOEndpointName, Value: "", Type: conf.TypeString, Group: model.SSO, Flag: model.PRIVATE},
{Key: conf.SSOJwtPublicKey, Value: "", Type: conf.TypeString, Group: model.SSO, Flag: model.PRIVATE},
// qbittorrent settings
{Key: conf.QbittorrentUrl, Value: "http://admin:adminadmin@localhost:8080/", Type: conf.TypeString, Group: model.SINGLE, Flag: model.PRIVATE},
{Key: conf.QbittorrentSeedtime, Value: "0", Type: conf.TypeNumber, Group: model.SINGLE, Flag: model.PRIVATE},
2022-06-28 06:18:10 +00:00
}
2022-08-07 05:09:59 +00:00
if flags.Dev {
2022-08-27 10:35:05 +00:00
initialSettingItems = append(initialSettingItems, []model.SettingItem{
{Key: "test_deprecated", Value: "test_value", Type: conf.TypeString, Flag: model.DEPRECATED},
{Key: "test_options", Value: "a", Type: conf.TypeSelect, Options: "a,b,c"},
{Key: "test_help", Type: conf.TypeString, Help: "this is a help message"},
}...)
2022-07-11 14:36:30 +00:00
}
2022-08-27 10:35:05 +00:00
return initialSettingItems
2022-06-28 06:18:10 +00:00
}