Don't reupload duplicates (#48)

This commit is contained in:
Tobias Reich 2014-08-22 22:02:58 +02:00
parent b4eccb6215
commit e942c9c525

View File

@ -96,26 +96,53 @@ class Photo extends Module {
$id = str_replace('.', '', microtime(true)); $id = str_replace('.', '', microtime(true));
while(strlen($id)<14) $id .= 0; while(strlen($id)<14) $id .= 0;
# Set paths
$tmp_name = $file['tmp_name']; $tmp_name = $file['tmp_name'];
$photo_name = md5($id) . $extension; $photo_name = md5($id) . $extension;
$path = LYCHEE_UPLOADS_BIG . $photo_name; $path = LYCHEE_UPLOADS_BIG . $photo_name;
# Import if not uploaded via web # Calculate checksum
if (!is_uploaded_file($tmp_name)) { $checksum = sha1_file($tmp_name);
if (!@copy($tmp_name, $path)) {
Log::error($this->database, __METHOD__, __LINE__, 'Could not copy photo to uploads'); # Check if image exists based on checksum
exit('Error: Could not copy photo to uploads!'); if ($checksum===false) {
} else @unlink($tmp_name);
$checksum = '';
$exists = false;
} else { } else {
if (!@move_uploaded_file($tmp_name, $path)) {
Log::error($this->database, __METHOD__, __LINE__, 'Could not move photo to uploads'); $query = "SELECT id, url, thumbUrl FROM lychee_photos WHERE checksum = '$checksum' LIMIT 1;";
exit('Error: Could not move photo to uploads!'); $result = $this->database->query($query);
if ($result->num_rows===1) {
$result = $result->fetch_assoc();
$photo_name = $result['url'];
$path = LYCHEE_UPLOADS_BIG . $result['url'];
$path_thumb = $result['thumbUrl'];
$exists = true;
} else {
$exists = false;
} }
} }
# Calculate checksum if ($exists===false) {
$checksum = sha1_file($path);
if ($checksum===false) $checksum = ''; # Import if not uploaded via web
if (!is_uploaded_file($tmp_name)) {
if (!@copy($tmp_name, $path)) {
Log::error($this->database, __METHOD__, __LINE__, 'Could not copy photo to uploads');
exit('Error: Could not copy photo to uploads!');
} else @unlink($tmp_name);
} else {
if (!@move_uploaded_file($tmp_name, $path)) {
Log::error($this->database, __METHOD__, __LINE__, 'Could not move photo to uploads');
exit('Error: Could not move photo to uploads!');
}
}
}
# Read infos # Read infos
$info = $this->getInfo($path); $info = $this->getInfo($path);
@ -126,18 +153,25 @@ class Photo extends Module {
# Use description parameter if set # Use description parameter if set
if ($description==='') $description = $info['description']; if ($description==='') $description = $info['description'];
# Set orientation based on EXIF data if ($exists===false) {
if ($file['type']==='image/jpeg'&&isset($info['orientation'])&&$info['orientation']!==''&&isset($info['width'])&&isset($info['height'])) {
if (!$this->adjustFile($path, $info)) Log::notice($this->database, __METHOD__, __LINE__, 'Could not adjust photo (' . $info['title'] . ')');
}
# Set original date # Set orientation based on EXIF data
if ($info['takestamp']!=='') @touch($path, $info['takestamp']); if ($file['type']==='image/jpeg'&&isset($info['orientation'])&&$info['orientation']!==''&&isset($info['width'])&&isset($info['height'])) {
if (!$this->adjustFile($path, $info)) Log::notice($this->database, __METHOD__, __LINE__, 'Could not adjust photo (' . $info['title'] . ')');
}
# Set original date
if ($info['takestamp']!=='') @touch($path, $info['takestamp']);
# Create Thumb
if (!$this->createThumb($path, $photo_name)) {
Log::error($this->database, __METHOD__, __LINE__, 'Could not create thumbnail for photo');
exit('Error: Could not create thumbnail for photo!');
}
# Set thumb url
$path_thumb = md5($id) . '.jpeg';
# Create Thumb
if (!$this->createThumb($path, $photo_name)) {
Log::error($this->database, __METHOD__, __LINE__, 'Could not create thumbnail for photo');
exit('Error: Could not create thumbnail for photo!');
} }
# Save to DB # Save to DB
@ -159,7 +193,7 @@ class Photo extends Module {
'" . $info['shutter'] . "', '" . $info['shutter'] . "',
'" . $info['focal'] . "', '" . $info['focal'] . "',
'" . $info['takestamp'] . "', '" . $info['takestamp'] . "',
'" . md5($id) . ".jpeg', '" . $path_thumb . "',
'" . $albumID . "', '" . $albumID . "',
'" . $public . "', '" . $public . "',
'" . $star . "', '" . $star . "',