33namespace Unicodeveloper \Cloudinary ;
44
55use Cloudinary \Cloudinary ;
6- use League \Flysystem \Config ;
7- use League \Flysystem \Util \MimeType ;
8- use League \Flysystem \AdapterInterface ;
96use League \Flysystem \Adapter \Polyfill \NotSupportingVisibilityTrait ;
7+ use League \Flysystem \AdapterInterface ;
8+ use League \Flysystem \Config ;
109
1110
11+ /**
12+ * Class CloudinaryAdapter
13+ * @package Unicodeveloper\Cloudinary
14+ */
1215class CloudinaryAdapter implements AdapterInterface
1316{
1417 use NotSupportingVisibilityTrait;
@@ -27,6 +30,20 @@ public function __construct(string $config)
2730 $ this ->cloudinary = new Cloudinary ($ config );
2831 }
2932
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+ }
3047
3148 /**
3249 * Write a new file.
@@ -35,24 +52,25 @@ public function __construct(string $config)
3552 *
3653 * @param string $path
3754 * @param string $contents
38- * @param Config $options Config object
55+ * @param Config $options Config object
3956 *
4057 * @return array|false false on failure file meta data on success
4158 */
4259 public function write ($ path , $ contents , Config $ options )
4360 {
44- fwrite (tmpfile (), $ contents );
61+ $ tempFile = tmpfile ();
62+
63+ fwrite ($ tempFile , $ contents );
4564
4665 return $ this ->writeStream ($ path , $ tempFile , $ options );
4766 }
4867
49-
5068 /**
5169 * Write a new file using a stream.
5270 *
53- * @param string $path
71+ * @param string $path
5472 * @param resource $resource
55- * @param Config $options Config object
73+ * @param Config $options Config object
5674 *
5775 * @return array|false false on failure file meta data on success
5876 */
@@ -64,7 +82,7 @@ public function writeStream($path, $resource, Config $options)
6482
6583 $ fileExtension = pathinfo ($ publicId , PATHINFO_EXTENSION );
6684
67- $ newPublicId = $ fileExtension ? substr ($ publicId , 0 , - (strlen ($ fileExtension ) + 1 )) : $ publicId ;
85+ $ newPublicId = $ fileExtension ? substr ($ publicId , 0 , -(strlen ($ fileExtension ) + 1 )) : $ publicId ;
6886
6987 $ uploadOptions = [
7088 'public_id ' => $ newPublicId ,
@@ -78,30 +96,13 @@ public function writeStream($path, $resource, Config $options)
7896 return $ result ;
7997 }
8098
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-
9899 /**
99100 * Update a file using a stream.
100101 * Cloudinary has no specific update method. Overwrite instead.
101102 *
102- * @param string $path
103+ * @param string $path
103104 * @param resource $resource
104- * @param Config $options Config object
105+ * @param Config $options Config object
105106 *
106107 * @return array|false false on failure file meta data on success
107108 */
@@ -134,6 +135,15 @@ public function rename($path, $newpath)
134135 return $ result ['public_id ' ] == $ newPathInfo ['filename ' ];
135136 }
136137
138+ /**
139+ * Expose the Cloudinary v2 Upload Functionality
140+ *
141+ */
142+ protected function uploadApi ()
143+ {
144+ return $ this ->cloudinary ->uploadApi ();
145+ }
146+
137147 /**
138148 * Copy a file.
139149 * Copy content from existing url.
@@ -149,6 +159,7 @@ public function copy($path, $newpath)
149159
150160 return is_array ($ result ) ? $ result ['public_id ' ] == $ newpath : false ;
151161 }
162+
152163 /**
153164 * Delete a file.
154165 *
@@ -177,6 +188,16 @@ public function deleteDir($dirname)
177188
178189 return true ;
179190 }
191+
192+ /**
193+ * Expose the Cloudinary v2 Upload Functionality
194+ *
195+ */
196+ protected function adminApi ()
197+ {
198+ return $ this ->cloudinary ->adminApi ();
199+ }
200+
180201 /**
181202 * Create a directory.
182203 *
@@ -213,7 +234,7 @@ public function has($path)
213234 */
214235 public function read ($ path )
215236 {
216- $ resource = (array ) $ this ->adminApi ()->resource ($ path );
237+ $ resource = (array )$ this ->adminApi ()->resource ($ path );
217238 $ contents = file_get_contents ($ resource ['secure_url ' ]);
218239
219240 return compact ('contents ' , 'path ' );
@@ -228,7 +249,7 @@ public function read($path)
228249 */
229250 public function readStream ($ path )
230251 {
231- $ resource = (array ) $ this ->adminApi ()->resource ($ path );
252+ $ resource = (array )$ this ->adminApi ()->resource ($ path );
232253
233254 $ stream = fopen ($ resource ['secure_url ' ], 'r ' );
234255
@@ -239,7 +260,7 @@ public function readStream($path)
239260 * List contents of a directory.
240261 *
241262 * @param string $directory
242- * @param bool $recursive
263+ * @param bool $recursive
243264 *
244265 * @return array
245266 */
@@ -250,12 +271,14 @@ public function listContents($directory = '', $hasRecursive = false)
250271 // get resources array
251272 $ response = null ;
252273 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+ );
259282 $ resources = array_merge ($ resources , $ response ['resources ' ]);
260283 } while (array_key_exists ('next_cursor ' , $ response ));
261284
@@ -266,6 +289,54 @@ public function listContents($directory = '', $hasRecursive = false)
266289 return $ resources ;
267290 }
268291
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+
269340 /**
270341 * Get all the meta data of a file or directory.
271342 *
@@ -275,17 +346,17 @@ public function listContents($directory = '', $hasRecursive = false)
275346 */
276347 public function getMetadata ($ path )
277348 {
278- return $ this ->prepareResourceMetadata ($ this ->getResource ($ path ));
349+ return $ this ->prepareResourceMetadata ($ this ->getResource ($ path ));
279350 }
280351
281352 /**
282353 * Get Resource data
283- * @param string $path
354+ * @param string $path
284355 * @return array
285356 */
286357 public function getResource ($ path )
287358 {
288- return (array ) $ this ->adminApi ()->resource ($ path );
359+ return (array )$ this ->adminApi ()->resource ($ path );
289360 }
290361
291362 /**
@@ -323,70 +394,4 @@ public function getTimestamp($path)
323394 {
324395 return $ this ->prepareTimestamp ($ this ->getResource ($ path ));
325396 }
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