From dc5ee0f8bf073aebc719dd5e0af5a5bdb0b05691 Mon Sep 17 00:00:00 2001 From: piratefinn Date: Tue, 16 Aug 2016 17:44:24 +0100 Subject: [PATCH] Added in the ".example" fully functional postgres support --- senddatatodb.pl.example | 62 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 senddatatodb.pl.example diff --git a/senddatatodb.pl.example b/senddatatodb.pl.example new file mode 100644 index 0000000..898d55f --- /dev/null +++ b/senddatatodb.pl.example @@ -0,0 +1,62 @@ +#!/usr/bin/env perl +# NOT READY FOR PRODUCTION + +use Mojolicious::Lite; +use Data::UUID; + +# connect to database +use DBI; + +my $config = plugin Config => {file => 'myapp.conf'}; +my $dbh = DBI->connect($config->{dsn},$config->{user},$config->{pass}) or die "Could not connect"; +use Devel::Dwarn; Dwarn $config; + +# shortcut for use in template +helper db => sub { $dbh }; + +# setup base route +#any '/' => 'index'; + +my $insert; +while (1) { + print "Checking if table exists"; + # create insert statement + $insert = eval { $dbh->prepare('INSERT INTO foodloop (username, company, currency, filename) VALUES (?,?,?,?)') }; + # break out of loop if statement prepared + last if $insert; + print "Creating new Table"; + # if statement didn't prepare, assume its because the table doesn't exist +# warn "Creating table 'foodloop'\n"; +# $dbh->do('CREATE TABLE foodloop ( +# username varchar(255), +# company varchar(255), +# currency integer, +#filename varchar(255) +#);' +); +} + +# setup route which receives data and returns to / +post '/' => sub { + my $self = shift; + # Fetch parameters to write to DB + my $username = $self->param('username'); + my $company = $self->param('company'); + my $currency = $self->param('currency'); + my $file = $self->req->upload('file'); + # Get image type and check extension + my $headers = $file->headers->content_type; + # Is content type wrong? + if ($headers ne 'image/jpeg') { + print "Upload fail. Content type is wrong.\n"; + }; + # Rewrite header data + my $ext = '.jpg'; + my $uuid = Data::UUID->new->create_str; + my $filename = $uuid . $ext; + $file->move_to('images/' . $filename); + $insert->execute($username, $company, $currency, $filename); + $self->render(text => 'It did not kaboom!'); +}; + +app->start;