forked from webmachine/webmachine
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a simple file upload demo, to exercise the webmachine_multipart…
… module. Files are saved in /priv/www/uploads.
- Loading branch information
Manu
committed
Sep 8, 2013
1 parent
a169963
commit 3d11d94
Showing
6 changed files
with
84 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
%%-*- mode: erlang -*- | ||
{["demo", '*'], webmachine_demo_resource, []}. | ||
{["fs", '*'], webmachine_demo_fs_resource, [{root, "/tmp/fs"}]}. | ||
{["upload", '*'], webmachine_demo_upload_resource, []}. |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Ignore everything in this directory | ||
* | ||
# Except this file | ||
!.gitignore |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"}} | ||
]}. |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}. |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |