diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/l10n/ro.js | 1 | ||||
-rw-r--r-- | lib/l10n/ro.json | 1 | ||||
-rw-r--r-- | lib/private/Files/ObjectStore/ObjectStoreStorage.php | 89 | ||||
-rw-r--r-- | lib/private/Files/Storage/Common.php | 20 | ||||
-rw-r--r-- | lib/private/TemplateLayout.php | 126 |
5 files changed, 153 insertions, 84 deletions
diff --git a/lib/l10n/ro.js b/lib/l10n/ro.js index 0b5283df7ea..8ab53097242 100644 --- a/lib/l10n/ro.js +++ b/lib/l10n/ro.js @@ -92,6 +92,7 @@ OC.L10N.register( "Administration settings" : "Setări de administrare", "Settings" : "Setări", "Log out" : "Ieșire", + "Accounts" : "Conturi", "Email" : "E-mail", "View %s on the fediverse" : "Vedeți %s pe fediverse", "Phone" : "Telefon", diff --git a/lib/l10n/ro.json b/lib/l10n/ro.json index 59cd4bb8864..d3163a26c3b 100644 --- a/lib/l10n/ro.json +++ b/lib/l10n/ro.json @@ -90,6 +90,7 @@ "Administration settings" : "Setări de administrare", "Settings" : "Setări", "Log out" : "Ieșire", + "Accounts" : "Conturi", "Email" : "E-mail", "View %s on the fediverse" : "Vedeți %s pe fediverse", "Phone" : "Telefon", diff --git a/lib/private/Files/ObjectStore/ObjectStoreStorage.php b/lib/private/Files/ObjectStore/ObjectStoreStorage.php index 9624c0db6d4..5a2aaa4dc50 100644 --- a/lib/private/Files/ObjectStore/ObjectStoreStorage.php +++ b/lib/private/Files/ObjectStore/ObjectStoreStorage.php @@ -60,6 +60,7 @@ class ObjectStoreStorage extends \OC\Files\Storage\Common implements IChunkedFil private bool $handleCopiesAsOwned; protected bool $validateWrites = true; + private bool $preserveCacheItemsOnDelete = false; /** * @param array $params @@ -196,7 +197,9 @@ class ObjectStoreStorage extends \OC\Files\Storage\Common implements IChunkedFil } } - $this->getCache()->remove($entry->getPath()); + if (!$this->preserveCacheItemsOnDelete) { + $this->getCache()->remove($entry->getPath()); + } return true; } @@ -231,7 +234,9 @@ class ObjectStoreStorage extends \OC\Files\Storage\Common implements IChunkedFil } //removing from cache is ok as it does not exist in the objectstore anyway } - $this->getCache()->remove($entry->getPath()); + if (!$this->preserveCacheItemsOnDelete) { + $this->getCache()->remove($entry->getPath()); + } return true; } @@ -628,31 +633,71 @@ class ObjectStoreStorage extends \OC\Files\Storage\Common implements IChunkedFil if (!$sourceCacheEntry) { $sourceCacheEntry = $sourceCache->get($sourceInternalPath); } - if ($sourceCacheEntry->getMimeType() === FileInfo::MIMETYPE_FOLDER) { - $this->mkdir($targetInternalPath); - foreach ($sourceCache->getFolderContents($sourceInternalPath) as $child) { - $this->moveFromStorage($sourceStorage, $child->getPath(), $targetInternalPath . '/' . $child->getName()); - } + if (!$sourceCacheEntry) { + return false; + } + + $this->copyObjects($sourceStorage, $sourceCache, $sourceCacheEntry); + if ($sourceStorage->instanceOfStorage(ObjectStoreStorage::class)) { + /** @var ObjectStoreStorage $sourceStorage */ + $sourceStorage->setPreserveCacheOnDelete(true); + } + if ($sourceCacheEntry->getMimeType() === ICacheEntry::DIRECTORY_MIMETYPE) { $sourceStorage->rmdir($sourceInternalPath); } else { - $sourceStream = $sourceStorage->fopen($sourceInternalPath, 'r'); - if (!$sourceStream) { - return false; - } - // move the cache entry before the contents so that we have the correct fileid/urn for the target - $this->getCache()->moveFromCache($sourceCache, $sourceInternalPath, $targetInternalPath); - try { - $this->writeStream($targetInternalPath, $sourceStream, $sourceCacheEntry->getSize()); - } catch (\Exception $e) { - // restore the cache entry - $sourceCache->moveFromCache($this->getCache(), $targetInternalPath, $sourceInternalPath); - throw $e; - } $sourceStorage->unlink($sourceInternalPath); } + if ($sourceStorage->instanceOfStorage(ObjectStoreStorage::class)) { + /** @var ObjectStoreStorage $sourceStorage */ + $sourceStorage->setPreserveCacheOnDelete(false); + } + $this->getCache()->moveFromCache($sourceCache, $sourceInternalPath, $targetInternalPath); + return true; } + /** + * Copy the object(s) of a file or folder into this storage, without touching the cache + */ + private function copyObjects(IStorage $sourceStorage, ICache $sourceCache, ICacheEntry $sourceCacheEntry) { + $copiedFiles = []; + try { + foreach ($this->getAllChildObjects($sourceCache, $sourceCacheEntry) as $file) { + $sourceStream = $sourceStorage->fopen($file->getPath(), 'r'); + if (!$sourceStream) { + throw new \Exception("Failed to open source file {$file->getPath()} ({$file->getId()})"); + } + $this->objectStore->writeObject($this->getURN($file->getId()), $sourceStream, $file->getMimeType()); + if (is_resource($sourceStream)) { + fclose($sourceStream); + } + $copiedFiles[] = $file->getId(); + } + } catch (\Exception $e) { + foreach ($copiedFiles as $fileId) { + try { + $this->objectStore->deleteObject($this->getURN($fileId)); + } catch (\Exception $e) { + // ignore + } + } + throw $e; + } + } + + /** + * @return \Iterator<ICacheEntry> + */ + private function getAllChildObjects(ICache $cache, ICacheEntry $entry): \Iterator { + if ($entry->getMimeType() === FileInfo::MIMETYPE_FOLDER) { + foreach ($cache->getFolderContentsById($entry->getId()) as $child) { + yield from $this->getAllChildObjects($cache, $child); + } + } else { + yield $entry; + } + } + public function copy($source, $target) { $source = $this->normalizePath($source); $target = $this->normalizePath($target); @@ -787,4 +832,8 @@ class ObjectStoreStorage extends \OC\Files\Storage\Common implements IChunkedFil $urn = $this->getURN($cacheEntry->getId()); $this->objectStore->abortMultipartUpload($urn, $writeToken); } + + public function setPreserveCacheOnDelete(bool $preserve) { + $this->preserveCacheItemsOnDelete = $preserve; + } } diff --git a/lib/private/Files/Storage/Common.php b/lib/private/Files/Storage/Common.php index fb4aa0a7c3c..ab9cd5c3326 100644 --- a/lib/private/Files/Storage/Common.php +++ b/lib/private/Files/Storage/Common.php @@ -49,6 +49,7 @@ use OC\Files\Cache\Scanner; use OC\Files\Cache\Updater; use OC\Files\Cache\Watcher; use OC\Files\Filesystem; +use OC\Files\ObjectStore\ObjectStoreStorage; use OC\Files\Storage\Wrapper\Jail; use OC\Files\Storage\Wrapper\Wrapper; use OCP\Files\EmptyFileNameException; @@ -704,10 +705,21 @@ abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage { $result = $this->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath, true); if ($result) { - if ($sourceStorage->is_dir($sourceInternalPath)) { - $result = $sourceStorage->rmdir($sourceInternalPath); - } else { - $result = $sourceStorage->unlink($sourceInternalPath); + if ($sourceStorage->instanceOfStorage(ObjectStoreStorage::class)) { + /** @var ObjectStoreStorage $sourceStorage */ + $sourceStorage->setPreserveCacheOnDelete(true); + } + try { + if ($sourceStorage->is_dir($sourceInternalPath)) { + $result = $sourceStorage->rmdir($sourceInternalPath); + } else { + $result = $sourceStorage->unlink($sourceInternalPath); + } + } finally { + if ($sourceStorage->instanceOfStorage(ObjectStoreStorage::class)) { + /** @var ObjectStoreStorage $sourceStorage */ + $sourceStorage->setPreserveCacheOnDelete(false); + } } } return $result; diff --git a/lib/private/TemplateLayout.php b/lib/private/TemplateLayout.php index d17189a3bd0..3f6521a8e37 100644 --- a/lib/private/TemplateLayout.php +++ b/lib/private/TemplateLayout.php @@ -56,11 +56,14 @@ use OCP\IInitialStateService; use OCP\INavigationManager; use OCP\IURLGenerator; use OCP\IUserSession; +use OCP\L10N\IFactory; use OCP\Support\Subscription\IRegistry; use OCP\Util; class TemplateLayout extends \OC_Template { private static $versionHash = ''; + /** @var string[] */ + private static $cacheBusterCache = []; /** @var CSSResourceLocator|null */ public static $cssLocator = null; @@ -68,38 +71,29 @@ class TemplateLayout extends \OC_Template { /** @var JSResourceLocator|null */ public static $jsLocator = null; - /** @var IConfig */ - private $config; - - /** @var IInitialStateService */ - private $initialState; - - /** @var INavigationManager */ - private $navigationManager; + private IConfig $config; + private IAppManager $appManager; + private InitialStateService $initialState; + private INavigationManager $navigationManager; /** * @param string $renderAs * @param string $appId application id */ public function __construct($renderAs, $appId = '') { - /** @var IConfig */ - $this->config = \OC::$server->get(IConfig::class); - - /** @var IInitialStateService */ - $this->initialState = \OC::$server->get(IInitialStateService::class); + $this->config = \OCP\Server::get(IConfig::class); + $this->appManager = \OCP\Server::get(IAppManager::class); + $this->initialState = \OCP\Server::get(InitialStateService::class); + $this->navigationManager = \OCP\Server::get(INavigationManager::class); - // Add fallback theming variables if theming is disabled - if ($renderAs !== TemplateResponse::RENDER_AS_USER - || !\OC::$server->getAppManager()->isEnabledForUser('theming')) { + // Add fallback theming variables if not rendered as user + if ($renderAs !== TemplateResponse::RENDER_AS_USER) { // TODO cache generated default theme if enabled for fallback if server is erroring ? Util::addStyle('theming', 'default'); } // Decide which page we show if ($renderAs === TemplateResponse::RENDER_AS_USER) { - /** @var INavigationManager */ - $this->navigationManager = \OC::$server->get(INavigationManager::class); - parent::__construct('core', 'layout.user'); if (in_array(\OC_App::getCurrentApp(), ['settings','admin', 'help']) !== false) { $this->assign('bodyid', 'body-settings'); @@ -120,7 +114,7 @@ class TemplateLayout extends \OC_Template { } // Set body data-theme $this->assign('enabledThemes', []); - if (\OC::$server->getAppManager()->isEnabledForUser('theming') && class_exists('\OCA\Theming\Service\ThemesService')) { + if ($this->appManager->isEnabledForUser('theming') && class_exists('\OCA\Theming\Service\ThemesService')) { /** @var \OCA\Theming\Service\ThemesService */ $themesService = \OC::$server->get(\OCA\Theming\Service\ThemesService::class); $this->assign('enabledThemes', $themesService->getEnabledThemes()); @@ -131,9 +125,9 @@ class TemplateLayout extends \OC_Template { $this->assign('logoUrl', $logoUrl); // Set default app name - $defaultApp = \OC::$server->getAppManager()->getDefaultAppForUser(); - $defaultAppInfo = \OC::$server->getAppManager()->getAppInfo($defaultApp); - $l10n = \OC::$server->getL10NFactory()->get($defaultApp); + $defaultApp = $this->appManager->getDefaultAppForUser(); + $defaultAppInfo = $this->appManager->getAppInfo($defaultApp); + $l10n = \OC::$server->get(IFactory::class)->get($defaultApp); $this->assign('defaultAppName', $l10n->t($defaultAppInfo['name'])); // Add navigation entry @@ -213,8 +207,7 @@ class TemplateLayout extends \OC_Template { $showSimpleSignup = true; } - $appManager = \OCP\Server::get(IAppManager::class); - if ($appManager->isEnabledForUser('registration')) { + if ($this->appManager->isEnabledForUser('registration')) { $urlGenerator = \OCP\Server::get(IURLGenerator::class); $signUpLink = $urlGenerator->getAbsoluteURL('/index.php/apps/registration/'); } @@ -232,7 +225,7 @@ class TemplateLayout extends \OC_Template { $this->assign('language', $lang); $this->assign('locale', $locale); - if (\OC::$server->getSystemConfig()->getValue('installed', false)) { + if ($this->config->getSystemValueBool('installed', false)) { if (empty(self::$versionHash)) { $v = \OC_App::getAppVersions(); $v['core'] = implode('.', \OCP\Util::getVersion()); @@ -252,7 +245,7 @@ class TemplateLayout extends \OC_Template { $jsConfigHelper = new JSConfigHelper( \OCP\Util::getL10N('lib'), \OCP\Server::get(Defaults::class), - \OC::$server->getAppManager(), + $this->appManager, \OC::$server->getSession(), \OC::$server->getUserSession()->getUser(), $this->config, @@ -287,6 +280,7 @@ class TemplateLayout extends \OC_Template { if (\OC::$server->getSystemConfig()->getValue('installed', false) && !\OCP\Util::needUpgrade() && $pathInfo !== '' + && $pathInfo !== false && !preg_match('/^\/login/', $pathInfo) && $renderAs !== TemplateResponse::RENDER_AS_ERROR ) { @@ -295,7 +289,7 @@ class TemplateLayout extends \OC_Template { // If we ignore the scss compiler, // we need to load the guest css fallback \OC_Util::addStyle('guest'); - $cssFiles = self::findStylesheetFiles(\OC_Util::$styles, false); + $cssFiles = self::findStylesheetFiles(\OC_Util::$styles); } $this->assign('cssfiles', []); @@ -324,51 +318,64 @@ class TemplateLayout extends \OC_Template { $this->assign('id-app-navigation', $renderAs === TemplateResponse::RENDER_AS_USER ? '#app-navigation' : null); } - /** - * @param string $path - * @param string $file - * @return string - */ - protected function getVersionHashSuffix($path = false, $file = false) { + protected function getVersionHashSuffix(string $path = '', string $file = ''): string { if ($this->config->getSystemValueBool('debug', false)) { // allows chrome workspace mapping in debug mode return ""; } - $themingSuffix = ''; - $v = []; - if ($this->config->getSystemValueBool('installed', false)) { - if (\OC::$server->getAppManager()->isInstalled('theming')) { - $themingSuffix = '-' . $this->config->getAppValue('theming', 'cachebuster', '0'); - } - $v = \OC_App::getAppVersions(); + if ($this->config->getSystemValueBool('installed', false) === false) { + // if not installed just return the version hash + return '?v=' . self::$versionHash; } - // Try the webroot path for a match - if ($path !== false && $path !== '') { - $appName = $this->getAppNamefromPath($path); - if (array_key_exists($appName, $v)) { - $appVersion = $v[$appName]; - return '?v=' . substr(md5($appVersion), 0, 8) . $themingSuffix; - } + $hash = false; + // Try the web-root first + if ($path !== '') { + $hash = $this->getVersionHashByPath($path); + } + // If not found try the file + if ($hash === false && $file !== '') { + $hash = $this->getVersionHashByPath($file); + } + // As a last resort we use the server version hash + if ($hash === false) { + $hash = self::$versionHash; } - // fallback to the file path instead - if ($file !== false && $file !== '') { - $appName = $this->getAppNamefromPath($file); - if (array_key_exists($appName, $v)) { - $appVersion = $v[$appName]; - return '?v=' . substr(md5($appVersion), 0, 8) . $themingSuffix; + + // The theming app is force-enabled thus the cache buster is always available + $themingSuffix = '-' . $this->config->getAppValue('theming', 'cachebuster', '0'); + + return '?v=' . $hash . $themingSuffix; + } + + private function getVersionHashByPath(string $path): string|false { + if (array_key_exists($path, self::$cacheBusterCache) === false) { + // Not yet cached, so lets find the cache buster string + $appId = $this->getAppNamefromPath($path); + if ($appId === false) { + // No app Id could be guessed + return false; } + + $appVersion = $this->appManager->getAppVersion($appId); + // For shipped apps the app version is not a single source of truth, we rather also need to consider the Nextcloud version + if ($this->appManager->isShipped($appId)) { + $appVersion .= '-' . self::$versionHash; + } + + $hash = substr(md5($appVersion), 0, 8); + self::$cacheBusterCache[$path] = $hash; } - return '?v=' . self::$versionHash . $themingSuffix; + return self::$cacheBusterCache[$path]; } /** * @param array $styles * @return array */ - public static function findStylesheetFiles($styles, $compileScss = true) { + public static function findStylesheetFiles($styles) { if (!self::$cssLocator) { self::$cssLocator = \OCP\Server::get(CSSResourceLocator::class); } @@ -377,11 +384,10 @@ class TemplateLayout extends \OC_Template { } /** - * @param string $path - * @return string|boolean + * @return string|false */ - public function getAppNamefromPath($path) { - if ($path !== '' && is_string($path)) { + public function getAppNamefromPath(string $path) { + if ($path !== '') { $pathParts = explode('/', $path); if ($pathParts[0] === 'css') { // This is a scss request |