JSCombiner.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. /**
  3. * @copyright 2017, Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Julius Härtl <jus@bitgrid.net>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. *
  11. * @license GNU AGPL version 3 or any later version
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License as
  15. * published by the Free Software Foundation, either version 3 of the
  16. * License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. */
  27. namespace OC\Template;
  28. use OC\SystemConfig;
  29. use OCP\Files\IAppData;
  30. use OCP\Files\NotFoundException;
  31. use OCP\Files\NotPermittedException;
  32. use OCP\Files\SimpleFS\ISimpleFolder;
  33. use OCP\ICache;
  34. use OCP\ICacheFactory;
  35. use OCP\IURLGenerator;
  36. use Psr\Log\LoggerInterface;
  37. class JSCombiner {
  38. /** @var IAppData */
  39. protected $appData;
  40. /** @var IURLGenerator */
  41. protected $urlGenerator;
  42. /** @var ICache */
  43. protected $depsCache;
  44. /** @var SystemConfig */
  45. protected $config;
  46. protected LoggerInterface $logger;
  47. /** @var ICacheFactory */
  48. private $cacheFactory;
  49. public function __construct(IAppData $appData,
  50. IURLGenerator $urlGenerator,
  51. ICacheFactory $cacheFactory,
  52. SystemConfig $config,
  53. LoggerInterface $logger) {
  54. $this->appData = $appData;
  55. $this->urlGenerator = $urlGenerator;
  56. $this->cacheFactory = $cacheFactory;
  57. $this->depsCache = $this->cacheFactory->createDistributed('JS-' . md5($this->urlGenerator->getBaseUrl()));
  58. $this->config = $config;
  59. $this->logger = $logger;
  60. }
  61. /**
  62. * @param string $root
  63. * @param string $file
  64. * @param string $app
  65. * @return bool
  66. */
  67. public function process($root, $file, $app) {
  68. if ($this->config->getValue('debug') || !$this->config->getValue('installed')) {
  69. return false;
  70. }
  71. $path = explode('/', $root . '/' . $file);
  72. $fileName = array_pop($path);
  73. $path = implode('/', $path);
  74. try {
  75. $folder = $this->appData->getFolder($app);
  76. } catch (NotFoundException $e) {
  77. // creating css appdata folder
  78. $folder = $this->appData->newFolder($app);
  79. }
  80. if ($this->isCached($fileName, $folder)) {
  81. return true;
  82. }
  83. return $this->cache($path, $fileName, $folder);
  84. }
  85. /**
  86. * @param string $fileName
  87. * @param ISimpleFolder $folder
  88. * @return bool
  89. */
  90. protected function isCached($fileName, ISimpleFolder $folder) {
  91. $fileName = str_replace('.json', '.js', $fileName);
  92. if (!$folder->fileExists($fileName)) {
  93. return false;
  94. }
  95. $fileName = $fileName . '.deps';
  96. try {
  97. $deps = $this->depsCache->get($folder->getName() . '-' . $fileName);
  98. $fromCache = true;
  99. if ($deps === null || $deps === '') {
  100. $fromCache = false;
  101. $depFile = $folder->getFile($fileName);
  102. $deps = $depFile->getContent();
  103. }
  104. // check again
  105. if ($deps === null || $deps === '') {
  106. $this->logger->info('JSCombiner: deps file empty: ' . $fileName);
  107. return false;
  108. }
  109. $deps = json_decode($deps, true);
  110. if ($deps === null) {
  111. return false;
  112. }
  113. foreach ($deps as $file => $mtime) {
  114. if (!file_exists($file) || filemtime($file) > $mtime) {
  115. return false;
  116. }
  117. }
  118. if ($fromCache === false) {
  119. $this->depsCache->set($folder->getName() . '-' . $fileName, json_encode($deps));
  120. }
  121. return true;
  122. } catch (NotFoundException $e) {
  123. return false;
  124. }
  125. }
  126. /**
  127. * @param string $path
  128. * @param string $fileName
  129. * @param ISimpleFolder $folder
  130. * @return bool
  131. */
  132. protected function cache($path, $fileName, ISimpleFolder $folder) {
  133. $deps = [];
  134. $fullPath = $path . '/' . $fileName;
  135. $data = json_decode(file_get_contents($fullPath));
  136. $deps[$fullPath] = filemtime($fullPath);
  137. $res = '';
  138. foreach ($data as $file) {
  139. $filePath = $path . '/' . $file;
  140. if (is_file($filePath)) {
  141. $res .= file_get_contents($filePath);
  142. $res .= PHP_EOL . PHP_EOL;
  143. $deps[$filePath] = filemtime($filePath);
  144. }
  145. }
  146. $fileName = str_replace('.json', '.js', $fileName);
  147. try {
  148. $cachedfile = $folder->getFile($fileName);
  149. } catch (NotFoundException $e) {
  150. $cachedfile = $folder->newFile($fileName);
  151. }
  152. $depFileName = $fileName . '.deps';
  153. try {
  154. $depFile = $folder->getFile($depFileName);
  155. } catch (NotFoundException $e) {
  156. $depFile = $folder->newFile($depFileName);
  157. }
  158. try {
  159. $gzipFile = $folder->getFile($fileName . '.gzip'); # Safari doesn't like .gz
  160. } catch (NotFoundException $e) {
  161. $gzipFile = $folder->newFile($fileName . '.gzip'); # Safari doesn't like .gz
  162. }
  163. try {
  164. $cachedfile->putContent($res);
  165. $deps = json_encode($deps);
  166. $depFile->putContent($deps);
  167. $this->depsCache->set($folder->getName() . '-' . $depFileName, $deps);
  168. $gzipFile->putContent(gzencode($res, 9));
  169. $this->logger->debug('JSCombiner: successfully cached: ' . $fileName);
  170. return true;
  171. } catch (NotPermittedException|NotFoundException $e) {
  172. $this->logger->error('JSCombiner: unable to cache: ' . $fileName);
  173. return false;
  174. }
  175. }
  176. /**
  177. * @param string $appName
  178. * @param string $fileName
  179. * @return string
  180. */
  181. public function getCachedJS($appName, $fileName) {
  182. $tmpfileLoc = explode('/', $fileName);
  183. $fileName = array_pop($tmpfileLoc);
  184. $fileName = str_replace('.json', '.js', $fileName);
  185. return substr($this->urlGenerator->linkToRoute('core.Js.getJs', ['fileName' => $fileName, 'appName' => $appName]), strlen(\OC::$WEBROOT) + 1);
  186. }
  187. /**
  188. * @param string $root
  189. * @param string $file
  190. * @return string[]
  191. */
  192. public function getContent($root, $file) {
  193. /** @var array $data */
  194. $data = json_decode(file_get_contents($root . '/' . $file));
  195. if (!is_array($data)) {
  196. return [];
  197. }
  198. $path = explode('/', $file);
  199. array_pop($path);
  200. $path = implode('/', $path);
  201. $result = [];
  202. foreach ($data as $f) {
  203. $result[] = $path . '/' . $f;
  204. }
  205. return $result;
  206. }
  207. /**
  208. * Clear cache with combined javascript files
  209. *
  210. * @throws NotFoundException
  211. */
  212. public function resetCache() {
  213. $this->cacheFactory->createDistributed('JS-')->clear();
  214. $appDirectory = $this->appData->getDirectoryListing();
  215. foreach ($appDirectory as $folder) {
  216. foreach ($folder->getDirectoryListing() as $file) {
  217. $file->delete();
  218. }
  219. }
  220. }
  221. }