Skip to content
This repository has been archived by the owner on Oct 22, 2021. It is now read-only.

Commit

Permalink
POST via http works and returns Location header.
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Newson committed Mar 9, 2011
1 parent b2f3254 commit ad612bf
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 20 deletions.
2 changes: 1 addition & 1 deletion priv/dispatch.conf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
%%-*- mode: erlang -*-

{[file], monic_file_resource, [{root, "tmp"}]}.
{[file, cookie, key], monic_item_resource, [{root, "tmp"}]}.
{[file, key, cookie], monic_item_resource, [{root, "tmp"}]}.
22 changes: 11 additions & 11 deletions src/monic_file_resource.erl
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,14 @@ content_types_provided(ReqData, Context) ->
{[{"application/json", show_file}], ReqData, Context}.

create_path(ReqData, Context) ->
File = wrq:path_info(file, ReqData),
Key = 1, %% TODO increment.
Cookie = crypto:rand_uniform(1, 1 bsl 32),
{io_lib:format("/~s/~B/~B", [File, Key, Cookie]), ReqData, Context}.
{"ignored", ReqData, Context}.

delete_resource(ReqData, Context) ->
case file:delete(monic_utils:path(ReqData, Context)) of
ok ->
{true, ReqData, Context};
_ ->
{false, ReqData, Context}
end.
Result = case file:delete(monic_utils:path(ReqData, Context)) of
ok -> true;
_ -> false
end,
{Result, ReqData, Context}.

init(ConfigProps) ->
{ok, ConfigProps}.
Expand Down Expand Up @@ -93,14 +89,18 @@ create_file(ReqData, Context) ->
end.

add_item(ReqData, Context) ->
erlang:display({disp, wrq:disp_path(ReqData)}),
case monic_file:open(monic_utils:path(ReqData, Context)) of
{ok, Pid} ->
try
Size = list_to_integer(wrq:get_req_header("Content-Length", ReqData)),
StreamBody = wrq:stream_req_body(ReqData, ?BUFFER_SIZE),
case monic_file:add(Pid, Size, StreamBody) of
{ok, Key, Cookie} ->
{true, ReqData, Context};
File = wrq:path_info(file, ReqData),
Location = io_lib:format("/~s/~B/~B",[File, Key, Cookie]),
ReqData1 = wrq:set_resp_header("Location", Location, ReqData),
{true, ReqData1, Context};
_ ->
{false, ReqData, Context}
end
Expand Down
8 changes: 0 additions & 8 deletions src/monic_item_resource.erl
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
-export([init/1,
allowed_methods/2,
content_types_provided/2,
create_path/2,
delete_resource/2,
post_is_create/2,
resource_exists/2]).

allowed_methods(ReqData, Context) ->
Expand All @@ -27,17 +25,11 @@ allowed_methods(ReqData, Context) ->
content_types_provided(ReqData, Context) ->
{[{"application/octet-stream", to_binary}], ReqData, Context}.

create_path(ReqData, Context) ->
{"/fixme", ReqData, Context}.

delete_resource(ReqData, Context) ->
{false, ReqData, Context}.

init(ConfigProps) ->
{ok, ConfigProps}.

post_is_create(ReqData, Context) ->
{true, ReqData, Context}.

resource_exists(ReqData, Context) ->
{true, ReqData, Context}.

0 comments on commit ad612bf

Please sign in to comment.