2016-08-16 17:44:24 +01:00
#!/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;
2016-09-01 13:30:08 +01:00
print "Make the table!";
2016-08-16 17:44:24 +01:00
}
# setup route which receives data and returns to /
post '/' => sub {
my $self = shift;
2016-09-01 13:30:08 +01:00
# Fetch parameters to write to DB
my $key = $self->param('key');
# This will include an if function to see if key matches
# unless ($key eq $config->{key}) {
# print "key does not match!";
# }
2016-08-16 17:44:24 +01:00
my $username = $self->param('username');
my $company = $self->param('company');
my $currency = $self->param('currency');
my $file = $self->req->upload('file');
2016-09-01 13:30:08 +01:00
# Get image type and check extension
2016-08-16 17:44:24 +01:00
my $headers = $file->headers->content_type;
2016-09-01 13:30:08 +01:00
# Is content type wrong?
2016-08-16 17:44:24 +01:00
if ($headers ne 'image/jpeg') {
print "Upload fail. Content type is wrong.\n";
};
2016-09-01 13:30:08 +01:00
# Rewrite header data
2016-08-16 17:44:24 +01:00
my $ext = '.jpg';
my $uuid = Data::UUID->new->create_str;
my $filename = $uuid . $ext;
2016-09-01 13:30:08 +01:00
# send photo to image folder on server
2016-08-16 17:44:24 +01:00
$file->move_to('images/' . $filename);
2016-09-01 13:30:08 +01:00
# send data to foodloop db
2016-08-16 17:44:24 +01:00
$insert->execute($username, $company, $currency, $filename);
$self->render(text => 'It did not kaboom!');
};
app->start;