2017-03-22 16:47:07 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright 2017, Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
*
|
2020-03-31 11:49:10 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Julius Härtl <jus@bitgrid.net>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2017-03-22 16:47:07 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2017-03-22 16:47:07 +03:00
|
|
|
*
|
|
|
|
*/
|
2019-11-22 22:52:10 +03:00
|
|
|
|
2017-03-22 16:47:07 +03:00
|
|
|
namespace OC\Template;
|
|
|
|
|
2017-03-22 17:42:17 +03:00
|
|
|
use OC\SystemConfig;
|
2017-03-22 16:47:07 +03:00
|
|
|
use OCP\Files\IAppData;
|
|
|
|
use OCP\Files\NotFoundException;
|
|
|
|
use OCP\Files\NotPermittedException;
|
|
|
|
use OCP\Files\SimpleFS\ISimpleFolder;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OCP\ICache;
|
2018-03-07 16:13:36 +03:00
|
|
|
use OCP\ICacheFactory;
|
2017-07-13 23:31:07 +03:00
|
|
|
use OCP\ILogger;
|
2017-03-22 16:47:07 +03:00
|
|
|
use OCP\IURLGenerator;
|
|
|
|
|
|
|
|
class JSCombiner {
|
|
|
|
|
|
|
|
/** @var IAppData */
|
|
|
|
protected $appData;
|
|
|
|
|
|
|
|
/** @var IURLGenerator */
|
|
|
|
protected $urlGenerator;
|
|
|
|
|
2017-03-22 17:20:29 +03:00
|
|
|
/** @var ICache */
|
|
|
|
protected $depsCache;
|
|
|
|
|
2017-03-22 17:42:17 +03:00
|
|
|
/** @var SystemConfig */
|
|
|
|
protected $config;
|
|
|
|
|
2017-07-13 23:31:07 +03:00
|
|
|
/** @var ILogger */
|
|
|
|
protected $logger;
|
|
|
|
|
2018-03-07 16:13:36 +03:00
|
|
|
/** @var ICacheFactory */
|
|
|
|
private $cacheFactory;
|
|
|
|
|
2017-03-22 16:47:07 +03:00
|
|
|
/**
|
|
|
|
* @param IAppData $appData
|
|
|
|
* @param IURLGenerator $urlGenerator
|
2018-03-07 16:13:36 +03:00
|
|
|
* @param ICacheFactory $cacheFactory
|
2017-03-25 17:25:06 +03:00
|
|
|
* @param SystemConfig $config
|
2017-07-13 23:31:07 +03:00
|
|
|
* @param ILogger $logger
|
2017-03-22 16:47:07 +03:00
|
|
|
*/
|
|
|
|
public function __construct(IAppData $appData,
|
2017-03-22 17:20:29 +03:00
|
|
|
IURLGenerator $urlGenerator,
|
2018-03-07 16:13:36 +03:00
|
|
|
ICacheFactory $cacheFactory,
|
2017-07-13 23:31:07 +03:00
|
|
|
SystemConfig $config,
|
|
|
|
ILogger $logger) {
|
2017-03-22 16:47:07 +03:00
|
|
|
$this->appData = $appData;
|
|
|
|
$this->urlGenerator = $urlGenerator;
|
2018-03-07 16:13:36 +03:00
|
|
|
$this->cacheFactory = $cacheFactory;
|
|
|
|
$this->depsCache = $this->cacheFactory->createDistributed('JS-' . md5($this->urlGenerator->getBaseUrl()));
|
2017-03-22 17:42:17 +03:00
|
|
|
$this->config = $config;
|
2017-07-13 23:31:07 +03:00
|
|
|
$this->logger = $logger;
|
2017-03-22 16:47:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $root
|
|
|
|
* @param string $file
|
|
|
|
* @param string $app
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function process($root, $file, $app) {
|
2017-03-25 17:25:06 +03:00
|
|
|
if ($this->config->getValue('debug') || !$this->config->getValue('installed')) {
|
2017-03-22 17:42:17 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-22 16:47:07 +03:00
|
|
|
$path = explode('/', $root . '/' . $file);
|
|
|
|
|
|
|
|
$fileName = array_pop($path);
|
|
|
|
$path = implode('/', $path);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$folder = $this->appData->getFolder($app);
|
|
|
|
} catch(NotFoundException $e) {
|
|
|
|
// creating css appdata folder
|
|
|
|
$folder = $this->appData->newFolder($app);
|
|
|
|
}
|
|
|
|
|
|
|
|
if($this->isCached($fileName, $folder)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return $this->cache($path, $fileName, $folder);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $fileName
|
|
|
|
* @param ISimpleFolder $folder
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function isCached($fileName, ISimpleFolder $folder) {
|
2018-03-07 14:55:05 +03:00
|
|
|
$fileName = str_replace('.json', '.js', $fileName);
|
|
|
|
|
|
|
|
if (!$folder->fileExists($fileName)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$fileName = $fileName . '.deps';
|
2017-03-22 17:20:29 +03:00
|
|
|
try {
|
|
|
|
$deps = $this->depsCache->get($folder->getName() . '-' . $fileName);
|
2017-03-29 02:43:39 +03:00
|
|
|
if ($deps === null || $deps === '') {
|
2017-03-22 17:20:29 +03:00
|
|
|
$depFile = $folder->getFile($fileName);
|
|
|
|
$deps = $depFile->getContent();
|
|
|
|
}
|
2018-03-07 14:55:05 +03:00
|
|
|
|
2017-07-13 23:31:07 +03:00
|
|
|
// check again
|
|
|
|
if ($deps === null || $deps === '') {
|
|
|
|
$this->logger->info('JSCombiner: deps file empty: ' . $fileName);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-22 17:20:29 +03:00
|
|
|
$deps = json_decode($deps, true);
|
|
|
|
|
2020-03-25 23:53:04 +03:00
|
|
|
if ($deps === null) {
|
2017-11-08 14:37:35 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-22 17:20:29 +03:00
|
|
|
foreach ($deps as $file=>$mtime) {
|
|
|
|
if (!file_exists($file) || filemtime($file) > $mtime) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} catch(NotFoundException $e) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-03-22 16:47:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
* @param string $fileName
|
|
|
|
* @param ISimpleFolder $folder
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function cache($path, $fileName, ISimpleFolder $folder) {
|
2017-03-22 17:20:29 +03:00
|
|
|
$deps = [];
|
|
|
|
$fullPath = $path . '/' . $fileName;
|
|
|
|
$data = json_decode(file_get_contents($fullPath));
|
|
|
|
$deps[$fullPath] = filemtime($fullPath);
|
2017-03-22 16:47:07 +03:00
|
|
|
|
|
|
|
$res = '';
|
|
|
|
foreach ($data as $file) {
|
|
|
|
$filePath = $path . '/' . $file;
|
|
|
|
|
|
|
|
if (is_file($filePath)) {
|
2017-03-22 17:20:29 +03:00
|
|
|
$res .= file_get_contents($filePath);
|
2017-03-22 16:47:07 +03:00
|
|
|
$res .= PHP_EOL . PHP_EOL;
|
2017-03-22 17:20:29 +03:00
|
|
|
$deps[$filePath] = filemtime($filePath);
|
2017-03-22 16:47:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$fileName = str_replace('.json', '.js', $fileName);
|
|
|
|
try {
|
|
|
|
$cachedfile = $folder->getFile($fileName);
|
|
|
|
} catch(NotFoundException $e) {
|
|
|
|
$cachedfile = $folder->newFile($fileName);
|
|
|
|
}
|
|
|
|
|
2017-03-22 17:20:29 +03:00
|
|
|
$depFileName = $fileName . '.deps';
|
|
|
|
try {
|
|
|
|
$depFile = $folder->getFile($depFileName);
|
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
$depFile = $folder->newFile($depFileName);
|
|
|
|
}
|
|
|
|
|
2017-03-26 17:30:08 +03:00
|
|
|
try {
|
2017-03-29 09:11:51 +03:00
|
|
|
$gzipFile = $folder->getFile($fileName . '.gzip'); # Safari doesn't like .gz
|
2017-03-26 17:30:08 +03:00
|
|
|
} catch (NotFoundException $e) {
|
2017-03-29 09:11:51 +03:00
|
|
|
$gzipFile = $folder->newFile($fileName . '.gzip'); # Safari doesn't like .gz
|
2017-03-26 17:30:08 +03:00
|
|
|
}
|
|
|
|
|
2017-03-22 16:47:07 +03:00
|
|
|
try {
|
|
|
|
$cachedfile->putContent($res);
|
2017-05-16 10:01:20 +03:00
|
|
|
$deps = json_encode($deps);
|
|
|
|
$depFile->putContent($deps);
|
|
|
|
$this->depsCache->set($folder->getName() . '-' . $depFileName, $deps);
|
2017-03-26 17:30:08 +03:00
|
|
|
$gzipFile->putContent(gzencode($res, 9));
|
2018-01-26 19:46:42 +03:00
|
|
|
$this->logger->debug('JSCombiner: successfully cached: ' . $fileName);
|
2017-03-22 16:47:07 +03:00
|
|
|
return true;
|
|
|
|
} catch (NotPermittedException $e) {
|
2018-01-26 19:46:42 +03:00
|
|
|
$this->logger->error('JSCombiner: unable to cache: ' . $fileName);
|
2017-03-22 16:47:07 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $appName
|
|
|
|
* @param string $fileName
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getCachedJS($appName, $fileName) {
|
|
|
|
$tmpfileLoc = explode('/', $fileName);
|
|
|
|
$fileName = array_pop($tmpfileLoc);
|
|
|
|
$fileName = str_replace('.json', '.js', $fileName);
|
|
|
|
|
2020-03-26 11:30:18 +03:00
|
|
|
return substr($this->urlGenerator->linkToRoute('core.Js.getJs', ['fileName' => $fileName, 'appName' => $appName]), strlen(\OC::$WEBROOT) + 1);
|
2017-03-22 16:47:07 +03:00
|
|
|
}
|
2017-03-22 17:42:17 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $root
|
|
|
|
* @param string $file
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function getContent($root, $file) {
|
2017-03-25 17:25:06 +03:00
|
|
|
/** @var array $data */
|
2017-03-22 17:42:17 +03:00
|
|
|
$data = json_decode(file_get_contents($root . '/' . $file));
|
2017-03-25 17:25:06 +03:00
|
|
|
if(!is_array($data)) {
|
|
|
|
return [];
|
|
|
|
}
|
2017-03-22 17:42:17 +03:00
|
|
|
|
|
|
|
$path = explode('/', $file);
|
|
|
|
array_pop($path);
|
|
|
|
$path = implode('/', $path);
|
|
|
|
|
|
|
|
$result = [];
|
|
|
|
foreach ($data as $f) {
|
|
|
|
$result[] = $path . '/' . $f;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
2018-01-26 19:46:42 +03:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear cache with combined javascript files
|
|
|
|
*
|
|
|
|
* @throws NotFoundException
|
|
|
|
*/
|
|
|
|
public function resetCache() {
|
2018-03-07 16:13:36 +03:00
|
|
|
$this->cacheFactory->createDistributed('JS-')->clear();
|
2018-01-26 19:46:42 +03:00
|
|
|
$appDirectory = $this->appData->getDirectoryListing();
|
|
|
|
foreach ($appDirectory as $folder) {
|
|
|
|
foreach ($folder->getDirectoryListing() as $file) {
|
|
|
|
$file->delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-22 16:47:07 +03:00
|
|
|
}
|