-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
704 additions
and
529 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
app/org/elastic4play/models/AttachmentAttributeFormat.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,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)) | ||
} |
Oops, something went wrong.