Permalink
Browse files

Specify charset=utf-8 for all text docs

  • Loading branch information...
Lauri Lehmijoki
Lauri Lehmijoki committed May 8, 2014
1 parent fb538e4 commit 42114bab83ebeb2a9007af7c4c3960c61eb5e40d
Showing with 29 additions and 1 deletion.
  1. +9 −1 src/main/scala/s3/website/model/push.scala
  2. +20 −0 src/test/scala/s3/website/S3WebsiteSpec.scala
@@ -96,7 +96,7 @@ object LocalFile {
contentEncoding = localFile.encodingOnS3.map(_ => "gzip"),
contentLength = sourceFile.length(),
maxAge = maxAge,
contentType = tika.detect(localFile.sourceFile),
contentType = resolveContentType(localFile.sourceFile),
openInputStream = () => new FileInputStream(sourceFile)
)
)
@@ -108,6 +108,14 @@ object LocalFile {
lazy val tika = new Tika()
def resolveContentType(file: File) = {
val mimeType = tika.detect(file)
if (mimeType.startsWith("text/") || mimeType == "application/json")
mimeType + "; charset=utf-8"
else
mimeType
}
def resolveLocalFiles(implicit site: Site): Either[ErrorReport, Seq[LocalFile]] = Try {
val files = recursiveListFiles(new File(site.rootDirectory)).filterNot(_.isDirectory)
files map { file =>
@@ -379,6 +379,26 @@ class S3WebsiteSpec extends Specification {
sentPutObjectRequest.getKey must equalTo(".vimrc")
}
}
"content type inference" should {
"add charset=utf-8 to all html documents" in new SiteDirectory with MockAWS {
implicit val site = siteWithFiles(localFiles = "file.html" :: Nil)
Push.pushSite
sentPutObjectRequest.getMetadata.getContentType must equalTo("text/html; charset=utf-8")
}
"add charset=utf-8 to all text documents" in new SiteDirectory with MockAWS {
implicit val site = siteWithFiles(localFiles = "file.txt" :: Nil)
Push.pushSite
sentPutObjectRequest.getMetadata.getContentType must equalTo("text/plain; charset=utf-8")
}
"add charset=utf-8 to all json documents" in new SiteDirectory with MockAWS {
implicit val site = siteWithFiles(localFiles = "file.json" :: Nil)
Push.pushSite
sentPutObjectRequest.getMetadata.getContentType must equalTo("application/json; charset=utf-8")
}
}
trait MockAWS extends MockS3 with MockCloudFront with Scope

0 comments on commit 42114ba

Please sign in to comment.