From c8df2b9821059912253eca35a0822a02addd390b Mon Sep 17 00:00:00 2001 From: huangjx Date: Sat, 5 Mar 2022 08:10:22 +0800 Subject: [PATCH 1/5] fixed #35;keep space chars in filename instead of replacing it with hyphens --- lib/Tools/Helper.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/Tools/Helper.php b/lib/Tools/Helper.php index b81a43c..1f452bd 100644 --- a/lib/Tools/Helper.php +++ b/lib/Tools/Helper.php @@ -127,7 +127,7 @@ class Helper '/–/' => '-', // UTF-8 hyphen to "normal" hyphen '/[’‘‹›‚]/u' => '', // Literally a single quote '/[“”«»„]/u' => '', // Double quote - '/ /' => '_', // nonbreaking space(equiv. to 0x160) + //'/ /' => '_', // nonbreaking space(equiv. to 0x160) // '/[^a-z0-9_\s.-]/i' => '_', ); return preg_replace(array_keys($replace), array_values($replace), $string); @@ -327,9 +327,9 @@ class Helper return self::doSignal($pid, 9); } - public static function pythonInstalled() + public static function pythonInstalled(): bool { - return (bool) self::findBinaryPath('python'); + return (self::findBinaryPath('python') || self::findBinaryPath('python3')); } public static function findSearchSites($dir, $suffix = 'php'): array From c7fd0f32afaf479f6d8d52bf84205bc68cf5eab9 Mon Sep 17 00:00:00 2001 From: huangjx Date: Sat, 5 Mar 2022 09:58:05 +0800 Subject: [PATCH 2/5] used a faster version of youtube-dl(#36);fixed yt binary download issuse;cleaning up --- lib/Tools/Helper.php | 11 ++++++++++- lib/Tools/Youtube.php | 41 +++++++++++++---------------------------- 2 files changed, 23 insertions(+), 29 deletions(-) diff --git a/lib/Tools/Helper.php b/lib/Tools/Helper.php index 1f452bd..fd6641a 100644 --- a/lib/Tools/Helper.php +++ b/lib/Tools/Helper.php @@ -174,8 +174,17 @@ class Helper curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); $result = curl_exec($ch); + if (curl_errno($ch)) { + $error = 'Error:' . curl_error($ch); + } curl_close($ch); - file_put_contents($file, $result); + if (isset($error)) { + throw new \Exception($error); + } else { + if (!file_put_contents($file, $result)) { + throw new \Exception("failed to save " . $file); + } + } } public static function is_function_enabled($function_name) diff --git a/lib/Tools/Youtube.php b/lib/Tools/Youtube.php index f369be5..84eff0a 100644 --- a/lib/Tools/Youtube.php +++ b/lib/Tools/Youtube.php @@ -3,7 +3,6 @@ namespace OCA\NCDownloader\Tools; use OCA\NCDownloader\Tools\Helper; use OCA\NCDownloader\Tools\YoutubeHelper; -use Symfony\Component\Process\Exception\ProcessFailedException; use Symfony\Component\Process\Process; class Youtube @@ -31,7 +30,7 @@ class Youtube if (isset($binary) && $this->isExecutable($binary)) { $this->bin = $binary; } else { - $this->bin = Helper::findBinaryPath('youtube-dl', __DIR__ . "/../../bin/youtube-dl"); + $this->bin = Helper::findBinaryPath('youtube-dl', __DIR__ . "/../../bin/yt-dlp"); } if ($this->isInstalled() && !$this->isExecutable()) { chmod($this->bin, 0744); @@ -90,7 +89,7 @@ class Youtube $this->setOption('--audio-format', $format); } - public function setvideoFormat($format) + public function setVideoFormat($format) { $this->videoFormat = $format; } @@ -122,29 +121,9 @@ class Youtube array_unshift($this->options, $option); } - public function downloadSync($url) - { - $this->downloadDir = $this->downloadDir ?? $this->defaultDir; - $this->prependOption($this->downloadDir . $this->outTpl); - $this->prependOption("--output"); - $this->setUrl($url); - $this->prependOption($this->bin); - // $this->buildCMD(); - $process = new Process($this->options, null, $this->env); - //the maximum time required to download the file - $process->setTimeout($this->timeout); - try { - $process->mustRun(); - $output = $process->getOutput(); - } catch (ProcessFailedException $exception) { - $output = $exception->getMessage(); - } - - return $output; - } - public function download($url) { + $this->install(); if ($this->audioOnly) { $this->audioMode(); } else { @@ -255,16 +234,22 @@ class Youtube { return $this->bin; } - public static function install() + public function install() { $url = $this->installUrl(); - $path = \OC::$server->getSystemConfig()->getValue('datadirectory'); - Helper::Download($url, $path . "/youtube-dl"); + $file = __DIR__ . "/../../bin/yt-dlp2"; + try { + Helper::Download($url, $file); + chmod($file, 0744); + } catch (\Exception $e) { + return $e->getMessage(); + } + return false; } public function installUrl() { - return "https://yt-dl.org/downloads/latest/youtube-dl"; + return "https://github.com/shiningw/ncdownloader-bin/raw/master/yt-dlp"; } } From 67ab4a1f9f22206ff8861fdae5e05861834127d9 Mon Sep 17 00:00:00 2001 From: huangjx Date: Sat, 5 Mar 2022 14:31:06 +0800 Subject: [PATCH 3/5] fixed bugs --- lib/Search/Sites/sliderkz.php | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/Search/Sites/sliderkz.php b/lib/Search/Sites/sliderkz.php index d4c5c96..1c10dc5 100644 --- a/lib/Search/Sites/sliderkz.php +++ b/lib/Search/Sites/sliderkz.php @@ -11,11 +11,11 @@ class sliderkz extends searchBase implements searchInterface protected $query = null; protected $tableTitles = []; - public function __construct($crawler,$client) + public function __construct($crawler, $client) { $this->client = $client; } - + public function search(string $keyword): array { $this->query = ['q' => trim($keyword)]; @@ -61,12 +61,11 @@ class sliderkz extends searchBase implements searchInterface try { $response = $this->client->request('GET', $this->searchUrl, ['query' => $this->query]); $resp = $response->toArray(); + if (isset($resp['audios'])) { + return array_values($resp["audios"])[0]; + } } catch (ExceptionInterface $e) { $this->errors[] = $e->getMessage(); - return []; - } - if (isset($resp['audios'])) { - return array_values($resp["audios"])[0]; } return []; From 802c593905d523ac6f1969b46d0ba7d563805f5c Mon Sep 17 00:00:00 2001 From: huangjx Date: Sat, 5 Mar 2022 17:09:04 +0800 Subject: [PATCH 4/5] added option for disabling bt protocol for non-admin uses close #3; --- lib/Controller/MainController.php | 23 ++++++++++++++++++----- src/components/toggleButton.vue | 2 +- src/settingsBar.vue | 30 ++++++++++++++++++++++-------- templates/Navigation.php | 2 ++ 4 files changed, 43 insertions(+), 14 deletions(-) diff --git a/lib/Controller/MainController.php b/lib/Controller/MainController.php index 3e3e89d..fbaacb6 100644 --- a/lib/Controller/MainController.php +++ b/lib/Controller/MainController.php @@ -39,8 +39,11 @@ class MainController extends Controller $this->dbconn = new DbHelper(); $this->counters = new Counters($aria2, $this->dbconn, $UserId); $this->youtube = $youtube; - $this->settings = new Settings($UserId); - + $this->settings = new Settings($this->uid); + $this->isAdmin = \OC_User::isAdminUser($this->uid); + $this->hideError = $this->settings->get("ncd_hide_errors", false); + $this->disable_bt_nonadmin = $this->settings->setType($this->settings::TYPE['SYSTEM'])->get("ncd_disable_bt", false); + $this->accessDenied = $this->l10n->t("Sorry,only admin users can download files via BT!"); } /** * @NoAdminRequired @@ -68,10 +71,11 @@ class MainController extends Controller $params['youtube_installed'] = $youtube_installed = $this->youtube->isInstalled(); $params['youtube_bin'] = $youtube_bin = $this->youtube->getBin(); $params['youtube_executable'] = $youtube_executable = $this->youtube->isExecutable(); - $params['ncd_hide_errors'] = $this->settings->get("ncd_hide_errors", false); + $params['ncd_hide_errors'] = $this->hideError; $params['counter'] = $this->counters->getCounters(); $params['python_installed'] = Helper::pythonInstalled(); $params['ffmpeg_installed'] = Helper::ffmpegInstalled(); + $params['is_admin'] = $this->isAdmin; $sites = []; foreach (Helper::getSearchSites() as $site) { $label = $site['class']::getLabel(); @@ -107,10 +111,11 @@ class MainController extends Controller $params['errors'] = $errors; $params['settings'] = json_encode([ - 'is_admin' => \OC_User::isAdminUser($this->uid), + 'is_admin' => $this->isAdmin, 'admin_url' => $this->urlGenerator->linkToRoute("settings.AdminSettings.index", ['section' => 'ncdownloader']), 'personal_url' => $this->urlGenerator->linkToRoute("settings.PersonalSettings.index", ['section' => 'ncdownloader']), - 'ncd_hide_errors' => $params['ncd_hide_errors'], + 'ncd_hide_errors' => $this->hideError, + 'ncd_disable_bt' => $this->disable_bt_nonadmin, ]); return $params; } @@ -121,6 +126,11 @@ class MainController extends Controller public function Download() { $url = trim($this->request->getParam('text-input-value')); + if (Helper::isMagnet($url)) { + if ($this->disable_bt_nonadmin && !($this->isAdmin)) { + return new JSONResponse(['error' => $this->accessDenied]); + } + } //$type = trim($this->request->getParam('type')); $resp = $this->_download($url); return new JSONResponse($resp); @@ -156,6 +166,9 @@ class MainController extends Controller */ public function Upload() { + if ($this->disable_bt_nonadmin && !$this->isAdmin) { + return new JSONResponse(['error' => $this->l10n->t($this->accessDenied)]); + } if (is_uploaded_file($file = $_FILES['torrentfile']['tmp_name'])) { $file = $this->aria2->getTorrentsDir() . '/' . Helper::cleanString($_FILES['torrentfile']['name']); diff --git a/src/components/toggleButton.vue b/src/components/toggleButton.vue index 55c4400..a42bd27 100644 --- a/src/components/toggleButton.vue +++ b/src/components/toggleButton.vue @@ -70,7 +70,7 @@ export default { set(value) { this.status = value; - this.$emit("changed", value); + this.$emit("changed", this.name,value); }, }, }, diff --git a/src/settingsBar.vue b/src/settingsBar.vue index 520c554..9dde049 100644 --- a/src/settingsBar.vue +++ b/src/settingsBar.vue @@ -1,11 +1,22 @@