From 73d01e558f2845af63a9f4c8f17514a46c8d7025 Mon Sep 17 00:00:00 2001 From: huangjx Date: Fri, 22 Apr 2022 16:47:31 +0800 Subject: [PATCH] fixed #47;some cleaning up and bugfixes; --- appinfo/application.php | 50 +----------- lib/Controller/MainController.php | 8 +- lib/Controller/YoutubeController.php | 13 +-- lib/Search/Sites/TPB.php | 1 - lib/Search/Sites/bitSearch.php | 1 - lib/Tools/Helper.php | 118 ++++++++++++++++++++++++++- lib/Tools/Youtube.php | 2 +- lib/Tools/folderScan.php | 31 ++----- 8 files changed, 138 insertions(+), 86 deletions(-) diff --git a/appinfo/application.php b/appinfo/application.php index 40694a2..6cd7107 100644 --- a/appinfo/application.php +++ b/appinfo/application.php @@ -19,11 +19,9 @@ class Application extends App public function __construct(array $urlParams = array()) { parent::__construct('ncdownloader', $urlParams); - $user = \OC::$server->getUserSession()->getUser(); + $user = Helper::getUser(); $this->uid = ($user) ? $user->getUID() : ''; $this->settings = new Settings($this->uid); - $this->dataDir = \OC::$server->getSystemConfig()->getValue('datadirectory'); - $this->appPath = \OC::$server->getAppManager()->getAppPath('ncdownloader'); $this->userFolder = Helper::getUserFolder($this->uid); $container = $this->getContainer(); $container->registerService('UserId', function (IContainer $container) { @@ -31,15 +29,11 @@ class Application extends App }); $container->registerService('Aria2', function (IContainer $container) { - return new Aria2($this->getConfig()); + return new Aria2(Helper::getAria2Config($this->uid)); }); $container->registerService('Youtube', function (IContainer $container) { - $config = [ - 'binary' => $this->settings->setType(Settings::TYPE['SYSTEM'])->get("ncd_yt_binary"), - 'downloadDir' => $this->getRealDownloadDir(), - 'settings' => $this->settings->setType(Settings::TYPE['USER'])->getYoutube(), - ]; + $config = Helper::getYoutubeConfig($this->uid); return new Youtube($config); }); @@ -100,42 +94,4 @@ class Application extends App } } - private function getRealDownloadDir() - { - - //relative nextcloud user path - $dir = $this->settings->get('ncd_downloader_dir') ?? "/Downloads"; - return $this->dataDir . $this->userFolder . $dir; - } - private function getRealTorrentsDir() - { - $dir = $this->settings->get('ncd_torrents_dir') ?? "/Torrents"; - return $this->dataDir . $this->userFolder . $dir; - } - - private function getConfig() - { - //$this->config = \OC::$server->getAppConfig(); - $realDownloadDir = $this->getRealDownloadDir(); - $torrentsDir = $this->getRealTorrentsDir(); - $aria2_dir = $this->dataDir . "/aria2"; - $settings['seed_time'] = $this->settings->get("ncd_seed_time"); - $settings['seed_ratio'] = $this->settings->get("ncd_seed_ratio"); - if (is_array($customSettings = $this->settings->getAria2())) { - $settings = array_merge($customSettings, $settings); - } - $token = $this->settings->setType(Settings::TYPE['SYSTEM'])->get('ncd_rpctoken'); - $config = [ - 'dir' => $realDownloadDir, - 'torrents_dir' => $torrentsDir, - 'conf_dir' => $aria2_dir, - 'token' => $token, - 'settings' => $settings, - 'binary' => $this->settings->setType(Settings::TYPE['SYSTEM'])->get('ncd_aria2_binary'), - 'startHook' => $this->appPath . "/hooks/startHook.sh", - 'completeHook' => $this->appPath . "/hooks/completeHook.sh", - ]; - return $config; - } - } diff --git a/lib/Controller/MainController.php b/lib/Controller/MainController.php index d17331e..b0463ed 100644 --- a/lib/Controller/MainController.php +++ b/lib/Controller/MainController.php @@ -11,11 +11,10 @@ use OCA\NCDownloader\Tools\Youtube; use OCP\AppFramework\Controller; use OCP\AppFramework\Http\JSONResponse; use OCP\AppFramework\Http\TemplateResponse; -//use OCP\Files\IRootFolder; use OCP\IL10N; +//use OCP\Files\IRootFolder; use OCP\IRequest; use OC_Util; -use OC\Files\Filesystem; class MainController extends Controller { @@ -57,7 +56,6 @@ class MainController extends Controller // OC_Util::addStyle($this->appName, 'table'); $params = $this->buildParams(); $response = new TemplateResponse($this->appName, 'Index', $params); - return $response; } @@ -125,6 +123,10 @@ class MainController extends Controller */ public function Download() { + $dlDir = $this->aria2->getDownloadDir(); + if (!is_writable($dlDir)) { + return new JSONResponse(['error' => sprintf("%s is not writable", $dlDir)]); + } $url = trim($this->request->getParam('text-input-value')); if (Helper::isMagnet($url)) { if ($this->disable_bt_nonadmin && !($this->isAdmin)) { diff --git a/lib/Controller/YoutubeController.php b/lib/Controller/YoutubeController.php index 549041a..3df4d8c 100644 --- a/lib/Controller/YoutubeController.php +++ b/lib/Controller/YoutubeController.php @@ -76,7 +76,10 @@ class YoutubeController extends Controller */ public function Download() { - $params = array(); + $dlDir = $this->youtube->getDownloadDir(); + if (!is_writable($dlDir)) { + return new JSONResponse(['error' => sprintf("%s is not writable", $dlDir)]); + } $url = trim($this->request->getParam('text-input-value')); $yt = $this->youtube; $yt->audioOnly = (bool) $this->request->getParam('audio-only'); @@ -117,7 +120,7 @@ class YoutubeController extends Controller } $row = $this->dbconn->getByGid($gid); - $data = $this->dbconn->getExtra($value["data"]);; + $data = $this->dbconn->getExtra($row["data"]); if (!isset($data['pid'])) { if ($this->dbconn->deleteByGid($gid)) { $msg = sprintf("%s is deleted from database!", $gid); @@ -193,9 +196,9 @@ class YoutubeController extends Controller private function installYTD() { try { - $filename = Helper::getFileName($yt->installUrl()); - $yt->setDownloadDir($this->dataDir . "/bin"); - $resp = $this->Save($yt->installUrl(), $filename); + $filename = Helper::getFileName($this->installUrl()); + $this->setDownloadDir($this->dataDir . "/bin"); + $resp = $this->Save($this->installUrl(), $filename); return $resp; } catch (\Exception $e) { return ['error' => $e->getMessage()]; diff --git a/lib/Search/Sites/TPB.php b/lib/Search/Sites/TPB.php index d17b1bc..15b265b 100644 --- a/lib/Search/Sites/TPB.php +++ b/lib/Search/Sites/TPB.php @@ -33,7 +33,6 @@ class TPB extends searchBase implements searchInterface if ($this->content) { return $this->content; } - $content; try { $response = $this->client->request('GET', $this->searchUrl); $content = $response->getContent(); diff --git a/lib/Search/Sites/bitSearch.php b/lib/Search/Sites/bitSearch.php index a275f32..8e18002 100644 --- a/lib/Search/Sites/bitSearch.php +++ b/lib/Search/Sites/bitSearch.php @@ -37,7 +37,6 @@ class bitSearch extends searchBase implements searchInterface if ($this->content) { return $this->content; } - $content; try { $response = $this->client->request('GET', $this->searchUrl, ['query' => $this->query]); $content = $response->getContent(); diff --git a/lib/Tools/Helper.php b/lib/Tools/Helper.php index eec2b2f..225d688 100644 --- a/lib/Tools/Helper.php +++ b/lib/Tools/Helper.php @@ -2,9 +2,13 @@ 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 { @@ -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,110 @@ 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 + { + //without calling this, filesystem::getLocalFolder doesn't work + OC_Util::setupFS(); + return Filesystem::getLocalFolder($path); + } + + public static function getRealDownloadDir($uid = null): string + { + $uid = $uid ?? self::getUID(); + $settings = new Settings($uid); + $dlDir = $settings->get('ncd_downloader_dir') ?? "/Downloads"; + return self::getLocalFolder($dlDir); + } + public static function getRealTorrentsDir($uid = null): string + { + $uid = $uid ?? self::getUID(); + $settings = new Settings($uid); + $dir = $settings->get('ncd_torrents_dir') ?? "/Torrents"; + return self::getLocalFolder($dir); + } + + public static function getUser(): ?IUser + { + return \OC::$server->getUserSession()->getUser(); + } + + public static function getUID(): string + { + return self::getUser()->getUID(); + } + + public static function getYoutubeConfig($uid = null): array + { + $uid = $uid ?? self::getUID(); + $settings = new Settings($uid); + $config = [ + 'binary' => $settings->setType(Settings::TYPE['SYSTEM'])->get("ncd_yt_binary"), + 'downloadDir' => Helper::getRealDownloadDir(), + 'settings' => $settings->setType(Settings::TYPE['USER'])->getYoutube(), + ]; + return $config; + } + + public static function getAria2Config($uid = null): array + { + $options = []; + $uid = $uid ?? self::getUID(); + $settings = new Settings($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; + } } diff --git a/lib/Tools/Youtube.php b/lib/Tools/Youtube.php index 0573dfd..36b5c0a 100644 --- a/lib/Tools/Youtube.php +++ b/lib/Tools/Youtube.php @@ -116,7 +116,7 @@ class Youtube public function getDownloadDir() { - return $this->getDownloadDir; + return $this->downloadDir; } public function prependOption(string $option) diff --git a/lib/Tools/folderScan.php b/lib/Tools/folderScan.php index b347855..24096d6 100644 --- a/lib/Tools/folderScan.php +++ b/lib/Tools/folderScan.php @@ -10,19 +10,18 @@ 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 $settings->get('ncd_downloader_dir') ?? "/Downloads"; } public static function create($path = null, $user = null) { @@ -42,7 +41,7 @@ class folderScan private function update() { - if (!(self::folderUpdated($this->realDir))) { + if (!(Helper::folderUpdated($this->realDir))) { return ['message' => "no change"]; } $this->scan(); @@ -65,25 +64,7 @@ class folderScan 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; - } + //update only folder is modified public static function sync($path = null, $user = null)