From 0a8ee2e4a1d7332c5c0f0ae02a1306289e385051 Mon Sep 17 00:00:00 2001 From: Aleisha Amohia Date: Wed, 11 Jan 2017 00:40:31 +0000 Subject: [PATCH] Bug 17181: Check for duplicate image names when uploading image to patron card creator This patch adds a check for duplicates before uploading the image. To test: 1) Go to Tools -> Patron card creator -> Manage images 2) If you haven't already, upload an image 3) Try to upload another image with the same image name 4) Notice the first image is replaced with the second image, with no warning. 5) Apply patch and refresh page 6) Try to upload an image with the same image name again 7) Notice you are now warned about a duplicate image name. 8) Check that uploading an image with a unique name still works. Sponsored-by: Catalyst IT Signed-off-by: Claire Gravely --- .../prog/en/includes/patroncards-errors.inc | 3 +- patroncards/image-manage.pl | 87 ++++++++++++++-------- 2 files changed, 57 insertions(+), 33 deletions(-) diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/patroncards-errors.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/patroncards-errors.inc index 4d55f48..485c825 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/patroncards-errors.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/patroncards-errors.inc @@ -29,6 +29,8 @@ Image exceeds 500KB. Please resize and import again. [% ELSIF ( error == 303 ) %] The database image quota currently only allows a maximum of [% image_limit %] images to be stored at any one time. Please delete one or more images to free up quota space. + [% ELSIF ( error == 304 ) %] + An image with the name '[% IMAGE_NAME %]' already exists. [% ELSIF ( error == 401 ) %] An error has occurred and the item(s) was not added to batch [% batch_id %]. Please have your system administrator check the error log for details. [% ELSIF ( error == 402 ) %] @@ -39,7 +41,6 @@ An error has occurred and batch [% batch_id %] was not deleted. Please have your system administrator check the error log for details. [% ELSIF ( error == 405 ) %] An error has occurred and batch [% batch_id %] not fully de-duplicated. - [% ELSE %] [% END %]

diff --git a/patroncards/image-manage.pl b/patroncards/image-manage.pl index 78f0463..6f8e45f 100755 --- a/patroncards/image-manage.pl +++ b/patroncards/image-manage.pl @@ -47,23 +47,31 @@ my $image_limit = C4::Context->preference('ImageLimit') || ''; my $errstr = ''; # NOTE: For error codes see error-messages.inc if ($op eq 'upload') { - if (!$upload_file) { - warn sprintf('An error occurred while attempting to upload file %s.', $source_file); - $errstr = 301; - $template->param( - IMPORT_SUCCESSFUL => 0, - SOURCE_FILE => $source_file, - IMAGE_NAME => $image_name, - TABLE => $table, - error => $errstr, - ); + # Checking for duplicate image name + my $duplicate; + my $dbh = C4::Context->dbh; + my $query = "SELECT image_name FROM creator_images"; + my $sth = $dbh->prepare($query); + $sth->execute(); + my $image_names = $sth->fetchall_arrayref; + foreach my $img ( @{$image_names} ) { + if ( $img->[0] eq $image_name ) { + $duplicate = 1; + warn sprintf('Image name already exists.'); + $errstr = 304; + $template->param( + IMPORT_SUCCESSFUL => 0, + SOURCE_FILE => $source_file, + IMAGE_NAME => $image_name, + TABLE => $table, + error => $errstr, + ); + } } - else { - my $image = Graphics::Magick->new; - eval{$image->Read($cgi->tmpFileName($file_name));}; - if ($@) { - warn sprintf('An error occurred while creating the image object: %s',$@); - $errstr = 202; + unless ($duplicate) { + if (!$upload_file) { + warn sprintf('An error occurred while attempting to upload file %s.', $source_file); + $errstr = 301; $template->param( IMPORT_SUCCESSFUL => 0, SOURCE_FILE => $source_file, @@ -73,31 +81,46 @@ if ($op eq 'upload') { ); } else { - my $errstr = ''; - my $size = $image->Get('filesize'); - $errstr = 302 if $size > 500000; - $image->Set(magick => 'png'); # convert all images to png as this is a lossless format which is important for resizing operations later on - my $err = put_image($image_name, $image->ImageToBlob()) || '0'; - $errstr = 101 if $err == 1; - $errstr = 303 if $err == 202; - if ($errstr) { + my $image = Graphics::Magick->new; + eval{$image->Read($cgi->tmpFileName($file_name));}; + if ($@) { + warn sprintf('An error occurred while creating the image object: %s',$@); + $errstr = 202; $template->param( IMPORT_SUCCESSFUL => 0, SOURCE_FILE => $source_file, IMAGE_NAME => $image_name, TABLE => $table, error => $errstr, - image_limit => $image_limit, ); } else { - $table = html_table($display_columns->{'image'}, get_image(undef, "image_id, image_name")); # refresh table data after successfully performing save operation - $template->param( - IMPORT_SUCCESSFUL => 1, - SOURCE_FILE => $source_file, - IMAGE_NAME => $image_name, - TABLE => $table, - ); + my $errstr = ''; + my $size = $image->Get('filesize'); + $errstr = 302 if $size > 500000; + $image->Set(magick => 'png'); # convert all images to png as this is a lossless format which is important for resizing operations later on + my $err = put_image($image_name, $image->ImageToBlob()) || '0'; + $errstr = 101 if $err == 1; + $errstr = 303 if $err == 202; + if ($errstr) { + $template->param( + IMPORT_SUCCESSFUL => 0, + SOURCE_FILE => $source_file, + IMAGE_NAME => $image_name, + TABLE => $table, + error => $errstr, + image_limit => $image_limit, + ); + } + else { + $table = html_table($display_columns->{'image'}, get_image(undef, "image_id, image_name")); # refresh table data after successfully performing save operation + $template->param( + IMPORT_SUCCESSFUL => 1, + SOURCE_FILE => $source_file, + IMAGE_NAME => $image_name, + TABLE => $table, + ); + } } } } -- 2.1.4