Serve files from subdirs
Anirudh Oppiliappan x@icyphox.sh
Wed, 07 Dec 2022 22:13:22 +0530
A
fs.go
@@ -0,0 +1,24 @@
+package main + +import ( + "net/http" + "os" +) + +type nodirFileSystem struct { + fs http.FileSystem +} + +func (nd nodirFileSystem) Open(path string) (http.File, error) { + f, err := nd.fs.Open(path) + if err != nil { + return nil, err + } + + s, err := f.Stat() + if s.IsDir() { + return nil, os.ErrNotExist + } + + return f, nil +}
M
main.go
→
main.go
@@ -91,12 +91,6 @@ func (s *settings) indexpage(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, s.index) } -func (s *settings) servefile(w http.ResponseWriter, r *http.Request) { - f := flow.Param(r.Context(), "file") - log.Printf("serving file: %s", f) - http.ServeFile(w, r, filepath.Join(s.storepath, f)) -} - func (s *settings) readSettings() { flag.StringVar(&s.url, "url", "localhost", "url for fsrv to serve files") flag.StringVar(&s.addr, "addr", "0.0.0.0:9393", "address to listen on")@@ -117,7 +111,7 @@ st.readSettings()
mux.HandleFunc("/", st.upload, "POST") mux.HandleFunc("/", st.indexpage, "GET") - mux.HandleFunc("/:file", st.servefile, "GET") + mux.Handle("/...", http.FileServer(nodirFileSystem{http.Dir(st.storepath)})) log.Println("listening on " + st.addr) http.ListenAndServe(st.addr, mux)