Skip to content

Commit

Permalink
Added a simple file upload demo, to exercise the webmachine_multipart…
Browse files Browse the repository at this point in the history
… module.

Files are saved in /priv/www/uploads.
  • Loading branch information
Manu committed Sep 8, 2013
1 parent a169963 commit 3d11d94
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 2 deletions.
10 changes: 9 additions & 1 deletion demo/README
Expand Up @@ -13,9 +13,12 @@ start.sh : simple startup script for running webmachine_demo
/webmachine_demo_app.erl : base module for the Erlang application
/webmachine_demo_sup.erl : OTP supervisor for the application
/webmachine_demo_resource.erl : a simple example Webmachine resource
/webmachine_demo_fs_resource.erl : a simple filesystem resource
/webmachine_demo_upload_resource.erl : a simple file upload resource
/priv
/dispatch.conf : the Webmachine URL-dispatching table
/www : a convenient place to put your static web content
/www/upload : where uploaded files are saved

You probably want to do one of a couple of things at this point:

Expand All @@ -38,6 +41,11 @@ You probably want to do one of a couple of things at this point:
$ echo "Hello World." > /tmp/fs/demo.txt
Visit http://localhost:8000/fs/demo.txt

5. Add some new resources:
5. Test the file upload resource:
Visit http://localhost:8000/upload
Upload a file
Look in folder /priv/www/uploads

6. Add some new resources:
edit src/YOUR_NEW_RESOURCE.erl
edit priv/dispatch.conf
1 change: 1 addition & 0 deletions demo/priv/dispatch.conf
@@ -1,3 +1,4 @@
%%-*- mode: erlang -*-
{["demo", '*'], webmachine_demo_resource, []}.
{["fs", '*'], webmachine_demo_fs_resource, [{root, "/tmp/fs"}]}.
{["upload", '*'], webmachine_demo_upload_resource, []}.
4 changes: 4 additions & 0 deletions demo/priv/www/uploads/.gitignore
@@ -0,0 +1,4 @@
# Ignore everything in this directory
*
# Except this file
!.gitignore
5 changes: 4 additions & 1 deletion demo/rebar.config
@@ -1,3 +1,6 @@
%%-*- mode: erlang -*-

{deps, [{webmachine, "1.10.*", {git, "git://github.com/basho/webmachine", "HEAD"}}]}.
{deps, [
{webmachine, "1.10.*", {git, "git://github.com/basho/webmachine", "HEAD"}},
{erlydtl, ".*", {git, "git://github.com/evanmiller/erlydtl", "HEAD"}}
]}.
53 changes: 53 additions & 0 deletions demo/src/webmachine_demo_upload_resource.erl
@@ -0,0 +1,53 @@
%% @author Voila <th3rac25@gmail.com>
%% @copyright 2007-2009 Basho Technologies, Inc. All Rights Reserved.
%% @doc Example webmachine_resource.

-module(webmachine_demo_upload_resource).
-export([init/1, to_html/2, allowed_methods/2, content_types_provided/2, process_post/2]).

-include_lib("webmachine/include/webmachine.hrl").

-define(UPLOAD_DIR, 'priv/www/uploads').

init([]) -> {ok, undefined}.

allowed_methods(ReqData, Context) ->
{['GET', 'POST', 'HEAD'], ReqData, Context}.

content_types_provided(ReqData, Context) ->
{[{"text/html", to_html}], ReqData, Context}.

process_post(ReqData, Context) ->
Body = wrq:req_body(ReqData),
Boundary = webmachine_multipart:find_boundary(ReqData),
Parts = webmachine_multipart:get_all_parts(Body, Boundary),
{FileName, FileData} = get_file(Parts),
ok = save_file(FileName, FileData),
Msg = ["File '", FileName, "' successfully uploaded."],
ReqData1 = wrq:set_resp_header("location", ["/upload?msg=", Msg], ReqData),
ReqData2 = wrq:do_redirect(true, ReqData1),
{true, ReqData2, Context}.


to_html(ReqData, Context) ->
Vars = case wrq:get_qs_value("msg", ReqData) of
undefined -> [];
Msg -> [{msg, Msg}]
end,
{ok, Content} = form_dtl:render(Vars),
{Content, ReqData, Context}.

%%-----------------------------------------------------------------------------
%% Internal Functions
%%-----------------------------------------------------------------------------
binary_to_atom(Bin) ->
list_to_atom(binary_to_list(Bin)).

save_file(FileName, Bin) ->
FilePath = [?UPLOAD_DIR, '/', binary_to_atom(FileName)],
file:write_file(FilePath, Bin).

get_file(Parts) ->
{_, {Params, _}, FileData} = lists:keyfind("file", 1, Parts),
{_, FileName} = proplists:lookup(<<"filename">>, Params),
{FileName, FileData}.
13 changes: 13 additions & 0 deletions demo/templates/form.dtl
@@ -0,0 +1,13 @@
<html>
<body>
{% if msg %}<p>{{ msg }}</p>{% endif %}
<form action="/upload" method="post" enctype="multipart/form-data">
<p>
<input type="file" name="file">
</p>
<p>
<button type="submit">Upload</button>
</p>
</form>
</body>
</html>

0 comments on commit 3d11d94

Please sign in to comment.