Completed
Push — master ( d1aa96...8d8654 )
by Morris
41:17 queued 23:12
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\ICache;
29
use OCP\Files\IAppData;
30
use OCP\Files\NotFoundException;
31
use OCP\Files\NotPermittedException;
32
use OCP\Files\SimpleFS\ISimpleFolder;
33
use OCP\ILogger;
34
use OCP\IURLGenerator;
35
36
class JSCombiner {
37
38
	/** @var IAppData */
39
	protected $appData;
40
41
	/** @var IURLGenerator */
42
	protected $urlGenerator;
43
44
	/** @var ICache */
45
	protected $depsCache;
46
47
	/** @var SystemConfig */
48
	protected $config;
49
50
	/** @var ILogger */
51
	protected $logger;
52
53
	/**
54
	 * @param IAppData $appData
55
	 * @param IURLGenerator $urlGenerator
56
	 * @param ICache $depsCache
57
	 * @param SystemConfig $config
58
	 * @param ILogger $logger
59
	 */
60
	public function __construct(IAppData $appData,
61
								IURLGenerator $urlGenerator,
62
								ICache $depsCache,
63
								SystemConfig $config,
64
								ILogger $logger) {
65
		$this->appData = $appData;
66
		$this->urlGenerator = $urlGenerator;
67
		$this->depsCache = $depsCache;
68
		$this->config = $config;
69
		$this->logger = $logger;
70
	}
71
72
	/**
73
	 * @param string $root
74
	 * @param string $file
75
	 * @param string $app
76
	 * @return bool
77
	 */
78
	public function process($root, $file, $app) {
79
		if ($this->config->getValue('debug') || !$this->config->getValue('installed')) {
80
			return false;
81
		}
82
83
		$path = explode('/', $root . '/' . $file);
84
85
		$fileName = array_pop($path);
86
		$path = implode('/', $path);
87
88
		try {
89
			$folder = $this->appData->getFolder($app);
90
		} catch(NotFoundException $e) {
91
			// creating css appdata folder
92
			$folder = $this->appData->newFolder($app);
93
		}
94
95
		if($this->isCached($fileName, $folder)) {
96
			return true;
97
		}
98
		return $this->cache($path, $fileName, $folder);
99
	}
100
101
	/**
102
	 * @param string $fileName
103
	 * @param ISimpleFolder $folder
104
	 * @return bool
105
	 */
106
	protected function isCached($fileName, ISimpleFolder $folder) {
107
		$fileName = str_replace('.json', '.js', $fileName);
108
109
		if (!$folder->fileExists($fileName)) {
110
			return false;
111
		}
112
113
		$fileName = $fileName . '.deps';
114
		try {
115
			$deps = $this->depsCache->get($folder->getName() . '-' . $fileName);
116
			if ($deps === null || $deps === '') {
117
				$depFile = $folder->getFile($fileName);
118
				$deps = $depFile->getContent();
119
			}
120
121
			// check again
122
			if ($deps === null || $deps === '') {
123
				$this->logger->info('JSCombiner: deps file empty: ' . $fileName);
124
				return false;
125
			}
126
127
			$deps = json_decode($deps, true);
128
129
			if ($deps === NULL) {
130
				return false;
131
			}
132
133 View Code Duplication
			foreach ($deps as $file=>$mtime) {
134
				if (!file_exists($file) || filemtime($file) > $mtime) {
135
					return false;
136
				}
137
			}
138
139
			return true;
140
		} catch(NotFoundException $e) {
141
			return false;
142
		}
143
	}
144
145
	/**
146
	 * @param string $path
147
	 * @param string $fileName
148
	 * @param ISimpleFolder $folder
149
	 * @return bool
150
	 */
151
	protected function cache($path, $fileName, ISimpleFolder $folder) {
152
		$deps = [];
153
		$fullPath = $path . '/' . $fileName;
154
		$data = json_decode(file_get_contents($fullPath));
155
		$deps[$fullPath] = filemtime($fullPath);
156
157
		$res = '';
158
		foreach ($data as $file) {
159
			$filePath = $path . '/' . $file;
160
161
			if (is_file($filePath)) {
162
				$res .= file_get_contents($filePath);
163
				$res .= PHP_EOL . PHP_EOL;
164
				$deps[$filePath] = filemtime($filePath);
165
			}
166
		}
167
168
		$fileName = str_replace('.json', '.js', $fileName);
169
		try {
170
			$cachedfile = $folder->getFile($fileName);
171
		} catch(NotFoundException $e) {
172
			$cachedfile = $folder->newFile($fileName);
173
		}
174
175
		$depFileName = $fileName . '.deps';
176
		try {
177
			$depFile = $folder->getFile($depFileName);
178
		} catch (NotFoundException $e) {
179
			$depFile = $folder->newFile($depFileName);
180
		}
181
182
		try {
183
			$gzipFile = $folder->getFile($fileName . '.gzip'); # Safari doesn't like .gz
184
		} catch (NotFoundException $e) {
185
			$gzipFile = $folder->newFile($fileName . '.gzip'); # Safari doesn't like .gz
186
		}
187
188
		try {
189
			$cachedfile->putContent($res);
190
			$deps = json_encode($deps);
191
			$depFile->putContent($deps);
192
			$this->depsCache->set($folder->getName() . '-' . $depFileName, $deps);
193
			$gzipFile->putContent(gzencode($res, 9));
194
			$this->logger->debug('JSCombiner: successfully cached: ' . $fileName);
195
			return true;
196
		} catch (NotPermittedException $e) {
197
			$this->logger->error('JSCombiner: unable to cache: ' . $fileName);
198
			return false;
199
		}
200
	}
201
202
	/**
203
	 * @param string $appName
204
	 * @param string $fileName
205
	 * @return string
206
	 */
207 View Code Duplication
	public function getCachedJS($appName, $fileName) {
208
		$tmpfileLoc = explode('/', $fileName);
209
		$fileName = array_pop($tmpfileLoc);
210
		$fileName = str_replace('.json', '.js', $fileName);
211
212
		return substr($this->urlGenerator->linkToRoute('core.Js.getJs', array('fileName' => $fileName, 'appName' => $appName)), strlen(\OC::$WEBROOT) + 1);
213
	}
214
215
	/**
216
	 * @param string $root
217
	 * @param string $file
218
	 * @return string[]
219
	 */
220
	public function getContent($root, $file) {
221
		/** @var array $data */
222
		$data = json_decode(file_get_contents($root . '/' . $file));
223
		if(!is_array($data)) {
224
			return [];
225
		}
226
227
		$path = explode('/', $file);
228
		array_pop($path);
229
		$path = implode('/', $path);
230
231
		$result = [];
232
		foreach ($data as $f) {
233
			$result[] = $path . '/' . $f;
234
		}
235
236
		return $result;
237
	}
238
239
240
	/**
241
	 * Clear cache with combined javascript files
242
	 *
243
	 * @throws NotFoundException
244
	 */
245
	public function resetCache() {
246
		$this->depsCache->clear();
247
		$appDirectory = $this->appData->getDirectoryListing();
248
		foreach ($appDirectory as $folder) {
249
			foreach ($folder->getDirectoryListing() as $file) {
250
				$file->delete();
251
			}
252
		}
253
	}
254
}
255