2222 */
2323class CloudinaryAdapter implements FilesystemAdapter
2424{
25-
26- /** Cloudinary\Cloudinary */
25+ /**
26+ * @var Cloudinary
27+ */
2728 protected Cloudinary $ cloudinary ;
2829
30+ /**
31+ * The media resource extensions supported by cloudinary.
32+ */
33+ public $ mediaExtensions = [
34+ 'jpg ' , 'jpeg ' , 'png ' , 'gif ' , 'pdf ' , 'bmp ' , 'tiff ' , 'svg ' , 'ico ' , 'eps ' , 'psd ' , 'webp ' , 'jxr ' , 'wdp ' ,
35+ 'mpeg ' , 'mp4 ' , 'mkv ' , 'mov ' , 'flv ' , 'avi ' , '3gp ' , '3g2 ' , 'wmv ' , 'webm ' , 'ogv ' , 'mxf ' , 'avif ' ,
36+ ];
2937
3038 /**
3139 * Constructor
@@ -70,28 +78,42 @@ public function write(string $path, string $contents, Config $config): void
7078 */
7179 public function writeStream (string $ path , $ contents , Config $ config ): void
7280 {
73- $ publicId = $ config ->get ('public_id ' , $ path );
81+ $ publicId = $ this -> preparePublicId ( $ config ->get ('public_id ' , $ path) );
7482
7583 $ resourceType = $ config ->get ('resource_type ' , 'auto ' );
7684
77- $ fileExtension = pathinfo ($ publicId , PATHINFO_EXTENSION );
78-
79- $ newPublicId = $ fileExtension ? substr ($ publicId , 0 , - (strlen ($ fileExtension ) + 1 )) : $ publicId ;
80-
8185 $ uploadOptions = [
82- 'public_id ' => $ newPublicId ,
86+ 'public_id ' => $ publicId ,
8387 'resource_type ' => $ resourceType
8488 ];
8589
8690 $ resourceMetadata = stream_get_meta_data ($ contents );
8791
88- resolve (CloudinaryEngine::class)->upload ($ resourceMetadata ['uri ' ], $ uploadOptions );
92+ $ this ->upload ($ resourceMetadata ['uri ' ], $ uploadOptions );
93+ }
94+
95+ /**
96+ * Prepare the given public ID for cloudinary.
97+ */
98+ public function preparePublicId ($ path ): string
99+ {
100+ $ extension = pathinfo ($ path , PATHINFO_EXTENSION );
101+
102+ return str ($ path )
103+ ->when ($ this ->isMedia ($ extension ))
104+ ->beforeLast ('. ' .$ extension );
89105 }
90106
107+ /**
108+ * Determine if the given extension is a media extension.
109+ */
110+ public function isMedia ($ extension ): bool
111+ {
112+ return in_array ($ extension , $ this ->mediaExtensions );
113+ }
91114
92115 /**
93116 * Rename a file.
94- * Paths without extensions.
95117 *
96118 * @param string $path
97119 * @param string $newpath
@@ -107,14 +129,16 @@ public function rename(string $path, string $newpath): bool
107129
108130 $ remoteNewPath = ($ pathInfo ['dirname ' ] != '. ' ) ? $ newPathInfo ['dirname ' ] . '/ ' . $ newPathInfo ['filename ' ] : $ newPathInfo ['filename ' ];
109131
110- $ result = $ this ->uploadApi ()->rename ($ remotePath , $ remoteNewPath );
132+ $ result = $ this ->uploadApi ()->rename (
133+ $ this ->preparePublicId ($ remotePath ),
134+ $ this ->preparePublicId ($ remoteNewPath )
135+ );
111136
112137 return $ result ['public_id ' ] == $ newPathInfo ['filename ' ];
113138 }
114139
115140 /**
116- * Expose the Cloudinary v2 Upload Functionality
117- *
141+ * Expose the Cloudinary v2 Upload Functionality.
118142 */
119143 protected function uploadApi (): UploadApi
120144 {
@@ -147,6 +171,9 @@ protected function upload(string $file, array $options = []): void
147171 */
148172 public function copy (string $ source , string $ destination , Config $ config ): void
149173 {
174+ $ source = $ this ->preparePublicId ($ source );
175+ $ destination = $ this ->preparePublicId ($ destination );
176+
150177 $ this ->uploadApi ()->upload ($ source , ['public_id ' => $ destination ]);
151178 }
152179
@@ -160,8 +187,7 @@ public function copy(string $source, string $destination, Config $config): void
160187 public function delete (string $ path ): void
161188 {
162189 try {
163-
164- $ result = $ this ->uploadApi ()->destroy ($ path );
190+ $ result = $ this ->uploadApi ()->destroy ($ this ->preparePublicId ($ path ));
165191 $ finalResult = is_array ($ result ) && $ result ['result ' ] == 'ok ' ;
166192
167193 if ($ finalResult != 'ok ' ) {
@@ -221,7 +247,7 @@ public function createDirectory(string $path, Config $config): void
221247 public function fileExists (string $ path ): bool
222248 {
223249 try {
224- $ this ->adminApi ()->asset ($ path );
250+ $ this ->adminApi ()->asset ($ this -> preparePublicId ( $ path) );
225251 } catch (Exception ) {
226252 return false ;
227253 }
@@ -250,7 +276,7 @@ public function directoryExists(string $path): bool
250276 */
251277 public function read (string $ path ): string
252278 {
253- $ resource = (array )$ this ->adminApi ()->asset ($ path );
279+ $ resource = (array )$ this ->adminApi ()->asset ($ this -> preparePublicId ( $ path) );
254280
255281 return file_get_contents ($ resource ['secure_url ' ]);
256282 }
@@ -264,7 +290,7 @@ public function read(string $path): string
264290 */
265291 public function readStream (string $ path ): bool
266292 {
267- $ resource = (array )$ this ->adminApi ()->asset ($ path );
293+ $ resource = (array )$ this ->adminApi ()->asset ($ this -> preparePublicId ( $ path) );
268294
269295 return fopen ($ resource ['secure_url ' ], 'rb ' );
270296 }
@@ -417,7 +443,7 @@ public function getMetadata(string $path): array
417443 */
418444 public function getResource (string $ path ): array
419445 {
420- return (array )$ this ->adminApi ()->asset ($ path );
446+ return (array )$ this ->adminApi ()->asset ($ this -> preparePublicId ( $ path) );
421447 }
422448
423449 /**
0 commit comments