initial import

This commit is contained in:
Олег Бородин
2023-10-20 13:43:23 +02:00
commit b1daca98b5
32 changed files with 11403 additions and 0 deletions

View File

@@ -0,0 +1,56 @@
package handler
import (
"net/http"
"os"
"path/filepath"
"strings"
"filer/pkg/logger"
"github.com/gin-gonic/gin"
)
type HandlerConfig struct {
Datadir string
}
type Handler struct {
log *logger.Logger
datadir string
}
func NewHandler(conf *HandlerConfig) (*Handler, error) {
var err error
hand := &Handler{
log: logger.NewLogger("handler"),
}
return hand, err
}
func (hand *Handler) GetFile(gctx *gin.Context) {
filePath := gctx.FullPath()
localFilePath := filepath.Join(hand.datadir, filePath)
if strings.Contains(localFilePath, "/../") {
gctx.Status(http.StatusBadRequest)
return
}
if len(filePath) > 0 && fileExists(localFilePath) {
gctx.FileAttachment(localFilePath, filepath.Base(filePath))
return
} else {
gctx.Status(http.StatusNotFound)
return
}
}
func fileExists(name string) bool {
fi, err := os.Stat(name)
if err != nil {
if os.IsNotExist(err) {
return false
}
}
return !fi.IsDir()
}