Skip to content

Commit

Permalink
Better duplicate detection
Browse files Browse the repository at this point in the history
Tells users which bench a duplicate image is attached to

Fixes #301
  • Loading branch information
edent committed Apr 9, 2023
1 parent d5dc6b8 commit 768203d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 28 deletions.
17 changes: 4 additions & 13 deletions www/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -628,10 +628,6 @@ function save_image($file, $media_type, $benchID, $userID) {
$filename = $file['name'];
$file = $file['tmp_name'];

if (duplicate_file($file)) {
return "<h3>Duplicate image: {$filename}</h3>";
}

// Check to see if this has the right EXIF tags for a photosphere
if (is_photosphere($file)) {
$media_type = "360";
Expand Down Expand Up @@ -674,15 +670,10 @@ function save_image($file, $media_type, $benchID, $userID) {
}
}

function duplicate_file($filename) {
$sha1 = sha1_file($filename);
$photo_full_path = get_path_from_hash($sha1, true);

// Does this photo already exit?
if(file_exists($photo_full_path)){
return true;
}
return false;
function duplicate_file( $filename ) {
$sha1 = sha1_file( $filename );
$benchID = get_bench_from_sha1( $sha1 );
return $benchID;
}

function get_image_cache($sha1, $size=IMAGE_DEFAULT_SIZE) {
Expand Down
21 changes: 21 additions & 0 deletions www/mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -1683,3 +1683,24 @@ function get_tags_from_bench($benchID) {
$get_tags->close();
return $tags;
}

function get_bench_from_sha1( $sha1 ) {
global $mysqli;
$get_bench = $mysqli->prepare(
"SELECT `benchID`
FROM `media`
WHERE `sha1` = ?");

$get_bench->bind_param('s', $sha1);

$get_bench->execute();
$get_bench->bind_result($benchID);

$id = 0;
while($get_bench->fetch()) {
$id = $benchID;
}

$get_bench->close();
return $id;
}
36 changes: 21 additions & 15 deletions www/upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,31 @@
$tags = null;
}


if ($_FILES['userfile1']['tmp_name'])
{ // Has a photo been posted?
$filename = $_FILES['userfile1']['tmp_name'];
$sha1 = sha1_file ($filename);

// For tweeting
$domain = $_SERVER['SERVER_NAME'];
$mediaURLs = array();
$mediaURLs[] = "https://{$domain}/image/{$sha1}/1024";

if (duplicate_file($filename))
{
$error_filename = $_FILES['userfile1']['name'];
$error_message .= "{$error_filename} already exists in the database.<br /><a href=\"/add\">Please reload this page and try a different photo</a>";
} else {
{
// Check for duplicates
$duplicates = false;
foreach( $_FILES as $file ) {
$duplicate = duplicate_file( $file['tmp_name'] );
if ( $duplicate > 0 )
{
$error_filename = htmlspecialchars( $file['name'] );
$error_message .= "{$error_filename} is already attached to <a href=\"/bench/{$duplicate}\">Bench #{$duplicate}</a>.<br /><a href=\"/add\">Please reload this page and try a different photo</a><br>";
$duplicates = true;
}
}

if ( !$duplicates ) {
// Does the first file have a GPS location?
$filename = $_FILES['userfile1']['tmp_name'];
$location = get_image_location($filename);

// For tweeting
$sha1 = sha1_file ($filename);
$domain = $_SERVER['SERVER_NAME'];
$mediaURLs = array();
$mediaURLs[] = "https://{$domain}/image/{$sha1}/1024";

// If there is a GPS tag on the photo
if (false != $location)
{
Expand Down

0 comments on commit 768203d

Please sign in to comment.