Skip to content

Commit

Permalink
Make configured filemetadata fields visible on UI
Browse files Browse the repository at this point in the history
  • Loading branch information
Asmau1234 committed Feb 23, 2021
1 parent 8b81fb9 commit f08b18a
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package com.gu.mediaservice.lib.config

import java.util.UUID

import com.gu.mediaservice.lib.aws.{AwsClientBuilderUtils, KinesisSenderConfig}
import com.typesafe.config.ConfigException
import com.typesafe.config.{ConfigException, ConfigObject}
import com.typesafe.scalalogging.StrictLogging
import play.api.Configuration

import java.util.UUID
import scala.util.Try


abstract class CommonConfig(val configuration: Configuration) extends AwsClientBuilderUtils with StrictLogging {
final val elasticsearchStack = "media-service"

Expand Down Expand Up @@ -94,6 +92,8 @@ abstract class CommonConfig(val configuration: Configuration) extends AwsClientB
final def boolean(key: String): Boolean =
configuration.getOptional[Boolean](key).getOrElse(false)

final def configObjectOpt(key : String) : Option[ConfigObject] = configuration.getOptional[ConfigObject](key)

private def missing(key: String, type_ : String): Nothing =
sys.error(s"Required $type_ configuration property missing: $key")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.gu.mediaservice.lib.config

import play.api.libs.functional.syntax._

case class FileMetadataConfig(
directory: String,
tag: String,
visible: Boolean = false,
searchable: Boolean = false,
alias: Option[String])

object FileMetadataConfig {
import play.api.libs.json._
implicit val MetadataConfigurationWrites: Writes[FileMetadataConfig] = (
(__ \ "directory").write[String] ~
(__ \ "tag").write[String] ~
(__ \ "visible").write[Boolean] ~
(__ \ "searchable").write[Boolean] ~
(__ \ "alias").writeNullable[String]
)(unlift(FileMetadataConfig.unapply))
}
7 changes: 5 additions & 2 deletions kahuna/app/controllers/KahunaController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import com.gu.mediaservice.lib.argo.ArgoHelpers
import com.gu.mediaservice.lib.auth.Authentication
import lib.KahunaConfig
import play.api.mvc.{BaseController, ControllerComponents}

import play.api.libs.json._
import scala.concurrent.ExecutionContext
import com.gu.mediaservice.lib.config.FileMetadataConfig._

class KahunaController(auth: Authentication, config: KahunaConfig, override val controllerComponents: ControllerComponents)
(implicit val ec: ExecutionContext) extends BaseController with ArgoHelpers {
Expand All @@ -14,6 +15,7 @@ class KahunaController(auth: Authentication, config: KahunaConfig, override val
val okPath = routes.KahunaController.ok.url
// If the auth is successful, we redirect to the kahuna domain so the iframe
// is on the same domain and can be read by the JS
val fileMetadataConfigs: String = Json.toJson(config.fileMetadataConfigs).toString()
val returnUri = config.rootUri + okPath
Ok(views.html.main(
config.mediaApiUri,
Expand All @@ -25,7 +27,8 @@ class KahunaController(auth: Authentication, config: KahunaConfig, override val
config.feedbackFormLink,
config.usageRightsHelpLink,
config.invalidSessionHelpLink,
config.supportEmail
config.supportEmail,
fileMetadataConfigs
))
}

Expand Down
22 changes: 21 additions & 1 deletion kahuna/app/lib/KahunaConfig.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package lib

import com.gu.mediaservice.lib.config.{CommonConfig, GridConfigResources}
import com.gu.mediaservice.lib.config.{CommonConfig, FileMetadataConfig, GridConfigResources}

import scala.collection.JavaConverters._

class KahunaConfig(resources: GridConfigResources) extends CommonConfig(resources.configuration) {
val rootUri: String = services.kahunaBaseUri
Expand All @@ -21,4 +23,22 @@ class KahunaConfig(resources: GridConfigResources) extends CommonConfig(resource
val supportEmail: Option[String]= stringOpt("links.supportEmail").filterNot(_.isEmpty)

val frameAncestors: Set[String] = getStringSet("security.frameAncestors")
val fileMetadataConfigs: List[FileMetadataConfig] = configObjectOpt("filemetadata.configurations") match {
case Some(config) => config.unwrapped()
.asInstanceOf[java.util.Map[String, java.util.HashMap[String, java.util.HashMap[String, Object]]]]
.asScala
.flatMap {
case (directory, directoryConfigs) if directory.nonEmpty =>
directoryConfigs.asScala.map {
case(_, directoryConfig) =>
FileMetadataConfig(
directory,
tag = directoryConfig.get("tag").asInstanceOf[String],
visible = directoryConfig.get("visible").asInstanceOf[Boolean],
searchable = directoryConfig.get("searchable").asInstanceOf[Boolean],
alias = Some(directoryConfig.getOrDefault("alias", directoryConfig.get("tag")).asInstanceOf[String]))
}
}.toList
case None => List.empty
}
}
6 changes: 4 additions & 2 deletions kahuna/app/views/main.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
feedbackFormLink: Option[String],
usageRightsHelpLink: Option[String],
invalidSessionHelpLink: Option[String],
supportEmail: Option[String])
supportEmail: Option[String],
fileMetadataConfigs: String)
<!DOCTYPE html>
<html>
<head>
Expand Down Expand Up @@ -43,7 +44,8 @@
feedbackFormLink: "@Html(feedbackFormLink.getOrElse(""))",
usageRightsHelpLink: "@Html(usageRightsHelpLink.getOrElse(""))",
invalidSessionHelpLink: "@Html(invalidSessionHelpLink.getOrElse(""))",
supportEmail: "@Html(supportEmail.getOrElse(""))"
supportEmail: "@Html(supportEmail.getOrElse(""))",
fileMetadataConfig: @Html(fileMetadataConfigs)
}
</script>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,14 +272,22 @@
ng-click="metadataExpanded = !metadataExpanded">
<span ng-hide="metadataExpanded">▸ Show</span>
<span ng-show="metadataExpanded">▾ Hide</span>
full metadata
image metadata
</button>

<div ng-if="metadataExpanded" class="metadata metadata-line image-info__group--dl">
<dl ng-repeat="(key, value) in ctrl.metadata" ng-if="ctrl.isUsefulMetadata(key)" class="metadata__body image-info__group--dl">
<dt class="metadata-line__key image-info__group--dl__key--full-metadata">{{key | spaceWords}}</dt>
<dd class="metadata-line__info image-info__group--dl__value--full-metadata">{{value}}</dd>
</dl>

<br>

<dl ng-repeat="(key, value) in ctrl.fileMetadata" class="metadata__body image-info__group--dl">
<dt class="metadata-line__key image-info__group--dl__key--full-metadata">{{key | spaceWords}}</dt>
<dd class="metadata-line__info image-info__group--dl__value--full-metadata">{{value}}</dd>
</dl>

<dl ng-repeat="(key, value) in ctrl.identifiers" class="metadata__body image-info__group--dl">
<dt class="metadata-line__key image-info__group--dl__key--full-metadata">{{key | spaceWords}}</dt>
<dd class="metadata-line__info image-info__group--dl__value--full-metadata">{{value}}</dd>
Expand Down
22 changes: 22 additions & 0 deletions kahuna/public/js/components/gr-image-metadata/gr-image-metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,28 @@ module.controller('grImageMetadataCtrl', [
ctrl.metadata = ctrl.image.data.metadata;
ctrl.identifiers = ctrl.image.data.identifiers;

mediaApi.fileMetadata(ctrl.image).then(resource => {
return resource.data;
}).then(fileMetadata => {
const groupedConfigsByDirectory = window._clientConfig.fileMetadataConfig.reduce((r, a) => {
r[a.directory] = [...r[a.directory] || [], a];
return r;
}, {});

let object = {};

Object.keys(groupedConfigsByDirectory).forEach(directory => {
groupedConfigsByDirectory[directory]
.filter(config => config.visible)
.filter(fileMetadataConfig => fileMetadata[directory][fileMetadataConfig.tag] !== undefined)
.map(fileMetadataConfig => {
object[fileMetadataConfig.alias] = fileMetadata[directory][fileMetadataConfig.tag];
});
});

ctrl.fileMetadata = object;
});

ctrl.isUsefulMetadata = isUsefulMetadata;

ctrl.hasLocationInformation = ctrl.metadata.subLocation ||
Expand Down
5 changes: 5 additions & 0 deletions kahuna/public/js/services/api/media-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ mediaApi.factory('mediaApi',
return root.follow('image', {id: id}).get();
}

function fileMetadata(image) {
return image.follow('fileMetadata', {}).get();
}

function getSession() {
// TODO: workout how we might be able to memoize this function but still
// play nice with changes that might occur in the API (cache-header?).
Expand All @@ -92,6 +96,7 @@ mediaApi.factory('mediaApi',
root,
search,
find,
fileMetadata,
getSession,
metadataSearch,
labelSearch,
Expand Down

0 comments on commit f08b18a

Please sign in to comment.