Skip to content

Commit

Permalink
Moves determining content types into theme_client
Browse files Browse the repository at this point in the history
This ensures that users of phoenix.LoadAsset will have the data
correctly loaded based on content type. Binary data will be set as
the attachment, compared to the value which was done previously.

Though, theme-watch pretty much has the same implementation of LoadAsset
which should be resolved in order to reduce unnecessary duplication.

fixes #11
  • Loading branch information
Chris Saunders committed Jan 1, 2015
1 parent 39cb81a commit 42fbca4
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
15 changes: 14 additions & 1 deletion load_asset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (s *LoadAssetSuite) TestWhenAFileIsEmpty() {
assert.False(s.T(), asset.IsValid(), "The returned asset should not be considered valid")
}

func (s *LoadAssetSuite) TestWhenAFileContainsData() {
func (s *LoadAssetSuite) TestWhenAFileContainsTextData() {
root, filename, err := s.allocateFile("hello world")
if err != nil {
log.Fatal(err)
Expand All @@ -48,6 +48,19 @@ func (s *LoadAssetSuite) TestWhenAFileContainsData() {
asset, err := LoadAsset(root, filename)
assert.Nil(s.T(), err, "There should not be an error returned")
assert.True(s.T(), asset.IsValid(), "Files that contain data should be valid")
assert.Equal(s.T(), "hello world", asset.Value)
}

func (s *LoadAssetSuite) TestWhenAFileContainsBinaryData() {
root, filename, err := s.allocateFile(string(BinaryTestData()))
if err != nil {
log.Fatal(err)
}

asset, err := LoadAsset(root, filename)
assert.Nil(s.T(), err, "There should not be an error returned")
assert.True(s.T(), asset.IsValid(), "Files that contain data should be valid")
assert.True(s.T(), len(asset.Attachment) > 0, "The attachment should not be blank")
}

func (s *LoadAssetSuite) TestWhenFileIsADirectory() {
Expand Down
22 changes: 21 additions & 1 deletion theme_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package phoenix

import (
"crypto/tls"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -50,10 +51,29 @@ func LoadAsset(root, filename string) (asset Asset, err error) {
if err != nil {
return
}
asset = Asset{Value: string(buffer), Key: filename}

asset = Asset{Key: filename}
if contentTypeFor(buffer) == "text" {
asset.Value = string(buffer)
} else {
asset.Attachment = encode64(buffer)
}
return
}

func contentTypeFor(data []byte) string {
contentType := http.DetectContentType(data)
if strings.Contains(contentType, "text") {
return "text"
} else {
return "binary"
}
}

func encode64(data []byte) string {
return base64.StdEncoding.EncodeToString(data)
}

type EventType int

func (e EventType) String() string {
Expand Down
10 changes: 10 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package phoenix

import (
"bytes"
"fmt"
"image"
"image/png"
"io/ioutil"
"log"
"os"
Expand Down Expand Up @@ -32,3 +35,10 @@ func TestFixture(name string) string {
}
return string(bytes)
}

func BinaryTestData() []byte {
img := image.NewRGBA(image.Rect(0, 0, 10, 10))
buff := bytes.NewBuffer([]byte{})
png.Encode(buff, img)
return buff.Bytes()
}

0 comments on commit 42fbca4

Please sign in to comment.