稍后在Scala中定义val

稍后在Scala中定义val,scala,scope,Scala,Scope,我使用此代码将表单上传的图像保存到DB: def saveImage(userIdSource: String) = Action.async(gridFSBodyParser(gridFS)) { request => // here is the future file! val futureFile = request.body.files.head.ref // when the upload is complete, we add the articl

我使用此代码将表单上传的图像保存到DB:

  def saveImage(userIdSource: String) = Action.async(gridFSBodyParser(gridFS)) { request =>
    // here is the future file!
    val futureFile = request.body.files.head.ref
    // when the upload is complete, we add the article id to the file entry (in order to find the attachments of the article)

    val futureUpdate = for {
      file <- futureFile
      // here, the file is completely uploaded, so it is time to update the user
      updateResult <- {

        gridFS.files.update(
          BSONDocument("_id" -> file.id),
          BSONDocument("$set" -> BSONDocument("metadata" ->
            BSONDocument("user" -> BSONObjectID(userIdSource),
              "size" -> "original"))))

        val iterator = gridFS.enumerate(file).run(Iteratee.consume[Array[Byte]]())
        iterator.flatMap {
          bytes => {
            // Create resized image
            val enumerator: Enumerator[Array[Byte]] = Enumerator.outputStream(
              out => {
                Image(bytes).bound(120, 120).writer(Format.JPEG).withCompression(90).write(out)
              }
            )

            val data = DefaultFileToSave(
              filename = file.filename,
              contentType = file.contentType,
              uploadDate = Some(DateTime.now().getMillis),
              metadata = file.metadata ++ BSONDocument(
                "user" -> BSONObjectID(userIdSource),
                "size" -> "thumb"
              )
            )

            Logger.warn(s"Saving resized image: [id=$userIdSource, metadata=${data.metadata}}]")
            gridFS.save(enumerator, data).map {
              image => Some(image)
            }
          }
        }
      }
    } yield updateResult

    futureUpdate.flatMap {
      case _ =>  //Some(_)

        val img = Image(BSONObjectID(userIdSource), DateTime.now(), None)
       //  insert the user
        val futureResult2 = snapsCollection.insert(img)
     //    when the insert is performed, send a OK 200 result
        futureResult2.map(_ => Ok("saved"))
    }.recover {
      case e => InternalServerError(e.getMessage())
    }
  }
def saveImage(userIdSource:String)=Action.async(gridFSBodyParser(gridFS)){request=>
//这是未来的文件!
val futureFile=request.body.files.head.ref
//上传完成后,我们将文章id添加到文件条目中(以便查找文章的附件)
val futureUpdate=for{
文件BSONDocument(“元数据”->
BSONDocument(“用户”->BSONObjectID(userIdSource),
“尺寸”->“原件”))
val iterator=gridFS.enumerate(file).run(Iteratee.consume[Array[Byte]]())
迭代器.flatMap{
字节=>{
//创建调整大小的图像
val枚举器:枚举器[数组[字节]]=enumerator.outputStream(
out=>{
图像(字节)。绑定(120120)。编写器(格式.JPEG)。带压缩(90)。写入(输出)
}
)
val data=DefaultFileToSave(
filename=file.filename,
contentType=file.contentType,
uploadDate=Some(DateTime.now().getMillis),
元数据=file.metadata++b文档(
“用户”->BSONObjectID(userIdSource),
“大小”->“拇指”
)
)
Logger.warn(s“正在保存调整大小的图像:[id=$userIdSource,metadata=${data.metadata}}]”)
保存(枚举器,数据).map{
image=>Some(图像)
}
}
}
}
}产量更新结果
futureUpdate.flatMap{
case=>//一些
val img=Image(BSONObjectID(userIdSource),DateTime.now(),无)
//插入用户
val futureResult2=snapsCollection.insert(img)
//执行插入时,发送OK 200结果
futureResult2.map(=>Ok(“已保存”))
}.恢复{
案例e=>InternalServerError(e.getMessage())
}
}
此代码从表单中获取图像,并将其存储在Mongo DB Grid FS中两次:

  • 原始图像
  • 拇指图像
我想在“img”对象中存储2个附加参数:

  • 原始图像的id-它存储在“file.id”中
  • 拇指图像的id-它存储在“image.id”中

如何将这两个值传递给“futureUpdate.flatMap”?

将收益率语句更改为元组
(updateResult,file.id,image.id)
,然后在模式匹配中为futureUpdate.flatMap提取它们。

谢谢Gangstead。我对Scala不太熟悉。你能告诉我如何将元组放入代码中吗?好的,我知道了如何在“file.id”中这样做。如何将“image.id”添加到元组中?它不在该范围内定义……如果不知道这段代码的作用,很难说出来,但它的updateResult类型似乎是
Future[Unit]
,因为它执行的最后一条语句是对
迭代器的赋值。如果将其更改为updateResult包含image.id,则可以访问image.id将其添加到元组中。抱歉,我仍然不知道如何执行此操作。下面是代码作用的说明:gridFS.files.update将上传的图像放入mongodb。然后我想从上传的图像中创建一个拇指,并将其放入mongodb中。我从某个地方复制了这段代码,它工作得很好。“gridFS.enumerate(file)”在文件上创建迭代器。“Enumerator.outputStream”创建thumb。“val data=DefaultFileToSave”创建thumb实例。“gridFS.save(枚举器、数据).map”执行实际的数据库存储。我希望它在某些方面有所帮助。。。