root/Mixxi/trunk/lib/Mixxi/Controller/URL.pm

Revision 2077 (checked in by miyagawa, 14 years ago)

redirect to /a/

Line 
1 package Mixxi::Controller::URL;
2
3 use strict;
4 use warnings;
5 use base 'Catalyst::Controller';
6
7 =head1 NAME
8
9 Mixxi::Controller::URL - Catalyst Controller
10
11 =head1 DESCRIPTION
12
13 Catalyst Controller.
14
15 =head1 METHODS
16
17 =cut
18
19
20 =head2 index
21
22 =cut
23
24 sub index : Private {
25     my ( $self, $c ) = @_;
26
27 }
28
29 sub create : Local {
30     my($self, $c) = @_;
31
32     my $url   = $c->req->param('url');
33     my $alias = $c->req->param('alias');
34
35     if ($alias && !$self->validate_alias($c, $alias)) {
36         $c->stash->{error} = 'alias is taken or invalid';
37         $c->stash->{template} = 'url/index.tt';
38         return;
39     }
40
41     my $rec = $c->model('DBIC::Url')->create({
42         url   => $url,
43         alias => $alias || undef,
44     });
45
46     $c->res->redirect($c->uri_for('/a/') . $rec->canon_path);
47 }
48
49 sub validate : Local {
50     my($self, $c) = @_;
51
52     my $alias = $c->req->param('alias');
53     if ($alias && !$self->validate_alias($c, $alias)) {
54         $c->stash->{json_data}->{error} = 'alias is taken or invalid';
55     } else {
56         $c->stash->{json_data}->{error} = 0;
57     }
58
59     $c->forward($c->view('JSON'));
60 }
61
62 my %reserved_alias = map { $_ => 1 } qw( url id qr );
63
64 sub validate_alias {
65     my($self, $c, $alias) = @_;
66     $alias =~ /^[a-zA-Z0-9\-\_]{2,16}$/ or return;
67     $alias =~ /^[0-9]+$/ and return;
68     $reserved_alias{$alias} and return;
69
70     my $rs = $c->model('DBIC::Url')->search(alias => $alias);
71     $rs->count and return;
72
73     return 1;
74 }
75
76 =head1 AUTHOR
77
78 Tatsuhiko Miyagawa
79
80 =head1 LICENSE
81
82 This library is free software, you can redistribute it and/or modify
83 it under the same terms as Perl itself.
84
85 =cut
86
87 1;
Note: See TracBrowser for help on using the browser.