From 9e2f049e11d6dd735976018a5156ad9df5552f38 Mon Sep 17 00:00:00 2001 From: Rose Hogenson Date: Fri, 16 Oct 2026 10:11:15 -0700 Subject: Serve files from a certain directory on root --- roseh.moe.go | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) (limited to 'roseh.moe.go') diff --git a/roseh.moe.go b/roseh.moe.go index 6ad701f..063a08a 100644 --- a/roseh.moe.go +++ b/roseh.moe.go @@ -321,6 +321,31 @@ func favicon(w http.ResponseWriter, r *http.Request) { serveStaticFile(w, r, "static/favicon.ico") } +func sharedFileHandler(w http.ResponseWriter, r *http.Request) { + fp, err := filepath.Localize(path.Clean(strings.TrimPrefix(r.URL.Path, "/"))) + if err != nil { + notFound(w, r) + return + } + f, err := os.OpenInRoot(filepath.Join(*nezuko, "www"), fp) + if err != nil { + notFound(w, r) + return + } + defer f.Close() + stat, err := f.Stat() + if err != nil { + log.Printf("Warning: sharedFileHandler: %s", err) + notFound(w, r) + return + } + if stat.IsDir() { + notFound(w, r) + return + } + http.ServeContent(w, r, fp, stat.ModTime(), f) +} + func qrHandler(w http.ResponseWriter, r *http.Request) { sig, err := base64.RawURLEncoding.DecodeString(r.FormValue("s")) if err != nil { @@ -1218,7 +1243,7 @@ func main() { apiMux := http.NewServeMux() apiMux.HandleFunc("PUT /api/wormhole/{name}", wormholeAPIUpload) mux.Handle("/api/", apiMux) - mux.Handle("/", &timeoutHandler{h: http.HandlerFunc(notFound), timeout: 5 * time.Minute}) + mux.Handle("/", &timeoutHandler{h: http.HandlerFunc(sharedFileHandler), timeout: 5 * time.Minute}) serverMux := http.NewServeMux() serverMux.Handle(selfURL.Hostname()+"/", http.NewCrossOriginProtection().Handler(&gzipHandler{mux})) jellyfinURL, err := url.Parse("http://127.0.0.1:8096") -- cgit v1.3.1