Skip to content

Commit

Permalink
Add new Asset.orientation field is an optional top level portrait / l…
Browse files Browse the repository at this point in the history
…andscape orientation.
  • Loading branch information
tonytw1 committed Oct 12, 2024
1 parent 53186bf commit 1425a94
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ object MappingTest {
secureUrl = Some(new URL("http://host/filename.jpg")),
orientationMetadata = Some(OrientationMetadata(exifOrientation = Some(6))),
orientedDimensions = Some(Dimensions(2000, 1000)),
orientation = Some("landscape")
)

val testUploader = "grid-internal-mapping-test-image" // Do not change this, we use it to clean up old test images
Expand All @@ -81,7 +82,9 @@ object MappingTest {
dimensions = Some(Dimensions(500, 1000)),
secureUrl = Some(new URL("http://host/thumb.jpg")),
orientationMetadata = Some(OrientationMetadata(exifOrientation = Some(6))),
orientedDimensions = Some(Dimensions(2000, 1000))
orientedDimensions = Some(Dimensions(2000, 1000)),
orientation = Some("landscape")

)),
optimisedPng = Some(Asset(
file = new URI("file://filename.png"),
Expand All @@ -90,7 +93,8 @@ object MappingTest {
dimensions = Some(Dimensions(1000, 2000)),
secureUrl = Some(new URL("http://host/filename.jpg")),
orientationMetadata = Some(OrientationMetadata(exifOrientation = Some(6))),
orientedDimensions = Some(Dimensions(2000, 1000))
orientedDimensions = Some(Dimensions(2000, 1000)),
orientation = Some("landscape")
)),
fileMetadata = FileMetadata(
iptc = Map("iptc1" -> "value1"),
Expand Down
24 changes: 22 additions & 2 deletions common-lib/src/main/scala/com/gu/mediaservice/model/Asset.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.gu.mediaservice.lib.aws.S3Object
case class Asset(file: URI, size: Option[Long], mimeType: Option[MimeType], dimensions: Option[Dimensions], secureUrl: Option[URL] = None,
orientationMetadata: Option[OrientationMetadata] = None,
orientedDimensions: Option[Dimensions] = None,
orientation: Option[String] = None
)

object Asset {
Expand All @@ -26,6 +27,13 @@ object Asset {
orientationMetadata.correctedDimensions(dimensions)
}

val orientation = for {
dimensions <- dims
orientationMetadata <- orientationMetadata
} yield {
orientationMetadata.correctedOrientation(dimensions)
}

Asset(
file = s3Object.uri,
size = Some(s3Object.size),
Expand All @@ -34,6 +42,7 @@ object Asset {
secureUrl = None,
orientationMetadata = orientationMetadata,
orientedDimensions = orientedDimensions,
orientation = orientation
)
}

Expand All @@ -44,7 +53,8 @@ object Asset {
(__ \ "dimensions").readNullable[Dimensions] ~
(__ \ "secureUrl").readNullable[String].map(_.map(new URL(_))) ~
(__ \ "orientationMetadata").readNullable[OrientationMetadata] ~
(__ \ "orientedDimensions").readNullable[Dimensions]
(__ \ "orientedDimensions").readNullable[Dimensions] ~
(__ \ "orientation").readNullable[String]
)(Asset.apply _)

implicit val assetWrites: Writes[Asset] =
Expand All @@ -54,7 +64,8 @@ object Asset {
(__ \ "dimensions").writeNullable[Dimensions] ~
(__ \ "secureUrl").writeNullable[String].contramap((_: Option[URL]).map(_.toString)) ~
(__ \ "orientationMetadata").writeNullable[OrientationMetadata] ~
(__ \ "orientedDimensions").writeNullable[Dimensions]
(__ \ "orientedDimensions").writeNullable[Dimensions] ~
(__ \ "orientation").writeNullable[String]
)(unlift(Asset.unapply))
}

Expand Down Expand Up @@ -85,6 +96,15 @@ case class OrientationMetadata(exifOrientation: Option[Int]) {
}
}

def correctedOrientation(sourceDimensions: Dimensions): String = {
val orientedDimensions = correctedDimensions(sourceDimensions)
if (orientedDimensions.width < orientedDimensions.height) {
"portrait"
} else {
"landscape"
}
}

private def flipsDimensions(): Boolean = exifOrientation.exists(OrientationMetadata.exifOrientationsWhichFlipWidthAndHeight.contains)

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ trait MetadataHelper {
lastModified = None,
identifiers = Map(),
uploadInfo = UploadInfo(),
source = Asset(URI.create("http://example.com/image.jpg"), Some(0), None, None),
source = Asset(URI.create("http://example.com/image.jpg"), Some(0), None, None, None, None),
thumbnail = None,
optimisedPng = None,
fileMetadata = FileMetadata(),
Expand Down

0 comments on commit 1425a94

Please sign in to comment.