Skip to content

Commit

Permalink
Eptic: Refactored some filesystem-specific parts of multipart handling
Browse files Browse the repository at this point in the history
into e_multipart_common module. Take some care not to mess up docbuilder
with angle brackets.
  • Loading branch information
Krzysztof Goj committed Jun 17, 2010
1 parent dbce0f7 commit 8acbf0f
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 29 deletions.
2 changes: 1 addition & 1 deletion lib/eptic-1.4/ebin/eptic.app
Expand Up @@ -4,7 +4,7 @@
{modules, [e_cache,e_cluster,e_conf,e_error,
e_db_couchdb,e_db,e_db_mnesia,
e_dict,e_dispatcher,e_file,e_json,e_lang,e_hook,
e_mod_gen,e_mod_inets,e_mod_yaws,e_mod_ewgi,e_multipart_inets,e_multipart_yaws,
e_mod_gen,e_mod_inets,e_mod_yaws,e_mod_ewgi,e_multipart_common,e_multipart_inets,e_multipart_yaws,
eptic,e_session,e_validator,e_component,e_cache_ets,e_cache_disk,e_annotation,e_user_annotation,
e_logger, e_logger_viewer,
e_start]},
Expand Down
52 changes: 52 additions & 0 deletions lib/eptic-1.4/src/e_multipart_common.erl
@@ -0,0 +1,52 @@
%% The contents of this file are subject to the Erlang Web Public License,
%% Version 1.0, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Web Public License along with this software. If not, it can be
%% retrieved via the world wide web at http://www.erlang-consulting.com/.
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%%
%% The Initial Developer of the Original Code is Erlang Training & Consulting
%% Ltd. Portions created by Erlang Training & Consulting Ltd are Copyright 2008,
%% Erlang Training & Consulting Ltd. All Rights Reserved.

%%%-------------------------------------------------------------------
%%% File : e_multipart_common.erl
%%% Author : Krzysztof Goj <krzysztof.goj@erlang-solutions.com>
%%% Description : Module responsible for server-agnostic things
%%% in multipart form handling
%%% @hidden
%%%-------------------------------------------------------------------

-module(e_multipart_common).

%% API
-export([base_dir/1, delete_tmp_files/1]).

%%====================================================================
%% API
%%====================================================================

%%
%% @doc Returns path to process-specific directory into which files
%% can be stored. This directory will be pruned when process terminates
%%
-spec(base_dir/1 :: (pid()) -> string()).
base_dir(Pid) ->
% remove angle brackets as they mess up docbuilder
"<" ++ SafePid = pid_to_list(Pid) -- ">",
LastDir = "e-multipart-" ++ SafePid,
filename:join(e_conf:upload_dir(), LastDir).

%%
%% @doc Deletes the directory {@link base_dir/1} with all it's files
%%
-spec(delete_tmp_files/1 :: (pid()) -> ok | {error, atom()}).
delete_tmp_files(Pid) ->
Dirname = base_dir(Pid),
lists:foreach(fun file:delete/1,
filelib:wildcard(filename:join(Dirname, "*"))),
file:del_dir(Dirname).
31 changes: 15 additions & 16 deletions lib/eptic-1.4/src/e_multipart_inets.erl
Expand Up @@ -24,11 +24,25 @@

-export([get_multipart/2, terminate/0, terminate/1]).

%%====================================================================
%% API
%%====================================================================

get_multipart(Body, Boundary) ->
Regexp = Boundary ++ "((\r\n)|(\-\-\r\n))",
{ok, Split} = regexp:split(Body, Regexp),
retrive_data(Split).

terminate() ->
e_multipart_common:delete_tmp_files(self()).

terminate(Pid) ->
e_multipart_common:delete_tmp_files(Pid).

%%====================================================================
%% Internal functions
%%====================================================================

retrive_data([]) ->
[];
retrive_data([[] | Rest]) ->
Expand Down Expand Up @@ -71,7 +85,7 @@ retrive_data(["Content-Disposition: form-data; " ++ Element | Rest]) ->
end.

save_file(SFilename, Content) ->
BaseDir = dirname(),
BaseDir = e_multipart_common:base_dir(self()),
FolderCreator = fun(Element, Acc) ->
Next =
if Acc =/= "" ->
Expand Down Expand Up @@ -108,18 +122,3 @@ save_file(SFilename, Content) ->
end,

Filename.

dirname() ->
filename:join([e_conf:upload_dir(), pid_to_list(self())]).

terminate() ->
Dirname = dirname(),
lists:foreach(fun file:delete/1,
filelib:wildcard(filename:join(Dirname, "*"))),
file:del_dir(Dirname).

terminate(Pid) ->
Dirname = filename:join([e_conf:upload_dir(), pid_to_list(Pid)]),
lists:foreach(fun file:delete/1,
filelib:wildcard(filename:join(Dirname, "*"))),
file:del_dir(Dirname).
16 changes: 4 additions & 12 deletions lib/eptic-1.4/src/e_multipart_yaws.erl
Expand Up @@ -29,17 +29,12 @@
%%====================================================================
%% API
%%====================================================================

terminate() ->
Dirname = dirname(),
lists:foreach(fun file:delete/1,
filelib:wildcard(filename:join(Dirname, "*"))),
file:del_dir(Dirname).
e_multipart_common:delete_tmp_files(self()).

terminate(Pid) ->
Dirname = filename:join([e_conf:upload_dir(), pid_to_list(Pid)]),
lists:foreach(fun file:delete/1,
filelib:wildcard(filename:join(Dirname, "*"))),
file:del_dir(Dirname).
e_multipart_common:delete_tmp_files(Pid).

is_multipart(#arg{req = R, headers = H}) ->
case {R#http_request.method, H#headers.content_type} of
Expand Down Expand Up @@ -95,7 +90,7 @@ add_body([], State) ->
add_chunk([{head, {Name, Opt}}|Res], {_, SoFar}) ->
case lists:keysearch(filename, 1, Opt) of
{value, {filename, Filename0}} ->
BaseDir = dirname(),
BaseDir = e_multipart_common:base_dir(self()),

FolderCreator = fun(Element, Acc) ->
Next =
Expand Down Expand Up @@ -170,9 +165,6 @@ basename(FilePath) ->
basename(string:substr(FilePath, N+1))
end.

dirname() ->
filename:join([e_conf:upload_dir(), pid_to_list(self())]).

append_file(Filename, Data) ->
{ok, Ref} = file:open(Filename, [append]),
ok = file:write(Ref, Data),
Expand Down

0 comments on commit 8acbf0f

Please sign in to comment.