|
|
|
@ -6,57 +6,26 @@ use utf8;
|
|
|
|
|
|
|
|
|
|
use Mojo::Base 'Mojolicious::Controller'; |
|
|
|
|
|
|
|
|
|
use File::Slurp qw(read_file write_file); |
|
|
|
|
use Mojo::URL; |
|
|
|
|
|
|
|
|
|
sub _gen_id { |
|
|
|
|
sub _gen_pageid { |
|
|
|
|
my ($self) = @_; |
|
|
|
|
my $url = $self->req->param('pageid') || |
|
|
|
|
$self->req->headers->referrer; |
|
|
|
|
my $maxlen = 64; |
|
|
|
|
|
|
|
|
|
return unless $url; |
|
|
|
|
my $id = Mojo::URL->new($url)->path; |
|
|
|
|
my $pageid = Mojo::URL->new($url)->path; |
|
|
|
|
|
|
|
|
|
$id =~ s{^/+}{}o; |
|
|
|
|
$id =~ s{/+$}{}o; |
|
|
|
|
$id =~ y{/.}{-}s; |
|
|
|
|
$id =~ s<\.[a-z0-9]{2,4}$><>io; |
|
|
|
|
$id = substr($id, -$maxlen, $maxlen); |
|
|
|
|
$self->app->log->debug("comments id: $id -- $url"); |
|
|
|
|
$pageid =~ s{^/+}{}o; |
|
|
|
|
$pageid =~ s{/+$}{}o; |
|
|
|
|
$pageid =~ y{/.}{-}s; |
|
|
|
|
$pageid =~ s<\.[a-z0-9]{2,4}$><>io; |
|
|
|
|
$pageid = substr($pageid, -$maxlen, $maxlen); |
|
|
|
|
$self->app->log->debug("comments id: $pageid -- $url"); |
|
|
|
|
|
|
|
|
|
return $id; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
sub _comments_load { |
|
|
|
|
my ($self) = @_; |
|
|
|
|
|
|
|
|
|
my $id = $self->_gen_id |
|
|
|
|
or die("can't get id\n"); |
|
|
|
|
my $path = $self->app->home->rel_file("data/comments/$id.json"); |
|
|
|
|
return [] unless -f $path; |
|
|
|
|
|
|
|
|
|
open my $FH, '<', $path |
|
|
|
|
or die("open (r): $path -- $!\n"); |
|
|
|
|
local $/ = undef; |
|
|
|
|
my $data = <$FH>; |
|
|
|
|
close $FH; |
|
|
|
|
|
|
|
|
|
return $self->app->json->decode($data); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
sub _comments_save { |
|
|
|
|
my ($self, $data) = @_; |
|
|
|
|
|
|
|
|
|
my $id = $self->_gen_id |
|
|
|
|
or die("can't get id\n"); |
|
|
|
|
my $path = $self->app->home->rel_file("data/comments/$id.json"); |
|
|
|
|
|
|
|
|
|
open my $FH, '>', $path |
|
|
|
|
or die("open (w): $path -- $!\n"); |
|
|
|
|
print $FH $self->app->json->encode($data); |
|
|
|
|
close $FH; |
|
|
|
|
|
|
|
|
|
return 1; |
|
|
|
|
return $pageid; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
sub add { |
|
|
|
@ -65,13 +34,26 @@ sub add {
|
|
|
|
|
eval { |
|
|
|
|
my $text = $self->req->param('text') |
|
|
|
|
or die("empty comment\n"); |
|
|
|
|
my $comments = $self->_comments_load(); |
|
|
|
|
my $comment = { |
|
|
|
|
my $pageid = $self->_gen_pageid() |
|
|
|
|
or die("can't get id\n"); |
|
|
|
|
|
|
|
|
|
my %opts = (binmode => ':bytes'); |
|
|
|
|
my $comments = []; |
|
|
|
|
my $path = $self->app->home->rel_file("data/comments/$pageid.json"); |
|
|
|
|
if (-f $path) { |
|
|
|
|
my $json = read_file($path, %opts); |
|
|
|
|
$comments = $self->app->json->decode($json); |
|
|
|
|
} |
|
|
|
|
push @{ $comments }, { |
|
|
|
|
text => $text, time => time(), |
|
|
|
|
user => $self->session('username') || 'anonymous', |
|
|
|
|
}; |
|
|
|
|
push @{ $comments }, $comment; |
|
|
|
|
$self->_comments_save($comments); |
|
|
|
|
write_file($path, \%opts, $self->app->json->encode($comments)); |
|
|
|
|
|
|
|
|
|
$path = $self->app->home->rel_file("data/comments/$pageid.html"); |
|
|
|
|
$self->stash({comments => $comments}); |
|
|
|
|
write_file($path, {binmode => ':utf8'}, $self->render_to_string(template => 'comments/list')); |
|
|
|
|
|
|
|
|
|
$self->render(text => 'OK'); |
|
|
|
|
} or do { |
|
|
|
|
chomp $@; |
|
|
|
@ -88,16 +70,15 @@ sub get {
|
|
|
|
|
my ($self) = @_; |
|
|
|
|
|
|
|
|
|
eval { |
|
|
|
|
my $comments = $self->_comments_load(); |
|
|
|
|
die("can't load comments\n") |
|
|
|
|
unless ref($comments) eq 'ARRAY'; |
|
|
|
|
if (my $count = scalar @{ $comments }) { |
|
|
|
|
$self->app->log->debug("loaded $count comments"); |
|
|
|
|
$self->stash({comments => $comments}); |
|
|
|
|
$self->render(template => 'comments/list'); |
|
|
|
|
my $pageid = $self->_gen_pageid() |
|
|
|
|
or die("can't get id\n"); |
|
|
|
|
my $path = $self->app->home->rel_file("data/comments/$pageid.html"); |
|
|
|
|
if (-f $path) { |
|
|
|
|
my $comments = read_file($path, binmode => ':utf8'); |
|
|
|
|
$self->render(text => $comments); |
|
|
|
|
} else { |
|
|
|
|
$self->render(template => 'comments/none'); |
|
|
|
|
}; |
|
|
|
|
} 1; |
|
|
|
|
} or do { |
|
|
|
|
chomp $@; |
|
|
|
|
$self->app->log->error($@); |
|
|
|
|