Skip to content

Commit 0d33d3b

Browse files
committed
Fixing GH Issue 21 with different datetime formats being returned
1 parent 2a08191 commit 0d33d3b

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

src/BunnyCDNAdapter.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ protected function normalizeObject(array $bunny_file_array): StorageAttributes
152152
),
153153
$bunny_file_array['Length'],
154154
Visibility::PUBLIC,
155-
date_create_from_format('Y-m-d\TH:i:s.v', $bunny_file_array['LastChanged'])->getTimestamp(),
155+
self::parse_bunny_timestamp($bunny_file_array['LastChanged']),
156156
$bunny_file_array['ContentType'],
157157
$this->extractExtraMetadata($bunny_file_array)
158158
)
@@ -170,7 +170,7 @@ private function extractExtraMetadata(array $bunny_file_array): array
170170
'dirname' => Util::splitPathIntoDirectoryAndFile($bunny_file_array['Path'])['dir'],
171171
'guid' => $bunny_file_array['Guid'],
172172
'object_name' => $bunny_file_array['ObjectName'],
173-
'timestamp' => date_create_from_format('Y-m-d\TH:i:s.v', $bunny_file_array['LastChanged'])->getTimestamp(),
173+
'timestamp' => self::parse_bunny_timestamp($bunny_file_array['LastChanged']),
174174
'server_id' => $bunny_file_array['ServerId'],
175175
'user_id' => $bunny_file_array['UserId'],
176176
'date_created' => $bunny_file_array['DateCreated'],
@@ -409,4 +409,9 @@ public function getUrl(string $path): string
409409

410410
return rtrim($this->pullzone_url, '/') . '/' . ltrim($path, '/');
411411
}
412+
413+
private static function parse_bunny_timestamp(string $timestamp): int
414+
{
415+
return (date_create_from_format('Y-m-d\TH:i:s.u', $timestamp) ?: date_create_from_format('Y-m-d\TH:i:s', $timestamp))->getTimestamp();
416+
}
412417
}

tests/FlysystemTestSuite.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Mockery;
1818
use PlatformCommunity\Flysystem\BunnyCDN\BunnyCDNAdapter;
1919
use PlatformCommunity\Flysystem\BunnyCDN\BunnyCDNClient;
20+
use PlatformCommunity\Flysystem\BunnyCDN\Exceptions\BunnyCDNException;
2021
use PlatformCommunity\Flysystem\BunnyCDN\Util;
2122
use Throwable;
2223

@@ -172,4 +173,53 @@ public function test_regression_pr_20()
172173
$this->assertEquals($image, stream_get_contents($stream));
173174
});
174175
}
176+
177+
/**
178+
* Github Issue - 21
179+
* https://github.com/PlatformCommunity/flysystem-bunnycdn/issues/21
180+
*
181+
* Issue present where the date format can come back in either one of the following formats:
182+
* - 2022-04-10T17:43:49.297
183+
* - 2022-04-10T17:43:49
184+
*
185+
* Pretty sure I'm just going to create a static method called "parse_bunny_date" within the client to handle this.
186+
* @throws FilesystemException
187+
*/
188+
public function test_regression_issue_21()
189+
{
190+
$client = new MockClient(self::STORAGE_ZONE, 'api-key');
191+
192+
$client->add_response(
193+
new Response(200, [], json_encode(
194+
[
195+
/**
196+
* First with the milliseconds
197+
*/
198+
array_merge(
199+
$client::example_file('/example_image.png', self::STORAGE_ZONE),
200+
[
201+
'LastChanged' => date('Y-m-d\TH:i:s.v'),
202+
'DateCreated' => date('Y-m-d\TH:i:s.v'),
203+
]
204+
),
205+
/**
206+
* Then without
207+
*/
208+
array_merge(
209+
$client::example_file('/example_image.png', self::STORAGE_ZONE),
210+
[
211+
'LastChanged' => date('Y-m-d\TH:i:s'),
212+
'DateCreated' => date('Y-m-d\TH:i:s'),
213+
]
214+
)
215+
]
216+
))
217+
);
218+
219+
$adapter = new Filesystem(new BunnyCDNAdapter($client));
220+
$response = $adapter->listContents('/')->toArray();
221+
222+
$this->assertIsArray($response);
223+
$this->assertCount(2, $response);
224+
}
175225
}

0 commit comments

Comments
 (0)