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() { +