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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
<?php N2Loader::import('libraries.cache.cache');
class N2CacheImage extends N2Cache {
protected $_storageEngine = 'filesystem';
protected $lazy = false;
protected function getScope() { return 'image'; }
public function setLazy($lazy) { $this->lazy = $lazy; }
public function makeCache($fileExtension, $callable, $parameters = array(), $hash = false) {
if (!$hash) { $hash = $this->generateHash($fileExtension, $callable, $parameters); }
if (strpos($parameters[1], '?') !== false) { $fileNameParts = explode('?', $parameters[1]); $keepFileName = pathinfo($fileNameParts[0], PATHINFO_FILENAME); } else { $keepFileName = pathinfo($parameters[1], PATHINFO_FILENAME); }
$fileName = $hash . (!empty($keepFileName) ? '/' . $keepFileName : ''); $fileNameWithExtension = $fileName . '.' . $fileExtension;
$isCached = $this->exists($fileNameWithExtension); if ($isCached) { if (!$this->testManifestFile($fileName, $parameters[1])) { $isCached = false; } }
if (!$isCached) { if ($this->lazy) { return $parameters[1]; }
array_unshift($parameters, $this->getPath($fileNameWithExtension)); call_user_func_array($callable, $parameters);
$this->createManifestFile($fileName, $parameters[2]); }
return $this->getPath($fileNameWithExtension); }
private function generateHash($fileExtension, $callable, $parameters) { return md5(json_encode(array( $fileExtension, $callable, $parameters ))); }
protected function testManifestFile($fileName, $originalFile) { $manifestKey = $this->getManifestKey($fileName); if ($this->exists($manifestKey)) {
$manifestData = json_decode($this->get($manifestKey), true);
$newManifestData = $this->getManifestData($originalFile); if ($manifestData['mtime'] == $newManifestData['mtime']) { return true; } } else { // Backward compatibility $this->createManifestFile($fileName, $originalFile);
return true; }
return false; }
protected function createManifestFile($fileName, $originalFile) {
$this->set($this->getManifestKey($fileName), json_encode($this->getManifestData($originalFile))); }
private function getManifestData($originalFile) { $manifestData = array(); if (strpos($originalFile, '//') !== false) { $manifestData['mtime'] = $this->getRemoteMTime($originalFile); } else { $manifestData['mtime'] = filemtime($originalFile); }
return $manifestData; }
private function getRemoteMTime($url) { $h = get_headers($url, 1); if (!$h || strpos($h[0], '200') !== false) { foreach ($h as $k => $v) { if (strtolower(trim($k)) == "last-modified") { return (new DateTime($v))->getTimestamp(); } } }
return 0; }
protected function getManifestKey($fileName) { return $fileName . '.manifest'; } }
class N2StoreImage extends N2Cache {
protected $_storageEngine = 'filesystem';
protected function getScope() { return 'image'; }
public function makeCache($fileName, $content) { if (!$this->isImage($fileName)) { return false; }
if (!$this->exists($fileName)) { $this->set($fileName, $content); }
return $this->getPath($fileName); }
private function isImage($fileName) { $supported_image = array( 'gif', 'jpg', 'jpeg', 'png', 'mp4', 'mp3', 'webp', 'svg' );
$ext = strtolower(pathinfo($fileName, PATHINFO_EXTENSION)); if (in_array($ext, $supported_image)) { return true; }
return false; } }
|