From 0cefa9353bd6641641f5c7e7d4c7712712056d4a Mon Sep 17 00:00:00 2001 From: discomrade <83621080+discomrade@users.noreply.github.com> Date: Tue, 6 Jul 2021 11:37:54 +0000 Subject: [PATCH 1/2] Recalculate filesize after stripping metadata --- post.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/post.php b/post.php index ebc2dfb1..420a45b8 100644 --- a/post.php +++ b/post.php @@ -1089,9 +1089,17 @@ if (isset($_POST['delete'])) { if ($config['redraw_image'] || (!@$file['exif_stripped'] && $config['strip_exif'] && ($file['extension'] == 'jpg' || $file['extension'] == 'jpeg'))) { if (!$config['redraw_image'] && $config['use_exiftool']) { - if($error = shell_exec_error('exiftool -overwrite_original -ignoreMinorErrors -q -q -all= ' . - escapeshellarg($file['tmp_name']))) + if ($error = shell_exec_error('exiftool -overwrite_original -ignoreMinorErrors -q -q -all= ' . + escapeshellarg($file['tmp_name']))) { error(_('Could not strip EXIF metadata!'), null, $error); + } else { + clearstatcache(true, $file['tmp_name']); + $ret = filesize($file['tmp_name']); + if ($ret === false) { + error(_('Could not calculate file size!'), null, $error); + } + $file['size'] = $ret; + } } else { $image->to($file['file']); $dont_copy_file = true; From e455080d4265c63906c4c7c217f2a3733db4d854 Mon Sep 17 00:00:00 2001 From: Zankaria Date: Thu, 21 Mar 2024 16:02:09 +0100 Subject: [PATCH 2/2] post.php: refactor image metadata stripping into function --- post.php | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/post.php b/post.php index 420a45b8..d0085dbf 100644 --- a/post.php +++ b/post.php @@ -165,6 +165,26 @@ function ocr_image(array $config, string $img_path): string { return trim($ret); } +/** + * Trim an image's EXIF metadata + * + * @param string $img_path The file path to the image. + * @return int The size of the stripped file. + * @throws RuntimeException Throws on IO errors. + */ +function strip_image_metadata(string $img_path): int { + $err = shell_exec_error('exiftool -overwrite_original -ignoreMinorErrors -q -q -all= ' . escapeshellarg($img_path)); + if ($err === false) { + throw new RuntimeException('Could not strip EXIF metadata!'); + } + clearstatcache(true, $img_path); + $ret = filesize($img_path); + if ($ret === false) { + throw new RuntimeException('Could not calculate file size!'); + } + return $ret; +} + /** * Method handling functions */ @@ -1089,16 +1109,14 @@ if (isset($_POST['delete'])) { if ($config['redraw_image'] || (!@$file['exif_stripped'] && $config['strip_exif'] && ($file['extension'] == 'jpg' || $file['extension'] == 'jpeg'))) { if (!$config['redraw_image'] && $config['use_exiftool']) { - if ($error = shell_exec_error('exiftool -overwrite_original -ignoreMinorErrors -q -q -all= ' . - escapeshellarg($file['tmp_name']))) { - error(_('Could not strip EXIF metadata!'), null, $error); - } else { - clearstatcache(true, $file['tmp_name']); - $ret = filesize($file['tmp_name']); - if ($ret === false) { - error(_('Could not calculate file size!'), null, $error); + try { + $file['size'] = strip_image_metadata($file['tmp_name']); + } catch (RuntimeException $e) { + if ($config['syslog']) { + _syslog(LOG_ERR, "Could not strip image metadata: {$e->getMessage()}"); + // Since EXIF metadata can countain sensible info, fail the request. + error(_('Could not strip EXIF metadata!'), null, $error); } - $file['size'] = $ret; } } else { $image->to($file['file']);