57 lines
974 B
Go
57 lines
974 B
Go
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()
|
|
}
|