Skip to content

Commit

Permalink
Added logging to help with debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamminf committed Sep 10, 2016
1 parent fe0d77d commit cc6077c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 2 additions & 0 deletions embeddedassets/models/EmbeddedAssetsModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public static function populateFromAsset(AssetFileModel $asset)
}
catch(\Exception $e)
{
EmbeddedAssetsPlugin::log("Error reading embedded asset data on asset {$asset->id} (\"{$e->getMessage()}\")", LogLevel::Error);

return null;
}
}
Expand Down
30 changes: 29 additions & 1 deletion embeddedassets/services/EmbeddedAssetsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,14 @@ public function parseUrl($url)
$essence = new \Essence\Essence();
$options = EmbeddedAssetsPlugin::getParameters();

EmbeddedAssetsPlugin::log("Requesting URL \"{$url}\"");

$result = $essence->extract($url, $options);

if($result && $result->html)
{
EmbeddedAssetsPlugin::log("Embed data found");

$properties = array();

foreach($result as $property => $value)
Expand All @@ -74,10 +78,17 @@ public function parseUrl($url)

if(!empty($properties))
{
EmbeddedAssetsPlugin::log("Some data missing - looking for related Open Graph metadata");

try
{
$data = $this->_readExternalFile($url);

if(!$data)
{
throw new \Exception("Could not read data");
}

$reader = new \Opengraph\Reader();
$reader->parse($data);

Expand All @@ -87,7 +98,10 @@ public function parseUrl($url)
$result->$property = $reader->getMeta($ogProperty);
}
}
catch(\Exception $e) {}
catch(\Exception $e)
{
EmbeddedAssetsPlugin::log("Error requesting/parsing Open Graph metadata (\"{$e->getMessage()}\")", LogLevel::Warning);
}
}
}

Expand Down Expand Up @@ -160,6 +174,8 @@ public function saveEmbeddedAsset(EmbeddedAssetsModel $media, $folderId)
}
catch(\Exception $e)
{
EmbeddedAssetsPlugin::log("Error saving embedded asset (\"{$e->getMessage()}\")", LogLevel::Error);

if($transaction)
{
$transaction->rollback();
Expand Down Expand Up @@ -207,6 +223,8 @@ private function _storeFile(EmbeddedAssetsModel $media, $folderId)

if($existingFile)
{
EmbeddedAssetsPlugin::log("File with name \"{$fileName}\" already exists in this location");

$fileUniqueId = DateTimeHelper::currentUTCDateTime()->format('ymd_His');
$fileName = $filePrefix . $fileLabel . '_' . $fileUniqueId . $fileExtension;
}
Expand Down Expand Up @@ -250,12 +268,16 @@ private function _addToFiles($key, $url, $data = null, $mimeType = 'text/plain')

if(function_exists('finfo_open'))
{
EmbeddedAssetsPlugin::log("Setting embedded asset file mime type with `finfo`");

$fileInfo = finfo_open(FILEINFO_MIME);
$mimeType = finfo_file($fileInfo, $tempName);
finfo_close($fileInfo);
}
else if(function_exists('mime_content_type'))
{
EmbeddedAssetsPlugin::log("Setting embedded asset file mime type with `mime_content_type`");

$mimeType = mime_content_type($tempName);
}

Expand All @@ -278,6 +300,8 @@ private function _readExternalFile($url)
{
if(function_exists('curl_init'))
{
EmbeddedAssetsPlugin::log("Reading file with `curl`");

$ch = curl_init();

curl_setopt($ch, CURLOPT_AUTOREFERER, true);
Expand All @@ -291,6 +315,8 @@ private function _readExternalFile($url)

if(!empty($error))
{
EmbeddedAssetsPlugin::log("Error reading file (\"{$error}\")", LogLevel::Error);

$data = false;
}

Expand All @@ -302,6 +328,8 @@ private function _readExternalFile($url)
$allowUrlFopen = preg_match('/1|yes|on|true/i', ini_get('allow_url_fopen'));
if($allowUrlFopen)
{
EmbeddedAssetsPlugin::log("Reading file with `file_get_contents`");

return @file_get_contents($url);
}

Expand Down

0 comments on commit cc6077c

Please sign in to comment.