Advertisement
Guest User

Untitled

a guest
Aug 1st, 2015
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. -module(web_store).
  2. -include_lib("cmon/include/logger.hrl").
  3. -include_lib("web/include/db.hrl").
  4.  
  5. -export([init/3,handle/2,terminate/3]).
  6.  
  7. init(_Type, Req, _Opts) ->
  8. ?INFO("OPTS: ~p", [_Opts]),
  9. {ok, Req, _Opts}.
  10.  
  11. handle_form({file, Name, _File, Type, _Enc}) -> {file, Type, Name};
  12. handle_form({data, <<"sid">>}) -> sid;
  13. handle_form(_) -> skip.
  14.  
  15. make_prop_elem({file, Type, Name}, Data) -> [{file, Data}, {mime, Type}, {type, Name}];
  16. make_prop_elem(R, Data) when is_atom(R) -> [{R, Data}].
  17.  
  18. handle_part({ok, Headers, Req}) ->
  19. {ok, Data, Rest} = cowboy_req:part_body(Req),
  20. { Rest, make_prop_elem( handle_form( cow_multipart:form_data(Headers) ), Data ) };
  21. handle_part({done, Req}) -> {Req, []}.
  22.  
  23. multipart(Req, Prop) ->
  24. {Rest, Elem} = handle_part(cowboy_req:part(Req)),
  25. case Elem of
  26. [] -> Prop;
  27. _ -> multipart(Rest, Elem ++ Prop)
  28. end.
  29.  
  30. get_props(Plist, Props) -> [ proplists:get_value(Prop, Plist) || Prop <- Props ].
  31.  
  32. user_file_store([[Id, _]], Mime, Store, _, _, Data) ->
  33. [File] = dbd:get(user_file, Id),
  34. Path = filename:join(Store, integer_to_list(Id)),
  35. file:write_file(Path, Data),
  36. dbd:put(File#user_file{ mime=Mime }),
  37. Id;
  38.  
  39. user_file_store([], Mime, Store, Uid, Type, Data) ->
  40. FileId = dbd:make_uid(),
  41. Path = filename:join(Store, integer_to_list(FileId)),
  42. file:write_file(Path, Data),
  43. dbd:put(#user_file{ id=FileId, user_id=Uid, stamp=now(), mime=Mime, type=Type }),
  44. FileId.
  45.  
  46. write_user_file(Store, Data, Uid, Type, Mime) when is_number(Uid) ->
  47. FileId = user_file_store( db_user:files(Uid, Type), Mime, Store, Uid, Type, Data),
  48. db_msg:sys_notify(Uid, [<<"avatar/upload">>, FileId]),
  49. ok.
  50.  
  51. handle_upload(Req, StorePath) ->
  52. Plist = multipart(Req, []),
  53. [Sid, Data, Mime, Type] = get_props(Plist, [sid,file,mime,type]),
  54. Uid = db_user:sid_to_uid(erlang:binary_to_integer(Sid)),
  55. write_user_file(StorePath, Data, Uid, Type, Mime),
  56. % cowboy_req:reply(200, Req). BUG?
  57. cowboy_req:reply(200, [{<<"connection">>, <<"close">>}], Req).
  58.  
  59. handle_get_file([#user_file{ id=FileId, mime=Type }], Req, StorePath ) ->
  60. Path = filename:join(StorePath, integer_to_list(FileId)),
  61. F = fun (Socket, Transport) -> Transport:sendfile(Socket, Path) end,
  62. Req2 = cowboy_req:set_resp_body_fun(F, Req),
  63. cowboy_req:reply(200, [{<<"content-type">>, Type}], Req2);
  64. handle_get_file(A,_,C) -> ?ERR("handle_get_file() ~p ~p", [A,C]).
  65.  
  66. handle_get_path(<<"avatar">>, Id, Req, StorePath) ->
  67. handle_get_file( db_file:get( binary_to_integer(Id) ), Req, StorePath).
  68.  
  69. handle_get(Req, StorePath) ->
  70. {[Type, Id], Req2} = cowboy_req:path_info(Req),
  71. handle_get_path(Type, Id, Req2, StorePath).
  72.  
  73. handle_method(<<"POST">>, Req, StorePath) -> handle_upload(Req, StorePath);
  74. handle_method(<<"GET">>, Req, StorePath) -> handle_get(Req, StorePath);
  75. handle_method(Method, _, _) -> ?ERR("Unhandled method: ~p", [Method]).
  76.  
  77. handle(Req, State = [StorePath]) ->
  78. ?INFO("web_store", []),
  79. {Method, Req2} = cowboy_req:method(Req),
  80. {ok, Req3} = handle_method(Method, Req2, StorePath),
  81. {ok, Req3, State}.
  82.  
  83. terminate(_, _, _) -> ?INFO("TERM", []), ok.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement