Skip to content

Commit

Permalink
Merge pull request #22 from PlatformCommunity/issue/21
Browse files Browse the repository at this point in the history
Fixing GH Issue 21 with different datetime formats being returned.
  • Loading branch information
sifex authored Apr 15, 2022
2 parents a1bc9ed + 716eb2a commit 6c5a309
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 13 deletions.
11 changes: 8 additions & 3 deletions src/BunnyCDNAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,11 @@ protected function normalizeObject(array $bunny_file_array): array
),
'object_name' => $bunny_file_array['ObjectName'],
'size' => $bunny_file_array['Length'],
'timestamp' => date_create_from_format('Y-m-d\TH:i:s.u', $bunny_file_array['LastChanged'].'000')->getTimestamp(),
'timestamp' => self::parse_bunny_timestamp($bunny_file_array['LastChanged']),
'server_id' => $bunny_file_array['ServerId'],
'user_id' => $bunny_file_array['UserId'],
'last_changed' => date_create_from_format('Y-m-d\TH:i:s.u', $bunny_file_array['LastChanged'].'000')->getTimestamp(),
'date_created' => date_create_from_format('Y-m-d\TH:i:s.u', $bunny_file_array['DateCreated'].'000')->getTimestamp(),
'last_changed' => self::parse_bunny_timestamp($bunny_file_array['LastChanged']),
'date_created' => self::parse_bunny_timestamp($bunny_file_array['DateCreated']),
'storage_zone_name' => $bunny_file_array['StorageZoneName'],
'storage_zone_id' => $bunny_file_array['StorageZoneId'],
'checksum' => $bunny_file_array['Checksum'],
Expand Down Expand Up @@ -331,4 +331,9 @@ public function getUrl(string $path): string

return rtrim($this->pullzone_url, '/') . '/' . ltrim($path, '/');
}

private static function parse_bunny_timestamp(string $timestamp): int
{
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();
}
}
53 changes: 43 additions & 10 deletions tests/FlysystemTestSuite.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PlatformCommunity\Flysystem\BunnyCDN\Tests;

use Exception;
use GuzzleHttp\Psr7\Response;
use League\Flysystem\Config;
use League\Flysystem\FileExistsException;
use League\Flysystem\FileNotFoundException;
Expand All @@ -15,6 +16,7 @@
use PlatformCommunity\Flysystem\BunnyCDN\BunnyCDNAdapter;
use PlatformCommunity\Flysystem\BunnyCDN\BunnyCDNClient;
use PlatformCommunity\Flysystem\BunnyCDN\BunnyCDNRegion;
use PlatformCommunity\Flysystem\BunnyCDN\Exceptions\BunnyCDNException;
use Prophecy\Argument;
use RuntimeException;

Expand Down Expand Up @@ -385,22 +387,53 @@ public function it_cant_get_public_url()
}

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

$filesystem = new Filesystem($this->adapter);
$client = new MockClient(self::STORAGE_ZONE, 'api-key');

$client->add_response(
new Response(200, [], json_encode(
[
/**
* First with the milliseconds
*/
array_merge(
$client::example_file('/subfolder/example_image.png', self::STORAGE_ZONE),
[
'LastChanged' => date('Y-m-d\TH:i:s.v'),
'DateCreated' => date('Y-m-d\TH:i:s.v'),
]
),
/**
* Then without
*/
array_merge(
$client::example_file('/subfolder/example_image.png', self::STORAGE_ZONE),
[
'LastChanged' => date('Y-m-d\TH:i:s'),
'DateCreated' => date('Y-m-d\TH:i:s'),
]
)
]
))
);

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

$this->assertIsResource($stream);
$this->assertEquals($image, stream_get_contents($stream));
$this->assertIsArray($response);
$this->assertCount(2, $response);
}

}

0 comments on commit 6c5a309

Please sign in to comment.