Made it upload images with a unique UUID each time. FULLY FUNCTIONAL APP (in-house)

This commit is contained in:
piratefinn 2016-08-15 16:11:13 +01:00
parent 69c39d7c5c
commit f2f6e9782c
2 changed files with 25 additions and 2 deletions

View file

@ -1,6 +1,7 @@
#!/usr/bin/env perl
use Mojolicious::Lite;
use Data::UUID;
# connect to database
use DBI;
@ -27,11 +28,23 @@ while (1) {
# setup route which receives data and returns to /
post '/' => sub {
my $self = shift;
# Fetch parameters to write to DB
my $user = $self->param('user');
my $company = $self->param('company');
my $currency = $self->param('currency');
my $file = $self->req->upload('file');
$insert->execute($user, $company, $currency, $file->filename);
# 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);
$self->render(text => 'It did not kaboom!');
};

View file

@ -1,5 +1,6 @@
#!/usr/bin/env perl
use Mojolicious::Lite;
use Mojo:Upload;
# /?user=sebastian&pass=secr3t
any '/' => sub {
@ -13,7 +14,16 @@ any '/' => sub {
use Devel::Dwarn;
Dwarn $c->req;
# Failed
$c->render(text => 'upload failed.');
$c->render(text => 'db entry data upload failed.');
};
# Uploading Image
post '/upload' => sub {
my $c = shift;
my $upload = Mojo::Upload->new;
say $upload->filename;
$upload->move_to('images/' . $upload->filename);
};
app->start;