Skip to content

Instantly share code, notes, and snippets.

@hntrmrrs
Created July 16, 2009 14:09
Show Gist options
  • Save hntrmrrs/148423 to your computer and use it in GitHub Desktop.
Save hntrmrrs/148423 to your computer and use it in GitHub Desktop.
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