This repository has been archived by the owner on Mar 21, 2023. It is now read-only.
/
Root.pm
173 lines (148 loc) · 4.5 KB
/
Root.pm
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package App::Root;
# ABSTRACT: Root App for Dancer APP SCK
=head1 DESCRIPTION
Root App for the dancer app SCK. This part manage request on "/" only
=cut
use strict;
use warnings;
use 5.014;
use Carp;
use Data::Dumper;
# VERSION
#Initialise dancer app
use Dancer ':syntax';
use Try::Tiny;
use URI::Escape;
#Load SCK module
use App::Error;
#api, call with a=1
any [ 'get', 'post' ] => '/' => sub {
return pass() unless defined params->{a} && defined params->{url};
content_type "text/plain";
try {
return vars->{base} . vars->{sck}->shorten( params->{url} );
}
catch {
#if any error on getting short url, return long
return params->{url};
};
};
#twitter / facebook / google+ / with url
any [ 'get', 'post' ] => '/' => sub {
my ($service) = grep { defined params->{$_} } qw/t f g/;
return pass() unless defined $service && defined params->{url};
my $title = params->{title} // vars->{sck}->title( params->{url} ) // "";
my $url;
try {
$url = vars->{base} . vars->{sck}->shorten( params->{url} );
}
catch {
#push long title if any error occur
$url = params->{url};
};
if ($service eq 't') { #twitter
return redirect( "http://twitter.com/?status="
. uri_escape_utf8( join( ' - ', $title, $url ) ) );
} elsif ( $service eq 'f' ) { #facebook
return redirect( "http://www.facebook.com/share.php"
. "?u=" . uri_escape_utf8($url)
. "&t=" . uri_escape_utf8($title) );
} elsif ( $service eq 'g' ) { #google +
return redirect( "https://plus.google.com/share?url="
. uri_escape_utf8($url) );
}
};
#normal call with url
any [ 'get', 'post' ] => '/' => sub {
return pass() unless defined params->{url};
my (
$short_url, $stats_url, $error_message,
$notice_message, $top10_members
);
try {
$short_url = vars->{base} . vars->{sck}->shorten( params->{url} );
$stats_url = $short_url . "?s=1";
if ( vars->{sck}->generated_times ) {
$notice_message =
"Generated after " . vars->{sck}->generated_times . " tries.";
}
else {
$notice_message = "Already registered in database";
}
}
catch {
$error_message = App::Error->get_error_message_from(
$_,
{
MAX_GENERATED_TIMES => vars->{sck}->max_generated_times,
STATUS => vars->{sck}->status,
}
);
};
#display the right template
my $tpl = "index";
my $opt = {};
#bookmarklet (the window box)
if ( params->{b} ) {
$tpl = "bookmarklet.tt";
}
#The main form, call in ajax, return the shorten link
if ( params->{x} ) {
$tpl = "_shortenlinks.tt";
$opt = { layout => undef };
}
#The main template, add top10
if ( $tpl eq "index" ) {
$top10_members = vars->{sck}->top10();
}
return template(
$tpl,
{
url => params->{url},
short_url => $short_url,
stats_url => $stats_url,
top10_members => $top10_members,
notice_message => $notice_message,
error_message => $error_message,
bookmarklet => params->{b},
version => $Celogeek::SCK::VERSION,
},
$opt
);
};
#no params, display the normal website
get qr{^/(.*)$}x => sub {
my ($path) = splat;
return pass() if $path && !params->{s};
my $stats_info_ref;
my $error_message;
if ($path) {
try {
$stats_info_ref =
vars->{sck}->stats( $path, 'date_format' => '%c UTC' );
$stats_info_ref->{short_url} =
vars->{base} . $stats_info_ref->{path};
$stats_info_ref->{version} = $Celogeek::SCK::VERSION;
}
catch {
$error_message = App::Error->get_error_message_from(
$_,
{
MAX_GENERATED_TIMES => vars->{sck}->max_generated_times,
STATUS => vars->{sck}->status,
}
);
};
}
return template(
"index",
{
top10_members => vars->{sck}->top10(),
bookmarklet_installed => params->{ib},
version => $Celogeek::SCK::VERSION,
stats => $stats_info_ref,
error_message => $error_message,
}
);
};
1;