Skip to content

Commit 6c5a309

Browse files
authored
Merge pull request #22 from PlatformCommunity/issue/21
Fixing GH Issue 21 with different datetime formats being returned.
2 parents a1bc9ed + 716eb2a commit 6c5a309

File tree

2 files changed

+51
-13
lines changed

2 files changed

+51
-13
lines changed

src/BunnyCDNAdapter.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,11 +247,11 @@ protected function normalizeObject(array $bunny_file_array): array
247247
),
248248
'object_name' => $bunny_file_array['ObjectName'],
249249
'size' => $bunny_file_array['Length'],
250-
'timestamp' => date_create_from_format('Y-m-d\TH:i:s.u', $bunny_file_array['LastChanged'].'000')->getTimestamp(),
250+
'timestamp' => self::parse_bunny_timestamp($bunny_file_array['LastChanged']),
251251
'server_id' => $bunny_file_array['ServerId'],
252252
'user_id' => $bunny_file_array['UserId'],
253-
'last_changed' => date_create_from_format('Y-m-d\TH:i:s.u', $bunny_file_array['LastChanged'].'000')->getTimestamp(),
254-
'date_created' => date_create_from_format('Y-m-d\TH:i:s.u', $bunny_file_array['DateCreated'].'000')->getTimestamp(),
253+
'last_changed' => self::parse_bunny_timestamp($bunny_file_array['LastChanged']),
254+
'date_created' => self::parse_bunny_timestamp($bunny_file_array['DateCreated']),
255255
'storage_zone_name' => $bunny_file_array['StorageZoneName'],
256256
'storage_zone_id' => $bunny_file_array['StorageZoneId'],
257257
'checksum' => $bunny_file_array['Checksum'],
@@ -331,4 +331,9 @@ public function getUrl(string $path): string
331331

332332
return rtrim($this->pullzone_url, '/') . '/' . ltrim($path, '/');
333333
}
334+
335+
private static function parse_bunny_timestamp(string $timestamp): int
336+
{
337+
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();
338+
}
334339
}

tests/FlysystemTestSuite.php

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace PlatformCommunity\Flysystem\BunnyCDN\Tests;
44

55
use Exception;
6+
use GuzzleHttp\Psr7\Response;
67
use League\Flysystem\Config;
78
use League\Flysystem\FileExistsException;
89
use League\Flysystem\FileNotFoundException;
@@ -15,6 +16,7 @@
1516
use PlatformCommunity\Flysystem\BunnyCDN\BunnyCDNAdapter;
1617
use PlatformCommunity\Flysystem\BunnyCDN\BunnyCDNClient;
1718
use PlatformCommunity\Flysystem\BunnyCDN\BunnyCDNRegion;
19+
use PlatformCommunity\Flysystem\BunnyCDN\Exceptions\BunnyCDNException;
1820
use Prophecy\Argument;
1921
use RuntimeException;
2022

@@ -385,22 +387,53 @@ public function it_cant_get_public_url()
385387
}
386388

387389
/**
388-
* Fix issue where `fopen` complains when opening downloaded image file#20
389-
* https://github.com/PlatformCommunity/flysystem-bunnycdn/pull/20
390+
* Github Issue - 21
391+
* https://github.com/PlatformCommunity/flysystem-bunnycdn/issues/21
390392
*
391-
* Seems to not be an issue out of v1, only v2 & v3
392-
* @throws FileNotFoundException
393+
* Issue present where the date format can come back in either one of the following formats:
394+
* - 2022-04-10T17:43:49.297
395+
* - 2022-04-10T17:43:49
396+
*
397+
* Pretty sure I'm just going to create a static method called "parse_bunny_date" within the client to handle this.
398+
* @throws BunnyCDNException
393399
*/
394400
public function test_regression_pr_20()
395401
{
396-
$image = base64_decode("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z/C/HgAGgwJ/lK3Q6wAAAABJRU5ErkJggg==");
397-
$this->givenItHasFile('path.png', $image);
398402

399-
$filesystem = new Filesystem($this->adapter);
403+
$client = new MockClient(self::STORAGE_ZONE, 'api-key');
404+
405+
$client->add_response(
406+
new Response(200, [], json_encode(
407+
[
408+
/**
409+
* First with the milliseconds
410+
*/
411+
array_merge(
412+
$client::example_file('/subfolder/example_image.png', self::STORAGE_ZONE),
413+
[
414+
'LastChanged' => date('Y-m-d\TH:i:s.v'),
415+
'DateCreated' => date('Y-m-d\TH:i:s.v'),
416+
]
417+
),
418+
/**
419+
* Then without
420+
*/
421+
array_merge(
422+
$client::example_file('/subfolder/example_image.png', self::STORAGE_ZONE),
423+
[
424+
'LastChanged' => date('Y-m-d\TH:i:s'),
425+
'DateCreated' => date('Y-m-d\TH:i:s'),
426+
]
427+
)
428+
]
429+
))
430+
);
400431

401-
$stream = $filesystem->readStream('path.png');
432+
$adapter = new BunnyCDNAdapter($client);
433+
$response = $adapter->listContents('/');
402434

403-
$this->assertIsResource($stream);
404-
$this->assertEquals($image, stream_get_contents($stream));
435+
$this->assertIsArray($response);
436+
$this->assertCount(2, $response);
405437
}
438+
406439
}

0 commit comments

Comments
 (0)