added options for tracking youtube-dl downloads;bugs fixing

This commit is contained in:
huangjx
2021-09-14 22:58:36 +08:00
parent ced82caf0a
commit 2e834d4f9c
23 changed files with 649 additions and 182 deletions

View File

@@ -3,7 +3,7 @@ namespace OCA\NCDownloader\Controller;
use OCA\NCDownloader\Tools\Aria2;
use OCA\NCDownloader\Tools\DBConn;
use OCA\NCDownloader\Tools\File;
use OCA\NCDownloader\Tools\folderScan;
use OCA\NCDownloader\Tools\Helper;
use OCA\NCDownloader\Tools\Settings;
use OCP\AppFramework\Controller;
@@ -16,11 +16,10 @@ use \OC\Files\Filesystem;
class Aria2Controller extends Controller
{
private $userId;
private $uid;
private $settings = null;
//@config OC\AppConfig
private $config;
private $aria2Opts;
private $l10n;
public function __construct($appName, IRequest $request, $UserId, IL10N $IL10N, IRootFolder $rootFolder, Aria2 $aria2)
@@ -43,6 +42,7 @@ class Aria2Controller extends Controller
public function Action($path)
{
$path = strtolower(trim($path));
$resp = [];
if (!in_array($path, ['start', 'check']) && !($gid = $this->request->getParam('gid'))) {
return new JSONResponse(['error' => "no gid value is received!"]);
@@ -55,22 +55,43 @@ class Aria2Controller extends Controller
$resp = $this->Start();
break;
case "pause":
$resp = $this->aria2->pause($gid);
$resp = $this->doAction('pause', $gid);
break;
case "remove":
$resp = $this->aria2->remove($gid);
$resp = $this->doAction('remove', $gid);
break;
case "unpause":
$resp = $this->aria2->unpause($gid);
$resp = $this->doAction('unpause', $gid);
break;
case "get":
$resp = $this->aria2->tellStatus($gid);
$resp = $this->doAction('tellStatus', $gid);
break;
case 'purge':
$resp = $this->aria2->removeDownloadResult($gid);
$resp = $this->doAction('removeDownloadResult', $gid);
if (isset($resp['status']) && $resp['status']) {
$this->dbconn->deleteByGid($gid);
}
}
return new JSONResponse($resp);
}
private function doAction($action, $gid)
{
if (!$action || !$gid) {
return [];
}
$resp = $this->aria2->{$action}($gid);
if (in_array($action, ['removeDownloadResult', 'remove'])) {
if (isset($resp['result']) && strtolower($resp['result']) === 'ok') {
return ['message' => $this->l10n->t("DONE!"), 'status' => 1];
} else {
return ['error' => $this->l10n->t("FAILED!"), 'status' => 0];
}
}
return $resp;
}
private function Start()
{
if ($this->aria2->isRunning()) {
@@ -82,8 +103,8 @@ class Aria2Controller extends Controller
}
public function Update()
{
$resp = File::syncFolder();
//return new JSONResponse($resp);
$resp = folderScan::create()->scan();
return new JSONResponse($resp);
}
private function createActionItem($name, $path)
@@ -97,7 +118,7 @@ class Aria2Controller extends Controller
{
//$path = $this->request->getRequestUri();
$counter = $this->aria2->getCounters();
$this->Update();
folderScan::sync();
switch (strtolower($path)) {
case "active":
$resp = $this->aria2->tellActive();
@@ -179,11 +200,12 @@ class Aria2Controller extends Controller
$value['progress'] = array(sprintf("%s(%.2f%%)", $completed, $percentage), $extraInfo);
$timestamp = $timestamp ?? 0;
//$prefix = $value['files'][0]['path'];
$filename = sprintf('<a class="download-file-folder" href="%s">%s</a>', $folderLink, $filename);
$fileInfo = sprintf("%s | %s", $total, date("Y-m-d H:i:s", $timestamp));
$tmp = [];
$actions = [];
$filename = sprintf('<a class="download-file-folder" href="%s">%s</a>', $folderLink, $filename);
$fileInfo = sprintf("%s | %s", $total, date("Y-m-d H:i:s", $timestamp));
$tmp['filename'] = array($filename, $fileInfo);
if ($this->aria2->methodName === "tellStopped") {
$actions[] = $this->createActionItem('purge', 'purge');
} else {
@@ -192,7 +214,6 @@ class Aria2Controller extends Controller
if ($this->aria2->methodName === "tellWaiting") {
$actions[] = $this->createActionItem('unpause', 'unpause');
}
$tmp['filename'] = array($filename, $fileInfo);
if ($this->aria2->methodName === "tellActive") {
$speed = [Helper::formatBytes($value['downloadSpeed']), $left . " left"];
$tmp['speed'] = $speed;

View File

@@ -2,10 +2,8 @@
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 OCP\AppFramework\Controller;
@@ -40,8 +38,8 @@ class MainController extends Controller
$this->aria2->init();
$this->youtube = $youtube;
$this->dbconn = new DBConn();
$this->tablename = $this->dbconn->queryBuilder->getTableName("ncdownloader_info");
}
/**
* @NoAdminRequired
* @NoCSRFRequired
@@ -69,62 +67,39 @@ class MainController extends Controller
return $response;
}
public function newDownload()
public function Download()
{
$params = array();
$inputValue = trim($this->request->getParam('form_input_text'));
$type = trim($this->request->getParam('type'));
if ($type == 'ytdl') {
$yt = $this->youtube;
if (!$yt->isInstalled()) {
try {
$filename = Helper::getFileName($yt->installUrl());
$this->aria2->setDownloadDir($this->dataDir . "/bin");
$resp = $this->Save($yt->installUrl(), $filename);
return new JSONResponse($resp);
} catch (\Exception $e) {
return new JSONResponse(['error' => $e->getMessage()]);
}
return new JSONResponse(['error' => $this->l10n->t("Youtube-dl NOT installed!")]);
}
$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'];
$resp['row'] = $data;
return new JSONResponse($resp);
}
$filename = Helper::getFileName($inputValue);
$resp = $this->Save($inputValue, $filename);
$url = trim($this->request->getParam('form_input_text'));
//$type = trim($this->request->getParam('type'));
$resp = $this->_download($url);
return new JSONResponse($resp);
}
private function Save($url, $filename = null)
private function _download($url)
{
if (isset($filename)) {
$filename = Helper::getFileName($url);
if ($filename) {
$this->aria2->setFileName($filename);
}
//$this->aria2->setDownloadDir("/tmp/downloads");
$result = $this->aria2->addUri([$url]);
$gid = $result['result'];
if (!is_string($gid)) {
return ['error' => 'Failed to add download task! ' . $result['error']];
} else {
$data = [
'uid' => $this->uid,
'gid' => $gid,
'type' => 1,
'filename' => $filename ?? 'unknown',
'timestamp' => time(),
'data' => serialize(['link' => $url]),
];
$this->dbconn->save($data);
$result = $this->aria2->download($url);
if (!$result) {
return ['error' => 'failed to download the file for some reason!'];
}
return ['gid' => $gid, 'file' => $filename, 'result' => $gid];
if (isset($result['error'])) {
return $result;
}
$data = [
'uid' => $this->uid,
'gid' => $result,
'type' => Helper::DOWNLOADTYPE['ARIA2'],
'filename' => $filename ?? 'unknown',
'timestamp' => time(),
'data' => serialize(['link' => $url]),
];
$this->dbconn->save($data);
$resp = ['gid' => $result, 'file' => $filename, 'result' => $result];
return $resp;
}
}

View File

@@ -0,0 +1,33 @@
<?php
namespace OCA\NCDownloader\Controller;
use OCA\NCDownloader\Search\torrentSearch;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IRequest;
class SearchController extends Controller
{
private $userId;
private $settings = null;
//@config OC\AppConfig
private $l10n;
public function __construct($appName, IRequest $request, $UserId)
{
parent::__construct($appName, $request);
$this->appName = $appName;
$this->uid = $UserId;
$this->urlGenerator = \OC::$server->getURLGenerator();
}
public function execute()
{
$keyword = trim($this->request->getParam('form_input_text'));
$data = torrentSearch::go($keyword);
$resp['title'] = ['title', 'seeders', 'info', 'actions'];
$resp['row'] = $data;
return new JSONResponse($resp);
}
}

View File

@@ -0,0 +1,152 @@
<?php
namespace OCA\NCDownloader\Controller;
use OCA\NCDownloader\Tools\Aria2;
use OCA\NCDownloader\Tools\DBConn;
use OCA\NCDownloader\Tools\folderScan;
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\IL10N;
use OCP\IRequest;
class YoutubeController extends Controller
{
private $userId;
private $settings = null;
//@config OC\AppConfig
private $l10n;
public function __construct($appName, IRequest $request, $UserId, IL10N $IL10N, Aria2 $aria2, Youtube $youtube)
{
parent::__construct($appName, $request);
$this->appName = $appName;
$this->uid = $UserId;
$this->urlGenerator = \OC::$server->getURLGenerator();
$this->l10n = $IL10N;
$this->settings = new Settings($UserId);
$this->downloadDir = $this->settings->get('ncd_downloader_dir') ?? "/Downloads";
$this->dbconn = new DBConn();
$this->youtube = $youtube;
$this->aria2 = $aria2;
$this->aria2->init();
$this->tablename = $this->dbconn->queryBuilder->getTableName("ncdownloader_info");
}
public function Index()
{
$data = $this->dbconn->getYoutubeByUid($this->uid);
if (is_array($data) && count($data) < 1) {
return [];
}
$resp['title'] = [];
$resp['row'] = [];
$params = ['dir' => $this->downloadDir];
$folderLink = $this->urlGenerator->linkToRoute('files.view.index', $params);
foreach ($data as $value) {
$tmp = [];
$filename = sprintf('<a class="download-file-folder" href="%s">%s</a>', $folderLink, $value['filename']);
$fileInfo = sprintf("%s | %s", $value['filesize'], date("Y-m-d H:i:s", $value['timestamp']));
$tmp['filename'] = array($filename, $fileInfo);
$tmp['speed'] = $value['speed'];
$tmp['progress'] = $value['progress'];
if ((int) $value['status'] == Helper::STATUS['COMPLETE']) {
$path = $this->urlGenerator->linkToRoute('ncdownloader.Youtube.Delete');
$tmp['actions'][] = ['name' => 'delete', 'path' => $path];
} else {
$tmp['actions'][] = ['name' => 'disabled', 'path' => '#'];
}
$tmp['data_gid'] = $value['gid'] ?? 0;
array_push($resp['row'], $tmp);
}
$resp['title'] = ['filename', 'speed', 'progress', 'actions'];
$resp['counter'] = ['youtube-dl' => count($data)];
return new JSONResponse($resp);
}
public function Download()
{
$params = array();
$url = trim($this->request->getParam('form_input_text'));
$yt = $this->youtube;
if (!$yt->isInstalled()) {
return new JSONResponse($this->installYTD());
}
if (Helper::isGetUrlSite($url)) {
return new JSONResponse($this->downloadUrlSite($url));
}
$resp = $yt->forceIPV4()->download($url);
folderScan::sync();
return new JSONResponse(['data' => $resp]);
}
private function downloadUrlSite($url)
{
$yt = $this->youtube;
if ($data = $yt->forceIPV4()->getDownloadUrl($url)) {
return $this->_download($data['url'], $data['filename']);
} else {
return ['error' => $this->l10n->t("failed to get any url!")];
}
}
public function Delete()
{
$gid = $this->request->getParam('gid');
if (!$gid) {
return new JSONResponse(['error' => "no gid value is received!"]);
}
if ($this->dbconn->deleteByGid($gid)) {
return new JSONResponse(['message' => $gid . " deleted!"]);
}
}
private function _download($url, $filename = null)
{
if (!$filename) {
$filename = Helper::getFileName($url);
}
$this->aria2->setFileName($filename);
$result = $this->aria2->download($url);
if (!$result) {
return ['error' => 'failed to download the file for some reason!'];
}
if (isset($result['error'])) {
return $result;
}
$data = [
'uid' => $this->uid,
'gid' => $result,
'type' => 1,
'filename' => $filename ?? 'unknown',
'timestamp' => time(),
'data' => serialize(['link' => $url]),
];
$this->dbconn->save($data);
$resp = ['gid' => $result, 'file' => $filename, 'result' => $result];
return $resp;
}
private function installYTD()
{
try {
$filename = Helper::getFileName($yt->installUrl());
$yt->setDownloadDir($this->dataDir . "/bin");
$resp = $this->Save($yt->installUrl(), $filename);
return $resp;
} catch (\Exception $e) {
return ['error' => $e->getMessage()];
}
return ['error' => $this->l10n->t("Youtube-dl NOT installed!")];
}
}