Skip to content
This repository has been archived by the owner on Sep 17, 2020. It is now read-only.

Set MIME types using Fiber's built-in method #3

Merged
merged 2 commits into from
Apr 25, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 2 additions & 38 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@
package embed

import (
"io"
"io/ioutil"
"log"
"mime"
"net/http"
"os"
"path/filepath"
"strings"

Expand Down Expand Up @@ -79,13 +75,9 @@ func New(config ...Config) func(*fiber.Ctx) {
}
contentLength := int(stat.Size())

contentType, err := detectContentType(file, stat)
if err != nil {
cfg.ErrorHandler(c, err)
return
}
// Set Content Type header
c.Type(getFileExtension(stat.Name()))

c.Fasthttp.SetContentType(contentType)
if c.Method() == fiber.MethodGet {
c.Fasthttp.SetBodyStream(file, contentLength)
return
Expand All @@ -104,20 +96,6 @@ func New(config ...Config) func(*fiber.Ctx) {
}
}

func detectContentType(f http.File, fileInfo os.FileInfo) (string, error) {
ext := getFileExtension(fileInfo.Name())
contentType := mime.TypeByExtension(ext)
if len(contentType) == 0 {
data, err := readFileHeader(f)
if err != nil {
return "", err
}
contentType = http.DetectContentType(data)
}

return contentType, nil
}

func getFileExtension(path string) string {
n := strings.LastIndexByte(path, '.')
if n < 0 {
Expand All @@ -126,17 +104,3 @@ func getFileExtension(path string) string {

return path[n:]
}

func readFileHeader(f http.File) ([]byte, error) {
r := io.Reader(f)
lr := &io.LimitedReader{
R: r,
N: 512,
}
data, err := ioutil.ReadAll(lr)
if _, err := f.Seek(0, 0); err != nil {
return nil, err
}

return data, err
}