I have an application that uses gridFS to store some images for things. Im using the gridFSBodyParser and controllers work. Now I need to seed some data from disk for a test for a particular edge case, for which I cannot use the controller because reasons. I have following code and relevant imports -
import java.nio.file.{Files, Paths}
import com.cookit.allergy.Allergy
import play.api.libs.iteratee.Enumerator
import play.modules.reactivemongo.MongoController.readFileReads
import play.modules.reactivemongo.ReactiveMongoApi
import reactivemongo.play.json._
def seedTestData(....) = {
for {
//create actual entity
obj <- createEntity(....)
//create avatar for entity
gridFS <- mongo.asyncGridFS.flatMap(gridFS => {
val url = this.getClass.getClassLoader.getResource("test.jpg")
gridFS.save(
Enumerator.enumerate(List(Files.readAllBytes(Paths.get(url.getPath)))),
gridFS.fileToSave(Some(name), Some("image/jpeg"), None, buildMD(obj)))
}).map(result => {
logger.info(.....)
}).recover({
case e: Exception =>
logger.error(....)
e.printStackTrace()
throw e
})
} yield {
....
}
}
My problem is with id generation. It complains that -
play.api.libs.json.JsResultException: JsResultException(errors:List((,List(JsonValidationError(List(List((,List(JsonValidationError(List(List((,List(JsonValidationError(List(List((,List(JsonValidationError(List(Wrong ObjectId (length != 24): '4c75f45d255045a2aac01fdbbe031c11'),WrappedArray()))))),WrappedArray()))))),WrappedArray()))))),WrappedArray())))))
I suspect it has something to do with the JSON serialization, but I cannot figure out exactly where the problem is.
基本上,我最终传递了一个自定义FileToSave[JSONSerializationPack.type, JsValue]
实现而不是gridFS.fileToSave(....)
并覆盖了包、文件名、内容类型、上传日期和元数据字段。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.