File tree Expand file tree Collapse file tree 2 files changed +29
-1
lines changed
main/scala/s3/website/model Expand file tree Collapse file tree 2 files changed +29
-1
lines changed Original file line number Diff line number Diff line change @@ -96,7 +96,7 @@ object LocalFile {
96
96
contentEncoding = localFile.encodingOnS3.map(_ => " gzip" ),
97
97
contentLength = sourceFile.length(),
98
98
maxAge = maxAge,
99
- contentType = tika.detect (localFile.sourceFile),
99
+ contentType = resolveContentType (localFile.sourceFile),
100
100
openInputStream = () => new FileInputStream (sourceFile)
101
101
)
102
102
)
@@ -108,6 +108,14 @@ object LocalFile {
108
108
109
109
lazy val tika = new Tika ()
110
110
111
+ def resolveContentType (file : File ) = {
112
+ val mimeType = tika.detect(file)
113
+ if (mimeType.startsWith(" text/" ) || mimeType == " application/json" )
114
+ mimeType + " ; charset=utf-8"
115
+ else
116
+ mimeType
117
+ }
118
+
111
119
def resolveLocalFiles (implicit site : Site ): Either [ErrorReport , Seq [LocalFile ]] = Try {
112
120
val files = recursiveListFiles(new File (site.rootDirectory)).filterNot(_.isDirectory)
113
121
files map { file =>
Original file line number Diff line number Diff line change @@ -379,6 +379,26 @@ class S3WebsiteSpec extends Specification {
379
379
sentPutObjectRequest.getKey must equalTo(" .vimrc" )
380
380
}
381
381
}
382
+
383
+ " content type inference" should {
384
+ " add charset=utf-8 to all html documents" in new SiteDirectory with MockAWS {
385
+ implicit val site = siteWithFiles(localFiles = " file.html" :: Nil )
386
+ Push .pushSite
387
+ sentPutObjectRequest.getMetadata.getContentType must equalTo(" text/html; charset=utf-8" )
388
+ }
389
+
390
+ " add charset=utf-8 to all text documents" in new SiteDirectory with MockAWS {
391
+ implicit val site = siteWithFiles(localFiles = " file.txt" :: Nil )
392
+ Push .pushSite
393
+ sentPutObjectRequest.getMetadata.getContentType must equalTo(" text/plain; charset=utf-8" )
394
+ }
395
+
396
+ " add charset=utf-8 to all json documents" in new SiteDirectory with MockAWS {
397
+ implicit val site = siteWithFiles(localFiles = " file.json" :: Nil )
398
+ Push .pushSite
399
+ sentPutObjectRequest.getMetadata.getContentType must equalTo(" application/json; charset=utf-8" )
400
+ }
401
+ }
382
402
383
403
trait MockAWS extends MockS3 with MockCloudFront with Scope
384
404
You can’t perform that action at this time.
0 commit comments