Skip to content

Commit 4af99eb

Browse files
committed
Allow vimeo thumbnail url
1 parent d6cf935 commit 4af99eb

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/ServiceBase.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@ protected function cacheThumbnailUrl(\Closure $callback, ?string $cacheKey = nul
4141
{
4242
$cacheKey = sprintf('laravel-embed-thumbnail::%s_%s', $this->url, $cacheKey ?: 'default');
4343

44-
return Cache::rememberForever($cacheKey, $callback);
44+
if (($url = Cache::get($cacheKey)) !== null) {
45+
return $url;
46+
}
47+
48+
$url = $callback();
49+
50+
if ($url) {
51+
Cache::forever($cacheKey, $url);
52+
} else {
53+
Cache::put($cacheKey, '', now()->addDay());
54+
}
55+
56+
return $url;
4557
}
4658
}

src/Services/Vimeo.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
use Code16\Embed\ServiceBase;
66
use Code16\Embed\Services\Utils\IsVideoService;
7+
use Illuminate\Http\Client\ConnectionException;
8+
use Illuminate\Http\Client\RequestException;
9+
use Illuminate\Support\Facades\Http;
710

811
class Vimeo extends ServiceBase
912
{
@@ -29,6 +32,23 @@ public function videoId(): ?string
2932

3033
public function thumbnailUrl(bool $maxResolution = true): ?string
3134
{
32-
return null;
35+
return $this->cacheThumbnailUrl(function () use ($maxResolution) {
36+
try {
37+
$oembed = Http::get(sprintf(
38+
'https://vimeo.com/api/oembed.json?url=%s&width=%d&height=%d',
39+
rawurlencode($this->url),
40+
$maxResolution ? 1920 : 640,
41+
$maxResolution ? 1080 : 360
42+
))
43+
->throw()
44+
->json();
45+
46+
return $oembed['thumbnail_url'] ?? '';
47+
} catch (RequestException|ConnectionException $e) {
48+
return '';
49+
}
50+
},
51+
$maxResolution ? 'max' : 'low'
52+
);
3353
}
3454
}

0 commit comments

Comments
 (0)