2017-03-22 09:47:07 -04:00
|
|
|
<?php
|
2025-06-30 09:04:05 -04:00
|
|
|
|
2017-03-22 09:47:07 -04:00
|
|
|
/**
|
2024-05-23 03:26:56 -04:00
|
|
|
* SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2017-03-22 09:47:07 -04:00
|
|
|
*/
|
|
|
|
|
namespace OC\Template;
|
|
|
|
|
|
2017-03-22 10:42:17 -04:00
|
|
|
use OC\SystemConfig;
|
2017-03-22 09:47:07 -04:00
|
|
|
use OCP\Files\IAppData;
|
|
|
|
|
use OCP\Files\NotFoundException;
|
|
|
|
|
use OCP\Files\NotPermittedException;
|
|
|
|
|
use OCP\Files\SimpleFS\ISimpleFolder;
|
2019-11-22 14:52:10 -05:00
|
|
|
use OCP\ICache;
|
2018-03-07 08:13:36 -05:00
|
|
|
use OCP\ICacheFactory;
|
2017-03-22 09:47:07 -04:00
|
|
|
use OCP\IURLGenerator;
|
2022-03-17 11:42:53 -04:00
|
|
|
use Psr\Log\LoggerInterface;
|
2017-03-22 09:47:07 -04:00
|
|
|
|
|
|
|
|
class JSCombiner {
|
|
|
|
|
/** @var IAppData */
|
|
|
|
|
protected $appData;
|
|
|
|
|
|
|
|
|
|
/** @var IURLGenerator */
|
|
|
|
|
protected $urlGenerator;
|
|
|
|
|
|
2017-03-22 10:20:29 -04:00
|
|
|
/** @var ICache */
|
|
|
|
|
protected $depsCache;
|
|
|
|
|
|
2017-03-22 10:42:17 -04:00
|
|
|
/** @var SystemConfig */
|
|
|
|
|
protected $config;
|
|
|
|
|
|
2022-03-17 11:42:53 -04:00
|
|
|
protected LoggerInterface $logger;
|
2017-07-13 16:31:07 -04:00
|
|
|
|
2018-03-07 08:13:36 -05:00
|
|
|
/** @var ICacheFactory */
|
|
|
|
|
private $cacheFactory;
|
|
|
|
|
|
2017-03-22 09:47:07 -04:00
|
|
|
public function __construct(IAppData $appData,
|
2017-03-22 10:20:29 -04:00
|
|
|
IURLGenerator $urlGenerator,
|
2018-03-07 08:13:36 -05:00
|
|
|
ICacheFactory $cacheFactory,
|
2017-07-13 16:31:07 -04:00
|
|
|
SystemConfig $config,
|
2022-03-17 11:42:53 -04:00
|
|
|
LoggerInterface $logger) {
|
2017-03-22 09:47:07 -04:00
|
|
|
$this->appData = $appData;
|
|
|
|
|
$this->urlGenerator = $urlGenerator;
|
2018-03-07 08:13:36 -05:00
|
|
|
$this->cacheFactory = $cacheFactory;
|
|
|
|
|
$this->depsCache = $this->cacheFactory->createDistributed('JS-' . md5($this->urlGenerator->getBaseUrl()));
|
2017-03-22 10:42:17 -04:00
|
|
|
$this->config = $config;
|
2017-07-13 16:31:07 -04:00
|
|
|
$this->logger = $logger;
|
2017-03-22 09:47:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $root
|
|
|
|
|
* @param string $file
|
|
|
|
|
* @param string $app
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function process($root, $file, $app) {
|
2017-03-25 10:25:06 -04:00
|
|
|
if ($this->config->getValue('debug') || !$this->config->getValue('installed')) {
|
2017-03-22 10:42:17 -04:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-22 09:47:07 -04: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 06:55:05 -05:00
|
|
|
$fileName = str_replace('.json', '.js', $fileName);
|
|
|
|
|
|
|
|
|
|
if (!$folder->fileExists($fileName)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$fileName = $fileName . '.deps';
|
2017-03-22 10:20:29 -04:00
|
|
|
try {
|
|
|
|
|
$deps = $this->depsCache->get($folder->getName() . '-' . $fileName);
|
2021-01-06 10:15:17 -05:00
|
|
|
$fromCache = true;
|
2017-03-28 19:43:39 -04:00
|
|
|
if ($deps === null || $deps === '') {
|
2021-01-06 10:15:17 -05:00
|
|
|
$fromCache = false;
|
2017-03-22 10:20:29 -04:00
|
|
|
$depFile = $folder->getFile($fileName);
|
|
|
|
|
$deps = $depFile->getContent();
|
|
|
|
|
}
|
2018-03-07 06:55:05 -05:00
|
|
|
|
2017-07-13 16:31:07 -04:00
|
|
|
// check again
|
|
|
|
|
if ($deps === null || $deps === '') {
|
|
|
|
|
$this->logger->info('JSCombiner: deps file empty: ' . $fileName);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-22 10:20:29 -04:00
|
|
|
$deps = json_decode($deps, true);
|
|
|
|
|
|
2020-03-25 16:53:04 -04:00
|
|
|
if ($deps === null) {
|
2017-11-08 06:37:35 -05:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-05 09:12:57 -04:00
|
|
|
foreach ($deps as $file => $mtime) {
|
2017-03-22 10:20:29 -04:00
|
|
|
if (!file_exists($file) || filemtime($file) > $mtime) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-06 10:15:17 -05:00
|
|
|
if ($fromCache === false) {
|
|
|
|
|
$this->depsCache->set($folder->getName() . '-' . $fileName, json_encode($deps));
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-22 10:20:29 -04:00
|
|
|
return true;
|
|
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2017-03-22 09:47:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $path
|
|
|
|
|
* @param string $fileName
|
|
|
|
|
* @param ISimpleFolder $folder
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
protected function cache($path, $fileName, ISimpleFolder $folder) {
|
2017-03-22 10:20:29 -04:00
|
|
|
$deps = [];
|
|
|
|
|
$fullPath = $path . '/' . $fileName;
|
|
|
|
|
$data = json_decode(file_get_contents($fullPath));
|
|
|
|
|
$deps[$fullPath] = filemtime($fullPath);
|
2017-03-22 09:47:07 -04:00
|
|
|
|
|
|
|
|
$res = '';
|
|
|
|
|
foreach ($data as $file) {
|
|
|
|
|
$filePath = $path . '/' . $file;
|
|
|
|
|
|
|
|
|
|
if (is_file($filePath)) {
|
2017-03-22 10:20:29 -04:00
|
|
|
$res .= file_get_contents($filePath);
|
2017-03-22 09:47:07 -04:00
|
|
|
$res .= PHP_EOL . PHP_EOL;
|
2017-03-22 10:20:29 -04:00
|
|
|
$deps[$filePath] = filemtime($filePath);
|
2017-03-22 09:47:07 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$fileName = str_replace('.json', '.js', $fileName);
|
|
|
|
|
try {
|
|
|
|
|
$cachedfile = $folder->getFile($fileName);
|
|
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
|
$cachedfile = $folder->newFile($fileName);
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-22 10:20:29 -04:00
|
|
|
$depFileName = $fileName . '.deps';
|
|
|
|
|
try {
|
|
|
|
|
$depFile = $folder->getFile($depFileName);
|
|
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
|
$depFile = $folder->newFile($depFileName);
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-26 10:30:08 -04:00
|
|
|
try {
|
2017-03-29 02:11:51 -04:00
|
|
|
$gzipFile = $folder->getFile($fileName . '.gzip'); # Safari doesn't like .gz
|
2017-03-26 10:30:08 -04:00
|
|
|
} catch (NotFoundException $e) {
|
2017-03-29 02:11:51 -04:00
|
|
|
$gzipFile = $folder->newFile($fileName . '.gzip'); # Safari doesn't like .gz
|
2017-03-26 10:30:08 -04:00
|
|
|
}
|
|
|
|
|
|
2017-03-22 09:47:07 -04:00
|
|
|
try {
|
|
|
|
|
$cachedfile->putContent($res);
|
2017-05-16 03:01:20 -04:00
|
|
|
$deps = json_encode($deps);
|
|
|
|
|
$depFile->putContent($deps);
|
|
|
|
|
$this->depsCache->set($folder->getName() . '-' . $depFileName, $deps);
|
2017-03-26 10:30:08 -04:00
|
|
|
$gzipFile->putContent(gzencode($res, 9));
|
2018-01-26 11:46:42 -05:00
|
|
|
$this->logger->debug('JSCombiner: successfully cached: ' . $fileName);
|
2017-03-22 09:47:07 -04:00
|
|
|
return true;
|
2022-12-08 05:50:50 -05:00
|
|
|
} catch (NotPermittedException|NotFoundException $e) {
|
2018-01-26 11:46:42 -05:00
|
|
|
$this->logger->error('JSCombiner: unable to cache: ' . $fileName);
|
2017-03-22 09:47:07 -04: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 04:30:18 -04:00
|
|
|
return substr($this->urlGenerator->linkToRoute('core.Js.getJs', ['fileName' => $fileName, 'appName' => $appName]), strlen(\OC::$WEBROOT) + 1);
|
2017-03-22 09:47:07 -04:00
|
|
|
}
|
2017-03-22 10:42:17 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $root
|
|
|
|
|
* @param string $file
|
|
|
|
|
* @return string[]
|
|
|
|
|
*/
|
|
|
|
|
public function getContent($root, $file) {
|
2017-03-25 10:25:06 -04:00
|
|
|
/** @var array $data */
|
2017-03-22 10:42:17 -04:00
|
|
|
$data = json_decode(file_get_contents($root . '/' . $file));
|
2017-03-25 10:25:06 -04:00
|
|
|
if (!is_array($data)) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
2017-03-22 10:42:17 -04:00
|
|
|
|
|
|
|
|
$path = explode('/', $file);
|
|
|
|
|
array_pop($path);
|
|
|
|
|
$path = implode('/', $path);
|
|
|
|
|
|
|
|
|
|
$result = [];
|
|
|
|
|
foreach ($data as $f) {
|
|
|
|
|
$result[] = $path . '/' . $f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
2018-01-26 11:46:42 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Clear cache with combined javascript files
|
|
|
|
|
*
|
|
|
|
|
* @throws NotFoundException
|
|
|
|
|
*/
|
|
|
|
|
public function resetCache() {
|
2018-03-07 08:13:36 -05:00
|
|
|
$this->cacheFactory->createDistributed('JS-')->clear();
|
2018-01-26 11:46:42 -05:00
|
|
|
$appDirectory = $this->appData->getDirectoryListing();
|
|
|
|
|
foreach ($appDirectory as $folder) {
|
|
|
|
|
foreach ($folder->getDirectoryListing() as $file) {
|
|
|
|
|
$file->delete();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-03-22 09:47:07 -04:00
|
|
|
}
|