2016-08-12 16:10:42 +00:00
|
|
|
#!/usr/bin/env perl
|
|
|
|
|
|
|
|
use Mojolicious::Lite;
|
2016-08-15 15:11:13 +00:00
|
|
|
use Data::UUID;
|
2016-08-12 16:10:42 +00:00
|
|
|
|
|
|
|
# connect to database
|
|
|
|
use DBI;
|
2016-08-15 16:44:29 +00:00
|
|
|
my $config = plugin Config => {file => 'myapp.conf'};
|
|
|
|
|
|
|
|
use Devel::Dwarn; Dwarn $config;
|
|
|
|
|
|
|
|
my $dbh = DBI->connect($config->{dsn}) or die "Could not connect";
|
2016-08-12 16:10:42 +00:00
|
|
|
|
|
|
|
# shortcut for use in template
|
|
|
|
helper db => sub { $dbh };
|
|
|
|
|
|
|
|
# setup base route
|
2016-08-12 17:24:05 +00:00
|
|
|
#any '/' => 'index';
|
2016-08-12 16:10:42 +00:00
|
|
|
|
|
|
|
my $insert;
|
|
|
|
while (1) {
|
|
|
|
# create insert statement
|
2016-08-12 17:24:05 +00:00
|
|
|
$insert = eval { $dbh->prepare('INSERT INTO foodloop (user, company, currency, filename) VALUES (?,?,?,?)') };
|
2016-08-12 16:10:42 +00:00
|
|
|
# break out of loop if statement prepared
|
|
|
|
last if $insert;
|
|
|
|
|
|
|
|
# if statement didn't prepare, assume its because the table doesn't exist
|
|
|
|
warn "Creating table 'foodloop'\n";
|
|
|
|
$dbh->do('CREATE TABLE foodloop (user varchar(255), company varchar(255), currency int, filename varchar(255));');
|
|
|
|
}
|
|
|
|
|
|
|
|
# setup route which receives data and returns to /
|
|
|
|
post '/' => sub {
|
|
|
|
my $self = shift;
|
2016-08-15 15:11:13 +00:00
|
|
|
# Fetch parameters to write to DB
|
2016-08-12 16:10:42 +00:00
|
|
|
my $user = $self->param('user');
|
|
|
|
my $company = $self->param('company');
|
2016-08-12 17:24:05 +00:00
|
|
|
my $currency = $self->param('currency');
|
|
|
|
my $file = $self->req->upload('file');
|
2016-08-15 15:11:13 +00:00
|
|
|
# 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($user, $company, $currency, $filename);
|
2016-08-12 16:10:42 +00:00
|
|
|
$self->render(text => 'It did not kaboom!');
|
|
|
|
};
|
|
|
|
|
|
|
|
app->start;
|