3
3
namespace Unicodeveloper \Cloudinary ;
4
4
5
5
use Cloudinary \Cloudinary ;
6
- use League \Flysystem \Config ;
7
- use League \Flysystem \Util \MimeType ;
8
- use League \Flysystem \AdapterInterface ;
9
6
use League \Flysystem \Adapter \Polyfill \NotSupportingVisibilityTrait ;
7
+ use League \Flysystem \AdapterInterface ;
8
+ use League \Flysystem \Config ;
10
9
11
10
11
+ /**
12
+ * Class CloudinaryAdapter
13
+ * @package Unicodeveloper\Cloudinary
14
+ */
12
15
class CloudinaryAdapter implements AdapterInterface
13
16
{
14
17
use NotSupportingVisibilityTrait;
@@ -27,6 +30,20 @@ public function __construct(string $config)
27
30
$ this ->cloudinary = new Cloudinary ($ config );
28
31
}
29
32
33
+ /**
34
+ * Update a file.
35
+ * Cloudinary has no specific update method. Overwrite instead.
36
+ *
37
+ * @param string $path
38
+ * @param string $contents
39
+ * @param Config $options Config object
40
+ *
41
+ * @return array|false false on failure file meta data on success
42
+ */
43
+ public function update ($ path , $ contents , Config $ options )
44
+ {
45
+ return $ this ->write ($ path , $ contents , $ options );
46
+ }
30
47
31
48
/**
32
49
* Write a new file.
@@ -35,24 +52,25 @@ public function __construct(string $config)
35
52
*
36
53
* @param string $path
37
54
* @param string $contents
38
- * @param Config $options Config object
55
+ * @param Config $options Config object
39
56
*
40
57
* @return array|false false on failure file meta data on success
41
58
*/
42
59
public function write ($ path , $ contents , Config $ options )
43
60
{
44
- fwrite (tmpfile (), $ contents );
61
+ $ tempFile = tmpfile ();
62
+
63
+ fwrite ($ tempFile , $ contents );
45
64
46
65
return $ this ->writeStream ($ path , $ tempFile , $ options );
47
66
}
48
67
49
-
50
68
/**
51
69
* Write a new file using a stream.
52
70
*
53
- * @param string $path
71
+ * @param string $path
54
72
* @param resource $resource
55
- * @param Config $options Config object
73
+ * @param Config $options Config object
56
74
*
57
75
* @return array|false false on failure file meta data on success
58
76
*/
@@ -64,7 +82,7 @@ public function writeStream($path, $resource, Config $options)
64
82
65
83
$ fileExtension = pathinfo ($ publicId , PATHINFO_EXTENSION );
66
84
67
- $ newPublicId = $ fileExtension ? substr ($ publicId , 0 , - (strlen ($ fileExtension ) + 1 )) : $ publicId ;
85
+ $ newPublicId = $ fileExtension ? substr ($ publicId , 0 , -(strlen ($ fileExtension ) + 1 )) : $ publicId ;
68
86
69
87
$ uploadOptions = [
70
88
'public_id ' => $ newPublicId ,
@@ -78,30 +96,13 @@ public function writeStream($path, $resource, Config $options)
78
96
return $ result ;
79
97
}
80
98
81
-
82
- /**
83
- * Update a file.
84
- * Cloudinary has no specific update method. Overwrite instead.
85
- *
86
- * @param string $path
87
- * @param string $contents
88
- * @param Config $options Config object
89
- *
90
- * @return array|false false on failure file meta data on success
91
- */
92
- public function update ($ path , $ contents , Config $ options )
93
- {
94
- return $ this ->write ($ path , $ contents , $ options );
95
- }
96
-
97
-
98
99
/**
99
100
* Update a file using a stream.
100
101
* Cloudinary has no specific update method. Overwrite instead.
101
102
*
102
- * @param string $path
103
+ * @param string $path
103
104
* @param resource $resource
104
- * @param Config $options Config object
105
+ * @param Config $options Config object
105
106
*
106
107
* @return array|false false on failure file meta data on success
107
108
*/
@@ -134,6 +135,15 @@ public function rename($path, $newpath)
134
135
return $ result ['public_id ' ] == $ newPathInfo ['filename ' ];
135
136
}
136
137
138
+ /**
139
+ * Expose the Cloudinary v2 Upload Functionality
140
+ *
141
+ */
142
+ protected function uploadApi ()
143
+ {
144
+ return $ this ->cloudinary ->uploadApi ();
145
+ }
146
+
137
147
/**
138
148
* Copy a file.
139
149
* Copy content from existing url.
@@ -149,6 +159,7 @@ public function copy($path, $newpath)
149
159
150
160
return is_array ($ result ) ? $ result ['public_id ' ] == $ newpath : false ;
151
161
}
162
+
152
163
/**
153
164
* Delete a file.
154
165
*
@@ -177,6 +188,16 @@ public function deleteDir($dirname)
177
188
178
189
return true ;
179
190
}
191
+
192
+ /**
193
+ * Expose the Cloudinary v2 Upload Functionality
194
+ *
195
+ */
196
+ protected function adminApi ()
197
+ {
198
+ return $ this ->cloudinary ->adminApi ();
199
+ }
200
+
180
201
/**
181
202
* Create a directory.
182
203
*
@@ -213,7 +234,7 @@ public function has($path)
213
234
*/
214
235
public function read ($ path )
215
236
{
216
- $ resource = (array ) $ this ->adminApi ()->resource ($ path );
237
+ $ resource = (array )$ this ->adminApi ()->resource ($ path );
217
238
$ contents = file_get_contents ($ resource ['secure_url ' ]);
218
239
219
240
return compact ('contents ' , 'path ' );
@@ -228,7 +249,7 @@ public function read($path)
228
249
*/
229
250
public function readStream ($ path )
230
251
{
231
- $ resource = (array ) $ this ->adminApi ()->resource ($ path );
252
+ $ resource = (array )$ this ->adminApi ()->resource ($ path );
232
253
233
254
$ stream = fopen ($ resource ['secure_url ' ], 'r ' );
234
255
@@ -239,7 +260,7 @@ public function readStream($path)
239
260
* List contents of a directory.
240
261
*
241
262
* @param string $directory
242
- * @param bool $recursive
263
+ * @param bool $recursive
243
264
*
244
265
* @return array
245
266
*/
@@ -250,12 +271,14 @@ public function listContents($directory = '', $hasRecursive = false)
250
271
// get resources array
251
272
$ response = null ;
252
273
do {
253
- $ response = (array ) $ this ->adminApi ()->resources ([
254
- 'type ' => 'upload ' ,
255
- 'prefix ' => $ directory ,
256
- 'max_results ' => 500 ,
257
- 'next_cursor ' => isset ($ response ['next_cursor ' ]) ? $ response ['next_cursor ' ] : null ,
258
- ]);
274
+ $ response = (array )$ this ->adminApi ()->resources (
275
+ [
276
+ 'type ' => 'upload ' ,
277
+ 'prefix ' => $ directory ,
278
+ 'max_results ' => 500 ,
279
+ 'next_cursor ' => isset ($ response ['next_cursor ' ]) ? $ response ['next_cursor ' ] : null ,
280
+ ]
281
+ );
259
282
$ resources = array_merge ($ resources , $ response ['resources ' ]);
260
283
} while (array_key_exists ('next_cursor ' , $ response ));
261
284
@@ -266,6 +289,54 @@ public function listContents($directory = '', $hasRecursive = false)
266
289
return $ resources ;
267
290
}
268
291
292
+ /**
293
+ * Prepare apropriate metadata for resource metadata given from cloudinary.
294
+ * @param array $resource
295
+ * @return array
296
+ */
297
+ protected function prepareResourceMetadata ($ resource )
298
+ {
299
+ $ resource ['type ' ] = 'file ' ;
300
+ $ resource ['path ' ] = $ resource ['public_id ' ];
301
+ $ resource = array_merge ($ resource , $ this ->prepareSize ($ resource ));
302
+ $ resource = array_merge ($ resource , $ this ->prepareTimestamp ($ resource ));
303
+ $ resource = array_merge ($ resource , $ this ->prepareMimetype ($ resource ));
304
+ return $ resource ;
305
+ }
306
+
307
+ /**
308
+ * prepare size response
309
+ * @param array $resource
310
+ * @return array
311
+ */
312
+ protected function prepareSize ($ resource )
313
+ {
314
+ $ size = $ resource ['bytes ' ];
315
+ return compact ('size ' );
316
+ }
317
+
318
+ /**
319
+ * prepare timestpamp response
320
+ * @param array $resource
321
+ * @return array
322
+ */
323
+ protected function prepareTimestamp ($ resource )
324
+ {
325
+ $ timestamp = strtotime ($ resource ['created_at ' ]);
326
+ return compact ('timestamp ' );
327
+ }
328
+
329
+ /**
330
+ * prepare mimetype response
331
+ * @param array $resource
332
+ * @return array
333
+ */
334
+ protected function prepareMimetype ($ resource )
335
+ {
336
+ $ mimetype = $ resource ['resource_type ' ];
337
+ return compact ('mimetype ' );
338
+ }
339
+
269
340
/**
270
341
* Get all the meta data of a file or directory.
271
342
*
@@ -275,17 +346,17 @@ public function listContents($directory = '', $hasRecursive = false)
275
346
*/
276
347
public function getMetadata ($ path )
277
348
{
278
- return $ this ->prepareResourceMetadata ($ this ->getResource ($ path ));
349
+ return $ this ->prepareResourceMetadata ($ this ->getResource ($ path ));
279
350
}
280
351
281
352
/**
282
353
* Get Resource data
283
- * @param string $path
354
+ * @param string $path
284
355
* @return array
285
356
*/
286
357
public function getResource ($ path )
287
358
{
288
- return (array ) $ this ->adminApi ()->resource ($ path );
359
+ return (array )$ this ->adminApi ()->resource ($ path );
289
360
}
290
361
291
362
/**
@@ -323,70 +394,4 @@ public function getTimestamp($path)
323
394
{
324
395
return $ this ->prepareTimestamp ($ this ->getResource ($ path ));
325
396
}
326
-
327
- /**
328
- * Prepare apropriate metadata for resource metadata given from cloudinary.
329
- * @param array $resource
330
- * @return array
331
- */
332
- protected function prepareResourceMetadata ($ resource )
333
- {
334
- $ resource ['type ' ] = 'file ' ;
335
- $ resource ['path ' ] = $ resource ['public_id ' ];
336
- $ resource = array_merge ($ resource , $ this ->prepareSize ($ resource ));
337
- $ resource = array_merge ($ resource , $ this ->prepareTimestamp ($ resource ));
338
- $ resource = array_merge ($ resource , $ this ->prepareMimetype ($ resource ));
339
- return $ resource ;
340
- }
341
-
342
- /**
343
- * prepare mimetype response
344
- * @param array $resource
345
- * @return array
346
- */
347
- protected function prepareMimetype ($ resource )
348
- {
349
- $ mimetype = $ resource ['resource_type ' ];
350
- return compact ('mimetype ' );
351
- }
352
-
353
- /**
354
- * prepare timestpamp response
355
- * @param array $resource
356
- * @return array
357
- */
358
- protected function prepareTimestamp ($ resource )
359
- {
360
- $ timestamp = strtotime ($ resource ['created_at ' ]);
361
- return compact ('timestamp ' );
362
- }
363
-
364
- /**
365
- * prepare size response
366
- * @param array $resource
367
- * @return array
368
- */
369
- protected function prepareSize ($ resource )
370
- {
371
- $ size = $ resource ['bytes ' ];
372
- return compact ('size ' );
373
- }
374
-
375
- /**
376
- * Expose the Cloudinary v2 Upload Functionality
377
- *
378
- */
379
- protected function uploadApi ()
380
- {
381
- return $ this ->cloudinary ->uploadApi ();
382
- }
383
-
384
- /**
385
- * Expose the Cloudinary v2 Upload Functionality
386
- *
387
- */
388
- protected function adminApi ()
389
- {
390
- return $ this ->cloudinary ->adminApi ();
391
- }
392
- }
397
+ }
0 commit comments