App-mojopaste

 view release on metacpan or  search on metacpan

script/mojopaste  view on Meta::CPAN

  my ($self, $app, $config) = @_;
  my $dir = $app->config('paste_dir');
  path($dir)->make_path unless -d $dir;
  $app->helper('paste.load_p' => sub { _load_p($dir, @_) });
  $app->helper('paste.save_p' => sub { _save_p($dir, @_) });
}

sub _load_p {
  my ($dir, $c, $id) = @_;
  my @res = ('', '');

  eval {
    die "Hacking attempt! paste_id=($id)" if !$id or $id =~ m!\W!;
    return Mojo::Promise->new->resolve(decode 'UTF-8', path($dir, $id)->slurp);
  } or do {
    return Mojo::Promise->new->reject($@ || 'Paste not found');
  };
}

sub _save_p {
  my ($dir, $c, $text) = @_;
  my $id  = substr Mojo::Util::md5_sum($$ . time . $ID++), 0, 12;
  my @res = ('', '');

  eval {
    path($dir, $id)->spurt(encode 'UTF-8', $text);
    return Mojo::Promise->new->resolve($id);
  } or do {
    return Mojo::Promise->new->reject($@ || 'Unknown error');
  };
}

package main;
use Mojolicious::Lite;

use Mojo::JSON 'true';

plugin 'config' if $ENV{MOJO_CONFIG};
app->config->{backend}   ||= $ENV{PASTE_BACKEND} || 'File';
app->config->{paste_dir} ||= $ENV{PASTE_DIR}     || 'paste';

app->defaults(
  brand_link    => app->config('brand_link') || $ENV{PASTE_BRAND_LINK} || 'index',
  brand_logo    => app->config('brand_logo') // $ENV{PASTE_BRAND_LOGO} // '/images/logo.png',
  brand_name    => app->config('brand_name') // $ENV{PASTE_BRAND_NAME} // 'Mojopaste',
  enable_charts => app->config('enable_charts') // $ENV{PASTE_ENABLE_CHARTS},
  embed         => 'description,graph,heading,nav',
  error         => '',
  paste         => '',
  placeholder   => 'Enter your text here and then press the "Save" button above.',
  title         => 'Mojopaste',
);

my $backend = app->config('backend');
plugin $backend =~ /::/ ? $backend : "App::mojopaste::Backend::$backend";

helper no_such_paste => sub {
  my ($c, $err) = @_;
  $c->app->log->debug("no_such_paste: $err");
  $c->stash($_ => 'Could not find paste') for qw(error heading title);
  $c->render(description => '', layout => 'mojopaste', status => 404);
};

helper set_title => sub {
  my ($c, $prefix, $suffix) = @_;
  my $brand_name = $c->stash('brand_name') || 'Mojopaste';
  $suffix = $suffix ? "$brand_name $suffix" : $brand_name;
  $prefix =~ s![\n\r]+! !g;
  $prefix =~ s!^\W+!!g;
  $prefix = substr $prefix, 0, 56 - length $suffix;
  return $c->stash(title => "$prefix - $suffix");
};

get(
  '/' => {layout => 'mojopaste'} => sub {
    my $c = shift;

    return $c->set_title("Create new paste") unless my $id = $c->param('edit');
    return $c->render_later->paste->load_p($id)->then(sub {
      return $c->no_such_paste('Could not find paste') unless my $paste = shift;
      $c->set_title(substr($paste, 0, 80), 'edit');
      $c->param(paste => $paste)->render;
    })->catch(sub { $c->no_such_paste(shift) });
  },
  'index'
);

post(
  '/' => {layout => 'mojopaste'},
  sub {
    my $c     = shift;
    my $paste = $c->param('paste') || '';

    return $c->render('index', placeholder => 'You neeed to enter some characters!', status => 400)
      unless $paste =~ /\w/;
    return $c->render_later->paste->save_p($paste)->then(sub {
      $c->redirect_to('show', paste_id => shift);
    })->catch(sub { $c->reply->exception(shift) });
  }
);

get(
  '/:paste_id',
  [format => ['html', 'txt']],
  {format => undef},
  sub {
    my $c      = shift;
    my $format = $c->stash('format') || '';

    $c->render_later->paste->load_p($c->stash('paste_id'))->then(sub {
      my $paste = shift;
      if (!$paste) {
        $c->no_such_paste('Could not find paste');
      }
      elsif ($c->param('raw') or $format eq 'txt') {
        $c->res->headers->content_type('text/plain; charset=utf-8');
        $c->render(text => $paste);
      }
      else {
        $c->set_title(substr($paste, 0, 80));
        $c->res->headers->header('X-Plain-Text-URL' => $c->url_for(format => 'txt')->userinfo(undef)->to_abs);



( run in 1.676 second using v1.01-cache-2.11-cpan-39bf76dae61 )