aboutsummaryrefslogtreecommitdiffhomepage
path: root/routes/storage.php
blob: 2ea0fc7f297b1466f5cf4989f56b5211d4b4ef88 (plain)
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
<?php

use Digitigrade\HttpResponseStatus\NotFound;
use Digitigrade\Model\StorageObject;
use Digitigrade\Router;
use Digitigrade\StorageProvider\FilesystemStorage;

Router::getInstance()->mount('/storage/:oid', function (array $args) {
    // find the storage object regardless of backend provider
    $obj = StorageObject::findByOid($args['oid']);
    if ($obj == null) {
        throw new NotFound('unknown object');
    }
    header('Cache-Control: max-age=604800'); // 1 week
    header("Content-Disposition: inline; filename*=UTF-8''" . urlencode($obj->filename));
    $obj->passthrough();
});

Router::getInstance()->mount('/upload/:token', function (array $args) {
    // for direct urls for the FilesystemStorage backend, in case the server
    // admin didn't put a special handler in their reverse proxy
    $storage = new FilesystemStorage();
    $token = $args['token'];
    if (!$storage->exists($token)) {
        throw new NotFound('unknown object');
    }
    header('Cache-Control: max-age=604800'); // 1 week
    $storage->passthrough($token);
});