1
0

Added FIleServer handlers for assets and file share.

This commit is contained in:
Alexander Andreev 2022-06-28 00:37:50 +04:00
parent 1506f1ac68
commit aec33a154f
Signed by: Arav
GPG Key ID: 0388CC8FAA51063F

View File

@ -7,6 +7,7 @@ import (
"dwelling-files/web" "dwelling-files/web"
"log" "log"
"net/http" "net/http"
"strings"
"github.com/julienschmidt/httprouter" "github.com/julienschmidt/httprouter"
) )
@ -22,11 +23,15 @@ type IndexData struct {
type FilesHandlers struct { type FilesHandlers struct {
directoryPath string directoryPath string
assetsServer http.Handler
fileServer http.Handler
} }
func New(directoryPath *string) *FilesHandlers { func New(directoryPath *string, assetsFS http.FileSystem) *FilesHandlers {
return &FilesHandlers{ return &FilesHandlers{
directoryPath: *directoryPath} directoryPath: *directoryPath,
assetsServer: http.FileServer(assetsFS),
fileServer: http.FileServer(http.Dir(*directoryPath))}
} }
func (FilesHandlers) AssetsFS() http.FileSystem { 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) { func (h *FilesHandlers) Index(w http.ResponseWriter, r *http.Request) {
path := httprouter.CleanPath(server.GetURLParam(r, "filepath")) 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) currentPath := files.CurrentPath(path)
entries, stats, err := files.ScanDirectory(h.directoryPath+path, path) entries, stats, err := files.ScanDirectory(h.directoryPath+path, path)