Skip to content

Commit

Permalink
Apply correct mime-type on already gzipped files
Browse files Browse the repository at this point in the history
  • Loading branch information
laurilehmijoki committed Aug 4, 2016
1 parent e3b2beb commit 143396e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

This project uses [Semantic Versioning](http://semver.org).

## Next

* Apply correct mime type on already-gzipped files

See <https://github.com/laurilehmijoki/s3_website/issues/229#issuecomment-237229421> for discussion

## 2.14.1

* Do not gzip a file that is already gzipped
Expand Down
13 changes: 12 additions & 1 deletion src/main/scala/s3/website/model/push.scala
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,18 @@ case class Upload(originalFile: File, uploadType: UploadType)(implicit site: Sit
lazy val gzipEnabledByConfig: Boolean = encodingOnS3.fold(false)((algorithm: Either[Gzip, Zopfli]) => true)

lazy val contentType: Try[String] = tika map { tika =>
val mimeType = tika.detect(originalFile)
val file = // This file contains the data that the user should see after decoding the the transport protocol (HTTP) encoding (practically: after ungzipping)
if (fileIsGzippedByExternalBuildTool) {
val unzippedFile = createTempFile(originalFile.getName, "unzipped")
unzippedFile.deleteOnExit()
using(new GZIPInputStream(fis(originalFile))) { stream =>
IOUtils.copy(stream, new FileOutputStream(unzippedFile))
}
unzippedFile
} else {
originalFile
}
val mimeType = tika.detect(file)
if (mimeType.startsWith("text/") || mimeType == "application/json")
mimeType + "; charset=utf-8"
else
Expand Down
9 changes: 9 additions & 0 deletions src/test/scala/s3/website/S3WebsiteSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ class S3WebsiteSpec extends Specification {
unzippedString must equalTo(htmlString)
}

"apply the correct mime type on an already gzipped file" in new BasicSetup {
val htmlString = "<html><body><h1>Hello world</h1></body></html>"
val gzippedHtml = gzip(htmlString.getBytes(StandardCharsets.UTF_8))
config = "gzip: true"
setLocalFileWithContent("index.html", gzippedHtml)
push()
sentPutObjectRequest.getMetadata.getContentType must equalTo("text/html; charset=utf-8")
}

"not gzip the file if it's already gzipped" in new BasicSetup {
config = "gzip: true"

Expand Down

0 comments on commit 143396e

Please sign in to comment.