Akka-Http文件上传在请求被完全读取之前发送响应

时间:2018-02-09 19:38:26

标签: scala akka-http

我的akka​​-http服务器中有以下路由:

def tempDestination(fileInfo: FileInfo): File = {
    log.info(s"File info for upload: [$fileInfo]")
    File.createTempFile(fileInfo.fileName, ".tmp")
}
...
post {
    log.info("Inside the file upload path")
    fileUpload("zip") {
      case (fileInfo, bytes) =>
        val dest = tempDestination(fileInfo)
        val uploadedF: Future[(FileInfo, File)] =
          bytes
            .runWith(FileIO.toPath(dest.toPath))
            .map(_ => (fileInfo, dest))

        onSuccess(uploadedF) { (info, file) =>
            val fileCreated: Future[MyFile] = (fileRegistryActor ? NewFile(file)).mapTo[MyFile]
            complete((StatusCodes.Created, fileCreated))
        }
    }
}

通过表单上传文件后,我在日志中看到了这一点:

Sending an 2xx 'early' response before end of request was received... Note that the connection will be closed after this response. Also, many clients will not read early responses! Consider only issuing this response after the request data has been completely read!

我认为在创建和完成Sink的IOResult之前,uploadedF未来不会完成。我错过了什么?以及确定我的整个请求是否已被读取以及我的文件是否已完全写入磁盘的正确机制是什么?

1 个答案:

答案 0 :(得分:0)

所以似乎为我解决这个问题的方法是改变从postpost & extractRequest的路线。

def tempDestination(fileInfo: FileInfo): File = {
    log.info(s"File info for upload: [$fileInfo]")
    File.createTempFile(fileInfo.fileName, ".tmp")
}
...
(post & extractRequest) { _ =>
    log.info("Inside the file upload path")
    fileUpload("zip") {
      case (fileInfo, bytes) =>
        val dest = tempDestination(fileInfo)
        val uploadedF: Future[(FileInfo, File)] =
          bytes
            .runWith(FileIO.toPath(dest.toPath))
            .map(_ => (fileInfo, dest))

        onSuccess(uploadedF) { (info, file) =>
            val fileCreated: Future[MyFile] = (fileRegistryActor ? NewFile(file)).mapTo[MyFile]
            complete((StatusCodes.Created, fileCreated))
        }
    }
}