use unique name for cache folder

Signed-off-by: Benjamin Brahmer <info@b-brahmer.de>
This commit is contained in:
Benjamin Brahmer 2023-08-22 18:26:42 +02:00
parent b99320dd4a
commit da83f9a9b3
7 changed files with 100 additions and 31 deletions

View File

@ -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

View File

@ -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.

View File

@ -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.

View File

@ -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;
});
}

View File

@ -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)) {

59
lib/Utility/Cache.php Normal file
View File

@ -0,0 +1,59 @@
<?php
/**
* Nextcloud - News
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Benjamin Brahmer <info@b-brahmer.de>
* @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;
}
}

View File

@ -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/';