added basic api for 3rd-party clients
This commit is contained in:
@@ -63,7 +63,7 @@ class Application extends App implements IBootstrap
|
||||
$context->registerService(Settings::class, function (ContainerInterface $c) use ($uid){
|
||||
return new Settings($uid);
|
||||
});
|
||||
|
||||
|
||||
//$context->injectFn([$this, 'registerSearchProviders']);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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'],
|
||||
],
|
||||
];
|
||||
|
||||
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!")];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user