alltube/classes/Controller/JsonController.php

46 lines
1.1 KiB
PHP
Raw Normal View History

<?php
/**
* JsonController class.
*/
namespace Alltube\Controller;
use Alltube\Library\Exception\AlltubeLibraryException;
use Slim\Http\Request;
use Slim\Http\Response;
2020-07-05 09:22:55 +00:00
use Slim\Http\StatusCode;
/**
* Controller that returns JSON.
*/
class JsonController extends BaseController
{
/**
* Return the JSON object generated by youtube-dl.
*
* @param Request $request PSR-7 request
* @param Response $response PSR-7 response
*
* @return Response HTTP response
* @throws AlltubeLibraryException
*/
2020-12-17 21:43:05 +00:00
public function json(Request $request, Response $response): Response
{
$url = $request->getQueryParam('url');
if (isset($url)) {
$this->video = $this->downloader->getVideo(
$url,
$this->getFormat($request),
$this->getPassword($request)
);
return $response->withJson($this->video->getJson());
} else {
return $response->withJson(['error' => 'You need to provide the url parameter'])
2020-07-05 09:22:55 +00:00
->withStatus(StatusCode::HTTP_BAD_REQUEST);
}
}
}