Improve cache generation

This commit is contained in:
grandeljay 2022-06-15 23:00:13 +02:00
parent 9208227d8e
commit adba2c5d0a
3 changed files with 79 additions and 70 deletions

View file

@ -75,7 +75,32 @@ switch ($_SERVER['REQUEST_METHOD']) {
$wish_description = $info->description; $wish_description = $info->description;
} }
$wish_image = is_null($info->image) ? 'NULL' : '"' . $info->image . '"'; if (null !== $info->image) {
/** URL */
$ch_options = array(
CURLOPT_AUTOREFERER => true,
CURLOPT_CONNECTTIMEOUT => 30,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HEADER => false,
CURLOPT_MAXREDIRS => 10,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_TIMEOUT => 30,
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:99.0) Gecko/20100101 Firefox/99.0',
);
$ch = curl_init($info->image);
curl_setopt_array($ch, $ch_options);
$favicon = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (200 === $code) {
$wish_image = '"' . $info->image . '"';
}
curl_close($ch);
}
$response = array( $response = array(
'info' => $info, 'info' => $info,
@ -113,6 +138,8 @@ switch ($_SERVER['REQUEST_METHOD']) {
' . $wish_price . ' ' . $wish_price . '
);' );'
); );
$response['lastInsertId'] = $wish_id;
} elseif (isset($_POST['wishlist_id'])) { } elseif (isset($_POST['wishlist_id'])) {
/** Insert wish */ /** Insert wish */
$wishlist_id = $_POST['wishlist_id']; $wishlist_id = $_POST['wishlist_id'];

View file

@ -8,14 +8,6 @@ namespace wishthis\Cache;
class Cache class Cache
{ {
/**
* Private
*/
private function getAge(): int
{
return time() - filemtime($this->getFilepath());
}
/** /**
* Protected * Protected
*/ */
@ -23,6 +15,11 @@ class Cache
protected string $directory = ROOT . '/src/cache'; protected string $directory = ROOT . '/src/cache';
protected int $maxAge = 2592000; // 30 days protected int $maxAge = 2592000; // 30 days
protected function getAge(): int
{
return time() - filemtime($this->getFilepath());
}
protected function getIdentifier(): string protected function getIdentifier(): string
{ {
return md5($this->url); return md5($this->url);

View file

@ -34,63 +34,51 @@ class Embed extends Cache
{ {
$filepath = $this->getFilepath(); $filepath = $this->getFilepath();
/** Get existing info */
$info = $this->exists() ? json_decode(file_get_contents($filepath)) : new \stdClass(); $info = $this->exists() ? json_decode(file_get_contents($filepath)) : new \stdClass();
if (true === $this->generateCache() || true === $generateCache) { if (($this->exists() && $this->getAge() > $this->maxAge) || true === $generateCache) {
/** $infoToSave = $info;
* @link https://github.com/oscarotero/Embed
*/
$embed = new \Embed\Embed();
$info_simplified = new \stdClass(); try {
$info_simplified->authorName = ''; /**
$info_simplified->authorUrl = ''; * Fetch embed info
$info_simplified->cms = ''; *
$info_simplified->code = ''; * @link https://github.com/oscarotero/Embed
$info_simplified->description = ''; */
$info_simplified->favicon = ''; $embed = new \Embed\Embed();
$info_simplified->feeds = array(); $info = $embed->get($this->url);
$info_simplified->icon = '';
$info_simplified->image = null; /** Convert embed info to a saveable format (JSON) stdClass */
$info_simplified->keywords = array(); $infoToSave = new \stdClass();
$info_simplified->language = ''; $infoToSave->authorName = (string) $info->authorName;
$info_simplified->languages = array(); $infoToSave->authorUrl = (string) $info->authorUrl;
$info_simplified->license = ''; $infoToSave->cms = (string) $info->cms;
$info_simplified->providerName = ''; $infoToSave->code = (string) $info->code;
$info_simplified->providerUrl = ''; $infoToSave->description = (string) $info->description;
$info_simplified->publishedTime = ''; $infoToSave->favicon = (string) $info->favicon;
$info_simplified->redirect = ''; $infoToSave->feeds = (array) $info->feeds;
$info_simplified->title = $this->url; $infoToSave->icon = (string) $info->icon;
$info_simplified->url = $this->url; $infoToSave->image = (string) $info->image;
$infoToSave->keywords = (array) $info->keywords;
$infoToSave->language = (string) $info->language;
$infoToSave->languages = (array) $info->languages;
$infoToSave->license = (string) $info->license;
$infoToSave->providerName = (string) $info->providerName;
$infoToSave->providerUrl = (string) $info->providerUrl;
$infoToSave->publishedTime = $info->publishedTime ? $info->publishedTime->format('d.m.Y') : '';
$infoToSave->redirect = (string) $info->redirect;
$infoToSave->title = (string) $info->title;
$infoToSave->url = (string) $info->url;
$info = $infoToSave;
} catch (\Throwable $ex) {
$generateCache = false;
echo $ex->getMessage();
}
if ($generateCache) { if ($generateCache) {
try {
$info = $embed->get($this->url);
$info_simplified->authorName = (string) $info->authorName;
$info_simplified->authorUrl = (string) $info->authorUrl;
$info_simplified->cms = (string) $info->cms;
$info_simplified->code = (string) $info->code;
$info_simplified->description = (string) $info->description;
$info_simplified->favicon = (string) $info->favicon;
$info_simplified->feeds = (array) $info->feeds;
$info_simplified->icon = (string) $info->icon;
$info_simplified->image = (string) $info->image;
$info_simplified->keywords = (array) $info->keywords;
$info_simplified->language = (string) $info->language;
$info_simplified->languages = (array) $info->languages;
$info_simplified->license = (string) $info->license;
$info_simplified->providerName = (string) $info->providerName;
$info_simplified->providerUrl = (string) $info->providerUrl;
$info_simplified->publishedTime = $info->publishedTime ? $info->publishedTime->format('d.m.Y') : '';
$info_simplified->redirect = (string) $info->redirect;
$info_simplified->title = (string) $info->title;
$info_simplified->url = (string) $info->url;
} catch (\Throwable $ex) {
$generateCache = false;
echo $ex->getMessage();
}
$ch_options = array( $ch_options = array(
CURLOPT_AUTOREFERER => true, CURLOPT_AUTOREFERER => true,
CURLOPT_CONNECTTIMEOUT => 30, CURLOPT_CONNECTTIMEOUT => 30,
@ -104,8 +92,8 @@ class Embed extends Cache
); );
/** Favicon */ /** Favicon */
if (str_contains(pathinfo($info_simplified->favicon, PATHINFO_EXTENSION), 'ico')) { if (str_contains(pathinfo($info->favicon, PATHINFO_EXTENSION), 'ico')) {
$ch = curl_init($info_simplified->favicon); $ch = curl_init($info->favicon);
curl_setopt_array($ch, $ch_options); curl_setopt_array($ch, $ch_options);
$favicon = curl_exec($ch); $favicon = curl_exec($ch);
@ -113,24 +101,21 @@ class Embed extends Cache
curl_close($ch); curl_close($ch);
$info_simplified->favicon = $favicon && 200 === $code ? 'data:image/x-icon;base64,' . base64_encode($favicon) : ''; $info->favicon = $favicon && 200 === $code ? 'data:image/x-icon;base64,' . base64_encode($favicon) : '';
} }
/** Response code */ /** URL */
$ch = curl_init($info_simplified->url); $ch = curl_init($info->url);
curl_setopt_array($ch, $ch_options); curl_setopt_array($ch, $ch_options);
$favicon = curl_exec($ch); $favicon = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE); $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (0 === $code || 404 === $code) { if (200 !== $code) {
$generateCache = false; $generateCache = false;
} }
curl_close($ch); curl_close($ch);
/** Update info */
$info = $info_simplified;
} }
if ($generateCache) { if ($generateCache) {