2019-02-16 00:06:51 +09:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\MetadataResolver;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
2019-02-17 02:58:36 +09:00
|
|
|
use GuzzleHttp\Client;
|
2019-02-16 00:06:51 +09:00
|
|
|
|
|
|
|
class CienResolver extends MetadataResolver
|
|
|
|
{
|
2019-02-17 02:58:36 +09:00
|
|
|
/**
|
|
|
|
* @var Client
|
|
|
|
*/
|
|
|
|
private $client;
|
|
|
|
/**
|
|
|
|
* @var OGPResolver
|
|
|
|
*/
|
|
|
|
private $ogpResolver;
|
|
|
|
|
|
|
|
public function __construct(Client $client, OGPResolver $ogpResolver)
|
|
|
|
{
|
|
|
|
$this->client = $client;
|
|
|
|
$this->ogpResolver = $ogpResolver;
|
|
|
|
}
|
|
|
|
|
2019-02-16 00:06:51 +09:00
|
|
|
public function resolve(string $url): Metadata
|
|
|
|
{
|
2019-02-17 02:58:36 +09:00
|
|
|
$res = $this->client->get($url);
|
2019-09-12 05:26:03 +09:00
|
|
|
$metadata = $this->ogpResolver->parse((string) $res->getBody());
|
2019-02-16 00:06:51 +09:00
|
|
|
|
2020-10-17 14:01:28 +09:00
|
|
|
// 画像URLのJWTから有効期限を拾う
|
2019-09-10 07:03:46 +09:00
|
|
|
parse_str(parse_url($metadata->image, PHP_URL_QUERY), $params);
|
2020-10-17 14:01:28 +09:00
|
|
|
if (empty($params['jwt'])) {
|
|
|
|
throw new \RuntimeException('Parameter "jwt" not found. Image=' . $metadata->image . ' Source=' . $url);
|
2019-02-16 00:06:51 +09:00
|
|
|
}
|
2020-10-17 14:01:28 +09:00
|
|
|
$parts = explode('.', $params['jwt']);
|
|
|
|
if (count($parts) !== 3) {
|
|
|
|
throw new \RuntimeException('Invalid jwt. Image=' . $metadata->image . ' Source=' . $url);
|
|
|
|
}
|
|
|
|
$payload = json_decode(base64_decode(str_replace(['-', '_'], ['+', '/'], $parts[1])), true);
|
|
|
|
|
|
|
|
$metadata->expires_at = Carbon::createFromTimestamp($payload['exp']);
|
2019-09-10 07:03:46 +09:00
|
|
|
|
|
|
|
return $metadata;
|
2019-02-16 00:06:51 +09:00
|
|
|
}
|
|
|
|
}
|