mirror of
				https://github.com/filebrowser/filebrowser.git
				synced 2025-10-31 17:23:09 +00:00 
			
		
		
		
	 cbdf3cafb6
			
		
	
	
		cbdf3cafb6
		
	
	
	
	
		
			
			License: MIT Signed-off-by: Henrique Dias <hacdias@gmail.com> Former-commit-id: aa32ec7e27f24aee497d3c1a6bb3cda4f3f30265 [formerly 92f1d95d44e0a1c0b8dd6d81b467829c8832acc8] [formerly d8c05dc476ae88b730547cce1e3644c69ee17278 [formerly 84f108f1c56592871af4a97cc5ab2a9986005b62]] Former-commit-id: 9f766a5b8ef847569fb8f4f16540c256fa9ca92f [formerly 0b6b1e48435b3fe5819327e311f08eb179a19b3e] Former-commit-id: 046dfa8350d2f1b4e7fb789fb026069e137437ef
		
			
				
	
	
		
			127 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			127 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package http
 | |
| 
 | |
| import (
 | |
| 	"encoding/json"
 | |
| 	"log"
 | |
| 	"net/http"
 | |
| 	"os"
 | |
| 	"path/filepath"
 | |
| 	"strings"
 | |
| 	"text/template"
 | |
| 
 | |
| 	rice "github.com/GeertJohan/go.rice"
 | |
| 	"github.com/filebrowser/filebrowser/v2/auth"
 | |
| 	"github.com/filebrowser/filebrowser/v2/settings"
 | |
| 	"github.com/filebrowser/filebrowser/v2/storage"
 | |
| 	"github.com/filebrowser/filebrowser/v2/version"
 | |
| )
 | |
| 
 | |
| func handleWithStaticData(w http.ResponseWriter, r *http.Request, d *data, box *rice.Box, file, contentType string) (int, error) {
 | |
| 	w.Header().Set("Content-Type", contentType)
 | |
| 
 | |
| 	staticURL := strings.TrimPrefix(d.server.BaseURL+"/static", "/")
 | |
| 
 | |
| 	auther, err := d.store.Auth.Get(d.settings.AuthMethod)
 | |
| 	if err != nil {
 | |
| 		return http.StatusInternalServerError, err
 | |
| 	}
 | |
| 
 | |
| 	data := map[string]interface{}{
 | |
| 		"Name":            d.settings.Branding.Name,
 | |
| 		"DisableExternal": d.settings.Branding.DisableExternal,
 | |
| 		"BaseURL":         d.server.BaseURL,
 | |
| 		"Version":         version.Version,
 | |
| 		"StaticURL":       staticURL,
 | |
| 		"Signup":          d.settings.Signup,
 | |
| 		"NoAuth":          d.settings.AuthMethod == auth.MethodNoAuth,
 | |
| 		"LoginPage":       auther.LoginPage(),
 | |
| 		"CSS":             false,
 | |
| 		"ReCaptcha":       false,
 | |
| 	}
 | |
| 
 | |
| 	if d.settings.Branding.Files != "" {
 | |
| 		path := filepath.Join(d.settings.Branding.Files, "custom.css")
 | |
| 		_, err := os.Stat(path)
 | |
| 
 | |
| 		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 {
 | |
| 		raw, err := d.store.Auth.Get(d.settings.AuthMethod)
 | |
| 		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
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	b, err := json.MarshalIndent(data, "", "  ")
 | |
| 	if err != nil {
 | |
| 		return http.StatusInternalServerError, err
 | |
| 	}
 | |
| 
 | |
| 	data["Json"] = string(b)
 | |
| 
 | |
| 	index := template.Must(template.New("index").Delims("[{[", "]}]").Parse(box.MustString(file)))
 | |
| 	err = index.Execute(w, data)
 | |
| 	if err != nil {
 | |
| 		return http.StatusInternalServerError, err
 | |
| 	}
 | |
| 
 | |
| 	return 0, nil
 | |
| }
 | |
| 
 | |
| func getStaticHandlers(storage *storage.Storage, server *settings.Server) (http.Handler, http.Handler) {
 | |
| 	box := rice.MustFindBox("../frontend/dist")
 | |
| 	handler := http.FileServer(box.HTTPBox())
 | |
| 
 | |
| 	index := handle(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) {
 | |
| 		if r.Method != http.MethodGet {
 | |
| 			return http.StatusNotFound, nil
 | |
| 		}
 | |
| 
 | |
| 		w.Header().Set("x-xss-protection", "1; mode=block")
 | |
| 		return handleWithStaticData(w, r, d, box, "index.html", "text/html; charset=utf-8")
 | |
| 	}, "", storage, server)
 | |
| 
 | |
| 	static := handle(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) {
 | |
| 		if r.Method != http.MethodGet {
 | |
| 			return http.StatusNotFound, nil
 | |
| 		}
 | |
| 
 | |
| 		if d.settings.Branding.Files != "" {
 | |
| 			if strings.HasPrefix(r.URL.Path, "img/") {
 | |
| 				path := filepath.Join(d.settings.Branding.Files, r.URL.Path)
 | |
| 				if _, err := os.Stat(path); err == nil {
 | |
| 					http.ServeFile(w, r, path)
 | |
| 					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
 | |
| 			}
 | |
| 		}
 | |
| 
 | |
| 		if !strings.HasSuffix(r.URL.Path, ".js") {
 | |
| 			handler.ServeHTTP(w, r)
 | |
| 			return 0, nil
 | |
| 		}
 | |
| 
 | |
| 		return handleWithStaticData(w, r, d, box, r.URL.Path, "application/javascript; charset=utf-8")
 | |
| 	}, "/static/", storage, server)
 | |
| 
 | |
| 	return index, static
 | |
| }
 |