-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Custom binary files are not compressed when fetched by the API (#4751)
* Custom binary files are not compressed when fetched by the API --------- Co-authored-by: Simon Dumas <[email protected]>
- Loading branch information
Showing
14 changed files
with
106 additions
and
30 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
.../src/main/scala/ch/epfl/bluebrain/nexus/delta/kernel/instances/ContentTypeInstances.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package ch.epfl.bluebrain.nexus.delta.kernel.instances | ||
|
||
import akka.http.scaladsl.model.MediaType.NotCompressible | ||
import akka.http.scaladsl.model.{ContentType, MediaType, MediaTypes} | ||
import cats.syntax.all._ | ||
import io.circe.{Decoder, Encoder} | ||
|
||
import java.util.Locale | ||
|
||
trait ContentTypeInstances { | ||
implicit val contentTypeEncoder: Encoder[ContentType] = | ||
Encoder.encodeString.contramap(_.value) | ||
|
||
implicit val contentTypeDecoder: Decoder[ContentType] = | ||
Decoder.decodeString.emap( | ||
ContentType | ||
.parse(_) | ||
.bimap( | ||
_.mkString("\n"), | ||
markBinaryAsNonCompressible | ||
) | ||
) | ||
|
||
/** | ||
* When parsing a custom binary media type, akka assumes that it is compressible which is traduced by a performance | ||
* hit when we compress responses so we revert this | ||
*/ | ||
private def markBinaryAsNonCompressible(contentType: ContentType) = | ||
contentType match { | ||
case b: ContentType.Binary if isCustomMediaType(b.mediaType) => | ||
ContentType.Binary(b.mediaType.withComp(NotCompressible)) | ||
case other => other | ||
} | ||
|
||
private def isCustomMediaType(mediaType: MediaType) = | ||
MediaTypes | ||
.getForKey(mediaType.mainType.toLowerCase(Locale.ROOT) -> mediaType.subType.toLowerCase(Locale.ROOT)) | ||
.isEmpty | ||
} |
2 changes: 1 addition & 1 deletion
2
...a/plugins/storage/instances/package.scala → ...us/delta/kernel/instances/instances.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
package ch.epfl.bluebrain.nexus.delta.plugins.storage | ||
package ch.epfl.bluebrain.nexus.delta.kernel | ||
|
||
package object instances extends ContentTypeInstances |
32 changes: 32 additions & 0 deletions
32
...test/scala/ch/epfl/bluebrain/nexus/delta/kernel/instances/ContentTypeInstancesSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package ch.epfl.bluebrain.nexus.delta.kernel.instances | ||
|
||
import io.circe.Json | ||
import munit.{FunSuite, Location} | ||
|
||
class ContentTypeInstancesSuite extends FunSuite with ContentTypeInstances { | ||
|
||
def assertContentType(value: String, binary: Boolean, compressible: Boolean)(implicit l: Location) = | ||
contentTypeDecoder.decodeJson(Json.fromString(value)) match { | ||
case Left(err) => fail(s"Could not decode $value: ${err.message}") | ||
case Right(contentType) => | ||
assertEquals(contentType.binary, binary) | ||
assertEquals(contentType.mediaType.comp.compressible, compressible) | ||
} | ||
|
||
test("'application/object-stream' is binary and not compressible") { | ||
assertContentType("application/octet-stream", true, false) | ||
} | ||
|
||
test("'application/json' is not binary and compressible") { | ||
assertContentType("application/json", false, true) | ||
} | ||
|
||
test("'application/cbor' is binary and compressible as it is registered as such in akka") { | ||
assertContentType("application/cbor", true, true) | ||
} | ||
|
||
test("'application/nrrd' is binary and not compressible as it is a custom type") { | ||
assertContentType("application/nrrd", true, false) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 0 additions & 13 deletions
13
.../scala/ch/epfl/bluebrain/nexus/delta/plugins/storage/instances/ContentTypeInstances.scala
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters