Skip to content

Commit

Permalink
Don't gzip videos
Browse files Browse the repository at this point in the history
  • Loading branch information
zackbloom committed May 15, 2015
1 parent 2634d82 commit 2d760d6
Showing 1 changed file with 33 additions and 6 deletions.
39 changes: 33 additions & 6 deletions src/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ const (

const UPLOAD_WORKERS = 20

var NO_GZIP = []string{
"mp4",
"webm",
"ogg",
}

func hashFile(path string) []byte {
hash := md5.New()
io.WriteString(hash, path)
Expand Down Expand Up @@ -81,20 +87,41 @@ func guessContentType(file string) string {
return mime.TypeByExtension(filepath.Ext(file))
}

func shouldCompress(file string) bool {
ext := filepath.Ext(file)
for _, e := range NO_GZIP {
if "."+e == ext {
return false
}
}

return true
}

func uploadFile(bucket *s3.Bucket, reader io.Reader, dest string, includeHash bool, caching int) string {
buffer := bytes.NewBuffer([]byte{})
writer := gzip.NewWriter(buffer)
must(io.Copy(writer, reader))
writer.Close()

compress := shouldCompress(dest)

if compress {
writer := gzip.NewWriter(buffer)
must(io.Copy(writer, reader))
writer.Close()
} else {
must(io.Copy(buffer, reader))
}

data := buffer.Bytes()

hash := hashBytes(data)
hashPrefix := fmt.Sprintf("%x", hash)[:12]
s3Opts := s3.Options{
ContentMD5: base64.StdEncoding.EncodeToString(hash),
ContentEncoding: "gzip",
CacheControl: fmt.Sprintf("public, max-age=%d", caching),
ContentMD5: base64.StdEncoding.EncodeToString(hash),
CacheControl: fmt.Sprintf("public, max-age=%d", caching),
}

if compress {
s3Opts.ContentEncoding = "gzip"
}

if includeHash {
Expand Down

0 comments on commit 2d760d6

Please sign in to comment.