mirror of
https://github.com/nim-lang/Nim.git
synced 2025-12-30 01:44:37 +00:00
fixed premature finishing of httpclient.downloadFile
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user