Open
Description
I download pictures and save it on my server as data. Is there an easier way to get data from the response?
in the previous version there was a simple command for this, I don't understand why it was necessary to complicate everything response.http.body.data
`import Vapor
class PictureManager {
static func saveImage(req: Request, externalUrl: String, name: String) throws -> EventLoopFuture<String> {
var urlString = externalUrl
if !urlString.contains("http") {
urlString = (baseUrl + externalUrl)
}
return req.client.get("\(urlString)").flatMap { (response) in
guard var body = response.body, let data = body.readData(length: body.readableBytes) else {
return req.eventLoop.makeFailedFuture(Abort(.badRequest))
}
let imageData = ImageData(name: name, data: data, id: UUID())
return imageData.save(on: req.db).flatMap({ req.eventLoop.tryFuture({ try imageData.url() }) })
}
}
}`