memories/lib/Controller/GenericApiController.php

92 lines
3.0 KiB
PHP

<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2022 Varun Patil <radialapps@gmail.com>
* @author Varun Patil <radialapps@gmail.com>
* @license AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace OCA\Memories\Controller;
use OCA\Memories\AppInfo\Application;
use OCA\Memories\Db\FsManager;
use OCA\Memories\Db\TimelineQuery;
use OCP\App\IAppManager;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\OCSController;
use OCP\Files\IRootFolder;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\IRequest;
use OCP\IUserSession;
use Psr\Log\LoggerInterface;
abstract class GenericApiController extends OCSController
{
protected IConfig $config;
protected IUserSession $userSession;
protected IRootFolder $rootFolder;
protected IAppManager $appManager;
protected IDBConnection $connection;
protected LoggerInterface $logger;
protected TimelineQuery $timelineQuery;
protected FsManager $fs;
public function __construct(
IRequest $request,
IConfig $config,
IUserSession $userSession,
IDBConnection $connection,
IRootFolder $rootFolder,
IAppManager $appManager,
LoggerInterface $logger,
TimelineQuery $timelineQuery,
FsManager $fs
) {
parent::__construct(Application::APPNAME, $request);
$this->config = $config;
$this->userSession = $userSession;
$this->connection = $connection;
$this->rootFolder = $rootFolder;
$this->appManager = $appManager;
$this->logger = $logger;
$this->timelineQuery = $timelineQuery;
$this->fs = $fs;
}
/**
* We need to extend OCSController instead of ApiController because
* these are external APIs and should be accessible without CSRF
* tokens. The OCS response, unfortunately, nukes status codes so
* we need to override the buildResponse method to force it back.
*
* @param mixed $response Response that cannot be rendered directly
* @param mixed $format Format of the response
*/
public function buildResponse($response, $format = 'json')
{
if ($response instanceof DataResponse) {
return new JSONResponse($response->getData(), $response->getStatus());
}
return new JSONResponse($response);
}
}