|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright 2017, Roeland Jago Douma <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* @author Roeland Jago Douma <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* @license GNU AGPL version 3 or any later version |
|
8
|
|
|
* |
|
9
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
10
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
11
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
12
|
|
|
* License, or (at your option) any later version. |
|
13
|
|
|
* |
|
14
|
|
|
* This program is distributed in the hope that it will be useful, |
|
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17
|
|
|
* GNU Affero General Public License for more details. |
|
18
|
|
|
* |
|
19
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
20
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
21
|
|
|
* |
|
22
|
|
|
*/ |
|
23
|
|
|
namespace OC\Template; |
|
24
|
|
|
|
|
25
|
|
|
use OC\SystemConfig; |
|
26
|
|
|
use OCP\ICache; |
|
27
|
|
|
use OCP\Files\IAppData; |
|
28
|
|
|
use OCP\Files\NotFoundException; |
|
29
|
|
|
use OCP\Files\NotPermittedException; |
|
30
|
|
|
use OCP\Files\SimpleFS\ISimpleFolder; |
|
31
|
|
|
use OCP\ILogger; |
|
32
|
|
|
use OCP\IURLGenerator; |
|
33
|
|
|
|
|
34
|
|
|
class JSCombiner { |
|
35
|
|
|
|
|
36
|
|
|
/** @var IAppData */ |
|
37
|
|
|
protected $appData; |
|
38
|
|
|
|
|
39
|
|
|
/** @var IURLGenerator */ |
|
40
|
|
|
protected $urlGenerator; |
|
41
|
|
|
|
|
42
|
|
|
/** @var ICache */ |
|
43
|
|
|
protected $depsCache; |
|
44
|
|
|
|
|
45
|
|
|
/** @var SystemConfig */ |
|
46
|
|
|
protected $config; |
|
47
|
|
|
|
|
48
|
|
|
/** @var ILogger */ |
|
49
|
|
|
protected $logger; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param IAppData $appData |
|
53
|
|
|
* @param IURLGenerator $urlGenerator |
|
54
|
|
|
* @param ICache $depsCache |
|
55
|
|
|
* @param SystemConfig $config |
|
56
|
|
|
* @param ILogger $logger |
|
57
|
|
|
*/ |
|
58
|
|
|
public function __construct(IAppData $appData, |
|
59
|
|
|
IURLGenerator $urlGenerator, |
|
60
|
|
|
ICache $depsCache, |
|
61
|
|
|
SystemConfig $config, |
|
62
|
|
|
ILogger $logger) { |
|
63
|
|
|
$this->appData = $appData; |
|
64
|
|
|
$this->urlGenerator = $urlGenerator; |
|
65
|
|
|
$this->depsCache = $depsCache; |
|
66
|
|
|
$this->config = $config; |
|
67
|
|
|
$this->logger = $logger; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param string $root |
|
72
|
|
|
* @param string $file |
|
73
|
|
|
* @param string $app |
|
74
|
|
|
* @return bool |
|
75
|
|
|
*/ |
|
76
|
|
|
public function process($root, $file, $app) { |
|
77
|
|
|
if ($this->config->getValue('debug') || !$this->config->getValue('installed')) { |
|
78
|
|
|
return false; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$path = explode('/', $root . '/' . $file); |
|
82
|
|
|
|
|
83
|
|
|
$fileName = array_pop($path); |
|
84
|
|
|
$path = implode('/', $path); |
|
85
|
|
|
|
|
86
|
|
|
try { |
|
87
|
|
|
$folder = $this->appData->getFolder($app); |
|
88
|
|
|
} catch(NotFoundException $e) { |
|
89
|
|
|
// creating css appdata folder |
|
90
|
|
|
$folder = $this->appData->newFolder($app); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
if($this->isCached($fileName, $folder)) { |
|
94
|
|
|
return true; |
|
95
|
|
|
} |
|
96
|
|
|
return $this->cache($path, $fileName, $folder); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param string $fileName |
|
101
|
|
|
* @param ISimpleFolder $folder |
|
102
|
|
|
* @return bool |
|
103
|
|
|
*/ |
|
104
|
|
|
protected function isCached($fileName, ISimpleFolder $folder) { |
|
105
|
|
|
$fileName = str_replace('.json', '.js', $fileName) . '.deps'; |
|
106
|
|
|
try { |
|
107
|
|
|
$deps = $this->depsCache->get($folder->getName() . '-' . $fileName); |
|
108
|
|
|
if ($deps === null || $deps === '') { |
|
109
|
|
|
$depFile = $folder->getFile($fileName); |
|
110
|
|
|
$deps = $depFile->getContent(); |
|
111
|
|
|
} |
|
112
|
|
|
// check again |
|
113
|
|
|
if ($deps === null || $deps === '') { |
|
114
|
|
|
$this->logger->info('JSCombiner: deps file empty: ' . $fileName); |
|
115
|
|
|
return false; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
$deps = json_decode($deps, true); |
|
119
|
|
|
|
|
120
|
|
View Code Duplication |
foreach ($deps as $file=>$mtime) { |
|
121
|
|
|
if (!file_exists($file) || filemtime($file) > $mtime) { |
|
122
|
|
|
return false; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
return true; |
|
127
|
|
|
} catch(NotFoundException $e) { |
|
128
|
|
|
return false; |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @param string $path |
|
134
|
|
|
* @param string $fileName |
|
135
|
|
|
* @param ISimpleFolder $folder |
|
136
|
|
|
* @return bool |
|
137
|
|
|
*/ |
|
138
|
|
|
protected function cache($path, $fileName, ISimpleFolder $folder) { |
|
139
|
|
|
$deps = []; |
|
140
|
|
|
$fullPath = $path . '/' . $fileName; |
|
141
|
|
|
$data = json_decode(file_get_contents($fullPath)); |
|
142
|
|
|
$deps[$fullPath] = filemtime($fullPath); |
|
143
|
|
|
|
|
144
|
|
|
$res = ''; |
|
145
|
|
|
foreach ($data as $file) { |
|
146
|
|
|
$filePath = $path . '/' . $file; |
|
147
|
|
|
|
|
148
|
|
|
if (is_file($filePath)) { |
|
149
|
|
|
$res .= file_get_contents($filePath); |
|
150
|
|
|
$res .= PHP_EOL . PHP_EOL; |
|
151
|
|
|
$deps[$filePath] = filemtime($filePath); |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
$fileName = str_replace('.json', '.js', $fileName); |
|
156
|
|
|
try { |
|
157
|
|
|
$cachedfile = $folder->getFile($fileName); |
|
158
|
|
|
} catch(NotFoundException $e) { |
|
159
|
|
|
$cachedfile = $folder->newFile($fileName); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
$depFileName = $fileName . '.deps'; |
|
163
|
|
|
try { |
|
164
|
|
|
$depFile = $folder->getFile($depFileName); |
|
165
|
|
|
} catch (NotFoundException $e) { |
|
166
|
|
|
$depFile = $folder->newFile($depFileName); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
try { |
|
170
|
|
|
$gzipFile = $folder->getFile($fileName . '.gzip'); # Safari doesn't like .gz |
|
171
|
|
|
} catch (NotFoundException $e) { |
|
172
|
|
|
$gzipFile = $folder->newFile($fileName . '.gzip'); # Safari doesn't like .gz |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
try { |
|
176
|
|
|
$cachedfile->putContent($res); |
|
177
|
|
|
$deps = json_encode($deps); |
|
178
|
|
|
$depFile->putContent($deps); |
|
179
|
|
|
$this->depsCache->set($folder->getName() . '-' . $depFileName, $deps); |
|
180
|
|
|
$gzipFile->putContent(gzencode($res, 9)); |
|
181
|
|
|
|
|
182
|
|
|
return true; |
|
183
|
|
|
} catch (NotPermittedException $e) { |
|
184
|
|
|
return false; |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* @param string $appName |
|
190
|
|
|
* @param string $fileName |
|
191
|
|
|
* @return string |
|
192
|
|
|
*/ |
|
193
|
|
View Code Duplication |
public function getCachedJS($appName, $fileName) { |
|
194
|
|
|
$tmpfileLoc = explode('/', $fileName); |
|
195
|
|
|
$fileName = array_pop($tmpfileLoc); |
|
196
|
|
|
$fileName = str_replace('.json', '.js', $fileName); |
|
197
|
|
|
|
|
198
|
|
|
return substr($this->urlGenerator->linkToRoute('core.Js.getJs', array('fileName' => $fileName, 'appName' => $appName)), strlen(\OC::$WEBROOT) + 1); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* @param string $root |
|
203
|
|
|
* @param string $file |
|
204
|
|
|
* @return string[] |
|
205
|
|
|
*/ |
|
206
|
|
|
public function getContent($root, $file) { |
|
207
|
|
|
/** @var array $data */ |
|
208
|
|
|
$data = json_decode(file_get_contents($root . '/' . $file)); |
|
209
|
|
|
if(!is_array($data)) { |
|
210
|
|
|
return []; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
$path = explode('/', $file); |
|
214
|
|
|
array_pop($path); |
|
215
|
|
|
$path = implode('/', $path); |
|
216
|
|
|
|
|
217
|
|
|
$result = []; |
|
218
|
|
|
foreach ($data as $f) { |
|
219
|
|
|
$result[] = $path . '/' . $f; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
return $result; |
|
223
|
|
|
} |
|
224
|
|
|
} |
|
225
|
|
|
|