fixed premature finishing of httpclient.downloadFile

This commit is contained in:
Araq 2017-02-25 17:26:03 +01:00
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: else:
break break
proc getWriteStream*(f: AsyncFile): FutureStream[string] = proc setWriteStream*(f: AsyncFile; fut: FutureStream[string]) {.async.} =
## Returns a new stream that can be used for writing to the file. await writeFromStream(f, fut)
result = newFutureStream[string]()
asyncCheck writeFromStream(f, result)

View file

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