Skip to content

Commit b160b42

Browse files
committed
Implement keycdn
1 parent 690c3c9 commit b160b42

File tree

6 files changed

+76
-28
lines changed

6 files changed

+76
-28
lines changed

config/ozu-client.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,9 @@
1717
// The following values should not be modified unless you know what you are doing
1818
'api_host' => env('OZU_API_HOST', 'https://ozu.code16.fr/'),
1919
'api_version' => env('OZU_API_VERSION', 'v1'),
20-
'cdn_url' => env('OZU_CDN_URL'),
20+
21+
'cdn' => [
22+
'provider' => env('OZU_CDN_PROVIDER'),
23+
'url' => env('OZU_CDN_URL'),
24+
],
2125
];

src/OzuServiceProvider.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
use Code16\OzuClient\Deploy\DeployServiceProvider;
66
use Code16\OzuClient\Support\Pagination\StaticLengthAwarePaginator;
77
use Code16\OzuClient\Support\Pagination\StaticPaginator;
8-
use Code16\OzuClient\Support\Thumbnails\CdnThumbnail;
8+
use Code16\OzuClient\Support\Thumbnails\ImageKitThumbnail;
9+
use Code16\OzuClient\Support\Thumbnails\KeyCdnThumbnail;
910
use Code16\OzuClient\Support\Thumbnails\LocalThumbnail;
1011
use Code16\OzuClient\Support\Thumbnails\Thumbnail;
1112
use Code16\OzuClient\View\Components\Content;
@@ -47,9 +48,15 @@ public function register()
4748
$this->app->bind(Paginator::class, StaticPaginator::class);
4849
$this->app->bind(LengthAwarePaginator::class, StaticLengthAwarePaginator::class);
4950
$this->app->bind(Thumbnail::class, function ($app) {
50-
return $app->environment('production')
51-
? $app->make(CdnThumbnail::class)
52-
: $app->make(LocalThumbnail::class);
51+
if (!$app->environment('production') || !config('ozu-client.cdn.url')) {
52+
return $app->make(LocalThumbnail::class);
53+
}
54+
55+
return match(config('ozu-client.cdn.provider')) {
56+
'imagekit' => $app->make(ImageKitThumbnail::class),
57+
'keycdn' => $app->make(KeyCdnThumbnail::class),
58+
default => $app->make(LocalThumbnail::class)
59+
};
5360
});
5461

5562
$this->app->register(DeployServiceProvider::class);

src/Support/Thumbnails/CdnThumbnail.php

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
namespace Code16\OzuClient\Support\Thumbnails;
44

5-
class CdnThumbnail extends Thumbnail
5+
abstract class CdnThumbnail extends Thumbnail
66
{
77
public function make(?int $width, ?int $height = null, bool $fit = false): ?string
88
{
9-
if($cdnUrl = str(config('ozu-client.cdn_url'))->rtrim('/')) {
9+
if($cdnUrl = str(config('ozu-client.cdn.url'))->rtrim('/')) {
1010
return sprintf(
1111
'%s/storage/%s?%s',
1212
$cdnUrl,
@@ -18,24 +18,5 @@ public function make(?int $width, ?int $height = null, bool $fit = false): ?stri
1818
return null;
1919
}
2020

21-
private function generateUrlParameters(?int $width, ?int $height, bool $fit): string
22-
{
23-
if (!$fit) {
24-
if ($width && $height) {
25-
return sprintf('tr=w-%s,h-%s,c-at_max', $width, $height);
26-
}
27-
28-
return $width
29-
? sprintf('tr=w-%s', $width)
30-
: sprintf('tr=h-%s', $height ?: 400);
31-
}
32-
33-
if ($width && $height) {
34-
return sprintf('tr=w-%s,h-%s,c-at_max', $width, $height);
35-
}
36-
37-
$side = ($width ?: $height) ?: 400;
38-
39-
return sprintf('tr=w-%s,h-%s,c-maintain_ratio', $side, $side);
40-
}
21+
abstract protected function generateUrlParameters(?int $width, ?int $height, bool $fit): string;
4122
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Code16\OzuClient\Support\Thumbnails;
4+
5+
class ImageKitThumbnail extends CdnThumbnail
6+
{
7+
protected function generateUrlParameters(?int $width, ?int $height, bool $fit): string
8+
{
9+
if (!$fit) {
10+
if ($width && $height) {
11+
return sprintf('tr=w-%s,h-%s,c-at_max', $width, $height);
12+
}
13+
14+
return $width
15+
? sprintf('tr=w-%s', $width)
16+
: sprintf('tr=h-%s', $height ?: 400);
17+
}
18+
19+
if ($width && $height) {
20+
return sprintf('tr=w-%s,h-%s,c-maintain_ratio', $width, $height);
21+
}
22+
23+
$side = ($width ?: $height) ?: 400;
24+
25+
return sprintf('tr=w-%s,h-%s,c-maintain_ratio', $side, $side);
26+
}
27+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Code16\OzuClient\Support\Thumbnails;
4+
5+
class KeyCdnThumbnail extends CdnThumbnail
6+
{
7+
8+
protected function generateUrlParameters(?int $width, ?int $height, bool $fit): string
9+
{
10+
if (!$fit) {
11+
if ($width && $height) {
12+
return sprintf('width=%s&height=%s&fit=inside', $width, $height);
13+
}
14+
15+
return $width
16+
? sprintf('width=%s', $width)
17+
: sprintf('height=%s', $height ?: 400);
18+
}
19+
20+
if ($width && $height) {
21+
return sprintf('width=%s&height=%s&fit=cover', $width, $height);
22+
}
23+
24+
$side = ($width ?: $height) ?: 400;
25+
26+
return sprintf('width=%s&height=%s&fit=cover', $side, $side);
27+
}
28+
}

src/Support/Thumbnails/LocalThumbnail.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Filesystem\FilesystemManager;
77
use Illuminate\Support\Str;
88
use Intervention\Image\Drivers\Imagick\Driver;
9+
use Intervention\Image\Exceptions\DecoderException;
910
use Intervention\Image\ImageManager;
1011

1112
class LocalThumbnail extends Thumbnail
@@ -76,7 +77,7 @@ private function generateThumbnail(string $sourceDisk, string $sourceRelPath, st
7677
}
7778

7879
$thumbnailDisk->put($thumbnailPath, $sourceImg->toJpeg(quality: $this->quality));
79-
} catch (FileNotFoundException $ex) {
80+
} catch (FileNotFoundException|DecoderException) {
8081
return null;
8182
}
8283
}

0 commit comments

Comments
 (0)