Simplify by using file object model API
This commit is contained in:
parent
7aa44cd1a9
commit
2034480679
@ -3,32 +3,22 @@
|
|||||||
-- Author: Marcos Kirsch
|
-- Author: Marcos Kirsch
|
||||||
|
|
||||||
return function (connection, req, args)
|
return function (connection, req, args)
|
||||||
--print("Begin sending:", args.file)
|
|
||||||
--print("node.heap(): ", node.heap())
|
|
||||||
dofile("httpserver-header.lc")(connection, 200, args.ext, args.isGzipped)
|
dofile("httpserver-header.lc")(connection, 200, args.ext, args.isGzipped)
|
||||||
-- Send file in little chunks
|
-- Send file in little chunks
|
||||||
local continue = true
|
local bytesRemaining = file.list()[args.file]
|
||||||
local size = file.list()[args.file]
|
|
||||||
local bytesSent = 0
|
|
||||||
-- Chunks larger than 1024 don't work.
|
-- Chunks larger than 1024 don't work.
|
||||||
-- https://github.com/nodemcu/nodemcu-firmware/issues/1075
|
-- https://github.com/nodemcu/nodemcu-firmware/issues/1075
|
||||||
local chunkSize = 1024
|
local chunkSize = 1024
|
||||||
while continue do
|
fileHandle = file.open(args.file)
|
||||||
collectgarbage()
|
while bytesRemaining > 0 do
|
||||||
|
local bytesToRead = 0
|
||||||
-- NodeMCU file API lets you open 1 file at a time.
|
if bytesRemaining > chunkSize then bytesToRead = chunkSize else bytesToRead = bytesRemaining end
|
||||||
-- So we need to open, seek, close each time in order
|
local chunk = fileHandle:read(bytesToRead)
|
||||||
-- to support multiple simultaneous clients.
|
|
||||||
file.open(args.file)
|
|
||||||
file.seek("set", bytesSent)
|
|
||||||
local chunk = file.read(chunkSize)
|
|
||||||
file.close()
|
|
||||||
|
|
||||||
connection:send(chunk)
|
connection:send(chunk)
|
||||||
bytesSent = bytesSent + #chunk
|
bytesRemaining = bytesRemaining - #chunk
|
||||||
chunk = nil
|
chunk = nil
|
||||||
--print("Sent: " .. bytesSent .. " of " .. size)
|
collectgarbage()
|
||||||
if bytesSent == size then continue = false end
|
|
||||||
end
|
end
|
||||||
--print("Finished sending: ", args.file)
|
print("Finished sending: ", args.file)
|
||||||
|
fileHandle:close()
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user