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

54 lines
1.1 KiB
Go
Raw Normal View History

2022-06-27 04:38:09 +04:00
package handlers
import (
"dwelling-files/pkg/files"
2022-06-27 23:37:12 +04:00
"dwelling-files/pkg/server"
"dwelling-files/pkg/utils"
2022-06-27 04:38:09 +04:00
"dwelling-files/web"
2022-06-27 23:37:12 +04:00
"log"
2022-06-27 04:38:09 +04:00
"net/http"
2022-06-27 23:37:12 +04:00
"github.com/julienschmidt/httprouter"
2022-06-27 04:38:09 +04:00
)
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) {
2022-06-27 23:37:12 +04:00
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
}
2022-06-27 04:38:09 +04:00
2022-06-27 23:37:12 +04:00
web.Index(utils.MainSite(r.Host), currentPath, &stats, &entries, r, w)
2022-06-27 04:38:09 +04:00
}