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

@@ -27,5 +27,8 @@ return [
['name' => 'Settings#saveYtdl', 'url' => '/personal/ytdl/save', 'verb' => 'POST'],
['name' => 'Settings#deleteYtdl', 'url' => '/personal/ytdl/delete', '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
*/
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]);

View File

@@ -1,4 +1,5 @@
<?php
namespace OCA\NCDownloader\Controller;
use OCA\NCDownloader\Search\siteSearch;
@@ -25,13 +26,12 @@ class SearchController extends Controller
/**
* @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);
}
}

View File

@@ -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!")];
}
}

View File

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

View File

@@ -29,6 +29,8 @@ const buttonHandler = (event, type) => {
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) {
console.log(error);
}).setHandler(function (data) {

View File

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