Permalink
Browse files

add db. use base58.

  • Loading branch information...
1 parent b3953c4 commit 8daed8a1bb06525886ad5a69f0f8275fc37158f6 @bricas bricas committed May 9, 2012
View
4 Makefile.PL
@@ -16,7 +16,9 @@ requires 'Catalyst::Action::RenderView';
requires 'Moose';
requires 'namespace::autoclean';
requires 'Config::General';
-requires 'Data::UUID::Base64URLSafe';
+requires 'DBIx::Class';
+requires 'Catalyst::Model::DBIC::Schema';
+requires 'Encode::Base58';
requires 'Image::TextMode';
requires 'HTML::FillInForm';
View
11 etc/deploy_schema.pl
@@ -0,0 +1,11 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use lib 'lib';
+
+use SixteenColors::Paste;
+
+my $schema = SixteenColors::Paste->model( 'DB' )->schema;
+$schema->deploy;
View
26 lib/SixteenColors/Paste/Controller/Root.pm
@@ -4,7 +4,6 @@ use namespace::autoclean;
BEGIN { extends 'Catalyst::Controller' }
-use Data::UUID::Base64URLSafe ();
use Image::TextMode::Loader;
use Image::TextMode::Renderer::GD;
use HTML::FillInForm;
@@ -15,8 +14,8 @@ sub index :Path :Args(0) {
my ( $self, $c ) = @_;
if( lc( $c->req->method ) eq 'post' && $c->req->params->{ file } ) {
- my $ug = Data::UUID::Base64URLSafe->new;
- my $id = $ug->create_b64_urlsafe;
+ my $paste = $c->model( 'DB::Paste' )->create( {} );
+ my $id = $paste->url_fragment;
my $upload = $c->req->upload( 'file' );
my( $ext ) = $upload->basename =~ m{\.([^.]+)$};
@@ -36,20 +35,25 @@ sub index :Path :Args(0) {
sub instance :Chained('/') PathPart('') CaptureArgs(1) {
my ( $self, $c, $id ) = @_;
- my $dir = Path::Class::Dir->new( $c->path_to( "root/static/paste/" ) );
- my $file;
-
- while( $file = $dir->next ) {
- last if $file->basename =~ m{^$id\.};
- }
+ my $paste = $c->model( 'DB::Paste' )->find( { url_fragment => $id }, { key => 'paste_url_fragment' } );
- if( !$file ) {
+ if( !$paste ) {
$c->response->body( 'Page not found' );
$c->response->status(404);
return;
}
- $c->stash( id => $id, file => $file );
+ my $dir = Path::Class::Dir->new( $c->path_to( "root/static/paste/" ) );
+ my $file;
+ while( $file = $dir->next ) {
+ last if $file->basename =~ m{^$id\.};
+ }
+
+ $c->stash(
+ id => $id,
+ file => $file,
+ paste => $paste
+ );
}
sub view :Chained('instance') PathPart('') Args(0) {
View
28 lib/SixteenColors/Paste/Model/DB.pm
@@ -0,0 +1,28 @@
+package SixteenColors::Paste::Model::DB;
+
+use strict;
+use warnings;
+use parent 'Catalyst::Model::DBIC::Schema';
+
+__PACKAGE__->config( schema_class => 'SixteenColors::Paste::Schema' );
+
+=head1 NAME
+
+SixteenColors::Paste::Model::DB - Catalyst Model
+
+=head1 DESCRIPTION
+
+Catalyst Model.
+
+=head1 AUTHOR
+
+Sixteen Colors <contact@sixteencolors.net>
+
+=head1 LICENSE
+
+This library is free software. You can redistribute it and/or modify
+it under the same terms as Perl itself.
+
+=cut
+
+1;
View
10 lib/SixteenColors/Paste/Schema.pm
@@ -0,0 +1,10 @@
+package SixteenColors::Paste::Schema;
+
+use strict;
+use warnings;
+
+use parent 'DBIx::Class::Schema';
+
+__PACKAGE__->load_namespaces;
+
+1;
View
39 lib/SixteenColors/Paste/Schema/Result/Paste.pm
@@ -0,0 +1,39 @@
+package SixteenColors::Paste::Schema::Result::Paste;
+
+use strict;
+use warnings;
+
+use parent qw( DBIx::Class );
+
+use Encode::Base58 ();
+
+__PACKAGE__->load_components( qw( TimeStamp Core ) );
+__PACKAGE__->table( 'paste' );
+__PACKAGE__->add_columns(
+ id => {
+ data_type => 'bigint',
+ is_auto_increment => 1,
+ is_nullable => 0,
+ },
+ url_fragment => {
+ data_type => 'varchar',
+ size => 256,
+ is_nullable => 1,
+ },
+ ctime => {
+ data_type => 'timestamp',
+ default_value => \'CURRENT_TIMESTAMP',
+ set_on_create => 1,
+ },
+);
+__PACKAGE__->set_primary_key( qw( id ) );
+__PACKAGE__->add_unique_constraint( [ 'url_fragment' ] );
+
+sub insert {
+ my ( $self, @args ) = @_;
+ $self->next::method( @args );
+ $self->update( { url_fragment => Encode::Base58::encode_base58( $self->id ) } );
+ return $self;
+}
+
+1;
View
10 sixteencolors_paste.conf
@@ -1,3 +1,9 @@
-# rename this file to sixteencolors::paste.yml and put a ':' after 'name' if
-# you want to use YAML like in old versions of Catalyst
name SixteenColors::Paste
+<Model::DB>
+ <connect_info>
+ dsn dbi:SQLite:dbname=__path_to(paste.db)__
+ user undef
+ password undef
+ quote_names 1
+ </connect_info>
+</Model::DB>

0 comments on commit 8daed8a

Please sign in to comment.