initial import
This commit is contained in:
56
internal/handler/handler.go
Normal file
56
internal/handler/handler.go
Normal 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()
|
||||
}
|
||||
Reference in New Issue
Block a user