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

@@ -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;
}
}