JSCombiner.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OC\Template;
  7. use OC\SystemConfig;
  8. use OCP\Files\IAppData;
  9. use OCP\Files\NotFoundException;
  10. use OCP\Files\NotPermittedException;
  11. use OCP\Files\SimpleFS\ISimpleFolder;
  12. use OCP\ICache;
  13. use OCP\ICacheFactory;
  14. use OCP\IURLGenerator;
  15. use Psr\Log\LoggerInterface;
  16. class JSCombiner {
  17. /** @var IAppData */
  18. protected $appData;
  19. /** @var IURLGenerator */
  20. protected $urlGenerator;
  21. /** @var ICache */
  22. protected $depsCache;
  23. /** @var SystemConfig */
  24. protected $config;
  25. protected LoggerInterface $logger;
  26. /** @var ICacheFactory */
  27. private $cacheFactory;
  28. public function __construct(IAppData $appData,
  29. IURLGenerator $urlGenerator,
  30. ICacheFactory $cacheFactory,
  31. SystemConfig $config,
  32. LoggerInterface $logger) {
  33. $this->appData = $appData;
  34. $this->urlGenerator = $urlGenerator;
  35. $this->cacheFactory = $cacheFactory;
  36. $this->depsCache = $this->cacheFactory->createDistributed('JS-' . md5($this->urlGenerator->getBaseUrl()));
  37. $this->config = $config;
  38. $this->logger = $logger;
  39. }
  40. /**
  41. * @param string $root
  42. * @param string $file
  43. * @param string $app
  44. * @return bool
  45. */
  46. public function process($root, $file, $app) {
  47. if ($this->config->getValue('debug') || !$this->config->getValue('installed')) {
  48. return false;
  49. }
  50. $path = explode('/', $root . '/' . $file);
  51. $fileName = array_pop($path);
  52. $path = implode('/', $path);
  53. try {
  54. $folder = $this->appData->getFolder($app);
  55. } catch (NotFoundException $e) {
  56. // creating css appdata folder
  57. $folder = $this->appData->newFolder($app);
  58. }
  59. if ($this->isCached($fileName, $folder)) {
  60. return true;
  61. }
  62. return $this->cache($path, $fileName, $folder);
  63. }
  64. /**
  65. * @param string $fileName
  66. * @param ISimpleFolder $folder
  67. * @return bool
  68. */
  69. protected function isCached($fileName, ISimpleFolder $folder) {
  70. $fileName = str_replace('.json', '.js', $fileName);
  71. if (!$folder->fileExists($fileName)) {
  72. return false;
  73. }
  74. $fileName = $fileName . '.deps';
  75. try {
  76. $deps = $this->depsCache->get($folder->getName() . '-' . $fileName);
  77. $fromCache = true;
  78. if ($deps === null || $deps === '') {
  79. $fromCache = false;
  80. $depFile = $folder->getFile($fileName);
  81. $deps = $depFile->getContent();
  82. }
  83. // check again
  84. if ($deps === null || $deps === '') {
  85. $this->logger->info('JSCombiner: deps file empty: ' . $fileName);
  86. return false;
  87. }
  88. $deps = json_decode($deps, true);
  89. if ($deps === null) {
  90. return false;
  91. }
  92. foreach ($deps as $file => $mtime) {
  93. if (!file_exists($file) || filemtime($file) > $mtime) {
  94. return false;
  95. }
  96. }
  97. if ($fromCache === false) {
  98. $this->depsCache->set($folder->getName() . '-' . $fileName, json_encode($deps));
  99. }
  100. return true;
  101. } catch (NotFoundException $e) {
  102. return false;
  103. }
  104. }
  105. /**
  106. * @param string $path
  107. * @param string $fileName
  108. * @param ISimpleFolder $folder
  109. * @return bool
  110. */
  111. protected function cache($path, $fileName, ISimpleFolder $folder) {
  112. $deps = [];
  113. $fullPath = $path . '/' . $fileName;
  114. $data = json_decode(file_get_contents($fullPath));
  115. $deps[$fullPath] = filemtime($fullPath);
  116. $res = '';
  117. foreach ($data as $file) {
  118. $filePath = $path . '/' . $file;
  119. if (is_file($filePath)) {
  120. $res .= file_get_contents($filePath);
  121. $res .= PHP_EOL . PHP_EOL;
  122. $deps[$filePath] = filemtime($filePath);
  123. }
  124. }
  125. $fileName = str_replace('.json', '.js', $fileName);
  126. try {
  127. $cachedfile = $folder->getFile($fileName);
  128. } catch (NotFoundException $e) {
  129. $cachedfile = $folder->newFile($fileName);
  130. }
  131. $depFileName = $fileName . '.deps';
  132. try {
  133. $depFile = $folder->getFile($depFileName);
  134. } catch (NotFoundException $e) {
  135. $depFile = $folder->newFile($depFileName);
  136. }
  137. try {
  138. $gzipFile = $folder->getFile($fileName . '.gzip'); # Safari doesn't like .gz
  139. } catch (NotFoundException $e) {
  140. $gzipFile = $folder->newFile($fileName . '.gzip'); # Safari doesn't like .gz
  141. }
  142. try {
  143. $cachedfile->putContent($res);
  144. $deps = json_encode($deps);
  145. $depFile->putContent($deps);
  146. $this->depsCache->set($folder->getName() . '-' . $depFileName, $deps);
  147. $gzipFile->putContent(gzencode($res, 9));
  148. $this->logger->debug('JSCombiner: successfully cached: ' . $fileName);
  149. return true;
  150. } catch (NotPermittedException|NotFoundException $e) {
  151. $this->logger->error('JSCombiner: unable to cache: ' . $fileName);
  152. return false;
  153. }
  154. }
  155. /**
  156. * @param string $appName
  157. * @param string $fileName
  158. * @return string
  159. */
  160. public function getCachedJS($appName, $fileName) {
  161. $tmpfileLoc = explode('/', $fileName);
  162. $fileName = array_pop($tmpfileLoc);
  163. $fileName = str_replace('.json', '.js', $fileName);
  164. return substr($this->urlGenerator->linkToRoute('core.Js.getJs', ['fileName' => $fileName, 'appName' => $appName]), strlen(\OC::$WEBROOT) + 1);
  165. }
  166. /**
  167. * @param string $root
  168. * @param string $file
  169. * @return string[]
  170. */
  171. public function getContent($root, $file) {
  172. /** @var array $data */
  173. $data = json_decode(file_get_contents($root . '/' . $file));
  174. if (!is_array($data)) {
  175. return [];
  176. }
  177. $path = explode('/', $file);
  178. array_pop($path);
  179. $path = implode('/', $path);
  180. $result = [];
  181. foreach ($data as $f) {
  182. $result[] = $path . '/' . $f;
  183. }
  184. return $result;
  185. }
  186. /**
  187. * Clear cache with combined javascript files
  188. *
  189. * @throws NotFoundException
  190. */
  191. public function resetCache() {
  192. $this->cacheFactory->createDistributed('JS-')->clear();
  193. $appDirectory = $this->appData->getDirectoryListing();
  194. foreach ($appDirectory as $folder) {
  195. foreach ($folder->getDirectoryListing() as $file) {
  196. $file->delete();
  197. }
  198. }
  199. }
  200. }