15
15
16
16
class CloudinaryStorageAdapter implements ChecksumProvider, FilesystemAdapter
17
17
{
18
- private Cloudinary $ cloudinary ;
19
-
20
18
private PathPrefixer $ prefixer ;
21
19
22
20
private MimeTypeDetector $ mimeTypeDetector ;
23
21
24
- public function __construct (array $ config , string $ prefix = '' , ?MimeTypeDetector $ mimeTypeDetector = null )
22
+ public function __construct (private Cloudinary $ cloudinary , string $ prefix = '' , ?MimeTypeDetector $ mimeTypeDetector = null )
25
23
{
26
24
$ this ->prefixer = new PathPrefixer ($ prefix );
27
25
$ this ->mimeTypeDetector = $ mimeTypeDetector ?: new FinfoMimeTypeDetector ;
28
-
29
- if (isset ($ config ['url ' ])) {
30
- $ this ->cloudinary = new Cloudinary ($ config ['url ' ]);
31
- } else {
32
- $ this ->cloudinary = new Cloudinary ([
33
- 'cloud ' => [
34
- 'cloud_name ' => $ config ['cloud ' ],
35
- 'api_key ' => $ config ['key ' ],
36
- 'api_secret ' => $ config ['secret ' ],
37
- ],
38
- 'url ' => [
39
- 'secure ' => $ config ['secure ' ] ?? false ,
40
- ],
41
- ]);
42
- }
43
26
}
44
27
45
28
public function copy (string $ source , string $ destination , Config $ config ): void
@@ -79,46 +62,59 @@ public function directoryExists(string $path): bool
79
62
80
63
public function fileExists (string $ path ): bool
81
64
{
65
+ [$ id , $ type ] = $ this ->prepareResource ($ path );
66
+
82
67
try {
83
- $ this ->cloudinary ->adminApi ()->asset ($ path );
68
+ $ this ->cloudinary ->adminApi ()->asset ($ id , ['resource_type ' => $ type ]);
69
+
70
+ return true ;
84
71
} catch (\Throwable $ e ) {
85
72
return false ;
86
73
}
87
-
88
- return true ;
89
74
}
90
75
91
76
public function fileSize (string $ path ): FileAttributes
92
77
{
93
- $ resource = $ this ->cloudinary ->adminApi ()->asset ($ path );
78
+ [$ id , $ type ] = $ this ->prepareResource ($ path );
79
+ $ resource = $ this ->cloudinary ->adminApi ()->asset ($ id , ['resource_type ' => $ type ]);
94
80
95
- return new FileAttributes ($ path , $ resource[ 'bytes ' ] );
81
+ return new FileAttributes ($ path , $ resource-> offsetGet ( 'bytes ' ) );
96
82
}
97
83
98
84
public function lastModified (string $ path ): FileAttributes
99
85
{
100
- $ resource = $ this ->cloudinary ->adminApi ()->asset ($ path );
86
+ [$ id , $ type ] = $ this ->prepareResource ($ path );
87
+ $ resource = $ this ->cloudinary ->adminApi ()->asset ($ id , ['resource_type ' => $ type ]);
101
88
102
- return new FileAttributes ($ path , null , null , $ resource ['created_at ' ]);
89
+ $ dateTime = new \DateTime ($ resource ->offsetGet ('created_at ' ));
90
+
91
+ return new FileAttributes ($ path , null , null , $ dateTime ->getTimestamp ());
103
92
}
104
93
105
94
public function listContents (string $ path , bool $ deep ): array |\Traversable
106
95
{
107
96
$ resources = [];
108
-
109
97
$ response = null ;
110
98
111
99
do {
112
- $ response = ( array ) $ this ->cloudinary ->adminApi ()->assets ([
100
+ $ response = $ this ->cloudinary ->adminApi ()->assets ([
113
101
'type ' => 'upload ' ,
114
102
'prefix ' => $ path ,
115
103
'max_results ' => 500 ,
116
- 'next_cursor ' => $ response[ 'next_cursor ' ] ?? null ,
104
+ 'next_cursor ' => isset ( $ response) ? $ response -> offsetGet ( 'next_cursor ' ) : null ,
117
105
]);
118
- $ resources = array_merge ($ resources , $ response ['resources ' ]);
119
- } while (array_key_exists ('next_cursor ' , $ response ));
120
-
121
- return array_map (fn ($ resource ) => new FileAttributes ($ resource ['public_id ' ], $ resource ['bytes ' ], null , $ resource ['created_at ' ]), $ resources );
106
+ $ resources = array_merge ($ resources , $ response ->offsetGet ('resources ' ));
107
+ } while ($ response ->offsetExists ('next_cursor ' ));
108
+
109
+ return array_map (
110
+ fn ($ resource ) => new FileAttributes (
111
+ $ resource ['public_id ' ],
112
+ $ resource ['bytes ' ],
113
+ null ,
114
+ (new \DateTime ($ resource ['created_at ' ]))->getTimestamp ()
115
+ ),
116
+ $ resources
117
+ );
122
118
}
123
119
124
120
public function mimeType (string $ path ): FileAttributes
@@ -141,19 +137,17 @@ public function move(string $source, string $destination, Config $config): void
141
137
public function read (string $ path ): string
142
138
{
143
139
[$ id , $ type ] = $ this ->prepareResource ($ path );
144
-
145
140
$ resource = $ this ->cloudinary ->adminApi ()->asset ($ id , ['resource_type ' => $ type ]);
146
141
147
- return file_get_contents ($ resource[ 'secure_url ' ] );
142
+ return file_get_contents ($ resource-> offsetGet ( 'secure_url ' ) );
148
143
}
149
144
150
145
public function readStream (string $ path )
151
146
{
152
147
[$ id , $ type ] = $ this ->prepareResource ($ path );
153
-
154
148
$ resource = $ this ->cloudinary ->adminApi ()->asset ($ id , ['resource_type ' => $ type ]);
155
149
156
- return fopen ($ resource[ 'secure_url ' ] , 'rb ' );
150
+ return fopen ($ resource-> offsetGet ( 'secure_url ' ) , 'rb ' );
157
151
}
158
152
159
153
public function setVisibility (string $ path , string $ visibility ): void
@@ -181,7 +175,7 @@ public function checksum(string $path, Config $config): string
181
175
return hash ($ algo , file_get_contents ($ resource ['secure_url ' ]));
182
176
}
183
177
184
- private function prepareResource (string $ path ): array
178
+ public function prepareResource (string $ path ): array
185
179
{
186
180
$ id = pathinfo ($ path , PATHINFO_FILENAME );
187
181
$ mimeType = $ this ->mimeTypeDetector ->detectMimeTypeFromPath ($ path );
0 commit comments