summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorBenjamin Brahmer <info@b-brahmer.de>2023-08-22 18:26:42 +0200
committerBenjamin Brahmer <info@b-brahmer.de>2023-08-26 10:52:58 +0200
commitda83f9a9b3cc2fe1216896f9da69caf8d13dec7c (patch)
tree07a4f3c7b671c5a7917f417d31412d97209d41a3 /lib
parentb99320dd4aa9c5c732d331bd54d65e8010c4662d (diff)
use unique name for cache folder
Signed-off-by: Benjamin Brahmer <info@b-brahmer.de>
Diffstat (limited to 'lib')
-rw-r--r--lib/AppInfo/Application.php12
-rwxr-xr-xlib/Fetcher/FeedFetcher.php23
-rw-r--r--lib/Utility/Cache.php59
3 files changed, 75 insertions, 19 deletions
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;
@@ -59,11 +60,6 @@ class FeedFetcher implements IFeedFetcher
private $l10n;
/**
- * @var ITempManager
- */
- private $ITempManager;
-
- /**
* @var Time
*/
private $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 @@
+<?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;
+ }
+}