Skip to content

Commit 282374d

Browse files
earthboundkidbep
authored andcommitted
Content Type: Detect by looking at file contents
1 parent 1e75176 commit 282374d

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

lib/deployer.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package lib
88

99
import (
10+
"bufio"
1011
"bytes"
1112
"compress/gzip"
1213
"crypto/md5"
@@ -16,7 +17,7 @@ import (
1617
"io"
1718
"io/ioutil"
1819
"log"
19-
"mime"
20+
"net/http"
2021
"os"
2122
"path"
2223
"path/filepath"
@@ -412,11 +413,15 @@ func (d *Deployer) upload(source file, destBucket *s3.Bucket) error {
412413
}
413414
defer f.Close()
414415

415-
contentType := mime.TypeByExtension(filepath.Ext(source.path))
416-
if contentType == "" {
417-
contentType = "application/octet-stream"
416+
br := bufio.NewReader(f)
417+
const magicSize = 512 // Size that DetectContentType expects
418+
peek, err := br.Peek(magicSize)
419+
if err != nil {
420+
return err
418421
}
419422

423+
contentType := http.DetectContentType(peek)
424+
420425
headers := map[string][]string{
421426
"Content-Type": {contentType},
422427
}
@@ -428,7 +433,7 @@ func (d *Deployer) upload(source file, destBucket *s3.Bucket) error {
428433
}
429434

430435
var (
431-
r io.Reader = f
436+
r io.Reader = br
432437
size = source.size
433438
)
434439

0 commit comments

Comments
 (0)