2017-03-22 13:47:07 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright 2017, Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
*
|
2017-11-06 14:56:42 +00:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2017-03-22 13:47:07 +00: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
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
namespace OC\Template;
|
|
|
|
|
2017-03-22 14:42:17 +00:00
|
|
|
use OC\SystemConfig;
|
2017-03-22 14:20:29 +00:00
|
|
|
use OCP\ICache;
|
2017-03-22 13:47:07 +00:00
|
|
|
use OCP\Files\IAppData;
|
|
|
|
use OCP\Files\NotFoundException;
|
|
|
|
use OCP\Files\NotPermittedException;
|
|
|
|
use OCP\Files\SimpleFS\ISimpleFolder;
|
2017-07-13 20:31:07 +00:00
|
|
|
use OCP\ILogger;
|
2017-03-22 13:47:07 +00:00
|
|
|
use OCP\IURLGenerator;
|
|
|
|
|
|
|
|
class JSCombiner {
|
|
|
|
|
|
|
|
/** @var IAppData */
|
|
|
|
protected $appData;
|
|
|
|
|
|
|
|
/** @var IURLGenerator */
|
|
|
|
protected $urlGenerator;
|
|
|
|
|
2017-03-22 14:20:29 +00:00
|
|
|
/** @var ICache */
|
|
|
|
protected $depsCache;
|
|
|
|
|
2017-03-22 14:42:17 +00:00
|
|
|
/** @var SystemConfig */
|
|
|
|
protected $config;
|
|
|
|
|
2017-07-13 20:31:07 +00:00
|
|
|
/** @var ILogger */
|
|
|
|
protected $logger;
|
|
|
|
|
2017-03-22 13:47:07 +00:00
|
|
|
/**
|
|
|
|
* @param IAppData $appData
|
|
|
|
* @param IURLGenerator $urlGenerator
|
2017-03-22 14:20:29 +00:00
|
|
|
* @param ICache $depsCache
|
2017-03-25 14:25:06 +00:00
|
|
|
* @param SystemConfig $config
|
2017-07-13 20:31:07 +00:00
|
|
|
* @param ILogger $logger
|
2017-03-22 13:47:07 +00:00
|
|
|
*/
|
|
|
|
public function __construct(IAppData $appData,
|
2017-03-22 14:20:29 +00:00
|
|
|
IURLGenerator $urlGenerator,
|
2017-03-22 14:42:17 +00:00
|
|
|
ICache $depsCache,
|
2017-07-13 20:31:07 +00:00
|
|
|
SystemConfig $config,
|
|
|
|
ILogger $logger) {
|
2017-03-22 13:47:07 +00:00
|
|
|
$this->appData = $appData;
|
|
|
|
$this->urlGenerator = $urlGenerator;
|
2017-03-22 14:20:29 +00:00
|
|
|
$this->depsCache = $depsCache;
|
2017-03-22 14:42:17 +00:00
|
|
|
$this->config = $config;
|
2017-07-13 20:31:07 +00:00
|
|
|
$this->logger = $logger;
|
2017-03-22 13:47:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $root
|
|
|
|
* @param string $file
|
|
|
|
* @param string $app
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function process($root, $file, $app) {
|
2017-03-25 14:25:06 +00:00
|
|
|
if ($this->config->getValue('debug') || !$this->config->getValue('installed')) {
|
2017-03-22 14:42:17 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-22 13:47:07 +00: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) {
|
2017-03-22 14:20:29 +00:00
|
|
|
$fileName = str_replace('.json', '.js', $fileName) . '.deps';
|
|
|
|
try {
|
|
|
|
$deps = $this->depsCache->get($folder->getName() . '-' . $fileName);
|
2017-03-28 23:43:39 +00:00
|
|
|
if ($deps === null || $deps === '') {
|
2017-03-22 14:20:29 +00:00
|
|
|
$depFile = $folder->getFile($fileName);
|
|
|
|
$deps = $depFile->getContent();
|
|
|
|
}
|
2017-07-13 20:31:07 +00:00
|
|
|
// check again
|
|
|
|
if ($deps === null || $deps === '') {
|
|
|
|
$this->logger->info('JSCombiner: deps file empty: ' . $fileName);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-22 14:20:29 +00:00
|
|
|
$deps = json_decode($deps, true);
|
|
|
|
|
2017-11-08 11:37:35 +00:00
|
|
|
if ($deps === NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-22 14:20:29 +00: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 13:47:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
* @param string $fileName
|
|
|
|
* @param ISimpleFolder $folder
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function cache($path, $fileName, ISimpleFolder $folder) {
|
2017-03-22 14:20:29 +00:00
|
|
|
$deps = [];
|
|
|
|
$fullPath = $path . '/' . $fileName;
|
|
|
|
$data = json_decode(file_get_contents($fullPath));
|
|
|
|
$deps[$fullPath] = filemtime($fullPath);
|
2017-03-22 13:47:07 +00:00
|
|
|
|
|
|
|
$res = '';
|
|
|
|
foreach ($data as $file) {
|
|
|
|
$filePath = $path . '/' . $file;
|
|
|
|
|
|
|
|
if (is_file($filePath)) {
|
2017-03-22 14:20:29 +00:00
|
|
|
$res .= file_get_contents($filePath);
|
2017-03-22 13:47:07 +00:00
|
|
|
$res .= PHP_EOL . PHP_EOL;
|
2017-03-22 14:20:29 +00:00
|
|
|
$deps[$filePath] = filemtime($filePath);
|
2017-03-22 13:47:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$fileName = str_replace('.json', '.js', $fileName);
|
|
|
|
try {
|
|
|
|
$cachedfile = $folder->getFile($fileName);
|
|
|
|
} catch(NotFoundException $e) {
|
|
|
|
$cachedfile = $folder->newFile($fileName);
|
|
|
|
}
|
|
|
|
|
2017-03-22 14:20:29 +00:00
|
|
|
$depFileName = $fileName . '.deps';
|
|
|
|
try {
|
|
|
|
$depFile = $folder->getFile($depFileName);
|
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
$depFile = $folder->newFile($depFileName);
|
|
|
|
}
|
|
|
|
|
2017-03-26 14:30:08 +00:00
|
|
|
try {
|
2017-03-29 06:11:51 +00:00
|
|
|
$gzipFile = $folder->getFile($fileName . '.gzip'); # Safari doesn't like .gz
|
2017-03-26 14:30:08 +00:00
|
|
|
} catch (NotFoundException $e) {
|
2017-03-29 06:11:51 +00:00
|
|
|
$gzipFile = $folder->newFile($fileName . '.gzip'); # Safari doesn't like .gz
|
2017-03-26 14:30:08 +00:00
|
|
|
}
|
|
|
|
|
2017-03-22 13:47:07 +00:00
|
|
|
try {
|
|
|
|
$cachedfile->putContent($res);
|
2017-05-16 07:01:20 +00:00
|
|
|
$deps = json_encode($deps);
|
|
|
|
$depFile->putContent($deps);
|
|
|
|
$this->depsCache->set($folder->getName() . '-' . $depFileName, $deps);
|
2017-03-26 14:30:08 +00:00
|
|
|
$gzipFile->putContent(gzencode($res, 9));
|
2017-05-16 07:01:20 +00:00
|
|
|
|
2017-03-22 13:47:07 +00:00
|
|
|
return true;
|
|
|
|
} catch (NotPermittedException $e) {
|
|
|
|
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);
|
|
|
|
|
|
|
|
return substr($this->urlGenerator->linkToRoute('core.Js.getJs', array('fileName' => $fileName, 'appName' => $appName)), strlen(\OC::$WEBROOT) + 1);
|
|
|
|
}
|
2017-03-22 14:42:17 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $root
|
|
|
|
* @param string $file
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function getContent($root, $file) {
|
2017-03-25 14:25:06 +00:00
|
|
|
/** @var array $data */
|
2017-03-22 14:42:17 +00:00
|
|
|
$data = json_decode(file_get_contents($root . '/' . $file));
|
2017-03-25 14:25:06 +00:00
|
|
|
if(!is_array($data)) {
|
|
|
|
return [];
|
|
|
|
}
|
2017-03-22 14:42:17 +00:00
|
|
|
|
|
|
|
$path = explode('/', $file);
|
|
|
|
array_pop($path);
|
|
|
|
$path = implode('/', $path);
|
|
|
|
|
|
|
|
$result = [];
|
|
|
|
foreach ($data as $f) {
|
|
|
|
$result[] = $path . '/' . $f;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
2017-03-22 13:47:07 +00:00
|
|
|
}
|