From 439f275d4ef43cde93c3bfcc2d3fff641b42202b Mon Sep 17 00:00:00 2001 From: plegall Date: Sun, 15 Oct 2023 13:11:10 +0200 Subject: [PATCH] fixes #860 detect duplicates during upload --- admin/configuration.php | 1 + admin/include/functions_upload.inc.php | 104 +++++++++++------- .../default/template/configuration_main.tpl | 11 ++ install/config.sql | 1 + install/db/170-database.php | 22 ++++ language/en_UK/admin.lang.php | 2 + language/fr_FR/admin.lang.php | 2 + 7 files changed, 106 insertions(+), 37 deletions(-) create mode 100644 install/db/170-database.php diff --git a/admin/configuration.php b/admin/configuration.php index ce0a98cc2..76cef29de 100644 --- a/admin/configuration.php +++ b/admin/configuration.php @@ -49,6 +49,7 @@ $main_checkboxes = array( 'history_guest', 'show_mobile_app_banner_in_gallery', 'show_mobile_app_banner_in_admin', + 'upload_detect_duplicate', ); $sizes_checkboxes = array( diff --git a/admin/include/functions_upload.inc.php b/admin/include/functions_upload.inc.php index 0a46f2dbb..e3c383f6c 100644 --- a/admin/include/functions_upload.inc.php +++ b/admin/include/functions_upload.inc.php @@ -141,10 +141,7 @@ function add_uploaded_file($source_filepath, $original_filename=null, $categorie // // 3) register in database - // TODO - // * check md5sum (already exists?) - - global $conf, $user; + global $conf, $user, $logger; if (!is_null($original_filename)) { @@ -160,6 +157,33 @@ function add_uploaded_file($source_filepath, $original_filename=null, $categorie $md5sum = md5_file($source_filepath); } + // we only try to detect duplicate on a new image, not when updating an existing image + if (!isset($image_id) and conf_get_param('upload_detect_duplicate', false)) + { + $query = ' +SELECT + id + FROM '. IMAGES_TABLE .' + WHERE md5sum = \''.$md5sum.'\' +;'; + $images_found = query2array($query); + + if (count($images_found) > 0) + { + $image_id = $images_found[0]['id']; + $logger->info('['.__FUNCTION__.'] image already exist #'.$image_id.', we delete the newly uploaded file : '.$source_filepath); + unlink($source_filepath); + + // if the destination category is already linked to this photo, no worry, + // associate_images_to_categories perfectly handles this case + add_uploaded_file_add_to_categories($image_id, $categories); + + // TODO should we update level? If yes, then we should invalidate_user_cache + + return $image_id; + } + } + $file_path = null; if (isset($image_id)) @@ -262,7 +286,6 @@ SELECT // pwg_representative file. $representative_ext = trigger_change('upload_file', null, $file_path); - global $logger; $logger->info("Handling " . (string)$file_path . " got " . (string)$representative_ext); // If it is set to either true (the file didn't need a @@ -360,6 +383,45 @@ SELECT pwg_activity('photo', $image_id, 'add'); } + add_uploaded_file_add_to_categories($image_id, $categories); + + // update metadata from the uploaded file (exif/iptc) + if ($conf['use_exif'] and !function_exists('exif_read_data')) + { + $conf['use_exif'] = false; + } + sync_metadata(array($image_id)); + + // cache a derivative + $query = ' +SELECT + id, + path, + representative_ext + FROM '.IMAGES_TABLE.' + WHERE id = '.$image_id.' +;'; + $image_infos = pwg_db_fetch_assoc(pwg_query($query)); + $src_image = new SrcImage($image_infos); + + set_make_full_url(); + // in case we are on uploadify.php, we have to replace the false path + $derivative_url = preg_replace('#admin/include/i#', 'i', DerivativeImage::url(IMG_MEDIUM, $src_image)); + unset_make_full_url(); + + $logger->info(__FUNCTION__.' : force cache generation, derivative_url = '.$derivative_url); + + fetchRemote($derivative_url, $dest); + + trigger_notify('loc_end_add_uploaded_file', $image_infos); + + return $image_id; +} + +function add_uploaded_file_add_to_categories($image_id, $categories) +{ + global $conf; + if (!isset($conf['lounge_active'])) { conf_update_param('lounge_active', false, true); @@ -387,42 +449,10 @@ SELECT } } - // update metadata from the uploaded file (exif/iptc) - if ($conf['use_exif'] and !function_exists('exif_read_data')) - { - $conf['use_exif'] = false; - } - sync_metadata(array($image_id)); - if (!$conf['lounge_active']) { invalidate_user_cache(); } - - // cache a derivative - $query = ' -SELECT - id, - path, - representative_ext - FROM '.IMAGES_TABLE.' - WHERE id = '.$image_id.' -;'; - $image_infos = pwg_db_fetch_assoc(pwg_query($query)); - $src_image = new SrcImage($image_infos); - - set_make_full_url(); - // in case we are on uploadify.php, we have to replace the false path - $derivative_url = preg_replace('#admin/include/i#', 'i', DerivativeImage::url(IMG_MEDIUM, $src_image)); - unset_make_full_url(); - - $logger->info(__FUNCTION__.' : force cache generation, derivative_url = '.$derivative_url); - - fetchRemote($derivative_url, $dest); - - trigger_notify('loc_end_add_uploaded_file', $image_infos); - - return $image_id; } function add_format($source_filepath, $format_ext, $format_of) diff --git a/admin/themes/default/template/configuration_main.tpl b/admin/themes/default/template/configuration_main.tpl index cd208323b..03d3ef656 100644 --- a/admin/themes/default/template/configuration_main.tpl +++ b/admin/themes/default/template/configuration_main.tpl @@ -242,6 +242,17 @@ jQuery("input[name='email_admin_on_new_user_filter']").change(function() { +
  • + + + +
  • + +
  • diff --git a/install/config.sql b/install/config.sql index 91511959c..862055d2d 100644 --- a/install/config.sql +++ b/install/config.sql @@ -77,3 +77,4 @@ INSERT INTO piwigo_config (param,value) VALUES ('show_mobile_app_banner_in_admin INSERT INTO piwigo_config (param,value) VALUES ('show_mobile_app_banner_in_gallery','false'); INSERT INTO piwigo_config (param,value) VALUES ('index_search_in_set_button','true'); INSERT INTO piwigo_config (param,value) VALUES ('index_search_in_set_action','true'); +INSERT INTO piwigo_config (param,value) VALUES ('upload_detect_duplicate','true'); diff --git a/install/db/170-database.php b/install/db/170-database.php new file mode 100644 index 000000000..5f0ca9c48 --- /dev/null +++ b/install/db/170-database.php @@ -0,0 +1,22 @@ + diff --git a/language/en_UK/admin.lang.php b/language/en_UK/admin.lang.php index 7ed6ad4a2..754fdf13d 100644 --- a/language/en_UK/admin.lang.php +++ b/language/en_UK/admin.lang.php @@ -1348,4 +1348,6 @@ $lang['Unlock it'] = 'Unlock it'; $lang['All admins'] = 'All admins'; $lang['Only admins in a specific group'] = 'Only admins in a specific group'; $lang['Activate button "%s"'] = 'Activate button "%s"'; +$lang['Detect and avoid duplicates during upload'] = 'Detect and avoid duplicates during upload'; +$lang['During upload, if Piwigo detects the photo already exists, associate the existing photo to the destination album, without duplicating file'] = 'During upload, if Piwigo detects the photo already exists, associate the existing photo to the destination album, without duplicating file'; // Leave this line empty diff --git a/language/fr_FR/admin.lang.php b/language/fr_FR/admin.lang.php index d517cefc0..83c0f538b 100644 --- a/language/fr_FR/admin.lang.php +++ b/language/fr_FR/admin.lang.php @@ -1348,4 +1348,6 @@ $lang['Unlock it'] = 'Le déverrouiller'; $lang['All admins'] = 'Tous les administrateurs'; $lang['Only admins in a specific group'] = 'Uniquement les administrations d\'un groupe spécifique'; $lang['Activate button "%s"'] = 'Afficher le bouton "%s"'; +$lang['Detect and avoid duplicates during upload'] = 'Détecter et éviter les doublons à l\'import'; +$lang['During upload, if Piwigo detects the photo already exists, associate the existing photo to the destination album, without duplicating file'] = 'Pendant l\'ajout de photo, si Piwigo détecte que la photo existe déjà, associer la photo existante à l\'album de destination, sans dupliquer le fichier.'; // Leave this line empty