Repository navigation
Expand file tree
/
Copy pathAssetRoute.php
More file actions
107 lines (97 loc) · 2.97 KB
/
Copy pathAssetRoute.php
File metadata and controls
107 lines (97 loc) · 2.97 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
namespace Tripomatic\NetteAssetic\Application;
use Assetic\Asset\AssetInterface;
use Assetic\AssetManager;
use Nette\Application;
use Nette\Http;
class AssetRoute implements Application\IRouter
{
/** @var AssetManager */
private $assetManager;
private static $extensionsToMimeTypes = [
'ai' => 'application/postscript',
'bmp' => 'image/bmp',
'cab' => 'application/vnd.ms-cab-compressed',
'css' => 'text/css',
'doc' => 'application/msword',
'eot' => 'application/vnd.ms-fontobject',
'eps' => 'application/postscript',
'exe' => 'application/x-msdownload',
'flv' => 'video/x-flv',
'gif' => 'image/gif',
'htm' => 'text/html',
'html' => 'text/html',
'ico' => 'image/vnd.microsoft.icon',
'jpe' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpeg',
'js' => 'application/javascript',
'json' => 'application/json',
'mov' => 'video/quicktime',
'mp3' => 'audio/mpeg',
'msi' => 'application/x-msdownload',
'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
'odt' => 'application/vnd.oasis.opendocument.text',
'otf' => 'application/x-font-opentype',
'pdf' => 'application/pdf',
'php' => 'text/html',
'png' => 'image/png',
'ppt' => 'application/vnd.ms-powerpoint',
'ps' => 'application/postscript',
'psd' => 'image/vnd.adobe.photoshop',
'qt' => 'video/quicktime',
'rar' => 'application/x-rar-compressed',
'rtf' => 'application/rtf',
'sfnt' => 'application/font-sfnt',
'svg' => 'image/svg+xml',
'svg' => 'image/svg+xml',
'svgz' => 'image/svg+xml',
'swf' => 'application/x-shockwave-flash',
'tif' => 'image/tiff',
'tiff' => 'image/tiff',
'ttf' => 'application/x-font-truetype',
'txt' => 'text/plain',
'woff' => 'application/font-woff',
'woff2' => 'application/font-woff2',
'xls' => 'application/vnd.ms-excel',
'xml' => 'application/xml',
'zip' => 'application/zip',
];
public function __construct(
AssetManager $assetManager
) {
$this->assetManager = $assetManager;
}
public function match(Http\IRequest $httpRequest)
{
$path = $httpRequest->getUrl()->getPath();
foreach ($this->assetManager->getNames() as $name) {
$asset = $this->assetManager->get($name);
if ('/' . $asset->getTargetPath() === $path) {
$this->dumpAsset($asset);
}
}
}
public function constructUrl(Application\Request $appRequest, Http\Url $refUrl)
{
}
private function dumpAsset(AssetInterface $asset)
{
// HTTP caching
$mtime = gmdate('D, d M y H:i:s', $asset->getLastModified()) . 'GMT';
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $mtime = $_SERVER['HTTP_IF_MODIFIED_SINCE']) {
header('HTTP/1.1 304 Not Modified');
exit();
}
// set headers & dump asset
$extension = pathinfo($asset->getTargetPath(), PATHINFO_EXTENSION);
if (array_key_exists($extension, self::$extensionsToMimeTypes)) {
$mimeType = self::$extensionsToMimeTypes[$extension];
header("Content-Type: $mimeType");
header("Last-Modified: $mtime");
header('Cache-Control: public');
}
echo $asset->dump();
exit();
}
}