mirror of
https://github.com/HChaZZY/alist.git
synced 2025-12-17 23:56:25 +08:00
feat: Added SubFolder and localAssets configuration(#1324)
Co-authored-by: eaglexmw <eagle_xmw@163.com>
This commit is contained in:
@@ -28,6 +28,8 @@ type Config struct {
|
|||||||
Address string `json:"address" env:"ADDR"`
|
Address string `json:"address" env:"ADDR"`
|
||||||
Port int `json:"port" env:"PORT"`
|
Port int `json:"port" env:"PORT"`
|
||||||
Assets string `json:"assets" env:"ASSETS"`
|
Assets string `json:"assets" env:"ASSETS"`
|
||||||
|
LocalAssets string `json:"localassets" env:"LOCALASSETS"`
|
||||||
|
SubFolder string `json:"subfolder" env:"SUBFOLDER"`
|
||||||
Database Database `json:"database"`
|
Database Database `json:"database"`
|
||||||
Scheme Scheme `json:"scheme"`
|
Scheme Scheme `json:"scheme"`
|
||||||
Cache CacheConfig `json:"cache"`
|
Cache CacheConfig `json:"cache"`
|
||||||
@@ -39,6 +41,8 @@ func DefaultConfig() *Config {
|
|||||||
Address: "0.0.0.0",
|
Address: "0.0.0.0",
|
||||||
Port: 5244,
|
Port: 5244,
|
||||||
Assets: "https://npm.elemecdn.com/alist-web@$version/dist",
|
Assets: "https://npm.elemecdn.com/alist-web@$version/dist",
|
||||||
|
SubFolder: "",
|
||||||
|
LocalAssets: "",
|
||||||
TempDir: "data/temp",
|
TempDir: "data/temp",
|
||||||
Database: Database{
|
Database: Database{
|
||||||
Type: "sqlite3",
|
Type: "sqlite3",
|
||||||
|
|||||||
@@ -2,11 +2,14 @@ package server
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"io/fs"
|
"io/fs"
|
||||||
|
"os"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/pprof"
|
"net/http/pprof"
|
||||||
"strings"
|
"strings"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
|
"github.com/Xhofe/alist/utils"
|
||||||
"github.com/Xhofe/alist/conf"
|
"github.com/Xhofe/alist/conf"
|
||||||
"github.com/Xhofe/alist/public"
|
"github.com/Xhofe/alist/public"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@@ -19,31 +22,65 @@ func InitIndex() {
|
|||||||
if !strings.Contains(conf.Conf.Assets, "/") {
|
if !strings.Contains(conf.Conf.Assets, "/") {
|
||||||
conf.Conf.Assets = conf.DefaultConfig().Assets
|
conf.Conf.Assets = conf.DefaultConfig().Assets
|
||||||
}
|
}
|
||||||
|
// if LocalAssets is local path, read local index.html.
|
||||||
|
if (utils.IsDir(filepath.Dir(conf.Conf.LocalAssets))) && utils.Exists(filepath.Join(conf.Conf.LocalAssets, "index.html")) {
|
||||||
|
index, err = os.Open(filepath.Join(conf.Conf.LocalAssets, "index.html"))
|
||||||
|
defer index.Close()
|
||||||
|
log.Infof("used local index.html")
|
||||||
|
} else {
|
||||||
index, err = public.Public.Open("index.html")
|
index, err = public.Public.Open("index.html")
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err.Error())
|
log.Fatal(err.Error())
|
||||||
}
|
}
|
||||||
data, _ := ioutil.ReadAll(index)
|
data, _ := ioutil.ReadAll(index)
|
||||||
|
conf.RawIndexHtml = string(data)
|
||||||
|
// if exist SUB_FOLDER, replace it by config: SubFolder
|
||||||
|
subfolder := strings.Trim(conf.Conf.SubFolder, "/")
|
||||||
|
if strings.Contains(conf.RawIndexHtml, "SUB_FOLDER") {
|
||||||
|
conf.RawIndexHtml = strings.ReplaceAll(conf.RawIndexHtml, "SUB_FOLDER", subfolder)
|
||||||
|
}
|
||||||
cdnUrl := strings.ReplaceAll(conf.Conf.Assets, "$version", conf.WebTag)
|
cdnUrl := strings.ReplaceAll(conf.Conf.Assets, "$version", conf.WebTag)
|
||||||
cdnUrl = strings.TrimRight(cdnUrl, "/")
|
cdnUrl = strings.TrimRight(cdnUrl, "/")
|
||||||
conf.RawIndexHtml = string(data)
|
|
||||||
if strings.Contains(conf.RawIndexHtml, "CDN_URL") {
|
if strings.Contains(conf.RawIndexHtml, "CDN_URL") {
|
||||||
|
if (cdnUrl == "") && (subfolder != "") {
|
||||||
|
conf.RawIndexHtml = strings.ReplaceAll(conf.RawIndexHtml, "CDN_URL", subfolder)
|
||||||
|
conf.RawIndexHtml = strings.ReplaceAll(conf.RawIndexHtml, "assets/", "/" + subfolder+"/assets/")
|
||||||
|
} else {
|
||||||
conf.RawIndexHtml = strings.ReplaceAll(conf.RawIndexHtml, "/CDN_URL", cdnUrl)
|
conf.RawIndexHtml = strings.ReplaceAll(conf.RawIndexHtml, "/CDN_URL", cdnUrl)
|
||||||
conf.RawIndexHtml = strings.ReplaceAll(conf.RawIndexHtml, "assets/", cdnUrl+"/assets/")
|
conf.RawIndexHtml = strings.ReplaceAll(conf.RawIndexHtml, "assets/", cdnUrl+"/assets/")
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Static(r *gin.Engine) {
|
func Static(r *gin.Engine) {
|
||||||
|
var assets fs.FS
|
||||||
|
var pub fs.FS
|
||||||
|
var err error
|
||||||
|
var fsys fs.FS
|
||||||
//InitIndex()
|
//InitIndex()
|
||||||
assets, err := fs.Sub(public.Public, "assets")
|
// if LocalAssets is local path, read local assets.
|
||||||
|
fsys = os.DirFS(conf.Conf.LocalAssets)
|
||||||
|
if (utils.IsDir(filepath.Dir(conf.Conf.LocalAssets))) && utils.Exists(filepath.Join(conf.Conf.LocalAssets, "assets")) {
|
||||||
|
assets, err = fs.Sub(fsys, "assets")
|
||||||
|
log.Infof("used local assets")
|
||||||
|
} else {
|
||||||
|
assets, err = fs.Sub(public.Public, "assets")
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("can't find assets folder")
|
log.Fatalf("can't find assets folder")
|
||||||
}
|
}
|
||||||
pub, err := fs.Sub(public.Public, "public")
|
r.StaticFS("/assets/", http.FS(assets))
|
||||||
|
// if LocalAssets is local path, read local assets.
|
||||||
|
if (utils.IsDir(filepath.Dir(conf.Conf.LocalAssets))) && utils.Exists(filepath.Join(conf.Conf.LocalAssets, "public")) {
|
||||||
|
pub, err = fs.Sub(fsys, "public")
|
||||||
|
log.Infof("used local public")
|
||||||
|
} else {
|
||||||
|
pub, err = fs.Sub(public.Public, "public")
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("can't find public folder")
|
log.Fatalf("can't find public folder")
|
||||||
}
|
}
|
||||||
r.StaticFS("/assets/", http.FS(assets))
|
|
||||||
r.StaticFS("/public/", http.FS(pub))
|
r.StaticFS("/public/", http.FS(pub))
|
||||||
r.NoRoute(func(c *gin.Context) {
|
r.NoRoute(func(c *gin.Context) {
|
||||||
c.Header("Content-Type", "text/html")
|
c.Header("Content-Type", "text/html")
|
||||||
|
|||||||
Reference in New Issue
Block a user