UI overhaul, import fixes, fix user store

This commit is contained in:
2026-07-30 17:07:46 +02:00
parent 773149169b
commit 4b8bd11a4c
75 changed files with 1106 additions and 744 deletions

View File

@@ -14,7 +14,7 @@ import (
func (wc *WebController) serveDirectory(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
var err error
var td = wc.newTemplateData(w, r)
var td = wc.newTemplateData(r)
var path = strings.TrimPrefix(p.ByName("path"), "/")
// Prevent search engines from indexing this page for privacy reasons
@@ -25,7 +25,9 @@ func (wc *WebController) serveDirectory(w http.ResponseWriter, r *http.Request,
return
}
node, err := td.PixelAPI.GetFilesystemPath(path)
pdapi := wc.api.RealIP(util.RemoteAddress(r)).RealAgent(r.UserAgent())
node, err := pdapi.GetFilesystemPath(path)
if err != nil {
if apiErr, ok := errors.AsType[pixelapi.Error](err); ok {
switch apiErr.StatusCode {
@@ -59,15 +61,17 @@ func (wc *WebController) serveDirectory(w http.ResponseWriter, r *http.Request,
func (wc *WebController) serveForbidden(w http.ResponseWriter, r *http.Request) {
log.Debug("Forbidden: %s", r.URL)
w.WriteHeader(http.StatusForbidden)
wc.templates.Run(w, r, "403", wc.newTemplateData(w, r))
if err := wc.templates.Run(w, r, "wrap", wc.newTemplateData(r)); err != nil {
log.Error("Failed to run template 403: %s", err)
}
}
func (wc *WebController) serveNotFound(w http.ResponseWriter, r *http.Request) {
log.Debug("Not Found: %s", r.URL)
w.WriteHeader(http.StatusNotFound)
wc.templates.Run(w, r, "404", wc.newTemplateData(w, r))
wc.templates.Run(w, r, "404", wc.newTemplateData(r))
}
func (wc *WebController) serveUnavailableForLegalReasons(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusUnavailableForLegalReasons)
wc.templates.Run(w, r, "451", wc.newTemplateData(w, r))
wc.templates.Run(w, r, "451", wc.newTemplateData(r))
}

View File

@@ -14,20 +14,15 @@ import (
"time"
"fornaxian.tech/log"
"fornaxian.tech/pixeldrain_api_client/pixelapi"
"fornaxian.tech/util"
)
// TemplateData is a struct that every template expects when being rendered. In
// the field Other you can pass your own template-specific variables.
type TemplateData struct {
tpm *TemplateManager
Authenticated bool
User pixelapi.UserInfo
UserAgent string
APIEndpoint template.URL
PixelAPI pixelapi.PixelAPI
Hostname template.HTML
tpm *TemplateManager
APIEndpoint template.URL
Hostname template.HTML
// Only used on file viewer page
Title string
@@ -37,56 +32,12 @@ type TemplateData struct {
URLQuery url.Values
}
func (wc *WebController) newTemplateData(w http.ResponseWriter, r *http.Request) (t *TemplateData) {
func (wc *WebController) newTemplateData(r *http.Request) (t *TemplateData) {
t = &TemplateData{
tpm: wc.templates,
Authenticated: false,
UserAgent: r.UserAgent(),
APIEndpoint: template.URL(wc.config.APIURLExternal),
// Use the user's IP address for making requests
PixelAPI: wc.api.RealIP(util.RemoteAddress(r)).RealAgent(r.UserAgent()),
Hostname: template.HTML(wc.hostname),
URLQuery: r.URL.Query(),
}
// If the user is authenticated we'll indentify him and put the user info
// into the templatedata. This is used for putting the username in the menu
// and stuff like that
if key, err := wc.getAPIKey(r); err == nil {
t.PixelAPI = t.PixelAPI.Login(key) // Use the user's API key for all requests
if t.User, err = t.PixelAPI.GetUser(); err != nil {
// This session key doesn't work, or the backend is down, user
// cannot be authenticated
log.Debug("Session check for key '%s' failed: %s", key, err)
if err.Error() == "authentication_required" || err.Error() == "authentication_failed" {
// Disable API authentication
t.PixelAPI = wc.api.RealIP(util.RemoteAddress(r)).RealAgent(r.UserAgent())
// Remove the authentication cookie
log.Debug("Deleting invalid API key")
http.SetCookie(w, &http.Cookie{
Name: "pd_auth_key",
Value: "",
Path: "/",
Expires: time.Unix(0, 0),
Domain: wc.config.SessionCookieDomain,
})
http.SetCookie(w, &http.Cookie{
Name: "pd_auth_key",
Value: "",
Path: "/",
Expires: time.Unix(0, 0),
Domain: ".nova.storage",
})
}
return t
}
// Authentication succeeded
t.Authenticated = true
tpm: wc.templates,
APIEndpoint: template.URL(wc.config.APIURLExternal),
Hostname: template.HTML(wc.hostname),
URLQuery: r.URL.Query(),
}
return t

View File

@@ -87,7 +87,7 @@ func New(r *httprouter.Router, prefix string, conf Config) (wc *WebController) {
if conf.MaintenanceMode {
r.NotFound = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusServiceUnavailable)
wc.templates.Run(w, r, "maintenance", wc.newTemplateData(w, r))
wc.templates.Run(w, r, "maintenance", wc.newTemplateData(r))
})
return wc
}
@@ -118,7 +118,7 @@ func New(r *httprouter.Router, prefix string, conf Config) (wc *WebController) {
}
r.NotFound = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
wc.serveTemplate("wrap", handlerOpts{})(w, r, nil)
wc.serveTemplate("wrap")(w, r, nil)
})
// Request method shorthands. These help keep the array of handlers aligned
@@ -130,10 +130,10 @@ func New(r *httprouter.Router, prefix string, conf Config) (wc *WebController) {
path string // The URL path this API will be registered on
handler httprouter.Handle // The function to run when this API is called
}{
{GET, "api" /* */, wc.serveMarkdown("api.md", handlerOpts{})},
{GET, "api" /* */, wc.serveMarkdown("api.md")},
{GET, "d/*path" /* */, wc.serveDirectory},
{GET, "widgets" /* */, wc.serveTemplate("widgets", handlerOpts{})},
{GET, "status" /* */, wc.serveTemplate("status", handlerOpts{})},
{GET, "widgets" /* */, wc.serveTemplate("widgets")},
{GET, "status" /* */, wc.serveTemplate("status")},
{GET, "theme.css", wc.themeHandler},
} {
r.Handle(h.method, prefix+"/"+h.path, middleware(h.handler))
@@ -165,43 +165,27 @@ func middleware(handle httprouter.Handle) httprouter.Handle {
}
}
type handlerOpts struct {
Auth bool
NoEmbed bool
NoExec bool
}
func (wc *WebController) serveTemplate(tpl string, opts handlerOpts) httprouter.Handle {
func (wc *WebController) serveTemplate(tpl string) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
if opts.NoEmbed {
w.Header().Set("X-Frame-Options", "DENY")
}
// if opts.NoEmbed {
w.Header().Set("X-Frame-Options", "DENY")
// }
var td = wc.newTemplateData(w, r)
if opts.Auth && !td.Authenticated {
http.Redirect(w, r, "/login?redirect="+url.QueryEscape(r.URL.Path), http.StatusSeeOther)
return
}
err := wc.templates.Run(w, r, tpl, td)
err := wc.templates.Run(w, r, tpl, wc.newTemplateData(r))
if err != nil && !util.IsNetError(err) {
log.Error("Error executing template '%s': %s", tpl, err)
}
}
}
func (wc *WebController) serveMarkdown(tpl string, opts handlerOpts) httprouter.Handle {
func (wc *WebController) serveMarkdown(tpl string) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
var err error
if opts.NoEmbed {
w.Header().Set("X-Frame-Options", "DENY")
}
// if opts.NoEmbed {
w.Header().Set("X-Frame-Options", "DENY")
// }
var tpld = wc.newTemplateData(w, r)
if opts.Auth && !tpld.Authenticated {
http.Redirect(w, r, "/login", http.StatusSeeOther)
return
}
var tpld = wc.newTemplateData(r)
// Execute the raw markdown template and save the result in a buffer
var tplBuf bytes.Buffer