Merge branch 'master' into master

This commit is contained in:
Mohamed-Ismail Mejri
2022-04-25 13:33:53 +01:00
committed by GitHub
24 changed files with 389 additions and 417 deletions

View File

@@ -97,10 +97,14 @@ class Aria2
private function configure()
{
if (!is_dir($this->confDir)) {
if ($this->confDir && !is_dir($this->confDir)) {
mkdir($this->confDir, 0755, true);
}
if (!is_dir($dir = $this->getDownloadDir())) {
$dir = "";
if (($dir = $this->getDownloadDir()) && !is_dir($dir)) {
mkdir($dir, 0755, true);
}
if (($dir = $this->getTorrentsDir()) && !is_dir($dir)) {
mkdir($dir, 0755, true);
}
$this->followTorrent(true);
@@ -123,12 +127,9 @@ class Aria2
public function setTorrentsDir($dir)
{
$this->torrentsDir = $dir;
if (!is_dir($dir)) {
mkdir($dir, 0755, true);
}
return $this;
}
public function getTorrentsDir()
public function getTorrentsDir(): string
{
return $this->torrentsDir;
}
@@ -136,12 +137,9 @@ class Aria2
{
$this->setOption('dir', $dir);
$this->downloadDir = $dir;
if (!is_dir($dir)) {
mkdir($dir, 0755, true);
}
return $this;
}
public function getDownloadDir()
public function getDownloadDir(): string
{
return $this->downloadDir;
}

View File

@@ -67,6 +67,7 @@ class DbHelper
->andWhere('type = :type')
->setParameter('uid', $uid)
->setParameter('type', Helper::DOWNLOADTYPE['YOUTUBE-DL'])
->orderBy('id', 'DESC')
->execute();
return $qb->fetchAll();
}
@@ -132,7 +133,12 @@ class DbHelper
public function getExtra($data)
{
if ($this->getDBType() == "pgsql" && is_resource($data)) {
$extra = pg_unescape_bytea(stream_get_contents($data));
if (function_exists("pg_unescape_bytea")) {
$extra = pg_unescape_bytea(stream_get_contents($data));
}
else {
$extra = stream_get_contents($data);
}
return unserialize($extra);
}
return unserialize($data);

View File

@@ -2,15 +2,19 @@
namespace OCA\NCDownloader\Tools;
use Exception;
use OCA\NCDownloader\Search\Sites\searchInterface;
use OCA\NCDownloader\Tools\aria2Options;
use OCA\NCDownloader\Tools\Settings;
use OCP\IUser;
use OC\Files\Filesystem;
use OC_Util;
class Helper
{
public const DOWNLOADTYPE = ['ARIA2' => 1, 'YOUTUBE-DL' => 2, 'OTHERS' => 3];
public const STATUS = ['ACTIVE' => 1, 'PAUSED' => 2, 'COMPLETE' => 3, 'WAITING' => 4, 'ERROR' => 5];
const MAXLEN = 255;
const MAXFILELEN = 255;
public static function isUrl($URL)
{
@@ -61,8 +65,8 @@ class Helper
}
public static function clipFilename($filename)
{
if (($len = strlen($filename)) > 64) {
return substr($filename, $len - 64);
if (($len = strlen($filename)) > self::MAXFILELEN) {
return substr($filename, $len - self::MAXFILELEN);
}
return $filename;
}
@@ -73,7 +77,7 @@ class Helper
} else {
$filename = self::getUrlPath($url);
}
return substr($filename, 0, self::MAXLEN);
return substr($filename, 0, self::MAXFILELEN);
}
public static function formatBytes($size, $precision = 2)
{
@@ -135,6 +139,9 @@ class Helper
public static function debug($msg)
{
if (is_array($msg)) {
$msg = implode(",", $msg);
}
$logger = \OC::$server->getLogger();
$logger->error($msg, ['app' => 'ncdownloader']);
}
@@ -279,14 +286,13 @@ class Helper
];
return $titles[$type];
}
// the relative home folder of a nextcloud user
public static function getUserFolder($uid = null)
// the relative home folder of a nextcloud user,e.g. /admin/files
public static function getUserFolder($uid = null): string
{
if (!empty($rootFolder = Filesystem::getRoot())) {
return $rootFolder;
} else if (isset($uid)) {
return "/" . $uid . "/files";
}
return '';
}
@@ -374,4 +380,124 @@ class Helper
return $sites;
}
public static function getMountPoints(): ?array
{
return Filesystem::getMountPoints("/");
}
public static function getDataDir(): string
{
return \OC::$server->getSystemConfig()->getValue('datadirectory');
}
public static function getLocalFolder(string $path): string
{
if (self::getUID()) {
OC_Util::setupFS();
return Filesystem::getLocalFolder($path);
}
return "";
}
public static function getRealDownloadDir($uid = null): string
{
$dlDir = self::getDownloadDir();
return self::getLocalFolder($dlDir);
}
public static function getRealTorrentsDir($uid = null): string
{
$dir = self::getSettings('ncd_torrents_dir', "/Torrents");
return self::getLocalFolder($dir);
}
public static function getUser(): ?IUser
{
return \OC::$server->getUserSession()->getUser();
}
public static function getUID(): string
{
$user = self::getUser();
$uid = $user ? $user->getUID() : "";
return $uid;
}
public static function getSettings($key, $default = null, int $type = Settings::TYPE['USER'])
{
$settings = self::newSettings();
return $settings->setType($type)->get($key, $default);
}
public static function newSettings($uid = null)
{
$uid = $uid ?? self::getUID();
return Settings::create($uid);
}
public static function getYoutubeConfig($uid = null): array
{
$config = [
'binary' => self::getSettings("ncd_yt_binary", null, Settings::TYPE['SYSTEM']),
'downloadDir' => Helper::getRealDownloadDir(),
'settings' => self::newSettings()->getYoutube(),
];
return $config;
}
public static function getAria2Config($uid = null): array
{
$options = [];
$uid = $uid ?? self::getUID();
$settings = self::newSettings($uid);
$realDownloadDir = Helper::getRealDownloadDir($uid);
$torrentsDir = Helper::getRealTorrentsDir($uid);
$appPath = self::getAppPath();
$dataDir = self::getDataDir();
$aria2_dir = $dataDir . "/aria2";
$options['seed_time'] = $settings->get("ncd_seed_time");
$options['seed_ratio'] = $settings->get("ncd_seed_ratio");
if (is_array($customSettings = $settings->getAria2())) {
$options = array_merge($customSettings, $options);
}
$token = $settings->setType(Settings::TYPE['SYSTEM'])->get('ncd_rpctoken');
$config = [
'dir' => $realDownloadDir,
'torrents_dir' => $torrentsDir,
'conf_dir' => $aria2_dir,
'token' => $token,
'settings' => $options,
'binary' => $settings->setType(Settings::TYPE['SYSTEM'])->get('ncd_aria2_binary'),
'startHook' => $appPath . "/hooks/startHook.sh",
'completeHook' => $appPath . "/hooks/completeHook.sh",
];
return $config;
}
public static function getAppPath(): string
{
return \OC::$server->getAppManager()->getAppPath('ncdownloader');
}
public static function folderUpdated(string $dir): bool
{
if (!file_exists($dir)) {
return false;
}
$checkFile = $dir . "/.lastmodified";
if (!file_exists($checkFile)) {
$time = \filemtime($dir);
file_put_contents($checkFile, $time);
return false;
}
$lastModified = (int) file_get_contents($checkFile);
$time = \filemtime($dir);
if ($time > $lastModified) {
file_put_contents($checkFile, $time);
return true;
}
return false;
}
public static function getDownloadDir(): string
{
return self::getSettings('ncd_downloader_dir', "/Downloads");
}
}

View File

@@ -7,17 +7,19 @@ use OC\AllConfig;
class Settings extends AllConfig
{
//@config OC\AppConfig
private $config;
private $appConfig;
//@OC\SystemConfig
private $sysConfig;
//@OC\AllConfig
private $allConfig;
private $user;
private $appName;
//type of settings (system = 1 or app =2)
private $type;
public const TYPE = ['SYSTEM' => 0x001, 'USER' => 0x010, 'APP' => 0x100];
private static $instance = null;
public const TYPE = ['SYSTEM' => 1, 'USER' => 2, 'APP' => 3];
public function __construct($user = null)
{
$this->appConfig = \OC::$server->getAppConfig();
@@ -29,6 +31,14 @@ class Settings extends AllConfig
//$this->connAdapter = \OC::$server->getDatabaseConnection();
//$this->conn = $this->connAdapter->getInner();
}
public static function create($user = null)
{
if (!self::$instance) {
self::$instance = new static($user);
}
return self::$instance;
}
public function setType($type)
{
$this->type = $type;
@@ -104,141 +114,4 @@ class Settings extends AllConfig
}
return $value;
}
}
class customSettings
{
private $name = null;
private $dbType = 0;
private $table = 'ncdownloader_settings';
private $uid = null;
const PGSQL = 1, MYSQL = 2, SQL = 3;
/* @var OC\DB\ConnectionAdapter */
private $connAdapter;
/* @var OC\DB\Connection */
private $conn;
private $type = 2; //personal = 2,admin =1
public function __construct()
{
if (\OC::$server->getConfig()->getSystemValue('dbtype') == 'pgsql') {
$this->dbType = PGSQL;
}
$this->connAdapter = \OC::$server->getDatabaseConnection();
$this->conn = $this->connAdapter->getInner();
$this->prefixTable();
}
private function prefixTable()
{
$this->table = '*PREFIX*' . $this->table;
return $this->table;
}
public function set($name, $value)
{
if ($this->have($name)) {
$this->update($name, $value);
} else {
$this->insert($name, $value);
}
}
public function setType($type)
{
$this->type = $type;
}
public function get($name)
{
if (isset($this->uid)) {
$sql = sprintf("SELECT value FROM %s WHERE uid = ? AND name = ? LIMIT 1", $this->table);
$query = \OC_DB::prepare($sql);
$result = $query->execute(array($this->uid, $name));
} else {
$sql = sprintf("SELECT value FROM %s WHERE name = ? LIMIT 1", $this->table);
$query = \OC_DB::prepare($sql);
$result = $query->execute(array($name));
}
if ($query->rowCount() == 1) {
return $result->fetchOne();
}
return null;
}
public function setUID($uid)
{
$this->uid = $uid;
}
public function setTable($table)
{
$this->table = $table;
}
public function getTable()
{
return $this->table;
}
public function have($name)
{
if (isset($this->uid)) {
$sql = sprintf("SELECT value FROM %s WHERE uid = ? AND name = ? AND type = ? LIMIT 1", $this->table);
$query = \OC_DB::prepare($sql);
$query->execute(array($name, $this->uid, $this->type));
} else {
$sql = sprintf("SELECT value FROM %s WHERE name = ? AND type = ? LIMIT 1", $this->table);
$query = \OC_DB::prepare($sql);
$query->execute(array($name, $this->type));
}
if ($query->rowCount() == 1) {
return true;
}
return false;
}
public function getAll()
{
$sql = 'SELECT `name`, `value` FROM `*PREFIX*' . $this->table . '`'
. (!is_null($this->uid) ? ' WHERE `UID` = ?' : '');
if ($this->DbType == 1) {
$sql = 'SELECT "name", "value" FROM *PREFIX*' . $this->table . ''
. (!is_null($this->uid) ? ' WHERE "uid" = ?' : '');
}
$query = \OC_DB::prepare($sql);
if (!is_null($this->uid)) {
return $query->execute(array($this->uid));
} else {
return $query->execute();
}
}
public function update($value)
{
if (isset($this->uid)) {
$sql = sprintf("UPDATE %s SET value = ? WHERE name = ? AND type = ? AND uid = ?", $this->table);
//OCP\DB\IPreparedStatement
$query = \OC_DB::prepare($sql);
$query->execute(array($value, $name, $this->type, $this->uid));
} else {
$sql = sprintf("UPDATE %s SET value = ? WHERE name = ? AND type = ?", $this->table);
//OCP\DB\IPreparedStatement
$query = \OC_DB::prepare($sql);
$query->execute(array($value, $name, $this->type));
}
}
public function insert($name, $value)
{
$sql = sprintf("INSERT INTO %s (name,value,type,uid) VALUES(?,?,?,?)", $this->table);
//OCP\DB\IPreparedStatement
$query = \OC_DB::prepare($sql);
$query->execute(array($name, $value, $this->type, $this->uid));
}
}

View File

@@ -104,9 +104,9 @@ class Youtube
return $this;
}
public static function create()
public static function create($options)
{
return new self();
return new self($options);
}
public function setDownloadDir($dir)
@@ -116,7 +116,7 @@ class Youtube
public function getDownloadDir()
{
return $this->getDownloadDir;
return $this->downloadDir;
}
public function prependOption(string $option)

View File

@@ -66,7 +66,9 @@ class YoutubeHelper
if ($file) {
$extra = serialize($extra);
if($this->dbconn->getDBType() == "pgsql"){
$extra = pg_escape_bytea($extra);
if (function_exists("pg_escape_bytea")) {
$extra = pg_escape_bytea($extra);
}
}
$data = [
'uid' => $this->user,

View File

@@ -2,7 +2,6 @@
namespace OCA\NCDownloader\Tools;
use OCA\NCDownloader\Tools\Helper;
use OCA\NCDownloader\Tools\Settings;
use OC\Files\Utils\Scanner;
use \OCP\EventDispatcher\IEventDispatcher;
@@ -10,19 +9,17 @@ class folderScan
{
private $user;
private $path;
private $realDir;
public function __construct($path = null, $user = null)
{
$this->user = $user ?? \OC::$server->getUserSession()->getUser()->getUID();
$this->user = $user ?? Helper::getUID();
$this->path = $path ?? $this->getDefaultPath();
$this->realDir = \OC::$server->getSystemConfig()->getValue('datadirectory') . "/" . $this->path;
$this->realDir = $realDir ?? Helper::getLocalFolder($this->path);
}
public function getDefaultPath()
{
$settings = new Settings($this->user);
$rootFolder = Helper::getUserFolder($this->user);
$downloadDir = $settings->get('ncd_downloader_dir') ?? "/Downloads";
return $rootFolder . "/" . ltrim($downloadDir, '/\\');
return Helper::getUserFolder() . Helper::getDownloadDir();
}
public static function create($path = null, $user = null)
{
@@ -42,7 +39,7 @@ class folderScan
private function update()
{
if (!(self::folderUpdated($this->realDir))) {
if (!(Helper::folderUpdated($this->realDir))) {
return ['message' => "no change"];
}
$this->scan();
@@ -55,34 +52,15 @@ class folderScan
$this->scanner = new Scanner($this->user, \OC::$server->getDatabaseConnection(), \OC::$server->query(IEventDispatcher::class), $this->logger);
try {
$this->scanner->scan($this->path);
return ['status' => 'OK', 'path' => $this->path];
} catch (ForbiddenException $e) {
return ['status' => true, 'path' => $this->path];
} catch (\OCP\Files\ForbiddenException $e) {
$this->logger->warning("Make sure you're running the scan command only as the user the web server runs as");
} catch (\OCP\Files\NotFoundException $e) {
$this->logger->warning("Path for the scan command not found: " . $e->getMessage());
} catch (\Exception $e) {
$this->logger->warning("Exception during scan: " . $e->getMessage() . $e->getTraceAsString());
}
return ['status' => $e->getMessage(), 'path' => $this->path];
}
public static function folderUpdated($dir)
{
if (!file_exists($dir)) {
return false;
}
$checkFile = $dir . "/.lastmodified";
if (!file_exists($checkFile)) {
$time = \filemtime($dir);
file_put_contents($checkFile, $time);
return false;
}
$lastModified = (int) file_get_contents($checkFile);
$time = \filemtime($dir);
if ($time > $lastModified) {
file_put_contents($checkFile, $time);
return true;
}
return false;
return ['status' => false, 'path' => $this->path];
}
//update only folder is modified