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
22 changes: 14 additions & 8 deletions lib/private/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -414,9 +414,11 @@ public function __construct($webRoot, \OC\Config $config) {
$this->registerService(\OCP\IURLGenerator::class, function (Server $c) {
$config = $c->getConfig();
$cacheFactory = $c->getMemCacheFactory();
$request = $c->getRequest();
return new \OC\URLGenerator(
$config,
$cacheFactory
$cacheFactory,
$request
);
});
$this->registerAlias('URLGenerator', \OCP\IURLGenerator::class);
Expand All @@ -433,27 +435,31 @@ public function __construct($webRoot, \OC\Config $config) {
$this->registerAlias('UserCache', \OCP\ICache::class);

$this->registerService(Factory::class, function (Server $c) {

$arrayCacheFactory = new \OC\Memcache\Factory('', $c->getLogger(),
'\\OC\\Memcache\\ArrayCache',
'\\OC\\Memcache\\ArrayCache',
'\\OC\\Memcache\\ArrayCache'
);
$config = $c->getConfig();
$request = $c->getRequest();
$urlGenerator = new URLGenerator($config, $arrayCacheFactory, $request);

if ($config->getSystemValue('installed', false) && !(defined('PHPUNIT_RUN') && PHPUNIT_RUN)) {
$v = \OC_App::getAppVersions();
$v['core'] = md5(file_get_contents(\OC::$SERVERROOT . '/version.php'));
$v['core'] = implode(',', \OC_Util::getVersion());
$version = implode(',', $v);
$instanceId = \OC_Util::getInstanceId();
$path = \OC::$SERVERROOT;
$prefix = md5($instanceId . '-' . $version . '-' . $path . '-' . \OC::$WEBROOT);
$prefix = md5($instanceId . '-' . $version . '-' . $path . '-' . $urlGenerator->getBaseUrl());
return new \OC\Memcache\Factory($prefix, $c->getLogger(),
$config->getSystemValue('memcache.local', null),
$config->getSystemValue('memcache.distributed', null),
$config->getSystemValue('memcache.locking', null)
);
}
return $arrayCacheFactory;

return new \OC\Memcache\Factory('', $c->getLogger(),
'\\OC\\Memcache\\ArrayCache',
'\\OC\\Memcache\\ArrayCache',
'\\OC\\Memcache\\ArrayCache'
);
});
$this->registerAlias('MemCacheFactory', Factory::class);
$this->registerAlias(ICacheFactory::class, Factory::class);
Expand Down
15 changes: 12 additions & 3 deletions lib/private/Template/SCSSCacher.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function process($root, $file, $app) {
$path = explode('/', $root . '/' . $file);

$fileNameSCSS = array_pop($path);
$fileNameCSS = str_replace('.scss', '.css', $fileNameSCSS);
$fileNameCSS = $this->prependBaseurlPrefix(str_replace('.scss', '.css', $fileNameSCSS));

$path = implode('/', $path);

Expand All @@ -119,7 +119,7 @@ public function process($root, $file, $app) {
*/
public function getCachedCSS($appName, $fileName) {
$folder = $this->appData->getFolder($appName);
return $folder->getFile($fileName);
return $folder->getFile($this->prependBaseurlPrefix($fileName));
}

/**
Expand Down Expand Up @@ -292,8 +292,17 @@ private function rebaseUrls($css, $webDir) {
public function getCachedSCSS($appName, $fileName) {
$tmpfileLoc = explode('/', $fileName);
$fileName = array_pop($tmpfileLoc);
$fileName = str_replace('.scss', '.css', $fileName);
$fileName = $this->prependBaseurlPrefix(str_replace('.scss', '.css', $fileName));

return substr($this->urlGenerator->linkToRoute('core.Css.getCss', array('fileName' => $fileName, 'appName' => $appName)), strlen(\OC::$WEBROOT) + 1);
}

/**
* Prepend hashed base url to the css file
* @param $cssFile
* @return string
*/
private function prependBaseurlPrefix($cssFile) {
return md5($this->urlGenerator->getBaseUrl()) . '-' . $cssFile;
}
}
28 changes: 20 additions & 8 deletions lib/private/URLGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@

use OCP\ICacheFactory;
use OCP\IConfig;
use OCP\IRequest;
use OCP\IURLGenerator;
use OCP\Route\IRoute;
use RuntimeException;

/**
Expand All @@ -46,15 +48,20 @@ class URLGenerator implements IURLGenerator {
private $config;
/** @var ICacheFactory */
private $cacheFactory;
/** @var IRequest */
private $request;

/**
* @param IConfig $config
* @param ICacheFactory $cacheFactory
* @param IRequest $request
*/
public function __construct(IConfig $config,
ICacheFactory $cacheFactory) {
ICacheFactory $cacheFactory,
IRequest $request) {
$this->config = $config;
$this->cacheFactory = $cacheFactory;
$this->request = $request;
}

/**
Expand Down Expand Up @@ -142,7 +149,7 @@ public function linkTo( $app, $file, $args = array() ) {
* Returns the path to the image.
*/
public function imagePath($app, $image) {
$cache = $this->cacheFactory->create('imagePath');
$cache = $this->cacheFactory->create('imagePath-'.md5($this->getBaseUrl()).'-');
$cacheKey = $app.'-'.$image;
if($key = $cache->get($cacheKey)) {
return $key;
Expand Down Expand Up @@ -223,14 +230,12 @@ public function getAbsoluteURL($url) {
if (\OC::$CLI && !defined('PHPUNIT_RUN')) {
return rtrim($this->config->getSystemValue('overwrite.cli.url'), '/') . '/' . ltrim($url, '/');
}

// The ownCloud web root can already be prepended.
$webRoot = substr($url, 0, strlen(\OC::$WEBROOT)) === \OC::$WEBROOT
? ''
: \OC::$WEBROOT;
if(substr($url, 0, strlen(\OC::$WEBROOT)) === \OC::$WEBROOT) {
$url = substr($url, strlen(\OC::$WEBROOT));
}

$request = \OC::$server->getRequest();
return $request->getServerProtocol() . '://' . $request->getServerHost() . $webRoot . $separator . $url;
return $this->getBaseUrl() . $separator . $url;
}

/**
Expand All @@ -241,4 +246,11 @@ public function linkToDocs($key) {
$theme = \OC::$server->getThemingDefaults();
return $theme->buildDocLinkToKey($key);
}

/**
* @return string base url of the current request
*/
public function getBaseUrl() {
return $this->request->getServerProtocol() . '://' . $this->request->getServerHost() . \OC::$WEBROOT;
}
}
6 changes: 6 additions & 0 deletions lib/public/IURLGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,10 @@ public function getAbsoluteURL($url);
* @since 8.0.0
*/
public function linkToDocs($key);

/**
* @return string base url of the current request
* @since 13.0.0
*/
public function getBaseUrl();
}
51 changes: 31 additions & 20 deletions tests/lib/Template/SCSSCacherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ protected function setUp() {
$this->depsCache
);
$this->themingDefaults->expects($this->any())->method('getScssVariables')->willReturn([]);

$this->urlGenerator->expects($this->any())
->method('getBaseUrl')
->willReturn('http://localhost/nextcloud');
}

public function testProcessUncachedFileNoAppDataFolder() {
Expand All @@ -84,24 +88,29 @@ public function testProcessUncachedFileNoAppDataFolder() {

$fileDeps = $this->createMock(ISimpleFile::class);
$gzfile = $this->createMock(ISimpleFile::class);
$filePrefix = md5('http://localhost/nextcloud') . '-';

$folder->method('getFile')
->will($this->returnCallback(function($path) use ($file, $gzfile) {
if ($path === 'styles.css') {
->will($this->returnCallback(function($path) use ($file, $gzfile, $filePrefix) {
if ($path === $filePrefix.'styles.css') {
return $file;
} else if ($path === 'styles.css.deps') {
} else if ($path === $filePrefix.'styles.css.deps') {
throw new NotFoundException();
} else if ($path === 'styles.css.gzip') {
} else if ($path === $filePrefix.'styles.css.gzip') {
return $gzfile;
} else {
$this->fail();
}
}));
$folder->expects($this->once())
->method('newFile')
->with('styles.css.deps')
->with($filePrefix.'styles.css.deps')
->willReturn($fileDeps);

$this->urlGenerator->expects($this->once())
->method('getBaseUrl')
->willReturn('http://localhost/nextcloud');

$actual = $this->scssCacher->process(\OC::$SERVERROOT, '/core/css/styles.scss', 'core');
$this->assertTrue($actual);
}
Expand All @@ -113,22 +122,23 @@ public function testProcessUncachedFile() {
$file->expects($this->any())->method('getSize')->willReturn(1);
$fileDeps = $this->createMock(ISimpleFile::class);
$gzfile = $this->createMock(ISimpleFile::class);
$filePrefix = md5('http://localhost/nextcloud') . '-';

$folder->method('getFile')
->will($this->returnCallback(function($path) use ($file, $gzfile) {
if ($path === 'styles.css') {
->will($this->returnCallback(function($path) use ($file, $gzfile, $filePrefix) {
if ($path === $filePrefix.'styles.css') {
return $file;
} else if ($path === 'styles.css.deps') {
} else if ($path === $filePrefix.'styles.css.deps') {
throw new NotFoundException();
} else if ($path === 'styles.css.gzip') {
} else if ($path === $filePrefix.'styles.css.gzip') {
return $gzfile;
}else {
$this->fail();
}
}));
$folder->expects($this->once())
->method('newFile')
->with('styles.css.deps')
->with($filePrefix.'styles.css.deps')
->willReturn($fileDeps);

$actual = $this->scssCacher->process(\OC::$SERVERROOT, '/core/css/styles.scss', 'core');
Expand All @@ -142,14 +152,15 @@ public function testProcessCachedFile() {
$fileDeps = $this->createMock(ISimpleFile::class);
$fileDeps->expects($this->any())->method('getSize')->willReturn(1);
$gzFile = $this->createMock(ISimpleFile::class);
$filePrefix = md5('http://localhost/nextcloud') . '-';

$folder->method('getFile')
->will($this->returnCallback(function($name) use ($file, $fileDeps, $gzFile) {
if ($name === 'styles.css') {
->will($this->returnCallback(function($name) use ($file, $fileDeps, $gzFile, $filePrefix) {
if ($name === $filePrefix.'styles.css') {
return $file;
} else if ($name === 'styles.css.deps') {
} else if ($name === $filePrefix.'styles.css.deps') {
return $fileDeps;
} else if ($name === 'styles.css.gzip') {
} else if ($name === $filePrefix.'styles.css.gzip') {
return $gzFile;
}
$this->fail();
Expand All @@ -174,14 +185,14 @@ public function testProcessCachedFileMemcache() {
$fileDeps->expects($this->any())->method('getSize')->willReturn(1);

$gzFile = $this->createMock(ISimpleFile::class);

$filePrefix = md5('http://localhost/nextcloud') . '-';
$folder->method('getFile')
->will($this->returnCallback(function($name) use ($file, $fileDeps, $gzFile) {
if ($name === 'styles.css') {
->will($this->returnCallback(function($name) use ($file, $fileDeps, $gzFile, $filePrefix) {
if ($name === $filePrefix.'styles.css') {
return $file;
} else if ($name === 'styles.css.deps') {
} else if ($name === $filePrefix.'styles.css.deps') {
return $fileDeps;
} else if ($name === 'styles.css.gzip') {
} else if ($name === $filePrefix.'styles.css.gzip') {
return $gzFile;
}
$this->fail();
Expand Down Expand Up @@ -374,7 +385,7 @@ public function testGetCachedSCSS($appName, $fileName, $result) {
$this->urlGenerator->expects($this->once())
->method('linkToRoute')
->with('core.Css.getCss', [
'fileName' => 'styles.css',
'fileName' => md5('http://localhost/nextcloud') . '-styles.css',
'appName' => $appName
])
->willReturn(\OC::$WEBROOT . $result);
Expand Down
Loading