Created
July 16, 2009 14:09
-
-
Save hntrmrrs/148423 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
serve_file(Response, File) -> | |
case file:open(File, [raw, binary]) of | |
{ok, IoDevice} -> | |
%% Set ChunkSize to an optimal value | |
ChunkSize = 1024, | |
Stream = iodevice_stream(IoDevice, ChunkSize), | |
Response1 = setelement(4, Response, Stream), | |
file:close(IoDevice), | |
Response1; | |
_ -> | |
%% Respond with 404... | |
setelement(2, Response, {404, "NOT FOUND"}) | |
end. | |
iodevice_stream(IoDevice, ChunkSize) -> | |
fun() -> | |
case file:read(IoDevice, ChunkSize) of | |
eof -> | |
{}; | |
{ok, Data} -> | |
{Data, iodevice_stream(IoDevice, ChunkSize)} | |
end | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment