Skip to content

Commit

Permalink
Split attributes definition file
Browse files Browse the repository at this point in the history
  • Loading branch information
To-om committed Jun 29, 2017
1 parent 7f71b5e commit 5fccc8e
Show file tree
Hide file tree
Showing 21 changed files with 704 additions and 529 deletions.
59 changes: 59 additions & 0 deletions app/org/elastic4play/models/AttachmentAttributeFormat.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.elastic4play.models

import com.sksamuel.elastic4s.ElasticDsl.field
import com.sksamuel.elastic4s.mappings.FieldType.{ LongType, NestedType, StringType }
import com.sksamuel.elastic4s.mappings.NestedFieldDefinition
import org.elastic4play.controllers.{ AttachmentInputValue, FileInputValue, InputValue, JsonInputValue }
import org.elastic4play.services.Attachment
import org.elastic4play.services.JsonFormat.attachmentFormat
import org.elastic4play.controllers.JsonFormat._
import org.elastic4play.{ AttributeError, InvalidFormatAttributeError }
import org.scalactic._
import play.api.Logger
import play.api.libs.json.{ JsValue, Json }

object AttachmentAttributeFormat extends AttributeFormat[Attachment]("attachment") {
private[AttachmentAttributeFormat] lazy val logger = Logger(getClass)

override def checkJson(subNames: Seq[String], value: JsValue): Or[JsValue, One[InvalidFormatAttributeError]] = {
lazy val validJson = fileInputValueFormat.reads(value).asOpt orElse jsFormat.reads(value).asOpt
val result = if (subNames.isEmpty && validJson.isDefined)
Good(value)
else
formatError(JsonInputValue(value))
logger.debug(s"checkJson($subNames, $value) => $result")
result
}

val forbiddenChar = Seq('/', '\n', '\r', '\t', '\u0000', '\f', '`', '?', '*', '\\', '<', '>', '|', '\"', ':', ';')

override def inputValueToJson(subNames: Seq[String], value: InputValue): JsValue Or Every[AttributeError] = {
val result = if (subNames.nonEmpty)
formatError(value)
else
value match {
case fiv: FileInputValue if fiv.name.intersect(forbiddenChar).isEmpty Good(Json.toJson(fiv)(fileInputValueFormat))
case aiv: AttachmentInputValue Good(Json.toJson(aiv.toAttachment)(jsFormat))
case JsonInputValue(json) if attachmentInputValueReads.reads(json).isSuccess Good(json)
case _ formatError(value)
}
logger.debug(s"inputValueToJson($subNames, $value) => $result")
result
}

override def fromInputValue(subNames: Seq[String], value: InputValue): Attachment Or Every[AttributeError] = {
val result = value match {
case JsonInputValue(json) if subNames.isEmpty attachmentInputValueReads.reads(json).map(aiv Good(aiv.toAttachment)).getOrElse(formatError(value))
case _ formatError(value)
}
logger.debug(s"fromInputValue($subNames, $value) => $result")
result
}

override def elasticType(attributeName: String): NestedFieldDefinition = field(attributeName, NestedType) as (
field("name", StringType) index "not_analyzed",
field("hashes", StringType) index "not_analyzed",
field("size", LongType),
field("contentType", StringType),
field("id", StringType))
}
Loading

0 comments on commit 5fccc8e

Please sign in to comment.