Completed
Pull Request — master (#9367)
by John
16:25
created

JSCombiner::getCachedJS()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 7
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 7
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright 2017, Roeland Jago Douma <[email protected]>
4
 *
5
 * @author Lukas Reschke <[email protected]>
6
 * @author Morris Jobke <[email protected]>
7
 * @author Roeland Jago Douma <[email protected]>
8
 *
9
 * @license GNU AGPL version 3 or any later version
10
 *
11
 * This program is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License as
13
 * published by the Free Software Foundation, either version 3 of the
14
 * License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
 *
24
 */
25
namespace OC\Template;
26
27
use OC\SystemConfig;
28
use OCP\App\IAppManager;
29
use OCP\ICache;
30
use OCP\Files\IAppData;
31
use OCP\Files\NotFoundException;
32
use OCP\Files\NotPermittedException;
33
use OCP\Files\SimpleFS\ISimpleFolder;
34
use OCP\ICacheFactory;
35
use OCP\ILogger;
36
use OCP\IURLGenerator;
37
38
class JSCombiner {
39
40
	/** @var IAppData */
41
	protected $appData;
42
43
	/** @var IURLGenerator */
44
	protected $urlGenerator;
45
46
	/** @var ICache */
47
	protected $depsCache;
48
49
	/** @var SystemConfig */
50
	protected $config;
51
52
	/** @var ILogger */
53
	protected $logger;
54
55
	/** @var ICacheFactory */
56
	private $cacheFactory;
57
58
	/** @var IAppManager */
59
	private $appManager;
60
61
	/**
62
	 * @param IAppData $appData
63
	 * @param IURLGenerator $urlGenerator
64
	 * @param ICacheFactory $cacheFactory
65
	 * @param SystemConfig $config
66
	 * @param ILogger $logger
67
	 * @param IAppManager $appManager
68
	 */
69 View Code Duplication
	public function __construct(IAppData $appData,
70
								IURLGenerator $urlGenerator,
71
								ICacheFactory $cacheFactory,
72
								SystemConfig $config,
73
								ILogger $logger,
74
								IAppManager $appManager) {
75
		$this->appData = $appData;
76
		$this->urlGenerator = $urlGenerator;
77
		$this->cacheFactory = $cacheFactory;
78
		$this->depsCache = $this->cacheFactory->createDistributed('JS-' . md5($this->urlGenerator->getBaseUrl()));
79
		$this->config = $config;
80
		$this->logger = $logger;
81
		$this->appManager = $appManager;
82
	}
83
84
	/**
85
	 * @param string $root
86
	 * @param string $file
87
	 * @param string $app
88
	 * @return bool
89
	 */
90
	public function process($root, $file, $app) {
91
		if ($this->config->getValue('debug') || !$this->config->getValue('installed')) {
92
			return false;
93
		}
94
95
		$path = explode('/', $root . '/' . $file);
96
97
		$fileName = array_pop($path);
98
		$path = implode('/', $path);
99
100
		try {
101
			$folder = $this->appData->getFolder($app);
102
		} catch(NotFoundException $e) {
103
			// creating css appdata folder
104
			$folder = $this->appData->newFolder($app);
105
		}
106
107
		if($this->isCached($fileName, $folder, $app)) {
108
			return true;
109
		}
110
		return $this->cache($path, $fileName, $folder, $app);
111
	}
112
113
	/**
114
	 * @param string $fileName
115
	 * @param ISimpleFolder $folder
116
	 * @return bool
117
	 */
118
	protected function isCached($fileName, ISimpleFolder $folder, string $app) {
119
		$fileName = $this->prependVersionPrefix(str_replace('.json', '.js', $fileName), $app);
120
121
		if (!$folder->fileExists($fileName)) {
122
			return false;
123
		}
124
125
		$fileName = $fileName . '.deps';
126
		try {
127
			$deps = $this->depsCache->get($folder->getName() . '-' . $fileName);
128
			if ($deps === null || $deps === '') {
129
				$depFile = $folder->getFile($fileName);
130
				$deps = $depFile->getContent();
131
			}
132
133
			// check again
134
			if ($deps === null || $deps === '') {
135
				$this->logger->info('JSCombiner: deps file empty: ' . $fileName);
136
				return false;
137
			}
138
139
			$deps = json_decode($deps, true);
140
141
			if ($deps === NULL) {
142
				return false;
143
			}
144
145 View Code Duplication
			foreach ($deps as $file=>$mtime) {
146
				if (!file_exists($file) || filemtime($file) > $mtime) {
147
					return false;
148
				}
149
			}
150
151
			return true;
152
		} catch(NotFoundException $e) {
153
			return false;
154
		}
155
	}
156
157
	/**
158
	 * @param string $path
159
	 * @param string $fileName
160
	 * @param ISimpleFolder $folder
161
	 * @return bool
162
	 */
163
	protected function cache($path, $fileName, ISimpleFolder $folder, $app) {
164
		$deps = [];
165
		$fullPath = $path . '/' . $fileName;
166
		$data = json_decode(file_get_contents($fullPath));
167
		$deps[$fullPath] = filemtime($fullPath);
168
169
		$res = '';
170
		foreach ($data as $file) {
171
			$filePath = $path . '/' . $file;
172
173
			if (is_file($filePath)) {
174
				$res .= file_get_contents($filePath);
175
				$res .= PHP_EOL . PHP_EOL;
176
				$deps[$filePath] = filemtime($filePath);
177
			}
178
		}
179
180
		$fileNameCached = $this->prependVersionPrefix(str_replace('.json', '.js', $fileName), $app);
181
		try {
182
			$cachedfile = $folder->getFile($fileNameCached);
183
		} catch(NotFoundException $e) {
184
			$cachedfile = $folder->newFile($fileNameCached);
185
		}
186
187
		$depFileName = $fileNameCached . '.deps';
188
		try {
189
			$depFile = $folder->getFile($depFileName);
190
		} catch (NotFoundException $e) {
191
			$depFile = $folder->newFile($depFileName);
192
		}
193
194
		try {
195
			$gzipFile = $folder->getFile($fileNameCached . '.gzip'); # Safari doesn't like .gz
196
		} catch (NotFoundException $e) {
197
			$gzipFile = $folder->newFile($fileNameCached . '.gzip'); # Safari doesn't like .gz
198
		}
199
200
		try {
201
			$cachedfile->putContent($res);
202
			$deps = json_encode($deps);
203
			$depFile->putContent($deps);
204
			$this->depsCache->set($folder->getName() . '-' . $depFileName, $deps);
205
			$gzipFile->putContent(gzencode($res, 9));
206
			$this->logger->debug('JSCombiner: successfully cached: ' . $fileNameCached);
207
			return true;
208
		} catch (NotPermittedException $e) {
209
			$this->logger->error('JSCombiner: unable to cache: ' . $fileNameCached);
210
			return false;
211
		}
212
	}
213
214
	/**
215
	 * @param string $appName
216
	 * @param string $fileName
217
	 * @return string
218
	 */
219 View Code Duplication
	public function getCachedJS($appName, $fileName) {
220
		$tmpfileLoc = explode('/', $fileName);
221
		$fileName = array_pop($tmpfileLoc);
222
		$fileName = $this->prependVersionPrefix(str_replace('.json', '.js', $fileName), $appName);
223
224
		return substr($this->urlGenerator->linkToRoute('core.Js.getJs', array('fileName' => $fileName, 'appName' => $appName)), strlen(\OC::$WEBROOT) + 1);
225
	}
226
227
	/**
228
	 * @param string $root
229
	 * @param string $file
230
	 * @return string[]
231
	 */
232
	public function getContent($root, $file) {
233
		/** @var array $data */
234
		$data = json_decode(file_get_contents($root . '/' . $file));
235
		if(!is_array($data)) {
236
			return [];
237
		}
238
239
		$path = explode('/', $file);
240
		array_pop($path);
241
		$path = implode('/', $path);
242
243
		$result = [];
244
		foreach ($data as $f) {
245
			$result[] = $path . '/' . $f;
246
		}
247
248
		return $result;
249
	}
250
251
252
	/**
253
	 * Clear cache with combined javascript files
254
	 *
255
	 * @throws NotFoundException
256
	 */
257
	public function resetCache() {
258
		$this->cacheFactory->createDistributed('JS-')->clear();
259
		$appDirectory = $this->appData->getDirectoryListing();
260
		foreach ($appDirectory as $folder) {
261
			foreach ($folder->getDirectoryListing() as $file) {
262
				$file->delete();
263
			}
264
		}
265
	}
266
267
	/**
268
	 * Prepend hashed app version hash
269
	 * @param string $jsFile
270
	 * @param string $appId
271
	 * @return string
272
	 */
273 View Code Duplication
	private function prependVersionPrefix(string $jsFile, string $appId): string {
274
		$appVersion = $this->appManager->getAppInfo($appId)['version'];
275
		if (!is_null($appVersion)) {
276
			return substr(md5($appVersion), 0, 4) . '-' . $jsFile;
277
		}
278
		$coreVersion = \OC_Util::getVersionString();
279
		return substr(md5($coreVersion), 0, 4) . '-' . $jsFile;
280
	}
281
}
282