- substantial bit rot accrued in 4 years of non-maintenance which made Reader unusable
 - Reader now works reliably on public pages - or at least it _Works For Me™_

 - Refactored a substantial part of the code to comply to the "current" (ha ha) Nextcloud API
 - Dropped Owncloud compatibility for lack of a testing installation
 - Dropped IE (<11) support
 - Dropped compatibility with older (<20) Nextcloud versions
 - Dropped app-specific ajax code, now handled by SettingsController
 - Updated dependencies where applicable
This commit is contained in:
Frank de Lange 2022-09-24 00:00:03 +00:00
parent 16afbe45fe
commit b190e180ef
137 changed files with 30984 additions and 2 deletions

View file

@ -0,0 +1,124 @@
<?php
/**
* @author Frank de Lange
* @copyright 2017 Frank de Lange
*
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
namespace OCA\Files_Reader\Controller;
use OCP\IRequest;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\DataResponse;
use OCA\Files_Reader\Service\BookmarkService;
class BookmarkController extends Controller {
private $bookmarkService;
/**
* @param string $AppName
* @param IRequest $request
* @param BookmarkService $bookmarkService
*/
public function __construct($AppName,
IRequest $request,
BookmarkService $bookmarkService ) {
parent::__construct($AppName, $request);
$this->bookmarkService = $bookmarkService;
}
/**
* @brief return bookmark
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @param int $fileId
* @param string $name
*
* @return array|\OCP\AppFramework\Http\JSONResponse
*/
public function get($fileId, $name, $type=null) {
return $this->bookmarkService->get($fileId, $name, $type);
}
/**
* @brief write bookmark
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @param int $fileId
* @param string $name
* @param string $value
*
* @return array|\OCP\AppFramework\Http\JSONResponse
*/
public function set($fileId, $name, $value, $type=null, $content=null) {
return $this->bookmarkService->set($fileId, $name, $value, $type, $content);
}
/**
* @brief return cursor for $fileId
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @param int $fileId
*
* @return array|\OCP\AppFramework\Http\JSONResponse
*/
public function getCursor($fileId) {
return $this->bookmarkService->getCursor($fileId);
}
/**
* @brief write cursor for $fileId
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @param int $fileId
* @param string $value
*
* @return array|\OCP\AppFramework\Http\JSONResponse
*/
public function setCursor($fileId, $value) {
return $this->bookmarkService->setCursor($fileId, $value);
}
/**
* @brief delete bookmark
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @param int $fileId
* @param string name
*
*/
public function delete($fileId, $name) {
return $this->bookmarkService->delete($fileId, $name);
}
/**
* @brief delete cursor
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @param int $fileId
*
*/
public function deleteCursor($fileId) {
return $this->bookmarkService->deleteCursor($fileId);
}
}

View file

@ -0,0 +1,80 @@
<?php
/**
* @author Frank de Lange
* @copyright 2017 Frank de Lange
*
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
namespace OCA\Files_Reader\Controller;
use OCP\IRequest;
use OCP\AppFramework\Controller;
use OCA\Files_Reader\Service\MetadataService;
class MetadataController extends Controller {
private $metadataService;
/**
* @param string $AppName
* @param IRequest $request
* @param MetadataService $metadataService
*/
public function __construct($AppName,
IRequest $request,
MetadataService $metadataService ) {
parent::__construct($AppName, $request);
$this->metadataService = $metadataService;
}
/**
* @brief write metadata
*
* @NoAdminRequired
*
* @param int $fileId
* @param string $value
*
* @return array|\OCP\AppFramework\Http\JSONResponse
*/
public function setAll($fileId, $value) {
return $this->metadataService->setAll($fileId, $value);
}
/**
* @brief return metadata item
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @param int $fileId
* @param string $name
*
* @return array|\OCP\AppFramework\Http\JSONResponse
*/
public function get($fileId, $name) {
return $this->metadataService->get($fileId, $name);
}
/**
* @brief write metadata item
*
* @NoAdminRequired
*
* @param int $fileId
* @param string $name
* @param string $value
*
* @return array|\OCP\AppFramework\Http\JSONResponse
*/
public function set($fileId, $name, $value) {
return $this->metadataService->set($fileId, $name, $value);
}
}

View file

@ -0,0 +1,177 @@
<?php
/**
* @author Frank de Lange
* @copyright 2015 Frank de Lange
*
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
namespace OCA\Files_Reader\Controller;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Http\ContentSecurityPolicy;
use OCP\IRequest;
use OCP\IURLGenerator;
use OCP\Files\IRootFolder;
use OCP\Share\IManager;
use OCP\Files\FileInfo;
use OCP\Files\NotFoundException;
use OCA\Files_Reader\Service\BookmarkService;
use OCA\Files_Reader\Service\MetadataService;
use OCA\Files_Reader\Service\PreferenceService;
class PageController extends Controller {
/** @var IURLGenerator */
private $urlGenerator;
/** @var IRootFolder */
private $rootFolder;
private $shareManager;
private $userId;
private $bookmarkService;
private $metadataService;
private $preferenceService;
/**
* @param string $AppName
* @param IRequest $request
* @param IURLGenerator $urlGenerator
* @param IRootFolder $rootFolder
* @param IManager $shareManager
* @param string $UserId
* @param BookmarkService $bookmarkService
* @param PreferenceService $preferenceService
* @param MetadataService $metadataService
*/
public function __construct(
$AppName,
IRequest $request,
IURLGenerator $urlGenerator,
IRootFolder $rootFolder,
IManager $shareManager,
$UserId,
BookmarkService $bookmarkService,
PreferenceService $preferenceService,
MetadataService $metadataService) {
parent::__construct($AppName, $request);
$this->urlGenerator = $urlGenerator;
$this->rootFolder = $rootFolder;
$this->shareManager = $shareManager;
$this->userId = $UserId;
$this->bookmarkService = $bookmarkService;
$this->metadataService = $metadataService;
$this->preferenceService = $preferenceService;
}
/**
* @PublicPage
* @NoCSRFRequired
*
* @return TemplateResponse
*/
public function showReader() {
$templates= [
'epub' => 'epubreader',
'pdf' => 'pdfreader',
'cbx' => 'cbreader'
];
/**
* $fileInfo = [
* fileId => null,
* fileName => null,
* fileType => null
* ];
*/
$fileInfo = $this->getFileInfo($this->request->get['file']);
$fileId = $fileInfo['fileId'];
$type = $this->request->get["type"];
$scope = $template = $templates[$type];
$params = [
'urlGenerator' => $this->urlGenerator,
'downloadLink' => $this->request->get['file'],
'scope' => $scope,
'fileId' => $fileInfo['fileId'],
'fileName' => $fileInfo['fileName'],
'fileType' => $fileInfo['fileType'],
'cursor' => $this->toJson($this->bookmarkService->getCursor($fileId)),
'defaults' => $this->toJson($this->preferenceService->getDefault($scope)),
'preferences' => $this->toJson($this->preferenceService->get($scope, $fileId)),
'defaults' => $this->toJson($this->preferenceService->getDefault($scope)),
'metadata' => $this->toJson($this->metadataService->get($fileId)),
'annotations' => $this->toJson($this->bookmarkService->get($fileId))
];
$policy = new ContentSecurityPolicy();
$policy->addAllowedStyleDomain('\'self\'');
$policy->addAllowedStyleDomain('blob:');
$policy->addAllowedScriptDomain('\'self\'');
$policy->addAllowedFrameDomain('\'self\'');
$policy->addAllowedChildSrcDomain('\'self\'');
$policy->addAllowedFontDomain('\'self\'');
$policy->addAllowedFontDomain('data:');
$policy->addAllowedFontDomain('blob:');
$policy->addAllowedImageDomain('blob:');
$response = new TemplateResponse($this->appName, $template, $params, 'blank');
$response->setContentSecurityPolicy($policy);
return $response;
}
/**
* @brief sharing-aware file info retriever
*
* Work around the differences between normal and shared file access
* (this should be abstracted away in OC/NC IMnsHO)
*
* @param string $path path-fragment from url
* @return array
* @throws NotFoundException
*/
private function getFileInfo($path) {
$count = 0;
$shareToken = preg_replace("/(?:\/index\.php)?\/s\/([A-Za-z0-9]{15,32})\/download.*/", "$1", $path, 1,$count);
if ($count === 1) {
/* shared file or directory */
$node = $this->shareManager->getShareByToken($shareToken)->getNode();
$type = $node->getType();
/* shared directory, need file path to continue, */
if ($type == \OCP\Files\FileInfo::TYPE_FOLDER) {
$query = [];
parse_str(parse_url($path, PHP_URL_QUERY), $query);
if (isset($query['path']) && isset($query['files'])) {
$node = $node->get($query['path'])->get($query['files']);
} else {
throw new NotFoundException('Shared file path or name not set');
}
}
$filePath = $node->getPath();
$fileId = $node->getId();
} else {
$filePath = $path;
$fileId = $this->rootFolder->getUserFolder($this->userId)
->get(preg_replace("/.*\/remote.php\/webdav(.*)/", "$1", rawurldecode($this->request->get['file'])))
->getFileInfo()
->getId();
}
return [
'fileName' => pathInfo($filePath, PATHINFO_FILENAME),
'fileType' => strtolower(pathInfo($filePath, PATHINFO_EXTENSION)),
'fileId' => $fileId
];
}
private function toJson($value) {
return htmlspecialchars(json_encode($value), ENT_QUOTES, 'UTF-8');
}
}

View file

@ -0,0 +1,128 @@
<?php
/**
* @author Frank de Lange
* @copyright 2017 Frank de Lange
*
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
namespace OCA\Files_Reader\Controller;
use OCP\IRequest;
use OCP\IURLGenerator;
use OCP\AppFramework\Http;
use OCP\AppFramework\Controller;
use OCA\Files_Reader\Service\PreferenceService;
class PreferenceController extends Controller {
private $urlGenerator;
private $preferenceService;
/**
* @param string $AppName
* @param IRequest $request
* @param IURLGenerator $urlGenerator
* @param PreferenceService $preferenceService
*/
public function __construct($AppName,
IRequest $request,
IURLGenerator $urlGenerator,
PreferenceService $preferenceService ) {
parent::__construct($AppName, $request);
$this->urlGenerator = $urlGenerator;
$this->preferenceService = $preferenceService;
}
/**
* @brief return preference for $fileId
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @param string $scope
* @param int $fileId
* @param string $name if null, return all preferences for $scope + $fileId
*
* @return array|\OCP\AppFramework\Http\JSONResponse
*/
public function get($scope, $fileId, $name) {
return $this->preferenceService->get($scope, $fileId, $name);
}
/**
* @brief write preference for $fileId
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @param string $scope
* @param int $fileId
* @param string $name
* @param string $value
*
* @return array|\OCP\AppFramework\Http\JSONResponse
*/
public function set($scope, $fileId, $name, $value) {
return $this->preferenceService->set($scope, $fileId, $name, $value);
}
/**
* @brief return default preference
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @param string $scope
* @param string $name if null, return all default preferences for scope
*
* @return array|\OCP\AppFramework\Http\JSONResponse
*/
public function getDefault($scope, $name) {
return $this->preferenceService->getDefault($scope, $name);
}
/**
* @brief write default preference
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @param string $scope
* @param string $name
* @param string $value
*
* @return array|\OCP\AppFramework\Http\JSONResponse
*/
public function setDefault($scope, $name, $value) {
return $this->preferenceService->setDefault($scope, $name, $value);
}
/**
* @brief delete preference
*
* @param string $scope
* @param int $fileId
* @param string $name
*
*/
public function delete($scope, $fileId, $name) {
return $this->preferenceService->delete($scope, $fileId, $name);
}
/**
* @brief delete default preference
*
* @param $scope
* @param $name
*
*/
public function deleteDefault($scope, $name) {
return $this->preferenceService->deleteDefault($scope, $name);
}
}

View file

@ -0,0 +1,60 @@
<?php
declare(strict_types=1);
/**
* @author Frank de Lange
* @copyright 2022 Frank de Lange
*
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
namespace OCA\Files_Reader\Controller;
use OCA\Files_Reader\AppInfo\Application;
use OCP\IL10N;
use OCP\IConfig;
use OCP\IRequest;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\DataResponse;
class SettingsController extends Controller {
protected IConfig $config;
protected Il10n $l10n;
protected string $userId;
public function __construct(
IRequest $request,
IConfig $config,
string $userId,
IL10N $l10n) {
parent::__construct(Application::APP_ID, $request);
$this->config = $config;
$this->userId = $userId;
$this->l10n = $l10n;
}
public function personal(string $epub_enable, string $pdf_enable, string $cbx_enable) {
$this->config->setUserValue(
$this->userId, Application::APP_ID, 'epub_enable', $epub_enable
);
$this->config->setUserValue(
$this->userId, Application::APP_ID, 'pdf_enable', $pdf_enable
);
$this->config->setUserValue(
$this->userId, Application::APP_ID, 'cbx_enable', $cbx_enable
);
return new DataResponse([
'data' => [
'message' => $this->l10n->t('Your settings have been updated.'),
],
]);
}
}