From f2f6e9782c4a59790766770a2c14f30403ade826 Mon Sep 17 00:00:00 2001 From: piratefinn Date: Mon, 15 Aug 2016 16:11:13 +0100 Subject: [PATCH] Made it upload images with a unique UUID each time. FULLY FUNCTIONAL APP (in-house) --- senddatatodb.pl | 15 ++++++++++++++- uploadimage.pl | 12 +++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/senddatatodb.pl b/senddatatodb.pl index ea1ecb5..fe880d1 100644 --- a/senddatatodb.pl +++ b/senddatatodb.pl @@ -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!'); }; diff --git a/uploadimage.pl b/uploadimage.pl index 9a3fc67..cc57a85 100644 --- a/uploadimage.pl +++ b/uploadimage.pl @@ -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;