<commit_before>%% Feel free to use, reuse and abuse the code in this file.

%% @doc Pastebin handler.
-module(toppage_handler).

%% REST Callbacks
-export([init/3]).
-export([allowed_methods/2]).
-export([content_types_provided/2]).
-export([content_types_accepted/2]).
-export([resource_exists/2]).

%% Callback Callbacks
-export([create_paste/2]).
-export([paste_html/2]).
-export([paste_text/2]).

init(_Transport, _Req, []) ->
	% For the random number generator:
	{X, Y, Z} = now(),
	random:seed(X, Y, Z),
	{upgrade, protocol, cowboy_rest}.

allowed_methods(Req, State) ->
	{[<<"GET">>, <<"POST">>], Req, State}.

content_types_provided(Req, State) ->
	{[
		{{<<"text">>, <<"plain">>, []}, paste_text},
		{{<<"text">>, <<"html">>, []}, paste_html}
	], Req, State}.

content_types_accepted(Req, State) ->
	{[{{<<"application">>, <<"x-www-form-urlencoded">>, []}, create_paste}],
		Req, State}.

resource_exists(Req, _State) ->
	case cowboy_req:binding(paste_id, Req) of
		{undefined, Req2} ->
			{true, Req2, index};
		{PasteID, Req2} ->
			case valid_path(PasteID) and file_exists(PasteID) of
				true -> {true, Req2, PasteID};
				false -> {false, Req2, PasteID}
			end
	end.

create_paste(Req, State) ->
	PasteID = new_paste_id(),
	{ok, [{<<"paste">>, Paste}], Req3} = cowboy_req:body_qs(Req),
	ok = file:write_file(full_path(PasteID), Paste),
	case cowboy_req:method(Req3) of
		{<<"POST">>, Req4} ->
			{<<$/, PasteID/binary>>, Req4, State};
		{_, Req4} ->
			{true, Req4, State}
	end.

paste_html(Req, index) ->
	{read_file("index.html"), Req, index};
paste_html(Req, Paste) ->
	{Style, Req2} = cowboy_req:qs_val(<<"lang">>, Req, plain),
	{format_html(Paste, Style), Req2, Paste}.

paste_text(Req, index) ->
	{read_file("index.txt"), Req, index};
paste_text(Req, Paste) ->
	{Style, Req2} = cowboy_req:qs_val(<<"lang">>, Req, plain),
	{format_text(Paste, Style), Req2, Paste}.

% Private

read_file(Name) ->
	{ok, Binary} = file:read_file(full_path(Name)),
	Binary.
<commit_msg>Fix rest_pastebin example<commit_after>%% Feel free to use, reuse and abuse the code in this file.

%% @doc Pastebin handler.
-module(toppage_handler).

%% REST Callbacks
-export([init/3]).
-export([allowed_methods/2]).
-export([content_types_provided/2]).
-export([content_types_accepted/2]).
-export([resource_exists/2]).

%% Callback Callbacks
-export([create_paste/2]).
-export([paste_html/2]).
-export([paste_text/2]).

init(_Transport, _Req, []) ->
	% For the random number generator:
	{X, Y, Z} = now(),
	random:seed(X, Y, Z),
	{upgrade, protocol, cowboy_rest}.

allowed_methods(Req, State) ->
	{[<<"GET">>, <<"POST">>], Req, State}.

content_types_provided(Req, State) ->
	{[
		{{<<"text">>, <<"plain">>, []}, paste_text},
		{{<<"text">>, <<"html">>, []}, paste_html}
	], Req, State}.

content_types_accepted(Req, State) ->
	{[{{<<"application">>, <<"x-www-form-urlencoded">>, []}, create_paste}],
		Req, State}.

resource_exists(Req, _State) ->
	case cowboy_req:binding(paste_id, Req) of
		{undefined, Req2} ->
			{true, Req2, index};
		{PasteID, Req2} ->
			case valid_path(PasteID) and file_exists(PasteID) of
				true -> {true, Req2, PasteID};
				false -> {false, Req2, PasteID}
			end
	end.

create_paste(Req, State) ->
	PasteID = new_paste_id(),
	{ok, [{<<"paste">>, Paste}], Req3} = cowboy_req:body_qs(Req),
	ok = file:write_file(full_path(PasteID), Paste),
	case cowboy_req:method(Req3) of
		{<<"POST">>, Req4} ->
			{{true, <<$/, PasteID/binary>>}, Req4, State};
		{_, Req4} ->
			{true, Req4, State}
	end.

paste_html(Req, index) ->
	{read_file("index.html"), Req, index};
paste_html(Req, Paste) ->
	{Style, Req2} = cowboy_req:qs_val(<<"lang">>, Req, plain),
	{format_html(Paste, Style), Req2, Paste}.

paste_text(Req, index) ->
	{read_file("index.txt"), Req, index};
paste_text(Req, Paste) ->
	{Style, Req2} = cowboy_req:qs_val(<<"lang">>, Req, plain),
	{format_text(Paste, Style), Req2, Paste}.

% Private

read_file(Name) ->
	{ok, Binary} = file:read_file(full_path(Name)),
	Binary.

{'fraction_non_alphanumeric': 0.22130325814536342, 'fraction_numerical': 0.011528822055137845, 'mean_word_length': 4.9924924924924925, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 89, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3740228', 'n_tokens_mistral': 1719, 'n_tokens_neox': 1631, 'n_words': 323}