Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions apps/files_sharing/appinfo/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,8 @@
$eventDispatcher->addListener(
'OCA\Files::loadAdditionalScripts',
function() {
\OCP\Util::addScript('files_sharing', 'share');
\OCP\Util::addScript('files_sharing', 'sharetabview');
\OCP\Util::addScript('files_sharing', 'sharebreadcrumbview');
\OCP\Util::addStyle('files_sharing', 'mergedAdditionalStyles');
\OCP\Util::addScript('files_sharing', 'additionalScripts');
}
);

Expand Down
5 changes: 5 additions & 0 deletions apps/files_sharing/js/additionalScripts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
"share.js",
"sharetabview.js",
"sharebreadcrumbview.js"
]
10 changes: 10 additions & 0 deletions core/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

namespace OC\Core;

use OC\AppFramework\Utility\SimpleContainer;
use OC\Core\Controller\JsController;
use OC\Core\Controller\OCJSController;
use OC\Security\IdentityProof\Manager;
use OC\Server;
Expand Down Expand Up @@ -87,5 +89,13 @@ public function __construct() {
$server->getURLGenerator()
);
});
$container->registerService(JsController::class, function () use ($container) {
return new JsController(
$container->query('AppName'),
$container->query(IRequest::class),
$container->getServer()->getAppDataDir('js'),
$container->query(ITimeFactory::class)
);
});
}
}
80 changes: 80 additions & 0 deletions core/Controller/JsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php
/**
* @copyright 2017, Roeland Jago Douma <roeland@famdouma.nl>
*
* @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\Core\Controller;

use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\NotFoundResponse;
use OCP\AppFramework\Http\FileDisplayResponse;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Files\IAppData;
use OCP\Files\NotFoundException;
use OCP\IRequest;

class JsController extends Controller {

/** @var IAppData */
protected $appData;

/** @var ITimeFactory */
protected $timeFactory;

/**
* @param string $appName
* @param IRequest $request
* @param IAppData $appData
* @param ITimeFactory $timeFactory
*/
public function __construct($appName, IRequest $request, IAppData $appData, ITimeFactory $timeFactory) {
parent::__construct($appName, $request);

$this->appData = $appData;
$this->timeFactory = $timeFactory;
}

/**
* @PublicPage
* @NoCSRFRequired
*
* @param string $fileName css filename with extension
* @param string $appName css folder name
* @return FileDisplayResponse|NotFoundResponse
*/
public function getJs($fileName, $appName) {
try {
$folder = $this->appData->getFolder($appName);
$jsFile = $folder->getFile($fileName);
} catch(NotFoundException $e) {
return new NotFoundResponse();
}

$response = new FileDisplayResponse($jsFile, Http::STATUS_OK, ['Content-Type' => 'application/javascript']);
$response->cacheFor(86400);
$expires = new \DateTime();
$expires->setTimestamp($this->timeFactory->getTime());
$expires->add(new \DateInterval('PT24H'));
$response->addHeader('Expires', $expires->format(\DateTime::RFC1123));
$response->addHeader('Pragma', 'cache');
return $response;
}
}
1 change: 1 addition & 0 deletions core/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
['name' => 'Preview#getPreview', 'url' => '/core/preview', 'verb' => 'GET'],
['name' => 'Preview#getPreview', 'url' => '/core/preview.png', 'verb' => 'GET'],
['name' => 'Css#getCss', 'url' => '/css/{appName}/{fileName}', 'verb' => 'GET'],
['name' => 'Js#getJs', 'url' => '/js/{appName}/{fileName}', 'verb' => 'GET'],
],
'ocs' => [
['root' => '/cloud', 'name' => 'OCS#getCapabilities', 'url' => '/capabilities', 'verb' => 'GET'],
Expand Down
2 changes: 2 additions & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@
'OC\\Core\\Command\\User\\Setting' => $baseDir . '/core/Command/User/Setting.php',
'OC\\Core\\Controller\\AvatarController' => $baseDir . '/core/Controller/AvatarController.php',
'OC\\Core\\Controller\\CssController' => $baseDir . '/core/Controller/CssController.php',
'OC\\Core\\Controller\\JsController' => $baseDir . '/core/Controller/JsController.php',
'OC\\Core\\Controller\\LoginController' => $baseDir . '/core/Controller/LoginController.php',
'OC\\Core\\Controller\\LostController' => $baseDir . '/core/Controller/LostController.php',
'OC\\Core\\Controller\\OCJSController' => $baseDir . '/core/Controller/OCJSController.php',
Expand Down Expand Up @@ -802,6 +803,7 @@
'OC\\TemplateLayout' => $baseDir . '/lib/private/TemplateLayout.php',
'OC\\Template\\Base' => $baseDir . '/lib/private/Template/Base.php',
'OC\\Template\\CSSResourceLocator' => $baseDir . '/lib/private/Template/CSSResourceLocator.php',
'OC\\Template\\JSCombiner' => $baseDir . '/lib/private/Template/JSCombiner.php',
'OC\\Template\\JSConfigHelper' => $baseDir . '/lib/private/Template/JSConfigHelper.php',
'OC\\Template\\JSResourceLocator' => $baseDir . '/lib/private/Template/JSResourceLocator.php',
'OC\\Template\\ResourceLocator' => $baseDir . '/lib/private/Template/ResourceLocator.php',
Expand Down
2 changes: 2 additions & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OC\\Core\\Command\\User\\Setting' => __DIR__ . '/../../..' . '/core/Command/User/Setting.php',
'OC\\Core\\Controller\\AvatarController' => __DIR__ . '/../../..' . '/core/Controller/AvatarController.php',
'OC\\Core\\Controller\\CssController' => __DIR__ . '/../../..' . '/core/Controller/CssController.php',
'OC\\Core\\Controller\\JsController' => __DIR__ . '/../../..' . '/core/Controller/JsController.php',
'OC\\Core\\Controller\\LoginController' => __DIR__ . '/../../..' . '/core/Controller/LoginController.php',
'OC\\Core\\Controller\\LostController' => __DIR__ . '/../../..' . '/core/Controller/LostController.php',
'OC\\Core\\Controller\\OCJSController' => __DIR__ . '/../../..' . '/core/Controller/OCJSController.php',
Expand Down Expand Up @@ -832,6 +833,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OC\\TemplateLayout' => __DIR__ . '/../../..' . '/lib/private/TemplateLayout.php',
'OC\\Template\\Base' => __DIR__ . '/../../..' . '/lib/private/Template/Base.php',
'OC\\Template\\CSSResourceLocator' => __DIR__ . '/../../..' . '/lib/private/Template/CSSResourceLocator.php',
'OC\\Template\\JSCombiner' => __DIR__ . '/../../..' . '/lib/private/Template/JSCombiner.php',
'OC\\Template\\JSConfigHelper' => __DIR__ . '/../../..' . '/lib/private/Template/JSConfigHelper.php',
'OC\\Template\\JSResourceLocator' => __DIR__ . '/../../..' . '/lib/private/Template/JSResourceLocator.php',
'OC\\Template\\ResourceLocator' => __DIR__ . '/../../..' . '/lib/private/Template/ResourceLocator.php',
Expand Down
199 changes: 199 additions & 0 deletions lib/private/Template/JSCombiner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
<?php
/**
* @copyright 2017, Roeland Jago Douma <roeland@famdouma.nl>
*
* @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;

use OC\SystemConfig;
use OCP\ICache;
use OCP\Files\IAppData;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\Files\SimpleFS\ISimpleFolder;
use OCP\IURLGenerator;

class JSCombiner {

/** @var IAppData */
protected $appData;

/** @var IURLGenerator */
protected $urlGenerator;

/** @var ICache */
protected $depsCache;

/** @var SystemConfig */
protected $config;

/**
* JSCombiner constructor.
*
* @param IAppData $appData
* @param IURLGenerator $urlGenerator
* @param ICache $depsCache
*/
public function __construct(IAppData $appData,
IURLGenerator $urlGenerator,
ICache $depsCache,
SystemConfig $config) {
$this->appData = $appData;
$this->urlGenerator = $urlGenerator;
$this->depsCache = $depsCache;
$this->config = $config;
}

/**
* @param string $root
* @param string $file
* @param string $app
* @return bool
*/
public function process($root, $file, $app) {
if ($this->config->getValue('debug')) {
return false;
}

$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) {
$fileName = str_replace('.json', '.js', $fileName) . '.deps';
try {
$deps = $this->depsCache->get($folder->getName() . '-' . $fileName);
if ($deps === null) {
$depFile = $folder->getFile($fileName);
$deps = $depFile->getContent();
$this->depsCache->set($folder->getName() . '-' . $fileName, $deps);
}
$deps = json_decode($deps, true);

foreach ($deps as $file=>$mtime) {
if (!file_exists($file) || filemtime($file) > $mtime) {
return false;
}
}

return true;
} catch(NotFoundException $e) {
return false;
}
}

/**
* @param string $path
* @param string $fileName
* @param ISimpleFolder $folder
* @return bool
*/
protected function cache($path, $fileName, ISimpleFolder $folder) {
$deps = [];
$fullPath = $path . '/' . $fileName;
$data = json_decode(file_get_contents($fullPath));
$deps[$fullPath] = filemtime($fullPath);

$res = '';
foreach ($data as $file) {
$filePath = $path . '/' . $file;

if (is_file($filePath)) {
$res .= file_get_contents($filePath);
$res .= PHP_EOL . PHP_EOL;
$deps[$filePath] = filemtime($filePath);
}
}

$fileName = str_replace('.json', '.js', $fileName);
try {
$cachedfile = $folder->getFile($fileName);
} catch(NotFoundException $e) {
$cachedfile = $folder->newFile($fileName);
}

$depFileName = $fileName . '.deps';
try {
$depFile = $folder->getFile($depFileName);
} catch (NotFoundException $e) {
$depFile = $folder->newFile($depFileName);
}

try {
$cachedfile->putContent($res);
$depFile->putContent(json_encode($deps));
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);
}

/**
* @param string $root
* @param string $file
* @return string[]
*/
public function getContent($root, $file) {
$data = json_decode(file_get_contents($root . '/' . $file));

$path = explode('/', $file);
array_pop($path);
$path = implode('/', $path);

$result = [];
foreach ($data as $f) {
$result[] = $path . '/' . $f;
}

return $result;
}
}
Loading