-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
47 lines (37 loc) · 1.33 KB
/
index.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
<?php
declare(strict_types=1);
require __DIR__ . '/vendor/autoload.php';
use Aws\S3\S3Client;
use Nyholm\Psr7\Response;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use function Psl\Json\typed;
use function Psl\Type\non_empty_dict;
use function Psl\Type\non_empty_string;
return new class($_ENV['BUCKET_NAME']) implements RequestHandlerInterface
{
public function __construct(private readonly string $bucketName) {}
public function handle(ServerRequestInterface $request): ResponseInterface
{
$path = $request->getUri()->getPath();
$client = new S3Client([
'region' => 'eu-west-3',
'version' => '2006-03-01'
]);
$client->registerStreamWrapper();
$fileContent = file_get_contents(sprintf('s3://%s/links.json', $this->bucketName));
if (false === $fileContent) {
throw new RuntimeException('The file "links.json" is missing.');
}
$registeredLinks = typed(
$fileContent,
non_empty_dict(
non_empty_string(),
non_empty_string()
)
);
$targetLocation = $registeredLinks[$path] ?? 'https://hephaist.io/404';
return new Response(302, ['Location' => $targetLocation]);
}
};