fixed premature finishing of httpclient.downloadFile

This commit is contained in:
Araq
2017-02-25 17:26:03 +01:00
parent 1961e444c3
commit 45765601e0
2 changed files with 8 additions and 12 deletions

View File

@@ -484,7 +484,5 @@ proc writeFromStream(f: AsyncFile, fut: FutureStream[string]) {.async.} =
else:
break
proc getWriteStream*(f: AsyncFile): FutureStream[string] =
## Returns a new stream that can be used for writing to the file.
result = newFutureStream[string]()
asyncCheck writeFromStream(f, result)
proc setWriteStream*(f: AsyncFile; fut: FutureStream[string]) {.async.} =
await writeFromStream(f, fut)

View File

@@ -1247,16 +1247,14 @@ proc downloadFile*(client: HttpClient | AsyncHttpClient,
client.bodyStream = newFileStream(filename, fmWrite)
if client.bodyStream.isNil:
fileError("Unable to open file")
else:
var f = openAsync(filename, fmWrite)
client.bodyStream = f.getWriteStream()
await parseBody(client, resp.headers, resp.version)
when client is HttpClient:
parseBody(client, resp.headers, resp.version)
client.bodyStream.close()
else:
client.bodyStream = newFutureStream[string]()
var f = openAsync(filename, fmWrite)
asyncCheck parseBody(client, resp.headers, resp.version)
await f.setWriteStream(client.bodyStream)
f.close()
if resp.code.is4xx or resp.code.is5xx:
raise newException(HttpRequestError, resp.status)
raise newException(HttpRequestError, resp.status)