I have this code:
proc pipeTo(input: Stream, output: Stream, chunkSize = 512) =
var buff = repeat("\0", chunkSize)
while true:
let len = input.readDataStr(buff, 0..<chunkSize)
if len == 0:
break
output.write(buff[0..<len])
output.flush()
echo buff.len
output.close()
proc downloadFile(client: HttpClient, fileItem: FileItem) =
let outputDirPath = joinPath([paramStr(2), fileItem.album])
let outputPath = joinPath([outputDirPath, fileItem.name])
ensureDirExists(outputDirPath)
let response = client.get(fileItem.url)
let file = newFileStream(outputPath, fmWrite)
response.bodyStream.pipeTo(file)
Run
In theory, it should stream chunks of data between the network stream and the
file stream, this way the memory usage should stay low. But something is wrong
and it seems the entire file is inside the ram instead of being flushed to the
disk.