-
Notifications
You must be signed in to change notification settings - Fork 0
/
urlshortener.pl
executable file
·133 lines (113 loc) · 3.3 KB
/
urlshortener.pl
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
#! /usr/bin/env perl
use lib 'lib';
use Mojolicious::Lite;
use MIME::Base64;
# Fonctions
# ---------------------------
# Enregistre l'url dans fichier urls
sub store_url{
my ($url_orig) = @_;
chomp $url_orig;
my $b64_url = encode_base64($url_orig, "");
my $short_url = "!".sprintf("%x",time);
open(STORAGE, ">> urls.txt") or die "erreur d'acces au stockage\n$!";
print STORAGE "$short_url|$b64_url\n";
close STORAGE;
return $short_url;
}
# Récupère l'url dans fichier urls
sub get_url{
my ($short_url) = @_;
my $self = shift;
open(FILE, "urls.txt") || die;
# Lecture non bloquante
flock(FILE, 4);
while(<FILE>){
if ($_ =~ /$short_url/){
my @url_line = split(/\|/,$_);
return decode_base64($url_line[1]);
}
}
close(FILE);
# Url non trouvee, retourne -1
return -1;
}
# Controllers
# ---------------------------
# Home
get '/' => sub {
my $self = shift;
return $self->render;
} => 'index';
# Post
post '/sendurl' => sub {
my $self = shift;
# Si c'est bien une URL
my $url = Mojo::URL->new($self->param('orig_url'));
if(!$url->is_abs){
return $self->redirect_to('index');
}
# Retoune url raccourcie
my $short_url = store_url($self->param('orig_url'));
# retourne les valeurs
return $self->render('confirm', shortened => $short_url, host => $self->req->url->base->host, port => $self->req->url->base->port);
};
# Urlshortener
get '/:shorturl' => ([shorturl => qr/\![a-f0-9]{8}/]) => sub {
my $self = shift;
my $redirect_url = get_url($self->param('shorturl'));
if($redirect_url == "-1"){
return $self->redirect_to('index');
}else{
return $self->redirect_to($redirect_url);
}
};
# Lancement
app->start();
# Vues
# ---------------------------
__DATA__
# Layout
@@ layouts/default.html.ep
<!doctype html><html>
<head><title><%= title %></title>
<link href="http://bootswatch.com/cosmo/bootstrap.min.css" media="screen" rel="stylesheet" />
</head>
<body><div class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<a class="brand" href="/">Url Shortener</a>
<div class="nav-collapse collapse" id="main-menu">
<ul class="nav" id="main-menu-left">
<li><a href="/">News</a></li>
</ul>
</div>
</div>
</div>
</div><div id="content"><%= content %></div></body>
</html>
@@ index.html.ep
% layout 'default';
% title 'Acceuil';
<%= form_for sendurl => (method => 'post') => begin %>
<p>URL :
<%= text_field 'orig_url' %>
<%= submit_button 'Raccourcir' %>
</p>
<% end %>
@@ confirm.html.ep
% layout 'default';
% title 'Confirmation';
<h1><a href="/">shorturl.local</a></h1>
<p>Votre adresse a ete enregistree</p><br>
<% if ($port == 80) { %>
<p>URL courte : <a href="http://<%=$host%>/<%=$shortened%>">http://<%=$host%>/<%=$shortened%></a></p><br>
<%} else { %>
<p>URL courte : <a href="http://<%=$host%>:<%=$port%>/<%=$shortened%>">http://<%=$host%>:<%=$port%>/<%=$shortened%></a></p><br>
<% } %>
<a href="/">retour</a>