-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathCloudinaryAdapter.php
More file actions
107 lines (92 loc) · 2.88 KB
/
CloudinaryAdapter.php
File metadata and controls
107 lines (92 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
namespace Silvanite\NovaFieldCloudinary\Adapters;
use Cloudinary\Uploader;
use League\Flysystem\Config;
use CarlosOCarvalho\Flysystem\Cloudinary\CloudinaryAdapter as CloudinaryBaseAdapter;
class CloudinaryAdapter extends CloudinaryBaseAdapter
{
protected $resource_types = ['image', 'raw', 'video'];
/**
* Write a new file using a stream.
*
* @param string $path
* @param resource $resource
* @param Config $config Config object
*
* @return array|false false on failure file meta data on success
*/
public function writeStream($path, $resource, Config $config)
{
$cloudinary_options = $config->get('cloudinary') ?? [];
$path = pathinfo($path)['filename'];
// Create the options object
$options = array_merge([
'public_id' => $path,
'resource_type' => 'auto'
], $cloudinary_options);
$resource_metadata = stream_get_meta_data($resource);
$uploaded_metadata = Uploader::upload($resource_metadata['uri'], $options);
return $uploaded_metadata;
}
/**
* Write a new file.
*
* @param string $path
* @param resource $resource
* @param Config $config Config object
*
* @return array|false false on failure file meta data on success
*/
public function write($path, $resource, Config $config)
{
$path = pathinfo($path)['filename'];
return parent::write($path, $resource, $config);
}
/**
* Get the URL of an image with optional transformation parameters
*
* @param string|array $path
* @return string
*/
public function getUrl($path)
{
if (is_array($path)) {
return cloudinary_url($path['public_id'], $path['options']);
}
return cloudinary_url($path);
}
/**
* Delete a resource by the given public id.
*
* @param string $path
* @return boolean
*/
public function delete($path)
{
return collect($this->resource_types)->filter(function ($resource_type) use ($path) {
try {
$result = Uploader::destroy($path, ['resource_type' => $resource_type, 'invalidate' => true]);
is_array($result) ? $result['result'] == 'ok' : false;
} catch (\Exception $e) {
return false;
}
})->count() >= 1;
}
/**
* Check if a resource with the provided public id exists
*
* @param string $path
* @return boolean
*/
public function has($path)
{
return collect($this->resource_types)->filter(function ($resource_type) use ($path) {
try {
$this->api->resource($path, ['resource_type' => $resource_type]);
return true;
} catch (\Exception $e) {
return false;
}
})->count() >= 1;
}
}