fix incorrct content-type header

This commit is contained in:
Babibubebon 2020-01-20 01:09:35 +09:00
parent 00325bc094
commit 447d0168b3
2 changed files with 20 additions and 6 deletions

View file

@ -2,6 +2,7 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Http\Middleware\ContentNegotiatorMiddleware;
use Illuminate\Http\Request; use Illuminate\Http\Request;
class ResourceController extends Controller class ResourceController extends Controller
@ -64,6 +65,8 @@ class ResourceController extends Controller
} catch (\EasyRdf_Exception $e) { } catch (\EasyRdf_Exception $e) {
abort(400); abort(400);
} }
return $data; $type = ContentNegotiatorMiddleware::mimetypeFromExtension($ext);
return response($data)
->header('Content-Type', $type);
} }
} }

View file

@ -9,23 +9,34 @@ class ContentNegotiatorMiddleware
{ {
public $defaultType = 'text/html'; public $defaultType = 'text/html';
public $acceptableTypes = [ public static $acceptableTypes = [
'text/html' => 'html', 'text/html' => 'html',
'application/xhtml+xml' => 'html', 'application/xhtml+xml' => 'html',
'text/n3' => 'data', 'text/n3' => 'data',
'text/turtle' => 'data', 'text/turtle' => 'data',
'application/n-triples' => 'data', 'application/n-triples' => 'data',
'application/rdf+xml' => 'data', 'application/rdf+xml' => 'data',
'application/json' => 'data',
'application/ld+json' => 'data',
]; ];
public $fileExtensions = [ public static $fileExtensions = [
'text/html' => '.html', 'text/html' => '.html',
'application/xhtml+xml' => '.html', 'application/xhtml+xml' => '.html',
'text/turtle' => '.ttl', 'text/turtle' => '.ttl',
'application/n-triples' => '.nt', 'application/n-triples' => '.nt',
'application/rdf+xml' => '.rdf', 'application/rdf+xml' => '.rdf',
'application/json' => '.json',
'application/ld+json' => '.jsonld',
]; ];
public static function mimetypeFromExtension($ext)
{
$extensionToMimetype = array_flip(self::$fileExtensions);
return array_key_exists($ext, $extensionToMimetype)
? $extensionToMimetype[$ext] : null;
}
/** /**
* Handle an incoming request. * Handle an incoming request.
* *
@ -53,15 +64,15 @@ class ContentNegotiatorMiddleware
$id = $request->route('id'); $id = $request->route('id');
$negotiatedType = $this->defaultType; $negotiatedType = $this->defaultType;
foreach ($accepts as $mime => $q) { foreach ($accepts as $mime => $q) {
if (array_key_exists($mime, $this->acceptableTypes)) { if (array_key_exists($mime, self::$acceptableTypes)) {
$negotiatedType = $mime; $negotiatedType = $mime;
} }
} }
$redirectTo = $this->acceptableTypes[$negotiatedType] . '.' . $datasetName; $redirectTo = self::$acceptableTypes[$negotiatedType] . '.' . $datasetName;
$params = ['id' => $id]; $params = ['id' => $id];
if (substr($redirectTo, 0, 4) === 'data') { if (substr($redirectTo, 0, 4) === 'data') {
$params['ext'] = $this->fileExtensions[$negotiatedType]; $params['ext'] = self::$fileExtensions[$negotiatedType];
} }
return redirect()->route($redirectTo, $params, 303); return redirect()->route($redirectTo, $params, 303);
} }