From 8e5ebdd8e79e2b995869df9d2434e05a4e7b673c Mon Sep 17 00:00:00 2001 From: huangjx Date: Mon, 21 Feb 2022 15:50:42 +0800 Subject: [PATCH] improved user experience for novice user(including support for standalone aria2c and ytdl binaries);support for snap installation;close #19 --- .gitignore | 1 + appinfo/application.php | 3 ++- lib/Controller/MainController.php | 15 +++++++++++++-- lib/Controller/SettingsController.php | 1 - lib/Tools/Aria2.php | 18 ++++++++++++++---- lib/Tools/Helper.php | 1 + lib/Tools/Youtube.php | 16 +++++++++++++--- src/index.js | 13 ++++++++++++- src/utils/helper.js | 6 ++++++ templates/Navigation.php | 25 +++++++++++++++++++------ templates/settings/Settings.php | 7 +++++++ 11 files changed, 88 insertions(+), 18 deletions(-) diff --git a/.gitignore b/.gitignore index b405bfa..61e1a8f 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ build.sh test.php js/app* exclude +bin diff --git a/appinfo/application.php b/appinfo/application.php index 943bed9..0e0f2b6 100644 --- a/appinfo/application.php +++ b/appinfo/application.php @@ -56,7 +56,8 @@ class Application extends App $container->query('UserId'), \OC::$server->getL10N('ncdownloader'), //\OC::$server->getRootFolder(), - $container->query('Aria2') + $container->query('Aria2'), + $container->query('Youtube') ); }); diff --git a/lib/Controller/MainController.php b/lib/Controller/MainController.php index f4b6af5..90e4c41 100644 --- a/lib/Controller/MainController.php +++ b/lib/Controller/MainController.php @@ -6,6 +6,8 @@ use OCA\NCDownloader\Tools\Aria2; use OCA\NCDownloader\Tools\Counters; use OCA\NCDownloader\Tools\DbHelper; use OCA\NCDownloader\Tools\Helper; +use OCA\NCDownloader\Tools\Settings; +use OCA\NCDownloader\Tools\Youtube; use OCP\AppFramework\Controller; use OCP\AppFramework\Http\JSONResponse; use OCP\AppFramework\Http\TemplateResponse; @@ -23,7 +25,7 @@ class MainController extends Controller private $aria2Opts; private $l10n; - public function __construct($appName, IRequest $request, $UserId, IL10N $IL10N, Aria2 $aria2) + public function __construct($appName, IRequest $request, $UserId, IL10N $IL10N, Aria2 $aria2, Youtube $youtube) { parent::__construct($appName, $request); $this->appName = $appName; @@ -36,6 +38,9 @@ class MainController extends Controller $this->urlGenerator = \OC::$server->getURLGenerator(); $this->dbconn = new DbHelper(); $this->counters = new Counters($aria2, $this->dbconn, $UserId); + $this->youtube = $youtube; + $this->settings = new Settings($UserId); + } /** * @NoAdminRequired @@ -50,7 +55,13 @@ class MainController extends Controller $params = array(); $params['aria2_running'] = $this->aria2->isRunning(); $params['aria2_installed'] = $this->aria2->isInstalled(); - $params['youtube_installed'] = (bool) Helper::findBinaryPath('youtube-dl'); + $params['aria2_bin'] = $this->aria2->getBin(); + $params['aria2_executable'] = $this->aria2->isExecutable(); + $params['youtube_installed'] = $this->youtube->isInstalled(); + $params['youtube_bin'] = $this->youtube->getBin(); + $params['youtube_executable'] = $this->youtube->isExecutable(); + $params['ncd_hide_errors'] = $this->settings->get("ncd_hide_errors"); + $params['counter'] = $this->counters->getCounters(); $params['settings_url'] = $this->urlGenerator->linkToRoute("settings.PersonalSettings.index", ['section' => 'ncdownloader']); $params['admin_settings_url'] = $this->urlGenerator->linkToRoute("settings.AdminSettings.index", ['section' => 'ncdownloader']); diff --git a/lib/Controller/SettingsController.php b/lib/Controller/SettingsController.php index a24d18f..91f4a89 100644 --- a/lib/Controller/SettingsController.php +++ b/lib/Controller/SettingsController.php @@ -132,5 +132,4 @@ class SettingsController extends Controller } return ['message' => "Saved!"]; } - } diff --git a/lib/Tools/Aria2.php b/lib/Tools/Aria2.php index 08a21f6..1218c70 100644 --- a/lib/Tools/Aria2.php +++ b/lib/Tools/Aria2.php @@ -38,10 +38,10 @@ class Aria2 ); //turn keys in $options into variables extract($options); - if (isset($binary) && @is_executable($binary)) { + if (isset($binary) && $this->isExecutable($binary)) { $this->bin = $binary; } else { - $this->bin = Helper::findBinaryPath('aria2c'); + $this->bin = Helper::findBinaryPath('aria2c', __DIR__ . "/../../bin/aria2c"); } $this->setDownloadDir($dir); $this->setTorrentsDir($torrents_dir); @@ -379,17 +379,27 @@ class Aria2 } public function isInstalled() { - return (bool) (isset($this->bin) && @is_executable($this->bin)); + return @is_file($this->bin); } + public function isExecutable() + { + return @is_executable($this->bin); + } + public function isRunning() { $resp = $this->getSessionInfo(); return (bool) $resp; } + + public function getBin() + { + return $this->bin; + } public function stop() { $resp = $this->shutdown(); - sleep(1); + sleep(3); return $resp ?? null; } private function confTemplate() diff --git a/lib/Tools/Helper.php b/lib/Tools/Helper.php index 0c48dfc..e992e63 100644 --- a/lib/Tools/Helper.php +++ b/lib/Tools/Helper.php @@ -202,6 +202,7 @@ class Helper if ($memcache->hasKey($program)) { return $memcache->get($program); } + $dataPath = \OC::$server->getSystemConfig()->getValue('datadirectory'); $paths = ['/usr/local/sbin', '/usr/local/bin', '/usr/sbin', '/usr/bin', '/sbin', '/bin', '/opt/bin', $dataPath . "/bin"]; $result = $default; diff --git a/lib/Tools/Youtube.php b/lib/Tools/Youtube.php index 40698a5..eb517c1 100644 --- a/lib/Tools/Youtube.php +++ b/lib/Tools/Youtube.php @@ -18,6 +18,7 @@ class Youtube private $outTpl = "%(id)s-%(title).64s.%(ext)s"; private $defaultDir = "/tmp/downloads"; private $env = []; + private $bin; public function __construct(array $options) { @@ -27,7 +28,7 @@ class Youtube public function init(array $options) { extract($options); - if (isset($binary) && @is_executable($binary)) { + if (isset($binary) && $this->isExecutable($binary)) { $this->bin = $binary; } else { $this->bin = Helper::findBinaryPath('youtube-dl', __DIR__ . "/../../bin/youtube-dl"); @@ -157,7 +158,7 @@ class Youtube $data = ['link' => $url]; $process->run(function ($type, $buffer) use ($data, $process) { if (Process::ERR === $type) { - // $this->onError($buffer); + // $this->onError($buffer); } else { $data['pid'] = $process->getPid(); $this->onOutput($buffer, $data); @@ -235,7 +236,16 @@ class Youtube } public function isInstalled() { - return (bool) (isset($this->bin) && @is_executable($this->bin)); + return @is_file($this->bin); + } + public function isExecutable() + { + return @is_executable($this->bin); + } + + public function getBin() + { + return $this->bin; } public static function install() { diff --git a/src/index.js b/src/index.js index 91a6030..664c177 100644 --- a/src/index.js +++ b/src/index.js @@ -16,6 +16,7 @@ const basePath = "/apps/ncdownloader"; window.addEventListener('DOMContentLoaded', function () { + helper.showErrors('[data-error-message]'); // inputAction.run(); updatePage.run(); buttonActions.run(); @@ -60,10 +61,20 @@ window.addEventListener('DOMContentLoaded', function () { }).send(); }) eventHandler.add("click", "#app-navigation", "#search-download", helper.showDownload); - delegate('#ncdownloader-table-wrapper', + eventHandler.add("change", "#app-navigation", "#ncd-hide-errors", e => { + let data = {}; + data["ncd_hide_errors"] = e.target.checked === "true"; + console.log(data) + const url = helper.generateUrl(basePath + "/personal/save"); + Http.getInstance(url).setData(data).setHandler(data => { + console.log(data); + }).send(); + }) + delegate('#app-ncdownloader-wrapper', { target: '[data-tippy-content]' } ); + }); diff --git a/src/utils/helper.js b/src/utils/helper.js index cc67889..553264b 100644 --- a/src/utils/helper.js +++ b/src/utils/helper.js @@ -178,6 +178,12 @@ const helper = { }, getScrollTop() { return window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0; + }, + showErrors(target) { + let errors = document.querySelectorAll(target); + errors.forEach(element => { + helper.message(element.getAttribute('data-error-message'), 20000); + }) } } diff --git a/templates/Navigation.php b/templates/Navigation.php index 29bf405..f0e1c91 100644 --- a/templates/Navigation.php +++ b/templates/Navigation.php @@ -1,25 +1,38 @@
+ + +
+ + +
- + + +
diff --git a/templates/settings/Settings.php b/templates/settings/Settings.php index 3a24141..3a94028 100644 --- a/templates/settings/Settings.php +++ b/templates/settings/Settings.php @@ -1,5 +1,9 @@
@@ -11,6 +15,9 @@ extract($_);