Skip to content

Commit 8f006f2

Browse files
committed
Change extension lookup switch to map array.
1 parent bc860ac commit 8f006f2

1 file changed

Lines changed: 23 additions & 21 deletions

File tree

src/Server.php

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Pdsinterop\Solid\Resources;
44

5+
use EasyRdf\Format;
56
use Pdsinterop\Solid\SolidNotifications\SolidNotificationsInterface;
67
use EasyRdf\Exception as RdfException;
78
use EasyRdf\Graph as Graph;
@@ -113,6 +114,10 @@ final public function __construct(Filesystem $filesystem, Response $response, ?G
113114
$this->filesystem = $filesystem;
114115
$this->graph = $graph ?? new Graph();
115116
$this->response = $response;
117+
118+
// Store and serve json-ld with '.json' extension instead of '.jsonld''
119+
Format::getFormat('jsonld')->setExtensions('json');
120+
116121
// @TODO: Mention \EasyRdf_Namespace::set('lm', 'https://purl.org/pdsinterop/link-metadata#');
117122
}
118123

@@ -206,7 +211,8 @@ private function handle(string $method, string $path, $contents, $request): Resp
206211
$response = $response->withStatus(400);
207212
break;
208213
}
209-
break;
214+
break;
215+
210216
case 'POST':
211217
$pathExists = $filesystem->has($path);
212218
if ($pathExists) {
@@ -216,6 +222,7 @@ private function handle(string $method, string $path, $contents, $request): Resp
216222
$pathExists = true;
217223
$mimetype = self::MIME_TYPE_DIRECTORY;
218224
}
225+
219226
if ($pathExists === true) {
220227
if (isset($mimetype) && $mimetype === self::MIME_TYPE_DIRECTORY) {
221228
$contentType= explode(";", $request->getHeaderLine("Content-Type"))[0];
@@ -232,25 +239,18 @@ private function handle(string $method, string $path, $contents, $request): Resp
232239
$response = $this->handleCreateDirectoryRequest($response, $path . $filename);
233240
break;
234241
default:
235-
// FIXME: make this list complete for at least the things we'd expect (turtle, n3, jsonld, ntriples, rdf);
236-
switch ($contentType) {
237-
case '':
238-
// FIXME: if no content type was passed, we should reject the request according to the spec;
239-
break;
240-
case "text/plain":
241-
$filename .= ".txt";
242-
break;
243-
case "text/turtle":
244-
$filename .= ".ttl";
245-
break;
246-
case "text/html":
247-
$filename .= ".html";
248-
break;
249-
case "application/json":
250-
case "application/ld+json":
251-
$filename .= ".json";
252-
break;
242+
// FIXME: if no content type was passed, we should reject the request according to the spec;
243+
foreach (Format::getFormats() as $format) {
244+
$mimeTypes = array_keys($format->getMimeTypes());
245+
foreach ($mimeTypes as $mimeType) {
246+
$extensions[$mimeType] = '.'.$format->getDefaultExtension();
247+
}
248+
}
249+
250+
if (isset($extensions[$contentType])) {
251+
$filename .= $extensions[$contentType];
253252
}
253+
254254
$response = $this->handleCreateRequest($response, $path . $filename, $contents);
255255
break;
256256
}
@@ -260,7 +260,8 @@ private function handle(string $method, string $path, $contents, $request): Resp
260260
} else {
261261
$response = $this->handleCreateRequest($response, $path, $contents);
262262
}
263-
break;
263+
break;
264+
264265
case 'PUT':
265266
$link = $request->getHeaderLine("Link");
266267
switch ($link) {
@@ -275,7 +276,8 @@ private function handle(string $method, string $path, $contents, $request): Resp
275276
}
276277
break;
277278
}
278-
break;
279+
break;
280+
279281
default:
280282
throw Exception::create(self::ERROR_UNKNOWN_HTTP_METHOD, [$method]);
281283
break;

0 commit comments

Comments
 (0)