SimpleExifToolFacade.php in Exif 8.2
File
src/SimpleExifToolFacade.php
View source
<?php
namespace Drupal\exif;
use Drupal;
class SimpleExifToolFacade implements ExifInterface {
private static $instance = NULL;
private function __construct() {
}
public static function getInstance() {
if (is_null(self::$instance)) {
self::$instance = new self();
}
return self::$instance;
}
public static function checkConfiguration() {
$exiftoolLocation = self::getExecutable();
return isset($exiftoolLocation) && is_executable($exiftoolLocation);
}
private static function getExecutable() {
$config = Drupal::configFactory()
->get('exif.settings');
return $config
->get('exiftool_location');
}
public function getMetadataFields(array $arCckFields = []) {
foreach ($arCckFields as $drupal_field => $metadata_settings) {
$metadata_field = $metadata_settings['metadata_field'];
$ar = explode("_", $metadata_field);
if (isset($ar[0])) {
$section = $ar[0];
unset($ar[0]);
$arCckFields[$drupal_field]['metadata_field'] = [
'section' => $section,
'tag' => implode("_", $ar),
];
}
}
return $arCckFields;
}
public function readMetadataTags($file, $enable_sections = TRUE) {
if (!file_exists($file)) {
return [];
}
$data = $this
->readAllInformation($file, $enable_sections);
return $data;
}
private function readAllInformation($file, $enable_sections = TRUE, $enable_markerNote = FALSE, $enable_non_supported_tags = FALSE) {
$jsonAsString = $this
->runTool($file, $enable_sections, $enable_markerNote, $enable_non_supported_tags);
$json = json_decode($jsonAsString, TRUE);
$errorCode = json_last_error();
if ($errorCode == JSON_ERROR_NONE) {
return $this
->toLowerJsonResult($json[0]);
}
else {
$errorMessage = "";
switch ($errorCode) {
case JSON_ERROR_DEPTH:
$errorMessage = 'Maximum stack depth exceeded';
break;
case JSON_ERROR_STATE_MISMATCH:
$errorMessage = 'Underflow or the modes mismatch';
break;
case JSON_ERROR_CTRL_CHAR:
$errorMessage = 'Unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
$errorMessage = 'Syntax error, malformed JSON';
break;
case JSON_ERROR_UTF8:
$errorMessage = 'Malformed UTF-8 characters, possibly incorrectly encoded';
break;
default:
$errorMessage = 'Unknown error';
break;
}
\Drupal::logger('exif')
->notice(t($errorMessage));
return [];
}
}
private function runTool($file, $enable_sections = TRUE, $enable_markerNote = FALSE, $enable_non_supported_tags = FALSE) {
$params = "";
if ($enable_sections) {
$params = "-g -struct ";
}
if ($enable_markerNote) {
$params = $params . "-fast ";
}
else {
$params = $params . "-fast2 ";
}
if ($enable_non_supported_tags) {
$params = $params . " -u -U";
}
$commandline = self::getExecutable() . " -E -n -json " . $params . "\"" . $file . "\"";
$output = [];
$returnCode = 0;
exec($commandline, $output, $returnCode);
if ($returnCode != 0) {
$output = "";
Drupal::logger('exif')
->warning(t("exiftool return an error. can not extract metadata from file :file", [
':file' => $file,
]));
}
$info = implode("\n", $output);
return $info;
}
private function toLowerJsonResult(array $data) {
$result = [];
foreach ($data as $section => $values) {
if (is_array($values)) {
$result[strtolower($section)] = array_change_key_case($values);
}
else {
$result[strtolower($section)] = $values;
}
}
return $result;
}
public function getFieldKeys() {
return [];
}
}