2019-01-06 06:44:33 +08:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2021-04-19 20:49:40 +08:00
|
|
|
"fmt"
|
2021-03-10 01:59:19 +08:00
|
|
|
"io/fs"
|
2019-01-06 06:44:33 +08:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2020-06-01 04:24:18 +08:00
|
|
|
"path"
|
2019-01-06 06:44:33 +08:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2019-01-06 07:01:16 +08:00
|
|
|
"text/template"
|
2019-01-06 06:44:33 +08:00
|
|
|
|
|
|
|
"github.com/filebrowser/filebrowser/v2/auth"
|
2019-01-08 18:29:09 +08:00
|
|
|
"github.com/filebrowser/filebrowser/v2/settings"
|
2019-01-06 06:44:33 +08:00
|
|
|
"github.com/filebrowser/filebrowser/v2/storage"
|
|
|
|
"github.com/filebrowser/filebrowser/v2/version"
|
|
|
|
)
|
|
|
|
|
2021-03-10 01:59:19 +08:00
|
|
|
func handleWithStaticData(w http.ResponseWriter, _ *http.Request, d *data, fSys fs.FS, file, contentType string) (int, error) {
|
2019-01-06 06:44:33 +08:00
|
|
|
w.Header().Set("Content-Type", contentType)
|
|
|
|
|
2019-02-15 20:58:45 +08:00
|
|
|
auther, err := d.store.Auth.Get(d.settings.AuthMethod)
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
|
2019-01-06 06:44:33 +08:00
|
|
|
data := map[string]interface{}{
|
2023-02-16 06:30:48 +08:00
|
|
|
"Name": d.settings.Branding.Name,
|
|
|
|
"DisableExternal": d.settings.Branding.DisableExternal,
|
|
|
|
"DisableUsedPercentage": d.settings.Branding.DisableUsedPercentage,
|
|
|
|
"Color": d.settings.Branding.Color,
|
|
|
|
"BaseURL": d.server.BaseURL,
|
|
|
|
"Version": version.Version,
|
|
|
|
"StaticURL": path.Join(d.server.BaseURL, "/static"),
|
|
|
|
"Signup": d.settings.Signup,
|
|
|
|
"NoAuth": d.settings.AuthMethod == auth.MethodNoAuth,
|
|
|
|
"AuthMethod": d.settings.AuthMethod,
|
|
|
|
"LoginPage": auther.LoginPage(),
|
|
|
|
"CSS": false,
|
|
|
|
"ReCaptcha": false,
|
|
|
|
"Theme": d.settings.Branding.Theme,
|
|
|
|
"EnableThumbs": d.server.EnableThumbnails,
|
|
|
|
"ResizePreview": d.server.ResizePreview,
|
|
|
|
"EnableExec": d.server.EnableExec,
|
2023-07-29 00:15:44 +08:00
|
|
|
"TusSettings": d.settings.Tus,
|
2019-01-06 06:44:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if d.settings.Branding.Files != "" {
|
2020-06-01 07:12:36 +08:00
|
|
|
fPath := filepath.Join(d.settings.Branding.Files, "custom.css")
|
2021-03-04 07:10:08 +08:00
|
|
|
_, err := os.Stat(fPath) //nolint:govet
|
2019-01-06 06:44:33 +08:00
|
|
|
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
log.Printf("couldn't load custom styles: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
data["CSS"] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.settings.AuthMethod == auth.MethodJSONAuth {
|
2021-03-04 07:10:08 +08:00
|
|
|
raw, err := d.store.Auth.Get(d.settings.AuthMethod) //nolint:govet
|
2019-01-06 06:44:33 +08:00
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
|
|
|
|
auther := raw.(*auth.JSONAuth)
|
|
|
|
|
|
|
|
if auther.ReCaptcha != nil {
|
|
|
|
data["ReCaptcha"] = auther.ReCaptcha.Key != "" && auther.ReCaptcha.Secret != ""
|
|
|
|
data["ReCaptchaHost"] = auther.ReCaptcha.Host
|
|
|
|
data["ReCaptchaKey"] = auther.ReCaptcha.Key
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-15 20:28:19 +08:00
|
|
|
b, err := json.Marshal(data)
|
2019-01-06 06:44:33 +08:00
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
|
2021-08-09 22:44:23 +08:00
|
|
|
data["Json"] = strings.ReplaceAll(string(b), `'`, `\'`)
|
2019-01-06 06:44:33 +08:00
|
|
|
|
2021-03-10 01:59:19 +08:00
|
|
|
fileContents, err := fs.ReadFile(fSys, file)
|
2020-10-02 21:09:03 +08:00
|
|
|
if err != nil {
|
|
|
|
if err == os.ErrNotExist {
|
|
|
|
return http.StatusNotFound, err
|
|
|
|
}
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
2021-03-10 01:59:19 +08:00
|
|
|
index := template.Must(template.New("index").Delims("[{[", "]}]").Parse(string(fileContents)))
|
2019-01-06 06:44:33 +08:00
|
|
|
err = index.Execute(w, data)
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
2021-03-10 01:59:19 +08:00
|
|
|
func getStaticHandlers(store *storage.Storage, server *settings.Server, assetsFs fs.FS) (index, static http.Handler) {
|
2020-06-01 07:12:36 +08:00
|
|
|
index = handle(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) {
|
2019-01-06 06:44:33 +08:00
|
|
|
if r.Method != http.MethodGet {
|
|
|
|
return http.StatusNotFound, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("x-xss-protection", "1; mode=block")
|
2023-08-26 19:55:51 +08:00
|
|
|
return handleWithStaticData(w, r, d, assetsFs, "public/index.html", "text/html; charset=utf-8")
|
2020-06-01 07:12:36 +08:00
|
|
|
}, "", store, server)
|
2019-01-06 06:44:33 +08:00
|
|
|
|
2020-06-01 07:12:36 +08:00
|
|
|
static = handle(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) {
|
2019-01-06 06:44:33 +08:00
|
|
|
if r.Method != http.MethodGet {
|
|
|
|
return http.StatusNotFound, nil
|
|
|
|
}
|
|
|
|
|
2021-04-19 20:49:40 +08:00
|
|
|
const maxAge = 86400 // 1 day
|
|
|
|
w.Header().Set("Cache-Control", fmt.Sprintf("public, max-age=%v", maxAge))
|
|
|
|
|
2019-01-06 06:44:33 +08:00
|
|
|
if d.settings.Branding.Files != "" {
|
|
|
|
if strings.HasPrefix(r.URL.Path, "img/") {
|
2020-06-01 07:12:36 +08:00
|
|
|
fPath := filepath.Join(d.settings.Branding.Files, r.URL.Path)
|
|
|
|
if _, err := os.Stat(fPath); err == nil {
|
|
|
|
http.ServeFile(w, r, fPath)
|
2019-01-06 06:44:33 +08:00
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
} else if r.URL.Path == "custom.css" && d.settings.Branding.Files != "" {
|
|
|
|
http.ServeFile(w, r, filepath.Join(d.settings.Branding.Files, "custom.css"))
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-22 20:48:45 +08:00
|
|
|
if !strings.HasSuffix(r.URL.Path, ".js") {
|
|
|
|
http.FileServer(http.FS(assetsFs)).ServeHTTP(w, r)
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
fileContents, err := fs.ReadFile(assetsFs, r.URL.Path+".gz")
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusNotFound, err
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Encoding", "gzip")
|
|
|
|
w.Header().Set("Content-Type", "application/javascript; charset=utf-8")
|
|
|
|
|
|
|
|
if _, err := w.Write(fileContents); err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
2019-01-06 06:44:33 +08:00
|
|
|
|
2021-04-21 03:51:10 +08:00
|
|
|
return 0, nil
|
2020-06-01 07:12:36 +08:00
|
|
|
}, "/static/", store, server)
|
2019-01-06 06:44:33 +08:00
|
|
|
|
|
|
|
return index, static
|
|
|
|
}
|