From aec33a154fadcfdcaa28fd661779dc3ce33b8838 Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Tue, 28 Jun 2022 00:37:50 +0400 Subject: [PATCH] Added FIleServer handlers for assets and file share. --- internal/handlers/handlers.go | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/internal/handlers/handlers.go b/internal/handlers/handlers.go index 83776f6..7741eb0 100644 --- a/internal/handlers/handlers.go +++ b/internal/handlers/handlers.go @@ -7,6 +7,7 @@ import ( "dwelling-files/web" "log" "net/http" + "strings" "github.com/julienschmidt/httprouter" ) @@ -22,11 +23,15 @@ type IndexData struct { type FilesHandlers struct { directoryPath string + assetsServer http.Handler + fileServer http.Handler } -func New(directoryPath *string) *FilesHandlers { +func New(directoryPath *string, assetsFS http.FileSystem) *FilesHandlers { return &FilesHandlers{ - directoryPath: *directoryPath} + directoryPath: *directoryPath, + assetsServer: http.FileServer(assetsFS), + fileServer: http.FileServer(http.Dir(*directoryPath))} } func (FilesHandlers) AssetsFS() http.FileSystem { @@ -41,6 +46,17 @@ func (FilesHandlers) Robots(w http.ResponseWriter, r *http.Request) { func (h *FilesHandlers) Index(w http.ResponseWriter, r *http.Request) { path := httprouter.CleanPath(server.GetURLParam(r, "filepath")) + if strings.HasPrefix(path, "/assets") { + h.assetsServer.ServeHTTP(w, r) + return + } + + if strings.HasPrefix(path, "/file") { + r.URL.Path = path[5:] + h.fileServer.ServeHTTP(w, r) + return + } + currentPath := files.CurrentPath(path) entries, stats, err := files.ScanDirectory(h.directoryPath+path, path)