Close proc pipes explicitly

pull/37/head
Varun Patil 2022-08-19 21:42:23 +00:00
parent ebab5225e0
commit 4decbb91cb
1 changed files with 8 additions and 1 deletions

View File

@ -16,8 +16,8 @@ class Util {
private static function getExif(File $file) { private static function getExif(File $file) {
// Attempt to read exif data // Attempt to read exif data
// Assume it exists in the first 256 kb of the file
try { try {
// Assume it exists in the first 256 kb of the file
$handle = $file->fopen('rb'); $handle = $file->fopen('rb');
$data = stream_get_contents($handle, 256 * 1024); $data = stream_get_contents($handle, 256 * 1024);
fclose($handle); fclose($handle);
@ -26,6 +26,7 @@ class Util {
throw new \Exception('Could not read file'); throw new \Exception('Could not read file');
} }
// Start exiftool and output to json
$pipes = []; $pipes = [];
$proc = proc_open('exiftool -json -', [ $proc = proc_open('exiftool -json -', [
0 => array('pipe', 'rb'), 0 => array('pipe', 'rb'),
@ -33,11 +34,17 @@ class Util {
2 => array('pipe', 'w'), 2 => array('pipe', 'w'),
], $pipes); ], $pipes);
// Write the file to exiftool's stdin
fwrite($pipes[0], $data); fwrite($pipes[0], $data);
fclose($pipes[0]); fclose($pipes[0]);
$stdout = stream_get_contents($pipes[1]); $stdout = stream_get_contents($pipes[1]);
// Clean up
fclose($pipes[1]);
fclose($pipes[2]);
proc_close($proc); proc_close($proc);
// Parse the json
$json = json_decode($stdout, true); $json = json_decode($stdout, true);
if (empty($json)) { if (empty($json)) {
throw new \Exception('Could not read exif data'); throw new \Exception('Could not read exif data');