1
0
dwelling-files/internal/handlers/handlers.go

54 lines
1.1 KiB
Go

package handlers
import (
"dwelling-files/pkg/files"
"dwelling-files/pkg/server"
"dwelling-files/pkg/utils"
"dwelling-files/web"
"log"
"net/http"
"github.com/julienschmidt/httprouter"
)
type IndexData struct {
MainSite string
CurrentPath string
FilesTotalCount int64
FilesTotalSize string
DirectoryCount int64
Items []files.DirEntry
}
type FilesHandlers struct {
directoryPath string
}
func New(directoryPath *string) *FilesHandlers {
return &FilesHandlers{
directoryPath: *directoryPath}
}
func (FilesHandlers) AssetsFS() http.FileSystem {
return web.Assets()
}
func (FilesHandlers) Robots(w http.ResponseWriter, r *http.Request) {
fc, _ := web.AssetsGetFile("robots.txt")
w.Write(fc)
}
func (h *FilesHandlers) Index(w http.ResponseWriter, r *http.Request) {
path := httprouter.CleanPath(server.GetURLParam(r, "filepath"))
currentPath := files.CurrentPath(path)
entries, stats, err := files.ScanDirectory(h.directoryPath+path, path)
if err != nil {
log.Println(err)
return
}
web.Index(utils.MainSite(r.Host), currentPath, &stats, &entries, r, w)
}