-
Notifications
You must be signed in to change notification settings - Fork 0
/
GhoResourceFetcher.php
67 lines (55 loc) · 2.13 KB
/
GhoResourceFetcher.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
namespace Drupal\gho_general\OEmbed;
use Drupal\Component\Serialization\Json;
use GuzzleHttp\Exception\RequestException;
use Drupal\media\OEmbed\ResourceFetcher;
/**
* Fetches and caches oEmbed resources.
*/
class GhoResourceFetcher extends ResourceFetcher {
/**
* {@inheritdoc}
*
* Same code as Drupal\media\OEmbed\ResourceFetcher::fetchResource() with the
* added handling of youtube responses with a text/html content type and check
* on the decoded data.
*/
public function fetchResource($url) {
$cache_id = "media:oembed_resource:$url";
$cached = $this->cacheBackend->get($cache_id);
if ($cached) {
return $this->createResource($cached->data, $url);
}
try {
$response = $this->httpClient->get($url);
}
catch (RequestException $e) {
throw new ResourceException('Could not retrieve the oEmbed resource.', $url, [], $e);
}
[$format] = $response->getHeader('Content-Type');
$content = (string) $response->getBody();
if (strstr($format, 'text/xml') || strstr($format, 'application/xml')) {
$data = $this->parseResourceXml($content, $url);
}
elseif (strstr($format, 'text/javascript') || strstr($format, 'application/json')) {
$data = Json::decode($content);
}
// Sometimes the youtube oembed proxy returns the wrong content-type while
// the body is the correct JSON.
elseif (strstr($format, 'text/html') && strpos($url, 'youtube') !== FALSE) {
$data = Json::decode($content);
}
// If the response is neither XML nor JSON, we are in bat country.
else {
throw new ResourceException('The fetched resource did not have a valid Content-Type header.', $url);
}
// Ensure we have at least a semblance of correct data before caching it.
// Json::decode() doesn't throw an exception when failing to parse the data.
// ResourceFetcher::createResource() will do additional checks later.
if (!is_array($data)) {
throw new ResourceException('The fetched resource could not be parsed.', $url);
}
$this->cacheBackend->set($cache_id, $data);
return $this->createResource($data, $url);
}
}