added basic api for 3rd-party clients
This commit is contained in:
57
lib/Controller/ApiController.php
Normal file
57
lib/Controller/ApiController.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace OCA\NCDownloader\Controller;
|
||||
|
||||
use \OCP\AppFramework\ApiController as API;
|
||||
use \OCP\IRequest;
|
||||
use \OCP\AppFramework\Http\JSONResponse;
|
||||
use \OCP\IL10N;
|
||||
use OCA\NCDownloader\Controller\MainController as Main;
|
||||
use OCA\NCDownloader\Controller\YtdlController as YTD;
|
||||
use OCA\NCDownloader\Controller\SearchController as Search;
|
||||
|
||||
class ApiController extends API
|
||||
{
|
||||
|
||||
private $IL10N;
|
||||
private $ytdl;
|
||||
private $main;
|
||||
private $search;
|
||||
|
||||
public function __construct($appName, IRequest $request, IL10N $IL10N, YTD $ytdl, Main $main, Search $search)
|
||||
{
|
||||
$this->IL10N = $IL10N;
|
||||
$this->main = $main;
|
||||
$this->search = $search;
|
||||
$this->ytdl = $ytdl;
|
||||
parent::__construct($appName, $request);
|
||||
}
|
||||
|
||||
/**
|
||||
* @CORS
|
||||
* @NoAdminRequired
|
||||
* @NoCSRFRequired
|
||||
*/
|
||||
public function download(string $url, string $type = "aria2", array $options = []): JSONResponse
|
||||
{
|
||||
if ($type == "aria2") {
|
||||
return $this->main->Download($url);
|
||||
} else if ($type == "ytdl") {
|
||||
$extension = $options["extension"] ?? "mp4";
|
||||
return $this->ytdl->Download($url, $extension);
|
||||
} else if ($type == "bt") {
|
||||
return $this->main->Upload();
|
||||
}
|
||||
return new JSONResponse(["error" => "Invalid download type"]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @CORS
|
||||
* @NoAdminRequired
|
||||
* @NoCSRFRequired
|
||||
*/
|
||||
public function search(string $keyword, string $site = "TPB"): JSONResponse
|
||||
{
|
||||
return $this->search->execute($keyword, $site);
|
||||
}
|
||||
}
|
||||
@@ -126,13 +126,13 @@ class MainController extends Controller
|
||||
/**
|
||||
* @NoAdminRequired
|
||||
*/
|
||||
public function Download()
|
||||
public function Download(string $url)
|
||||
{
|
||||
$dlDir = $this->aria2->getDownloadDir();
|
||||
if (!is_writable($dlDir)) {
|
||||
return new JSONResponse(['error' => sprintf("%s is not writable", $dlDir)]);
|
||||
}
|
||||
$url = trim($this->request->getParam('text-input-value'));
|
||||
//$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]);
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
namespace OCA\NCDownloader\Controller;
|
||||
|
||||
use OCA\NCDownloader\Search\siteSearch;
|
||||
@@ -22,16 +23,15 @@ class SearchController extends Controller
|
||||
$this->urlGenerator = \OC::$server->getURLGenerator();
|
||||
$this->search = new siteSearch();
|
||||
}
|
||||
/**
|
||||
/**
|
||||
* @NoAdminRequired
|
||||
*/
|
||||
public function execute()
|
||||
public function execute(string $keyword,string $site = "TPB")
|
||||
{
|
||||
$keyword = Helper::sanitize($this->request->getParam('text-input-value'));
|
||||
$site = Helper::sanitize($this->request->getParam('select-value-search'));
|
||||
$keyword = Helper::sanitize($keyword);
|
||||
$site = Helper::sanitize($site);
|
||||
$this->search->setSite($site);
|
||||
$data = $this->search->go($keyword);
|
||||
return new JSONResponse($data);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
namespace OCA\NCDownloader\Controller;
|
||||
|
||||
use OCA\NCDownloader\Aria2\Aria2;
|
||||
@@ -71,19 +72,20 @@ class YtdlController extends Controller
|
||||
/**
|
||||
* @NoAdminRequired
|
||||
*/
|
||||
public function Download()
|
||||
public function Download(string $url, ?string $extension = "mp4")
|
||||
{
|
||||
$dlDir = $this->ytdl->getDownloadDir();
|
||||
if (!is_writable($dlDir)) {
|
||||
return new JSONResponse(['error' => sprintf("%s is not writable", $dlDir)]);
|
||||
}
|
||||
$url = trim($this->request->getParam('text-input-value'));
|
||||
//$url = trim($this->request->getParam('text-input-value'));
|
||||
$url = trim($url);
|
||||
$yt = $this->ytdl;
|
||||
if (in_array($this->request->getParam('extension'), $this->audio_extensions)) {
|
||||
if (in_array($extension, $this->audio_extensions)) {
|
||||
$yt->audioOnly = true;
|
||||
$yt->audioFormat = $this->request->getParam('extension');
|
||||
$yt->audioFormat = $extension;
|
||||
} else {
|
||||
$yt->videoFormat = $this->request->getParam('extension');
|
||||
$yt->videoFormat = $extension;
|
||||
}
|
||||
if (!$yt->isInstalled()) {
|
||||
return new JSONResponse(["error" => "Please install the latest yt-dlp or make the bundled binary file executable in ncdownloader/bin"]);
|
||||
@@ -108,9 +110,9 @@ class YtdlController extends Controller
|
||||
/**
|
||||
* @NoAdminRequired
|
||||
*/
|
||||
public function Delete()
|
||||
public function Delete(string $gid)
|
||||
{
|
||||
$gid = $this->request->getParam('gid');
|
||||
//$gid = $this->request->getParam('gid');
|
||||
if (!$gid) {
|
||||
return new JSONResponse(['error' => "no gid value is received!"]);
|
||||
}
|
||||
@@ -143,9 +145,9 @@ class YtdlController extends Controller
|
||||
/**
|
||||
* @NoAdminRequired
|
||||
*/
|
||||
public function Redownload()
|
||||
public function Redownload(string $gid)
|
||||
{
|
||||
$gid = $this->request->getParam('gid');
|
||||
//$gid = $this->request->getParam('gid');
|
||||
if (!$gid) {
|
||||
return new JSONResponse(['error' => "no gid value is received!"]);
|
||||
}
|
||||
@@ -211,5 +213,4 @@ class YtdlController extends Controller
|
||||
|
||||
return ['error' => $this->l10n->t("Youtube-dl NOT installed!")];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user