fixing the upload fail issue
Sometimes during upload it would fail, but leave the empty webp, so when you tried again it would say it was already uploaded
This commit is contained in:
@ -3,4 +3,4 @@ if (!defined('Such_a_good_girl')) {
|
||||
die("Direct access not allowed.");
|
||||
}
|
||||
|
||||
define('PTD_VERSION', '3.4.0');
|
||||
define('PTD_VERSION', '3.4.1');
|
@ -37,111 +37,110 @@ function parse_manual_date(string $manualDateStr)
|
||||
return $dt;
|
||||
}
|
||||
|
||||
$exifDate = extract_exif_date($tmpFile);
|
||||
$fullPath = null;
|
||||
$previewPath = null;
|
||||
$jsonPath = null;
|
||||
|
||||
if (!$exifDate) {
|
||||
if (!empty($_POST['manual_date'])) {
|
||||
$manualDate = parse_manual_date($_POST['manual_date']);
|
||||
if ($manualDate === null) {
|
||||
http_response_code(400);
|
||||
exit('Invalid manual_date format');
|
||||
try {
|
||||
$exifDate = extract_exif_date($tmpFile);
|
||||
|
||||
if (!$exifDate) {
|
||||
if (!empty($_POST['manual_date'])) {
|
||||
$manualDate = parse_manual_date($_POST['manual_date']);
|
||||
if ($manualDate === null) {
|
||||
throw new Exception('Invalid manual_date format', 400);
|
||||
}
|
||||
$exifDate = $manualDate;
|
||||
} else {
|
||||
throw new Exception('Could not determine a valid date from EXIF', 400);
|
||||
}
|
||||
$exifDate = $manualDate;
|
||||
} else {
|
||||
http_response_code(400);
|
||||
exit('Could not determine a valid date from EXIF');
|
||||
}
|
||||
}
|
||||
|
||||
$serverTime = new DateTime('now', new DateTimeZone($config['timezone'] ?? 'America/Los_Angeles'));
|
||||
$exifDiff = abs($serverTime->getTimestamp() - $exifDate->getTimestamp());
|
||||
$serverTime = new DateTime('now', new DateTimeZone($config['timezone'] ?? 'America/Los_Angeles'));
|
||||
$exifDiff = abs($serverTime->getTimestamp() - $exifDate->getTimestamp());
|
||||
|
||||
if ($exifDiff < 60) {
|
||||
if (!empty($_POST['manual_date'])) {
|
||||
$manualDate = parse_manual_date($_POST['manual_date']);
|
||||
if ($manualDate !== null) {
|
||||
$manualDiff = abs($serverTime->getTimestamp() - $manualDate->getTimestamp());
|
||||
if ($manualDiff >= 60) {
|
||||
$exifDate = $manualDate;
|
||||
if ($exifDiff < 60) {
|
||||
if (!empty($_POST['manual_date'])) {
|
||||
$manualDate = parse_manual_date($_POST['manual_date']);
|
||||
if ($manualDate !== null) {
|
||||
$manualDiff = abs($serverTime->getTimestamp() - $manualDate->getTimestamp());
|
||||
if ($manualDiff >= 60) {
|
||||
$exifDate = $manualDate;
|
||||
} else {
|
||||
throw new Exception('Upload rejected: Both EXIF and manual dates are too close to the current server time.', 400);
|
||||
}
|
||||
} else {
|
||||
http_response_code(400);
|
||||
exit('Upload rejected: Both EXIF and manual "taken" dates are too close to the current server time.');
|
||||
throw new Exception('Invalid manual_date format', 400);
|
||||
}
|
||||
} else {
|
||||
http_response_code(400);
|
||||
exit('Invalid manual_date format');
|
||||
throw new Exception('Upload rejected: The photo\'s EXIF "taken" date is too close to the current server time. If this is incorrect, try entering a manual date.', 400);
|
||||
}
|
||||
} else {
|
||||
http_response_code(400);
|
||||
exit('Upload rejected: The photo\'s EXIF "taken" date is too close to the current server time. If this is incorrect, try entering a manual date.');
|
||||
}
|
||||
}
|
||||
|
||||
$year = $exifDate->format('Y');
|
||||
$month = $exifDate->format('m');
|
||||
$timestamp = $exifDate->format('His');
|
||||
$year = $exifDate->format('Y');
|
||||
$month = $exifDate->format('m');
|
||||
$timestamp = $exifDate->format('His');
|
||||
|
||||
$baseName = pathinfo($origName, PATHINFO_FILENAME);
|
||||
if (preg_match('/^(IMG_\d{4})/', $baseName, $matches)) {
|
||||
$baseName = $matches[1];
|
||||
}
|
||||
$cleanBase = sanitize_filename($baseName);
|
||||
$filename = $cleanBase . "_$timestamp.webp";
|
||||
$baseName = pathinfo($origName, PATHINFO_FILENAME);
|
||||
if (preg_match('/^(IMG_\d{4})/', $baseName, $matches)) {
|
||||
$baseName = $matches[1];
|
||||
}
|
||||
$cleanBase = sanitize_filename($baseName);
|
||||
$filename = $cleanBase . "_$timestamp.webp";
|
||||
|
||||
$root = $_SERVER['DOCUMENT_ROOT'];
|
||||
$fullsDir = "$root/images/$year/$month/fulls/";
|
||||
$previewDir = "$root/images/$year/$month/previews/";
|
||||
$fullPath = $fullsDir . $filename;
|
||||
$previewPath = $previewDir . $filename;
|
||||
$root = $_SERVER['DOCUMENT_ROOT'];
|
||||
$fullsDir = "$root/images/$year/$month/fulls/";
|
||||
$previewDir = "$root/images/$year/$month/previews/";
|
||||
$fullPath = $fullsDir . $filename;
|
||||
$previewPath = $previewDir . $filename;
|
||||
$jsonPath = $fullsDir . pathinfo($filename, PATHINFO_FILENAME) . '.json';
|
||||
|
||||
if (file_exists($fullPath)) {
|
||||
http_response_code(409);
|
||||
exit('Upload rejected: This image (or a version of it) was already uploaded.');
|
||||
}
|
||||
if (file_exists($fullPath)) {
|
||||
throw new Exception('Upload rejected: This image (or a version of it) was already uploaded.', 409);
|
||||
}
|
||||
|
||||
foreach ([$fullsDir, $previewDir] as $dir) {
|
||||
foreach ([$fullsDir, $previewDir] as $dir) {
|
||||
if (!is_dir($dir)) {
|
||||
if (!mkdir($dir, 0775, true)) {
|
||||
error_log("Failed to create directory: $dir");
|
||||
http_response_code(500);
|
||||
exit("Failed to create directory: $dir");
|
||||
throw new Exception("Failed to create directory: $dir", 500);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$img = load_image($tmpFile, $ext);
|
||||
if (!$img) {
|
||||
http_response_code(400);
|
||||
exit('Image loading failed');
|
||||
}
|
||||
$img = load_image($tmpFile, $ext);
|
||||
if (!$img) {
|
||||
throw new Exception('Image loading failed', 400);
|
||||
}
|
||||
|
||||
$exif = @exif_read_data($tmpFile);
|
||||
if ($exif !== false) {
|
||||
$img = fix_orientation($img, $exif);
|
||||
}
|
||||
$exif = @exif_read_data($tmpFile);
|
||||
if ($exif !== false) {
|
||||
$img = fix_orientation($img, $exif);
|
||||
}
|
||||
|
||||
if (!imagewebp($img, $fullPath)) {
|
||||
error_log("Failed to save full image to $fullPath");
|
||||
http_response_code(500);
|
||||
exit('Failed to save full image');
|
||||
}
|
||||
if (!imagewebp($img, $fullPath)) {
|
||||
throw new Exception('Failed to save full image', 500);
|
||||
}
|
||||
|
||||
$thumb = imagescale($img, 500);
|
||||
if (!imagewebp($thumb, $previewPath)) {
|
||||
error_log("Failed to save preview image to $previewPath");
|
||||
http_response_code(500);
|
||||
exit('Failed to save preview image');
|
||||
}
|
||||
$thumb = imagescale($img, 500);
|
||||
if (!imagewebp($thumb, $previewPath)) {
|
||||
throw new Exception('Failed to save preview image', 500);
|
||||
}
|
||||
|
||||
imagedestroy($img);
|
||||
imagedestroy($thumb);
|
||||
imagedestroy($img);
|
||||
imagedestroy($thumb);
|
||||
|
||||
$timestampValue = $exifDate->getTimestamp();
|
||||
$json = json_encode(['timestamp' => $timestampValue]);
|
||||
|
||||
$jsonName = pathinfo($filename, PATHINFO_FILENAME) . '.json';
|
||||
if (!file_put_contents($fullsDir . $jsonName, $json)) {
|
||||
error_log("Failed to write JSON to $fullsDir$jsonName");
|
||||
$timestampValue = $exifDate->getTimestamp();
|
||||
if (!file_put_contents($jsonPath, json_encode(['timestamp' => $timestampValue]))) {
|
||||
error_log("Failed to write JSON to $jsonPath");
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
foreach ([$fullPath, $previewPath, $jsonPath] as $path) {
|
||||
if ($path && file_exists($path)) {
|
||||
unlink($path);
|
||||
}
|
||||
}
|
||||
http_response_code($e->getCode() ?: 500);
|
||||
exit($e->getMessage());
|
||||
}
|
||||
|
||||
if (!empty($integrations['discord_webhook_enabled']) && $integrations['discord_webhook_enabled'] === 'true') {
|
||||
|
Reference in New Issue
Block a user