alist/server/static/static.go

120 lines
3.1 KiB
Go
Raw Normal View History

2022-08-28 15:13:03 +00:00
package static
import (
"errors"
2022-08-28 15:13:03 +00:00
"fmt"
"io"
2022-08-28 15:13:03 +00:00
"io/fs"
"net/http"
"os"
2022-08-28 15:13:03 +00:00
"strings"
"github.com/alist-org/alist/v3/internal/conf"
"github.com/alist-org/alist/v3/internal/setting"
"github.com/alist-org/alist/v3/pkg/utils"
2024-02-03 11:44:50 +00:00
"github.com/alist-org/alist/v3/public"
2022-08-28 15:13:03 +00:00
"github.com/gin-gonic/gin"
)
var static fs.FS
func initStatic() {
if conf.Conf.DistDir == "" {
dist, err := fs.Sub(public.Public, "dist")
if err != nil {
utils.Log.Fatalf("failed to read dist dir")
}
static = dist
return
}
static = os.DirFS(conf.Conf.DistDir)
}
func initIndex() {
indexFile, err := static.Open("index.html")
2022-08-28 15:13:03 +00:00
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
utils.Log.Fatalf("index.html not exist, you may forget to put dist of frontend to public/dist")
}
utils.Log.Fatalf("failed to read index.html: %v", err)
2022-08-28 15:13:03 +00:00
}
defer func() {
_ = indexFile.Close()
}()
index, err := io.ReadAll(indexFile)
if err != nil {
utils.Log.Fatalf("failed to read dist/index.html")
}
2022-08-28 15:13:03 +00:00
conf.RawIndexHtml = string(index)
siteConfig := getSiteConfig()
replaceMap := map[string]string{
"cdn: undefined": fmt.Sprintf("cdn: '%s'", siteConfig.Cdn),
"base_path: undefined": fmt.Sprintf("base_path: '%s'", siteConfig.BasePath),
}
for k, v := range replaceMap {
conf.RawIndexHtml = strings.Replace(conf.RawIndexHtml, k, v, 1)
}
2022-08-28 15:13:03 +00:00
UpdateIndex()
}
func UpdateIndex() {
2022-09-06 06:39:21 +00:00
favicon := setting.GetStr(conf.Favicon)
title := setting.GetStr(conf.SiteTitle)
customizeHead := setting.GetStr(conf.CustomizeHead)
customizeBody := setting.GetStr(conf.CustomizeBody)
2022-09-08 14:21:52 +00:00
mainColor := setting.GetStr(conf.MainColor)
2022-08-28 15:13:03 +00:00
conf.ManageHtml = conf.RawIndexHtml
replaceMap1 := map[string]string{
"https://jsd.nn.ci/gh/alist-org/logo@main/logo.svg": favicon,
2022-09-08 14:21:52 +00:00
"Loading...": title,
"main_color: undefined": fmt.Sprintf("main_color: '%s'", mainColor),
2022-08-28 15:13:03 +00:00
}
for k, v := range replaceMap1 {
conf.ManageHtml = strings.Replace(conf.ManageHtml, k, v, 1)
}
conf.IndexHtml = conf.ManageHtml
replaceMap2 := map[string]string{
"<!-- customize head -->": customizeHead,
"<!-- customize body -->": customizeBody,
}
for k, v := range replaceMap2 {
conf.IndexHtml = strings.Replace(conf.IndexHtml, k, v, 1)
}
}
func Static(r *gin.RouterGroup, noRoute func(handlers ...gin.HandlerFunc)) {
initStatic()
initIndex()
folders := []string{"assets", "images", "streamer", "static"}
2022-12-15 09:48:29 +00:00
r.Use(func(c *gin.Context) {
for i := range folders {
if strings.HasPrefix(c.Request.RequestURI, fmt.Sprintf("/%s/", folders[i])) {
c.Header("Cache-Control", "public, max-age=15552000")
2022-12-15 09:48:29 +00:00
}
}
})
2022-08-28 15:13:03 +00:00
for i, folder := range folders {
sub, err := fs.Sub(static, folder)
2022-08-28 15:13:03 +00:00
if err != nil {
utils.Log.Fatalf("can't find folder: %s", folder)
2022-08-28 15:13:03 +00:00
}
r.StaticFS(fmt.Sprintf("/%s/", folders[i]), http.FS(sub))
}
noRoute(func(c *gin.Context) {
if c.Request.Method != "GET" && c.Request.Method != "POST" {
c.Status(405)
return
}
2022-08-28 15:13:03 +00:00
c.Header("Content-Type", "text/html")
c.Status(200)
if strings.HasPrefix(c.Request.URL.Path, "/@manage") {
_, _ = c.Writer.WriteString(conf.ManageHtml)
} else {
_, _ = c.Writer.WriteString(conf.IndexHtml)
}
c.Writer.Flush()
c.Writer.WriteHeaderNow()
})
}