Skip to content

Commit

Permalink
Update code
Browse files Browse the repository at this point in the history
  • Loading branch information
datlechin committed Feb 8, 2023
1 parent 5bbfc7e commit ad2b90d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 29 deletions.
4 changes: 2 additions & 2 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

namespace Datlechin\LinkPreview;

use Datlechin\LinkPreview\Api\Controllers\ScrapperController;
use Flarum\Extend;
use Flarum\Settings\Event\Deserializing;

return [
(new Extend\Frontend('forum'))
Expand All @@ -25,7 +25,7 @@
new Extend\Locales(__DIR__ . '/locale'),

(new Extend\Routes('api'))
->get('/datlechin-link-preview', 'datlechin-link-preview', Api\Controllers\ScrapperController::class),
->get('/datlechin-link-preview', 'datlechin-link-preview', ScrapperController::class),

(new Extend\Settings())
->default('datlechin-link-preview.blacklist', '')
Expand Down
6 changes: 5 additions & 1 deletion less/forum.less
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@

&-image {
display: flex;
align-items: flex-start;
align-items: center;
justify-content: center;
width: 60px;
padding: 5px 0;

@media @phone {
align-items: center;
}

img {
width: 100%;
height: auto;
Expand Down
47 changes: 21 additions & 26 deletions src/Api/Controllers/ScrapperController.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,17 @@ public function __construct(
$this->translator = $translator;
$this->cache = $cache;
$cacheTime = $settings->get('datlechin-link-preview.cache_time');
if (!is_numeric($cacheTime)) {

if (! is_numeric($cacheTime)) {
$cacheTime = 60;
}

$this->cacheTime = intval($cacheTime);
$this->settings = $settings;
$this->blacklist = $this->getMultiDimensionalSetting('datlechin-link-preview.blacklist');
$this->whitelist = $this->getMultiDimensionalSetting('datlechin-link-preview.whitelist');
}

/*
* @param Request $request
* @return Response
*/
public function handle(Request $request): Response
{
$url = $request->getQueryParams()['url'] ?? '';
Expand All @@ -69,6 +67,7 @@ public function handle(Request $request): Response
}

$normalizedUrl = preg_replace('/^https?:\/\/(.+?)\/?$/i', '$1', $url);

if (
($this->whitelist && ! $this->inList($normalizedUrl, $this->whitelist))
|| ($this->blacklist && $this->inList($normalizedUrl, $this->blacklist))
Expand All @@ -81,7 +80,8 @@ public function handle(Request $request): Response
if ($this->cacheTime) {
$cacheKey = 'datlechin-link-preview:' . md5($normalizedUrl);
$data = $this->cache->get($cacheKey);
if (null !== $data) {

if ($data) {
return new JsonResponse($data);
}
}
Expand All @@ -94,13 +94,14 @@ public function handle(Request $request): Response
}

try {
$this->web->go($url);
$web = $this->web;
$web->go($url);

$data = [
'site_name' => $this->web->openGraph['og:site_name'] ?? $this->web->twitterCard['twitter:site'] ?? null,
'title' => $this->web->title ?? $this->web->openGraph['og:title'] ?? $this->web->twitterCard['twitter:title'] ?? null,
'description' => $this->web->description ?? $this->web->openGraph['og:description'] ?? $this->web->twitterCard['twitter:description'] ?? null,
'image' => $this->web->image ?? $this->web->openGraph['og:image'] ?? $this->web->twitterCard['twitter:image'] ?? null,
'site_name' => $web->openGraph['og:site_name'] ?? $web->twitterCard['twitter:site'] ?? null,
'title' => $web->title ?? $web->openGraph['og:title'] ?? $web->twitterCard['twitter:title'] ?? null,
'description' => $web->description ?? $web->openGraph['og:description'] ?? $web->twitterCard['twitter:description'] ?? null,
'image' => $web->image ?? $web->openGraph['og:image'] ?? $web->twitterCard['twitter:image'] ?? null,
'accessed' => time(),
];

Expand All @@ -116,40 +117,34 @@ public function handle(Request $request): Response
}
}

/**
* @param string $setting
* @return array
*/
private function getMultiDimensionalSetting(string $setting): array
{
$items = preg_split('/[,\\n]/', $this->settings->get($setting) ?? '') ?: [];

return array_filter(array_map('trim', $items));
}

/**
* @param string $needle
* @param array $haystack
*/
private function inList(string $needle, array $haystack): bool
{
if (! $haystack) {
return false;
}

if (in_array($needle, $haystack, true)) {
return true;
}

foreach ($haystack as $item) {
$quoted = strtr(
preg_quote($item, '/'),
[
'\\*' => '.*',
'\\?' => '.',
]
);
$quoted = strtr(preg_quote($item, '/'), [
'\\*' => '.*',
'\\?' => '.',
]);

if (preg_match("/$quoted/i", $needle)) {
return true;
}
}

return false;
}
}

0 comments on commit ad2b90d

Please sign in to comment.