forked from gotthardp/lorawan-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlorawan_admin_upload.erl
More file actions
53 lines (43 loc) · 1.58 KB
/
lorawan_admin_upload.erl
File metadata and controls
53 lines (43 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
%
% Copyright (c) 2016-2019 Petr Gotthard <[email protected]>
% All rights reserved.
% Distributed under the terms of the MIT License. See the LICENSE file.
%
-module(lorawan_admin_upload).
-export([init/2]).
-export([allowed_methods/2]).
-export([is_authorized/2]).
-export([forbidden/2]).
-export([content_types_accepted/2]).
-export([handle_write/2]).
-record(state, {scopes, auth_fields}).
init(Req, Scopes) ->
{cowboy_rest, Req, #state{scopes=Scopes}}.
allowed_methods(Req, State) ->
{[<<"OPTIONS">>, <<"POST">>], Req, State}.
is_authorized(Req, #state{scopes=Scopes}=State) ->
case lorawan_admin:handle_authorization(Req, Scopes) of
{true, AuthFields} ->
{true, Req, State#state{auth_fields=AuthFields}};
Else ->
{Else, Req, State}
end.
forbidden(Req, #state{auth_fields=AuthFields}=State) ->
{lorawan_admin:fields_empty(AuthFields), Req, State}.
content_types_accepted(Req, State) ->
{[
{'*', handle_write}
], Req, State}.
handle_write(Req, State) ->
{ok, Headers, Req2} = cowboy_req:read_part(Req),
{ok, Data, Req3} = cowboy_req:read_part_body(Req2),
{file, <<"file">>, FileName, ContentType} = cow_multipart:form_data(Headers),
case file:write_file(FileName, Data) of
ok ->
lager:debug("Uploaded ~p of content-type ~p", [FileName, ContentType]),
{true, Req3, State};
{error, Error} ->
lager:error("Cannot upload ~p of content-type ~p: ~p", [FileName, ContentType, Error]),
{stop, cowboy_req:reply(400, Req3), State}
end.
% end of file