Files
OpenList/server/static.go

99 lines
3.1 KiB
Go
Raw Normal View History

2021-11-13 15:53:26 +08:00
package server
import (
"io/fs"
"os"
2021-11-13 15:53:26 +08:00
"io/ioutil"
"net/http"
2022-05-10 21:40:43 +08:00
"net/http/pprof"
"strings"
"path/filepath"
2022-05-10 21:40:43 +08:00
"github.com/Xhofe/alist/utils"
2022-05-10 21:40:43 +08:00
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/public"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
2021-11-13 15:53:26 +08:00
)
2021-12-20 23:56:17 +08:00
func InitIndex() {
var index fs.File
var err error
2022-02-20 15:14:18 +08:00
if !strings.Contains(conf.Conf.Assets, "/") {
2022-01-14 20:56:20 +08:00
conf.Conf.Assets = conf.DefaultConfig().Assets
2021-12-20 23:56:17 +08:00
}
// 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")
}
2021-11-15 19:06:10 +08:00
if err != nil {
2022-02-20 15:14:18 +08:00
log.Fatal(err.Error())
2021-11-15 19:06:10 +08:00
}
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)
}
2022-02-20 15:14:18 +08:00
cdnUrl := strings.ReplaceAll(conf.Conf.Assets, "$version", conf.WebTag)
cdnUrl = strings.TrimRight(cdnUrl, "/")
2022-02-21 21:47:40 +08:00
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, "assets/", cdnUrl+"/assets/")
}
2022-02-21 21:47:40 +08:00
}
2021-11-13 15:53:26 +08:00
}
func Static(r *gin.Engine) {
var assets fs.FS
var pub fs.FS
var err error
var fsys fs.FS
2021-12-21 00:32:09 +08:00
//InitIndex()
// 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")
}
2021-11-13 15:53:26 +08:00
if err != nil {
log.Fatalf("can't find assets folder")
}
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")
}
2021-12-25 17:07:59 +08:00
if err != nil {
log.Fatalf("can't find public folder")
}
r.StaticFS("/public/", http.FS(pub))
2021-11-13 15:53:26 +08:00
r.NoRoute(func(c *gin.Context) {
c.Header("Content-Type", "text/html")
2022-02-18 18:58:02 +08:00
c.Status(200)
if strings.HasPrefix(c.Request.URL.Path, "/@manage") {
_, _ = c.Writer.WriteString(conf.ManageHtml)
2022-05-15 16:17:52 +08:00
} else if strings.HasPrefix(c.Request.URL.Path, "/debug/pprof") && conf.Debug {
2022-05-10 21:40:43 +08:00
pprof.Index(c.Writer, c.Request)
} else {
_, _ = c.Writer.WriteString(conf.IndexHtml)
}
2021-11-13 15:53:26 +08:00
c.Writer.Flush()
2021-11-22 18:10:09 +08:00
c.Writer.WriteHeaderNow()
2021-11-13 15:53:26 +08:00
})
}