Debian Codegraph @

examples/rest_pastebin/src/toppage_h.erl

raw
1 %% Feel free to use, reuse and abuse the code in this file.
2
3 %% @doc Pastebin handler.
4 -module(toppage_h).
5
6 %% Standard callbacks.
7 -export([init/2]).
8 -export([allowed_methods/2]).
9 -export([content_types_provided/2]).
10 -export([content_types_accepted/2]).
11 -export([resource_exists/2]).
12
13 %% Custom callbacks.
14 -export([create_paste/2]).
15 -export([paste_html/2]).
16 -export([paste_text/2]).
17
18 init(Req, Opts) ->
19 {cowboy_rest, Req, Opts}.
20
21 allowed_methods(Req, State) ->
22 {[<<"GET">>, <<"POST">>], Req, State}.
23
24 content_types_provided(Req, State) ->
25 {[
26 {{<<"text">>, <<"plain">>, []}, paste_text},
27 {{<<"text">>, <<"html">>, []}, paste_html}
28 ], Req, State}.
29
30 content_types_accepted(Req, State) ->
31 {[{{<<"application">>, <<"x-www-form-urlencoded">>, '*'}, create_paste}],
32 Req, State}.
33
34 resource_exists(Req, _State) ->
35 case cowboy_req:binding(paste_id, Req) of
36 undefined ->
37 {true, Req, index};
38 PasteID ->
39 case valid_path(PasteID) and file_exists(PasteID) of
40 true -> {true, Req, PasteID};
41 false -> {false, Req, PasteID}
42 end
43 end.
44
45 create_paste(Req, State) ->
46 PasteID = new_paste_id(),
47 {ok, [{<<"paste">>, Paste}], Req2} = cowboy_req:read_urlencoded_body(Req),
48 ok = file:write_file(full_path(PasteID), Paste),
49 case cowboy_req:method(Req2) of
50 <<"POST">> ->
51 {{true, <<$/, PasteID/binary>>}, Req2, State};
52 _ ->
53 {true, Req2, State}
54 end.
55
56 paste_html(Req, index) ->
57 {read_file("index.html"), Req, index};
58 paste_html(Req, Paste) ->
59 #{lang := Lang} = cowboy_req:match_qs([{lang, [fun lang_constraint/2], plain}], Req),
60 {format_html(Paste, Lang), Req, Paste}.
61
62 paste_text(Req, index) ->
63 {read_file("index.txt"), Req, index};
64 paste_text(Req, Paste) ->
65 #{lang := Lang} = cowboy_req:match_qs([{lang, [fun lang_constraint/2], plain}], Req),
66 {format_text(Paste, Lang), Req, Paste}.
67
68 % Private
69
70 lang_constraint(forward, Bin) ->
71 case re:run(Bin, "^[a-z0-9_]+$", [{capture, none}]) of
72 match -> {ok, Bin};
73 nomatch -> {error, bad_lang}
74 end;
75 lang_constraint(format_error, {bad_lang, _}) ->
76 "Invalid lang parameter.".
77
78 read_file(Name) ->
79 {ok, Binary} = file:read_file(full_path(Name)),
80 Binary.
81
82 full_path(Name) ->
83 filename:join([code:priv_dir(rest_pastebin), Name]).
84
85 file_exists(Name) ->
86 case file:read_file_info(full_path(Name)) of
87 {ok, _Info} -> true;
88 {error, _Reason} -> false
89 end.
90
91 valid_path(<<>>) -> true;
92 valid_path(<<$., _T/binary>>) -> false;
93 valid_path(<<$/, _T/binary>>) -> false;
94 valid_path(<<_Char, T/binary>>) -> valid_path(T).
95
96 new_paste_id() ->
97 Initial = rand:uniform(62) - 1,
98 new_paste_id(<<Initial>>, 7).
99 new_paste_id(Bin, 0) ->
100 Chars = <<"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890">>,
101 << <<(binary_part(Chars, B, 1))/binary>> || <<B>> <= Bin >>;
102 new_paste_id(Bin, Rem) ->
103 Next = rand:uniform(62) - 1,
104 new_paste_id(<<Bin/binary, Next>>, Rem - 1).
105
106 format_html(Paste, plain) ->
107 Text = escape_html_chars(read_file(Paste)),
108 <<"<!DOCTYPE html><html>",
109 "<head><title>paste</title></head>",
110 "<body><pre><code>", Text/binary, "</code></pre></body></html>\n">>;
111 format_html(Paste, Lang) ->
112 highlight(full_path(Paste), Lang, "html").
113
114 format_text(Paste, plain) ->
115 read_file(Paste);
116 format_text(Paste, Lang) ->
117 highlight(full_path(Paste), Lang, "ansi").
118
119 highlight(Path, Lang, Type) ->
120 Path1 = binary_to_list(Path),
121 Lang1 = binary_to_list(Lang),
122 os:cmd(["highlight --syntax=", Lang1,
123 " --doc-title=paste ",
124 " --out-format=", Type,
125 " --include-style ", Path1]).
126
127 % Escape some HTML characters that might make a fuss
128 escape_html_chars(Bin) ->
129 << <<(escape_html_char(B))/binary>> || <<B>> <= Bin >>.
130
131 escape_html_char($<) -> <<"&lt;">>;
132 escape_html_char($>) -> <<"&gt;">>;
133 escape_html_char($&) -> <<"&amp;">>;
134 escape_html_char(C) -> <<C>>.