From da83f9a9b3cc2fe1216896f9da69caf8d13dec7c Mon Sep 17 00:00:00 2001 From: Benjamin Brahmer Date: Tue, 22 Aug 2023 18:26:42 +0200 Subject: use unique name for cache folder Signed-off-by: Benjamin Brahmer --- CHANGELOG.md | 1 + docs/install.md | 11 ++++++- docs/troubleshooting.md | 4 ++- lib/AppInfo/Application.php | 12 ++----- lib/Fetcher/FeedFetcher.php | 23 +++++++------ lib/Utility/Cache.php | 59 ++++++++++++++++++++++++++++++++++ tests/Unit/Fetcher/FeedFetcherTest.php | 21 ++++++------ 7 files changed, 100 insertions(+), 31 deletions(-) create mode 100644 lib/Utility/Cache.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 2f0581007..7b2b391b1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ The format is mostly based on [Keep a Changelog](https://keepachangelog.com/en/1 - Drop support for Nextcloud 25, Supported: 26, 27 (#2316) - Add a new command for occ `./occ news:updater:job` allows to check and reset the update job (#2166) - Check for available http(s) compression options and use them (gzip, deflate, brotli) (#2328) +- Change and unify [cache](https://nextcloud.github.io/news/install/#cache) to use the instance ID of Nextcloud (#2331) ### Fixed # Releases diff --git a/docs/install.md b/docs/install.md index 18598cc55..50bd69ce0 100644 --- a/docs/install.md +++ b/docs/install.md @@ -27,10 +27,19 @@ Also see the [Nextcloud documentation](https://docs.nextcloud.com/server/stable/ * Use MySQL/MariaDB or PostgreSQL for better database performance * Use the [updater script to thread and speed up the update](https://github.com/nextcloud/news-updater) +## Cache +News and it's libraries require a writeable temporary directory used as cache. The base directory depends on your system. +You can [configure a custom directory](https://docs.nextcloud.com/server/latest/admin_manual/configuration_server/config_sample_php_parameters.html?highlight=temp#tempdirectory) if you want. + +In most cases the base directory will be `/tmp`. News will create a folder `news-$instanceID` the [instance ID is defined by Nextcloud](https://docs.nextcloud.com/server/latest/admin_manual/configuration_server/config_sample_php_parameters.html?highlight=temp#instanceid). + +Inside that folder a subfolder `cache` is created, inside this cache folder news and libraries will try to create cache directories for caching images, html and more. + +You need to ensure that your web-server user can write to that directory. + ## Before you install/update the News app Before you install the app do the following: -* Check that your **nextcloud/data/** directory is owned by your web server user and that it is write/readable * Check that your installation fulfills the [requirements listed above](#dependencies) * [Set up Nextcloud Background Jobs](https://docs.nextcloud.org/server/latest/admin_manual/configuration_server/background_jobs_configuration.html#cron) to enable feed updates. diff --git a/docs/troubleshooting.md b/docs/troubleshooting.md index 5865665b4..5dad1604f 100644 --- a/docs/troubleshooting.md +++ b/docs/troubleshooting.md @@ -56,7 +56,9 @@ Feeds can be updated using Nextcloud's system cron or an external updater via th Follow this checklist: - Check admin settings of Nextcloud, was the last cron execution ok. -- Check the News admin settings, system cron is used to update news +- Check the logs for errors. +- Does your [cache configuration](install.md#cache) work? +- Check the News admin settings, system cron is used to update news. - You should see a info card at the top, which will tell you when the last job execution was. - If the card is red it is very likely that the update job is stuck. - If it is green then maybe only some feeds are failing to update, check the Nextcloud logs. diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index bc5e1b476..8bfa09c86 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -24,11 +24,11 @@ use OCA\News\Hooks\UserDeleteHook; use OCA\News\Search\FeedSearchProvider; use OCA\News\Search\FolderSearchProvider; use OCA\News\Search\ItemSearchProvider; +use OCA\News\Utility\Cache; use OCP\AppFramework\Bootstrap\IBootContext; use OCP\AppFramework\Bootstrap\IBootstrap; use OCP\AppFramework\Bootstrap\IRegistrationContext; -use OCP\ITempManager; use OCP\AppFramework\App; use OCA\News\Fetcher\FeedFetcher; @@ -92,15 +92,9 @@ class Application extends App implements IBootstrap $context->registerParameter('exploreDir', __DIR__ . '/../Explore/feeds'); $context->registerService(HTMLPurifier::class, function (ContainerInterface $c): HTMLPurifier { - $directory = $c->get(ITempManager::class)->getTempBaseDir() . '/news/cache/purifier'; - - if (!is_dir($directory)) { - mkdir($directory, 0770, true); - } - $config = HTMLPurifier_Config::createDefault(); $config->set('HTML.ForbiddenAttributes', 'class'); - $config->set('Cache.SerializerPath', $directory); + $config->set('Cache.SerializerPath', $c->get(Cache::class)->getCache("purifier")); $config->set('HTML.SafeIframe', true); $config->set( 'URI.SafeIframeRegexp', @@ -140,7 +134,7 @@ class Application extends App implements IBootstrap $context->registerService(Favicon::class, function (ContainerInterface $c): Favicon { $favicon = new Favicon(); - $favicon->cache(['dir' => $c->get(ITempManager::class)->getTempBaseDir()]); + $favicon->cache(['dir' => $c->get(Cache::class)->getCache("feedFavicon")]); return $favicon; }); } diff --git a/lib/Fetcher/FeedFetcher.php b/lib/Fetcher/FeedFetcher.php index 33fb2a508..001a50bb4 100755 --- a/lib/Fetcher/FeedFetcher.php +++ b/lib/Fetcher/FeedFetcher.php @@ -30,6 +30,7 @@ use OCP\ITempManager; use OCA\News\Db\Item; use OCA\News\Db\Feed; use OCA\News\Utility\Time; +use OCA\News\Utility\Cache; use OCA\News\Scraper\Scraper; use OCA\News\Config\FetcherConfig; use Psr\Log\LoggerInterface; @@ -58,11 +59,6 @@ class FeedFetcher implements IFeedFetcher */ private $l10n; - /** - * @var ITempManager - */ - private $ITempManager; - /** * @var Time */ @@ -77,25 +73,30 @@ class FeedFetcher implements IFeedFetcher * @var FetcherConfig */ private $fetcherConfig; + + /** + * @var Cache + */ + private $cache; public function __construct( FeedIo $fetcher, Favicon $favicon, Scraper $scraper, IL10N $l10n, - ITempManager $ITempManager, Time $time, LoggerInterface $logger, - FetcherConfig $fetcherConfig + FetcherConfig $fetcherConfig, + Cache $cache ) { $this->reader = $fetcher; $this->faviconFactory = $favicon; $this->scraper = $scraper; $this->l10n = $l10n; - $this->ITempManager = $ITempManager; $this->time = $time; $this->logger = $logger; $this->fetcherConfig = $fetcherConfig; + $this->cache = $cache; } @@ -395,8 +396,10 @@ class FeedFetcher implements IFeedFetcher return is_string($return) ? $return : null; } - // logo will be saved in the tmp folder provided by Nextcloud, file is named as md5 of the url - $favicon_path = join(DIRECTORY_SEPARATOR, [$this->ITempManager->getTempBaseDir(), md5($favicon)]); + $logo_cache = $this->cache->getCache("feedLogo"); + + // file name of the logo is md5 of the url + $favicon_path = join(DIRECTORY_SEPARATOR, [$logo_cache, md5($favicon)]); $downloaded = false; if (file_exists($favicon_path)) { diff --git a/lib/Utility/Cache.php b/lib/Utility/Cache.php new file mode 100644 index 000000000..6337487cf --- /dev/null +++ b/lib/Utility/Cache.php @@ -0,0 +1,59 @@ + + * @copyright 2023 Benjamin Brahmer + */ +namespace OCA\News\Utility; + +use OCP\ITempManager; +use OCP\IConfig; + +class Cache +{ + + + /** + * @var ITempManager + */ + private $ITempManager; + + /** + * @var IConfig + */ + private $IConfig; + + + public function __construct( + ITempManager $ITempManager, + IConfig $IConfig + ) { + $this->ITempManager = $ITempManager; + $this->IConfig = $IConfig; + } + + /** + * Get a news app cache directory + * + * @param String $name for the sub-directory, is created if not existing + * + * @return String $directory The path for the cache + */ + public function getCache(String $name): String + { + $baseDir = $this->ITempManager->getTempBaseDir(); + $instanceID = $this->IConfig->getSystemValue('instanceid'); + + $directory = join(DIRECTORY_SEPARATOR, [$baseDir, "news-" . $instanceID, 'cache', $name]); + + if (!is_dir($directory)) { + mkdir($directory, 0770, true); + } + + return $directory; + } +} diff --git a/tests/Unit/Fetcher/FeedFetcherTest.php b/tests/Unit/Fetcher/FeedFetcherTest.php index c46a9df7a..d73edc1ef 100644 --- a/tests/Unit/Fetcher/FeedFetcherTest.php +++ b/tests/Unit/Fetcher/FeedFetcherTest.php @@ -31,6 +31,7 @@ use OCA\News\Fetcher\FeedFetcher; use OCA\News\Config\FetcherConfig; use OCA\News\Utility\Time; +use OCA\News\Utility\Cache; use OCP\IL10N; use OCP\ITempManager; @@ -83,11 +84,6 @@ class FeedFetcherTest extends TestCase */ private $l10n; - /** - * @var MockObject|ITempManager - */ - private $ITempManager; - /** * @var MockObject|ItemInterface */ @@ -113,6 +109,11 @@ class FeedFetcherTest extends TestCase */ private $fetcherConfig; + /** + * @var MockObject|Cache + */ + private $cache; + //metadata /** * @var integer @@ -159,9 +160,6 @@ class FeedFetcherTest extends TestCase $this->l10n = $this->getMockBuilder(IL10N::class) ->disableOriginalConstructor() ->getMock(); - $this->ITempManager = $this->getMockBuilder(ITempManager::class) - ->disableOriginalConstructor() - ->getMock(); $this->reader = $this->getMockBuilder(FeedIo::class) ->disableOriginalConstructor() ->getMock(); @@ -198,15 +196,18 @@ class FeedFetcherTest extends TestCase $this->fetcherConfig = $this->getMockBuilder(FetcherConfig::class) ->disableOriginalConstructor() ->getMock(); + $this->cache = $this->getMockBuilder(Cache::class) + ->disableOriginalConstructor() + ->getMock(); $this->fetcher = new FeedFetcher( $this->reader, $this->favicon, $this->scraper, $this->l10n, - $this->ITempManager, $timeFactory, $this->logger, - $this->fetcherConfig + $this->fetcherConfig, + $this->cache ); $this->url = 'http://tests/'; -- cgit v1.2.3