Skip to content

Commit

Permalink
Fix for check for content-type when gzip
Browse files Browse the repository at this point in the history
FIxes #21
  • Loading branch information
natrim authored and bep committed Apr 24, 2018
1 parent e9d1e7c commit cc7116a
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions lib/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (f *osFile) Headers() map[string]string {
return headers
}

func (f *osFile) initContentType() error {
func (f *osFile) initContentType(peek []byte) error {
if f.route != nil {
if contentType, found := f.route.Headers["Content-Type"]; found {
f.contentType = contentType
Expand All @@ -129,7 +129,11 @@ func (f *osFile) initContentType() error {
}

// Have to look inside the file itself.
f.contentType = detectContentTypeFromContent(f.f.Bytes())
if peek != nil {
f.contentType = detectContentTypeFromContent(peek)
} else {
f.contentType = detectContentTypeFromContent(f.f.Bytes())
}

return nil
}
Expand Down Expand Up @@ -172,6 +176,7 @@ func newOSFile(routes routes, targetRoot, relPath, absPath string, fi os.FileInf
var (
mFile *memfile.File
size = fi.Size()
peek []byte
)

route := routes.get(relPath)
Expand All @@ -183,6 +188,8 @@ func newOSFile(routes routes, targetRoot, relPath, absPath string, fi os.FileInf
gz.Close()
mFile = memfile.New(b.Bytes())
size = int64(b.Len())
peek = make([]byte, 512)
file.Read(peek)

This comment has been minimized.

Copy link
@earthboundkid

earthboundkid May 14, 2018

Contributor

I think you need to do n, err := file.Read(peek); peek = peek[:n] here.

} else {
b, err := ioutil.ReadAll(file)
if err != nil {
Expand All @@ -193,7 +200,7 @@ func newOSFile(routes routes, targetRoot, relPath, absPath string, fi os.FileInf

of := &osFile{route: route, f: mFile, targetRoot: targetRoot, absPath: absPath, relPath: relPath, size: size}

if err := of.initContentType(); err != nil {
if err := of.initContentType(peek); err != nil {
return nil, err
}

Expand Down

0 comments on commit cc7116a

Please sign in to comment.