mirror of
https://github.com/gofiber/fiber.git
synced 2025-02-23 12:24:02 +00:00
update sendfile
Co-Authored-By: Renan Bastos <renanbastos.tec@gmail.com>
This commit is contained in:
parent
1b4c0432c0
commit
c78a5013c0
@ -54,13 +54,10 @@ var ConfigDefault = Config{
|
|||||||
MaxAge: 0,
|
MaxAge: 0,
|
||||||
}
|
}
|
||||||
|
|
||||||
// config current after to call New
|
|
||||||
var cfg Config
|
|
||||||
|
|
||||||
// New creates a new middleware handler
|
// New creates a new middleware handler
|
||||||
func New(config ...Config) fiber.Handler {
|
func New(config ...Config) fiber.Handler {
|
||||||
// Set default config
|
// Set default config
|
||||||
cfg = ConfigDefault
|
cfg := ConfigDefault
|
||||||
|
|
||||||
// Override config if provided
|
// Override config if provided
|
||||||
if len(config) > 0 {
|
if len(config) > 0 {
|
||||||
@ -84,6 +81,7 @@ func New(config ...Config) fiber.Handler {
|
|||||||
|
|
||||||
var once sync.Once
|
var once sync.Once
|
||||||
var prefix string
|
var prefix string
|
||||||
|
var cacheControlStr = "public, max-age=" + strconv.Itoa(cfg.MaxAge)
|
||||||
|
|
||||||
// Return new handler
|
// Return new handler
|
||||||
return func(c *fiber.Ctx) (err error) {
|
return func(c *fiber.Ctx) (err error) {
|
||||||
@ -110,26 +108,12 @@ func New(config ...Config) fiber.Handler {
|
|||||||
path = "/" + path
|
path = "/" + path
|
||||||
}
|
}
|
||||||
|
|
||||||
return SendFile(c, path)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// SendFile ...
|
|
||||||
func SendFile(c *fiber.Ctx, param string) (err error) {
|
|
||||||
var (
|
var (
|
||||||
file http.File
|
file http.File
|
||||||
stat os.FileInfo
|
stat os.FileInfo
|
||||||
)
|
)
|
||||||
method := c.Method()
|
|
||||||
|
|
||||||
// We only serve static assets on GET or HEAD methods
|
file, err = cfg.Root.Open(path)
|
||||||
if method != fiber.MethodGet && method != fiber.MethodHead {
|
|
||||||
return c.Next()
|
|
||||||
}
|
|
||||||
|
|
||||||
cacheControlStr := "public, max-age=" + strconv.Itoa(cfg.MaxAge)
|
|
||||||
|
|
||||||
file, err = cfg.Root.Open(param)
|
|
||||||
if err != nil && os.IsNotExist(err) && cfg.NotFoundFile != "" {
|
if err != nil && os.IsNotExist(err) && cfg.NotFoundFile != "" {
|
||||||
file, err = cfg.Root.Open(cfg.NotFoundFile)
|
file, err = cfg.Root.Open(cfg.NotFoundFile)
|
||||||
}
|
}
|
||||||
@ -147,7 +131,7 @@ func SendFile(c *fiber.Ctx, param string) (err error) {
|
|||||||
|
|
||||||
// Serve index if path is directory
|
// Serve index if path is directory
|
||||||
if stat.IsDir() {
|
if stat.IsDir() {
|
||||||
indexPath := strings.TrimSuffix(param, "/") + cfg.Index
|
indexPath := strings.TrimSuffix(path, "/") + cfg.Index
|
||||||
index, err := cfg.Root.Open(indexPath)
|
index, err := cfg.Root.Open(indexPath)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
indexStat, err := index.Stat()
|
indexStat, err := index.Stat()
|
||||||
@ -197,3 +181,71 @@ func SendFile(c *fiber.Ctx, param string) (err error) {
|
|||||||
|
|
||||||
return c.Next()
|
return c.Next()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// SendFile ...
|
||||||
|
func SendFile(c *fiber.Ctx, fs http.FileSystem, path string) (err error) {
|
||||||
|
var (
|
||||||
|
file http.File
|
||||||
|
stat os.FileInfo
|
||||||
|
)
|
||||||
|
|
||||||
|
file, err = fs.Open(path)
|
||||||
|
if err != nil {
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
return fiber.ErrNotFound
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if stat, err = file.Stat(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Serve index if path is directory
|
||||||
|
if stat.IsDir() {
|
||||||
|
indexPath := strings.TrimSuffix(path, "/") + ConfigDefault.Index
|
||||||
|
index, err := fs.Open(indexPath)
|
||||||
|
if err == nil {
|
||||||
|
indexStat, err := index.Stat()
|
||||||
|
if err == nil {
|
||||||
|
file = index
|
||||||
|
stat = indexStat
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return forbidden if no index found
|
||||||
|
if stat.IsDir() {
|
||||||
|
return fiber.ErrForbidden
|
||||||
|
}
|
||||||
|
|
||||||
|
modTime := stat.ModTime()
|
||||||
|
contentLength := int(stat.Size())
|
||||||
|
|
||||||
|
// Set Content Type header
|
||||||
|
c.Type(getFileExtension(stat.Name()))
|
||||||
|
|
||||||
|
// Set Last Modified header
|
||||||
|
if !modTime.IsZero() {
|
||||||
|
c.Set(fiber.HeaderLastModified, modTime.UTC().Format(http.TimeFormat))
|
||||||
|
}
|
||||||
|
|
||||||
|
method := c.Method()
|
||||||
|
if method == fiber.MethodGet {
|
||||||
|
c.Response().SetBodyStream(file, contentLength)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if method == fiber.MethodHead {
|
||||||
|
c.Request().ResetBody()
|
||||||
|
// Fasthttp should skipbody by default if HEAD?
|
||||||
|
c.Response().SkipBody = true
|
||||||
|
c.Response().Header.SetContentLength(contentLength)
|
||||||
|
if err := file.Close(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user