ResourceController.php in Drupal 9
File
core/modules/media/tests/modules/media_test_oembed/src/Controller/ResourceController.php
View source
<?php
namespace Drupal\media_test_oembed\Controller;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class ResourceController {
public function get(Request $request) {
$asset_url = $request->query
->get('url');
$resources = \Drupal::state()
->get(static::class, []);
if ($resources[$asset_url] === 404) {
$response = new Response('Not Found', 404);
}
else {
$content = file_get_contents($resources[$asset_url]);
$response = new Response($content);
$response->headers
->set('Content-Type', 'application/' . pathinfo($resources[$asset_url], PATHINFO_EXTENSION));
}
return $response;
}
public function getThumbnailWithNoExtension() {
$response = new BinaryFileResponse('core/misc/druplicon.png');
$response->headers
->set('Content-Type', 'image/png');
return $response;
}
public static function setResourceUrl($asset_url, $resource_path) {
$resources = \Drupal::state()
->get(static::class, []);
$resources[$asset_url] = $resource_path;
\Drupal::state()
->set(static::class, $resources);
}
public static function setResource404($asset_url) {
$resources = \Drupal::state()
->get(static::class, []);
$resources[$asset_url] = 404;
\Drupal::state()
->set(static::class, $resources);
}
}