diff --git a/appinfo/application.php b/appinfo/application.php index 03fe95a..219d641 100644 --- a/appinfo/application.php +++ b/appinfo/application.php @@ -2,33 +2,40 @@ namespace OCA\NCDownloader\AppInfo; -use OCA\NCDownloader\Controller\MainController; use OCA\NCDownloader\Controller\Aria2Controller; +use OCA\NCDownloader\Controller\MainController; use OCA\NCDownloader\Tools\Aria2; +use OCA\NCDownloader\Tools\Helper; use OCA\NCDownloader\Tools\Settings; +use OCA\NCDownloader\Tools\Youtube; use OCP\AppFramework\App; use OCP\IContainer; -use \OC\Files\Filesystem; class Application extends App { public function __construct(array $urlParams = array()) { parent::__construct('ncdownloader', $urlParams); + $user = \OC::$server->getUserSession()->getUser(); + $this->uid = ($user) ? $user->getUID() : ''; + $this->settings = new Settings($this->uid); + $this->dataDir = \OC::$server->getSystemConfig()->getValue('datadirectory'); + $this->userFolder = Helper::getUserFolder($this->uid); $container = $this->getContainer(); $container->registerService('UserId', function (IContainer $container) { - $user = \OC::$server->getUserSession()->getUser(); - return ($user) ? $user->getUID() : ''; + return $this->uid; }); $container->registerService('Aria2', function (IContainer $container) { - $uid = $container->query('UserId'); - return new Aria2($this->getConfig($uid)); + return new Aria2($this->getConfig()); + }); + + $container->registerService('Youtube', function (IContainer $container) { + return new Youtube(['downloadDir' => $this->getRealDownloadDir()]); }); $container->registerService('Settings', function (IContainer $container) { - $uid = $container->query('UserId'); - return new Settings($uid); + return new Settings($this->uid); }); $container->registerService('MainController', function (IContainer $container) { @@ -37,7 +44,8 @@ class Application extends App $container->query('Request'), $container->query('UserId'), \OC::$server->getL10N('ncdownloader'), - \OC::$server->getRootFolder(), + //\OC::$server->getRootFolder(), + $container->query("Youtube"), $container->query('Aria2') ); }); @@ -54,26 +62,28 @@ class Application extends App }); } - private function getConfig($uid) + private function getRealDownloadDir() + { + + //relative nextcloud user path + $dir = $this->settings->get('ncd_downloader_dir') ?? "/Downloads"; + return $this->dataDir . $this->userFolder . $dir; + } + + private function getConfig() { //$this->config = \OC::$server->getAppConfig(); - $this->settings = new Settings($uid); - $this->userFolder = Filesystem::getRoot(); - $this->dataDir = \OC::$server->getSystemConfig()->getValue('datadirectory'); - //relative nextcloud user path - $this->downloadDir = $this->settings->get('ncd_downloader_dir') ?? "/Downloads"; + $realDownloadDir = $this->getRealDownloadDir(); $this->torrentsDir = $this->settings->get('torrents_dir'); - //get the absolute path - $this->realDownloadDir = $this->dataDir . $this->userFolder . $this->downloadDir; $aria2_dir = $this->dataDir . "/aria2"; - $this->appPath = \OC::$server->getAppManager()->getAppPath('ncdownloader'); + //$this->appPath = \OC::$server->getAppManager()->getAppPath('ncdownloader'); $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(1)->get('ncd_rpctoken'); - $config = ['dir' => $this->realDownloadDir, 'conf_dir' => $aria2_dir, 'token' => $token, 'settings' => $settings]; + $config = ['dir' => $realDownloadDir, 'conf_dir' => $aria2_dir, 'token' => $token, 'settings' => $settings]; return $config; } diff --git a/lib/Controller/MainController.php b/lib/Controller/MainController.php index 8f724f5..e86a275 100644 --- a/lib/Controller/MainController.php +++ b/lib/Controller/MainController.php @@ -5,12 +5,13 @@ namespace OCA\NCDownloader\Controller; use OCA\NCDownloader\Search\torrentSearch; use OCA\NCDownloader\Tools\Aria2; use OCA\NCDownloader\Tools\DBConn; +use OCA\NCDownloader\Tools\File; use OCA\NCDownloader\Tools\Helper; -use OCA\NCDownloader\Tools\YouTube; +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\Files\IRootFolder; use OCP\IL10N; use OCP\IRequest; use OC_Util; @@ -25,18 +26,19 @@ class MainController extends Controller private $aria2Opts; private $l10n; - public function __construct($appName, IRequest $request, $UserId, IL10N $IL10N, IRootFolder $rootFolder, Aria2 $aria2) + public function __construct($appName, IRequest $request, $UserId, IL10N $IL10N, Youtube $youtube, Aria2 $aria2) { parent::__construct($appName, $request); $this->appName = $appName; $this->uid = $UserId; $this->l10n = $IL10N; - $this->rootFolder = $rootFolder; + $this->dataDir = \OC::$server->getSystemConfig()->getValue('datadirectory'); + //$this->rootFolder = $rootFolder; OC_Util::setupFS(); $this->urlGenerator = \OC::$server->getURLGenerator(); $this->aria2 = $aria2; $this->aria2->init(); - $this->youtube = new Youtube(); + $this->youtube = $youtube; $this->dbconn = new DBConn(); } @@ -86,17 +88,10 @@ class MainController extends Controller return new JSONResponse(['error' => $this->l10n->t("Youtube-dl NOT installed!")]); } - if (Helper::isGetUrlSite($inputValue)) { - if ($data = $yt->forceIPV4()->getDownloadUrl($inputValue)) { - $this->Save($data['url'], $data['filename']); - return new JSONResponse(['yt' => $data]); - } else { - return new JSONResponse(['error' => $this->l10n->t("failed to get any url!")]); - } - } else { - $yt->setDownloadDir($this->realDownloadDir); - return new JSONResponse(['yt' => $yt->download($inputValue)]); - } + $resp = $yt->forceIPV4()->download($inputValue); + File::syncFolder(); + return new JSONResponse(['yt' => $resp]); + } else if ($type === 'search') { $data = torrentSearch::go($inputValue); $resp['title'] = ['title', 'seeders', 'info', 'actions']; diff --git a/lib/Tools/YouTube.php b/lib/Tools/Youtube.php similarity index 87% rename from lib/Tools/YouTube.php rename to lib/Tools/Youtube.php index fefaa54..1a99670 100644 --- a/lib/Tools/YouTube.php +++ b/lib/Tools/Youtube.php @@ -5,15 +5,18 @@ use OCA\NCDownloader\Tools\Helper; use Symfony\Component\Process\Exception\ProcessFailedException; use Symfony\Component\Process\Process; -class YouTube +class Youtube { private $ipv4Only; private $audioOnly = 0; private $audioFormat, $videoFormat = 'mp4'; private $options = []; - public function __construct() + private $downloadDir; + public function __construct($config) { + $config += ['downloadDir' => '/tmp/downloads']; $this->bin = Helper::findBinaryPath('youtube-dl'); + $this->setDownloadDir($config['downloadDir']); } public function GetUrlOnly() @@ -33,6 +36,11 @@ class YouTube $this->downloadDir = rtrim($dir, '/'); } + public function getDownloadDir() + { + return $this->getDownloadDir; + } + public function prependOption($option) { array_unshift($this->options, $option); @@ -47,6 +55,8 @@ class YouTube $this->prependOption($this->bin); // $this->buildCMD(); $process = new Process($this->options); + //the maximum time required to download the file + $process->setTimeout(60*60*15); try { $process->mustRun(); $output = $process->getOutput(); @@ -101,7 +111,7 @@ class YouTube private function buildCMD() { - $this->cmd = $this->bin;//. " 2>&1"; + $this->cmd = $this->bin; //. " 2>&1"; foreach ($this->options as $option) { $this->cmd .= " " . $option;