simplified the process of adding more search sites
This commit is contained in:
59
lib/Tools/File.php
Normal file
59
lib/Tools/File.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace OCA\NCDownloader\Tools;
|
||||
|
||||
class File
|
||||
{
|
||||
|
||||
private $dirName;
|
||||
|
||||
//$dir_name = iconv("utf-8", "gb2312", $dir_name);
|
||||
|
||||
public function __construct($dirname, $suffix = "php")
|
||||
{
|
||||
|
||||
$this->dirName = $dirname;
|
||||
$this->suffix = $suffix;
|
||||
|
||||
if (!is_dir($dirname)) {
|
||||
throw new \Exception("directory ${dirname} doesn't exit");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static function create($dir, $suffix)
|
||||
{
|
||||
return new static($dir, $suffix);
|
||||
}
|
||||
|
||||
public function scandir($recursive = false)
|
||||
{
|
||||
if ($recursive) {
|
||||
return $this->scandirRecursive();
|
||||
}
|
||||
|
||||
$files = \glob($this->dirName . DIRECTORY_SEPARATOR . "*.{$this->suffix}");
|
||||
$this->Files = $files;
|
||||
return $files;
|
||||
}
|
||||
|
||||
protected function scandirRecursive()
|
||||
{
|
||||
|
||||
$directory = new \RecursiveDirectoryIterator($this->dirName);
|
||||
$iterator = new \RecursiveIteratorIterator($directory);
|
||||
$iterators = new \RegexIterator($iterator, '/.*\.' . $this->suffix . '$/', \RegexIterator::GET_MATCH);
|
||||
|
||||
$files = array();
|
||||
foreach ($iterators as $info) {
|
||||
if ($info) {
|
||||
$files[] = reset($info);
|
||||
}
|
||||
}
|
||||
$this->Files = $files;
|
||||
return $files;
|
||||
}
|
||||
public function getBasename($file){
|
||||
return basename($file,".".$this->suffix);
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace OCA\NCDownloader\Tools;
|
||||
|
||||
use OCA\NCDownloader\Search\Sites\searchInterface;
|
||||
use OCA\NCDownloader\Tools\aria2Options;
|
||||
use OC\Files\Filesystem;
|
||||
|
||||
@@ -331,4 +332,37 @@ class Helper
|
||||
return (bool) self::findBinaryPath('python');
|
||||
}
|
||||
|
||||
public static function findSearchSites($dir, $suffix = 'php'): array
|
||||
{
|
||||
$filetool = File::create($dir, $suffix);
|
||||
$files = $filetool->scandir();
|
||||
$sites = [];
|
||||
foreach ($files as $file) {
|
||||
$basename = $filetool->getBasename($file);
|
||||
$namespace = 'OCA\\NCDownloader\\Search\\Sites\\';
|
||||
$className = $namespace . $basename;
|
||||
if (in_array(searchInterface::class, class_implements($className))) {
|
||||
$sites[] = ['class' => $className, 'name' => $basename];
|
||||
}
|
||||
}
|
||||
return $sites;
|
||||
}
|
||||
|
||||
public static function getSearchSites(): array
|
||||
{
|
||||
$key = 'searchSites';
|
||||
$memcache = \OC::$server->getMemCacheFactory()->createDistributed($key);
|
||||
if ($memcache->hasKey($key)) {
|
||||
$sites = $memcache->get($key);
|
||||
} else {
|
||||
try {
|
||||
$sites = Helper::findSearchSites(__DIR__ . "/../Search/Sites/");
|
||||
$memcache->set($key, $sites, 300);
|
||||
} catch (\Exception $e) {
|
||||
self::debug($e->getMessage());
|
||||
}
|
||||
}
|
||||
return $sites;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user