2019-05-08 14:45:16 +00:00
|
|
|
<?php
|
2020-09-21 12:25:50 +00:00
|
|
|
/*
|
2024-02-02 18:27:50 +00:00
|
|
|
* Copyright (c) 2020-2024. The Nextcloud Bookmarks contributors.
|
2019-05-08 14:45:16 +00:00
|
|
|
*
|
2020-09-21 12:25:50 +00:00
|
|
|
* This file is licensed under the Affero General Public License version 3 or later. See the COPYING file.
|
2019-05-08 14:45:16 +00:00
|
|
|
*/
|
2019-12-28 16:41:31 +00:00
|
|
|
|
2019-10-26 12:10:49 +00:00
|
|
|
namespace OCA\Bookmarks\Service;
|
2019-05-08 14:45:16 +00:00
|
|
|
|
2021-02-07 11:17:43 +00:00
|
|
|
use OCP\AppFramework\Utility\ITimeFactory;
|
2019-05-08 14:45:16 +00:00
|
|
|
use OCP\Files\IAppData;
|
2019-10-26 12:10:49 +00:00
|
|
|
use OCP\Files\NotFoundException;
|
|
|
|
use OCP\Files\NotPermittedException;
|
2021-02-07 11:17:43 +00:00
|
|
|
use OCP\Files\SimpleFS\ISimpleFolder;
|
2019-05-08 14:45:16 +00:00
|
|
|
use OCP\ICache;
|
|
|
|
|
|
|
|
class FileCache implements ICache {
|
2020-08-02 14:05:52 +00:00
|
|
|
public const TIMEOUT = 60 * 60 * 24 * 30 * 2; // two months
|
2019-05-08 14:45:16 +00:00
|
|
|
|
|
|
|
protected $storage;
|
|
|
|
|
2021-02-07 11:17:43 +00:00
|
|
|
/**
|
|
|
|
* @var ITimeFactory
|
|
|
|
*/
|
|
|
|
private $timeFactory;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var IAppData
|
|
|
|
*/
|
|
|
|
private $appData;
|
|
|
|
|
|
|
|
public function __construct(IAppData $appData, ITimeFactory $timeFactory) {
|
|
|
|
$this->appData = $appData;
|
|
|
|
$this->timeFactory = $timeFactory;
|
|
|
|
}
|
|
|
|
|
2022-04-11 18:43:50 +00:00
|
|
|
public static function isAvailable(): bool {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-02-07 11:17:43 +00:00
|
|
|
/**
|
|
|
|
* @return ISimpleFolder
|
|
|
|
* @throws NotPermittedException
|
|
|
|
*/
|
|
|
|
private function getStorage(): ISimpleFolder {
|
|
|
|
if ($this->storage !== null) {
|
|
|
|
return $this->storage;
|
|
|
|
}
|
2019-05-08 14:45:16 +00:00
|
|
|
try {
|
2021-02-07 11:17:43 +00:00
|
|
|
$this->storage = $this->appData->getFolder('cache');
|
2019-10-26 12:10:49 +00:00
|
|
|
} catch (NotFoundException $e) {
|
2021-02-07 11:17:43 +00:00
|
|
|
// noop
|
|
|
|
}
|
|
|
|
if ($this->storage === null || !$this->storage->fileExists('/')) {
|
|
|
|
$this->storage = $this->appData->newFolder('cache');
|
2019-05-08 14:45:16 +00:00
|
|
|
}
|
2021-02-01 12:51:29 +00:00
|
|
|
if (!$this->storage->fileExists('CACHEDIR.TAG')) {
|
|
|
|
try {
|
|
|
|
$this->storage->newFile('CACHEDIR.TAG',
|
|
|
|
'Signature: 8a477f597d28d172789f06886806bc55' . "\r\n" .
|
|
|
|
'# This file is a cache directory tag created by the nextcloud bookmarks app.' . "\r\n" .
|
|
|
|
'# For information about cache directory tags, see:' . "\r\n" .
|
|
|
|
'# http://www.brynosaurus.com/cachedir/)' . "\r\n"
|
|
|
|
);
|
|
|
|
} catch (NotPermittedException $e) {
|
|
|
|
// No op
|
|
|
|
}
|
|
|
|
}
|
2021-02-07 11:17:43 +00:00
|
|
|
return $this->storage;
|
2019-05-08 14:45:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $key
|
|
|
|
* @return mixed|null
|
|
|
|
*/
|
|
|
|
public function get($key) {
|
|
|
|
$result = null;
|
2021-02-07 11:17:43 +00:00
|
|
|
try {
|
|
|
|
$result = $this->getStorage()->getFile($key)->getContent();
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
// noop
|
2019-05-08 14:45:16 +00:00
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
2019-10-26 12:10:49 +00:00
|
|
|
|
2019-05-08 14:45:16 +00:00
|
|
|
/**
|
|
|
|
* Returns the size of the stored/cached data
|
|
|
|
*
|
|
|
|
* @param string $key
|
2023-02-19 16:32:44 +00:00
|
|
|
* @return int|float
|
|
|
|
* @throws NotFoundException|NotPermittedException
|
2019-05-08 14:45:16 +00:00
|
|
|
*/
|
2023-02-19 16:32:44 +00:00
|
|
|
public function size(string $key) {
|
2019-05-08 14:45:16 +00:00
|
|
|
$result = 0;
|
|
|
|
if ($this->hasKey($key)) {
|
2021-02-07 11:17:43 +00:00
|
|
|
$result = $this->getStorage()->getFile($key)->getSize();
|
2019-05-08 14:45:16 +00:00
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
2019-10-26 12:10:49 +00:00
|
|
|
|
2019-05-08 14:45:16 +00:00
|
|
|
/**
|
|
|
|
* @param string $key
|
|
|
|
* @param mixed $value
|
|
|
|
* @param int $ttl
|
2021-01-20 16:38:11 +00:00
|
|
|
* @return bool
|
2019-05-08 14:45:16 +00:00
|
|
|
*/
|
|
|
|
public function set($key, $value, $ttl = 0) {
|
2021-02-07 11:17:43 +00:00
|
|
|
try {
|
|
|
|
$this->getStorage()->newFile($key, $value);
|
|
|
|
} catch (NotPermittedException $e) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-05-08 14:45:16 +00:00
|
|
|
return true;
|
|
|
|
}
|
2019-12-28 16:41:31 +00:00
|
|
|
|
2019-05-08 14:45:16 +00:00
|
|
|
/**
|
|
|
|
* @param string $key
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function hasKey($key) {
|
2021-02-07 11:17:43 +00:00
|
|
|
if ($this->getStorage()->fileExists($key)) {
|
2019-05-08 14:45:16 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2019-10-26 12:10:49 +00:00
|
|
|
|
2019-05-08 14:45:16 +00:00
|
|
|
/**
|
|
|
|
* @param string $key
|
2021-01-20 16:38:11 +00:00
|
|
|
* @return bool
|
2019-10-26 12:10:49 +00:00
|
|
|
* @throws NotFoundException
|
|
|
|
* @throws NotPermittedException
|
2019-05-08 14:45:16 +00:00
|
|
|
*/
|
|
|
|
public function remove($key) {
|
2021-02-07 11:17:43 +00:00
|
|
|
return (boolean) $this->getStorage()->getFile($key)->delete();
|
2019-05-08 14:45:16 +00:00
|
|
|
}
|
2019-10-26 12:10:49 +00:00
|
|
|
|
2019-05-08 14:45:16 +00:00
|
|
|
/**
|
|
|
|
* @param string $prefix
|
2019-10-26 12:10:49 +00:00
|
|
|
* @return void
|
|
|
|
* @throws NotPermittedException
|
2019-05-08 14:45:16 +00:00
|
|
|
*/
|
|
|
|
public function clear($prefix = '') {
|
2021-02-07 11:17:43 +00:00
|
|
|
foreach ($this->getStorage()->getDirectoryListing() as $file) {
|
|
|
|
$file->delete();
|
|
|
|
}
|
2019-05-08 14:45:16 +00:00
|
|
|
}
|
2019-10-26 12:10:49 +00:00
|
|
|
|
2019-05-08 14:45:16 +00:00
|
|
|
/**
|
|
|
|
* Runs GC
|
2019-10-26 12:10:49 +00:00
|
|
|
*
|
|
|
|
* @throws NotPermittedException
|
2019-05-08 14:45:16 +00:00
|
|
|
*/
|
2020-09-21 12:17:46 +00:00
|
|
|
public function gc(): void {
|
2021-02-07 11:17:43 +00:00
|
|
|
foreach ($this->getStorage()->getDirectoryListing() as $file) {
|
|
|
|
if ($this->timeFactory->getTime() - self::TIMEOUT > $file->getMTime()) {
|
2019-05-08 14:45:16 +00:00
|
|
|
$file->delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|