added basic api for 3rd-party clients

This commit is contained in:
benson
2022-12-23 17:25:06 +08:00
parent a0eb5260cf
commit 44810e5df2
9 changed files with 90 additions and 20 deletions

View File

@@ -63,7 +63,7 @@ class Application extends App implements IBootstrap
$context->registerService(Settings::class, function (ContainerInterface $c) use ($uid){ $context->registerService(Settings::class, function (ContainerInterface $c) use ($uid){
return new Settings($uid); return new Settings($uid);
}); });
//$context->injectFn([$this, 'registerSearchProviders']); //$context->injectFn([$this, 'registerSearchProviders']);
} }
} }

View File

@@ -27,5 +27,8 @@ return [
['name' => 'Settings#saveYtdl', 'url' => '/personal/ytdl/save', 'verb' => 'POST'], ['name' => 'Settings#saveYtdl', 'url' => '/personal/ytdl/save', 'verb' => 'POST'],
['name' => 'Settings#deleteYtdl', 'url' => '/personal/ytdl/delete', 'verb' => 'POST'], ['name' => 'Settings#deleteYtdl', 'url' => '/personal/ytdl/delete', 'verb' => 'POST'],
['name' => 'Settings#getSettings', 'url' => '/getsettings', 'verb' => 'POST'], ['name' => 'Settings#getSettings', 'url' => '/getsettings', 'verb' => 'POST'],
//api routes
['name' => 'Api#download', 'url' => '/api/v1/download', 'verb' => 'POST'],
['name' => 'Api#search', 'url' => '/api/v1/search', 'verb' => 'POST'],
], ],
]; ];

View 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);
}
}

View File

@@ -126,13 +126,13 @@ class MainController extends Controller
/** /**
* @NoAdminRequired * @NoAdminRequired
*/ */
public function Download() public function Download(string $url)
{ {
$dlDir = $this->aria2->getDownloadDir(); $dlDir = $this->aria2->getDownloadDir();
if (!is_writable($dlDir)) { if (!is_writable($dlDir)) {
return new JSONResponse(['error' => sprintf("%s is not 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 (Helper::isMagnet($url)) {
if ($this->disable_bt_nonadmin && !($this->isAdmin)) { if ($this->disable_bt_nonadmin && !($this->isAdmin)) {
return new JSONResponse(['error' => $this->accessDenied]); return new JSONResponse(['error' => $this->accessDenied]);

View File

@@ -1,4 +1,5 @@
<?php <?php
namespace OCA\NCDownloader\Controller; namespace OCA\NCDownloader\Controller;
use OCA\NCDownloader\Search\siteSearch; use OCA\NCDownloader\Search\siteSearch;
@@ -22,16 +23,15 @@ class SearchController extends Controller
$this->urlGenerator = \OC::$server->getURLGenerator(); $this->urlGenerator = \OC::$server->getURLGenerator();
$this->search = new siteSearch(); $this->search = new siteSearch();
} }
/** /**
* @NoAdminRequired * @NoAdminRequired
*/ */
public function execute() public function execute(string $keyword,string $site = "TPB")
{ {
$keyword = Helper::sanitize($this->request->getParam('text-input-value')); $keyword = Helper::sanitize($keyword);
$site = Helper::sanitize($this->request->getParam('select-value-search')); $site = Helper::sanitize($site);
$this->search->setSite($site); $this->search->setSite($site);
$data = $this->search->go($keyword); $data = $this->search->go($keyword);
return new JSONResponse($data); return new JSONResponse($data);
} }
} }

View File

@@ -1,4 +1,5 @@
<?php <?php
namespace OCA\NCDownloader\Controller; namespace OCA\NCDownloader\Controller;
use OCA\NCDownloader\Aria2\Aria2; use OCA\NCDownloader\Aria2\Aria2;
@@ -71,19 +72,20 @@ class YtdlController extends Controller
/** /**
* @NoAdminRequired * @NoAdminRequired
*/ */
public function Download() public function Download(string $url, ?string $extension = "mp4")
{ {
$dlDir = $this->ytdl->getDownloadDir(); $dlDir = $this->ytdl->getDownloadDir();
if (!is_writable($dlDir)) { if (!is_writable($dlDir)) {
return new JSONResponse(['error' => sprintf("%s is not 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; $yt = $this->ytdl;
if (in_array($this->request->getParam('extension'), $this->audio_extensions)) { if (in_array($extension, $this->audio_extensions)) {
$yt->audioOnly = true; $yt->audioOnly = true;
$yt->audioFormat = $this->request->getParam('extension'); $yt->audioFormat = $extension;
} else { } else {
$yt->videoFormat = $this->request->getParam('extension'); $yt->videoFormat = $extension;
} }
if (!$yt->isInstalled()) { if (!$yt->isInstalled()) {
return new JSONResponse(["error" => "Please install the latest yt-dlp or make the bundled binary file executable in ncdownloader/bin"]); 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 * @NoAdminRequired
*/ */
public function Delete() public function Delete(string $gid)
{ {
$gid = $this->request->getParam('gid'); //$gid = $this->request->getParam('gid');
if (!$gid) { if (!$gid) {
return new JSONResponse(['error' => "no gid value is received!"]); return new JSONResponse(['error' => "no gid value is received!"]);
} }
@@ -143,9 +145,9 @@ class YtdlController extends Controller
/** /**
* @NoAdminRequired * @NoAdminRequired
*/ */
public function Redownload() public function Redownload(string $gid)
{ {
$gid = $this->request->getParam('gid'); //$gid = $this->request->getParam('gid');
if (!$gid) { if (!$gid) {
return new JSONResponse(['error' => "no gid value is received!"]); 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!")]; return ['error' => $this->l10n->t("Youtube-dl NOT installed!")];
} }
} }

View File

@@ -79,6 +79,8 @@ export default {
helper.info(message); helper.info(message);
} }
let url = formWrapper.getAttribute("action"); let url = formWrapper.getAttribute("action");
formData['url'] = formData["text-input-value"]
delete formData["text-input-value"]
helper.httpClient(url) helper.httpClient(url)
.setData(formData) .setData(formData)
.setHandler(function (data) { .setHandler(function (data) {
@@ -100,6 +102,11 @@ export default {
contentTable.getInstance().loading(); contentTable.getInstance().loading();
let url = formWrapper.getAttribute("action"); let url = formWrapper.getAttribute("action");
formData['keyword'] = formData["text-input-value"]
formData['site'] = formData["select-value-search"]
delete formData["text-input-value"]
delete formData['select-value-search']
helper.httpClient(url) helper.httpClient(url)
.setData(formData) .setData(formData)
.setHandler(function (data) { .setHandler(function (data) {

View File

@@ -29,6 +29,8 @@ const buttonHandler = (event, type) => {
console.log("gid is not set!"); console.log("gid is not set!");
} }
} }
data['url'] = data["text-input-value"]
delete data["text-input-value"]
helper.httpClient(url).setErrorHandler(function (xhr, textStatus, error) { helper.httpClient(url).setErrorHandler(function (xhr, textStatus, error) {
console.log(error); console.log(error);
}).setHandler(function (data) { }).setHandler(function (data) {

View File

@@ -173,7 +173,7 @@ const helper = {
if (element.hasAttribute('type') && element.getAttribute('type') === 'button') { if (element.hasAttribute('type') && element.getAttribute('type') === 'button') {
continue continue
} }
const key = element.getAttribute('id') const key = element.getAttribute('id') || element.getAttribute('name')
data[key] = element.value data[key] = element.value
for (let prop in element.dataset) { for (let prop in element.dataset) {
if (prop == "rel") { if (prop == "rel") {
@@ -189,7 +189,7 @@ const helper = {
} }
data[prop] = element.dataset[prop]; data[prop] = element.dataset[prop];
} }
const key = element.getAttribute('id') const key = element.getAttribute('id') || element.getAttribute('name')
data[key] = element.value data[key] = element.value
} }
return data; return data;