Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use unique name for cache folder #2331

Merged
merged 1 commit into from
Aug 26, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion docs/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
4 changes: 3 additions & 1 deletion docs/troubleshooting.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 3 additions & 9 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
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;
Expand Down Expand Up @@ -92,15 +92,9 @@ public function register(IRegistrationContext $context): void
$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',
Expand Down Expand Up @@ -140,7 +134,7 @@ public function register(IRegistrationContext $context): void

$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;
});
}
Expand Down
23 changes: 13 additions & 10 deletions lib/Fetcher/FeedFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
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;
Expand Down Expand Up @@ -58,11 +59,6 @@ class FeedFetcher implements IFeedFetcher
*/
private $l10n;

/**
* @var ITempManager
*/
private $ITempManager;

/**
* @var Time
*/
Expand All @@ -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;
}


Expand Down Expand Up @@ -395,8 +396,10 @@ protected function getFavicon(FeedInterface $feed, string $url): ?string
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)) {
Expand Down
59 changes: 59 additions & 0 deletions lib/Utility/Cache.php
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
* @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;
}
}
21 changes: 11 additions & 10 deletions tests/Unit/Fetcher/FeedFetcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use OCA\News\Config\FetcherConfig;

use OCA\News\Utility\Time;
use OCA\News\Utility\Cache;
use OCP\IL10N;
use OCP\ITempManager;

Expand Down Expand Up @@ -83,11 +84,6 @@ class FeedFetcherTest extends TestCase
*/
private $l10n;

/**
* @var MockObject|ITempManager
*/
private $ITempManager;

/**
* @var MockObject|ItemInterface
*/
Expand All @@ -113,6 +109,11 @@ class FeedFetcherTest extends TestCase
*/
private $fetcherConfig;

/**
* @var MockObject|Cache
*/
private $cache;

//metadata
/**
* @var integer
Expand Down Expand Up @@ -159,9 +160,6 @@ protected function setUp(): void
$this->l10n = $this->getMockBuilder(IL10N::class)
->disableOriginalConstructor()
->getMock();
$this->ITempManager = $this->getMockBuilder(ITempManager::class)
->disableOriginalConstructor()
->getMock();
$this->reader = $this->getMockBuilder(FeedIo::class)
->disableOriginalConstructor()
->getMock();
Expand Down Expand Up @@ -198,15 +196,18 @@ protected function setUp(): void
$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/';

Expand Down