1
|
|
|
<?php namespace Nord\Lumen\FileManager\Adapters; |
2
|
|
|
|
3
|
|
|
use Cloudinary; |
4
|
|
|
use Nord\Lumen\FileManager\Contracts\File; |
5
|
|
|
|
6
|
|
|
class CloudinaryAdapter extends DiskAdapter |
7
|
|
|
{ |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* CloudinaryAdapter constructor. |
11
|
|
|
* |
12
|
|
|
* @param array $config |
13
|
|
|
*/ |
14
|
|
|
public function __construct(array $config) |
15
|
|
|
{ |
16
|
|
|
parent::__construct($config); |
17
|
|
|
|
18
|
|
|
$this->configureClient($config); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @inheritdoc |
24
|
|
|
*/ |
25
|
|
|
public function getName() |
26
|
|
|
{ |
27
|
|
|
return 'cloudinary'; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @inheritdoc |
33
|
|
|
*/ |
34
|
|
|
public function saveFile($path, $contents, array $options) |
35
|
|
|
{ |
36
|
|
|
// Cloudinary paths must be given without the extension, therefore we remove it here |
37
|
|
|
return parent::saveFile(substr($path, 0, (strrpos($path, '.'))), $contents, $options); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @inheritdoc |
43
|
|
|
*/ |
44
|
|
|
public function getFileUrl(File $file, array $options) |
45
|
|
|
{ |
46
|
|
|
return cloudinary_url($this->createFilePath($file), $options); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param array $config |
52
|
|
|
*/ |
53
|
|
|
private function configureClient(array $config) |
54
|
|
|
{ |
55
|
|
|
$cloudName = array_get($config, 'cloudName', env('CLOUDINARY_NAME')); |
56
|
|
|
$apiKey = array_get($config, 'apiKey', env('CLOUDINARY_KEY')); |
57
|
|
|
$apiSecret = array_get($config, 'apiSecret', env('CLOUDINARY_SECRET')); |
58
|
|
|
|
59
|
|
|
Cloudinary::config([ |
60
|
|
|
'cloud_name' => $cloudName, |
61
|
|
|
'api_key' => $apiKey, |
62
|
|
|
'api_secret' => $apiSecret, |
63
|
|
|
]); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|