Skip to content

Commit

Permalink
Merge pull request #4047 from nextcloud/chore/cleanup-wopi
Browse files Browse the repository at this point in the history
chore: Move to attributes in WopiController
  • Loading branch information
juliusknorr authored Oct 8, 2024
2 parents 4ab6da5 + ae2654e commit f897059
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 60 deletions.
7 changes: 0 additions & 7 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,6 @@
// external api access
['name' => 'document#extAppGetData', 'url' => '/ajax/extapp/data/{fileId}', 'verb' => 'POST'],

// WOPI access
['name' => 'wopi#checkFileInfo', 'url' => 'wopi/files/{fileId}', 'verb' => 'GET'],
['name' => 'wopi#getFile', 'url' => 'wopi/files/{fileId}/contents', 'verb' => 'GET'],
['name' => 'wopi#putFile', 'url' => 'wopi/files/{fileId}/contents', 'verb' => 'POST'],
['name' => 'wopi#postFile', 'url' => 'wopi/files/{fileId}', 'verb' => 'POST'],
['name' => 'wopi#getTemplate', 'url' => 'wopi/template/{fileId}', 'verb' => 'GET'],

// Settings
['name' => 'settings#setPersonalSettings', 'url' => 'ajax/personal.php', 'verb' => 'POST'],
['name' => 'settings#setSettings', 'url' => 'ajax/admin.php', 'verb' => 'POST'],
Expand Down
82 changes: 29 additions & 53 deletions lib/Controller/WopiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@
use OCA\Richdocuments\TemplateManager;
use OCA\Richdocuments\TokenManager;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\FrontpageRoute;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
use OCP\AppFramework\Http\Attribute\PublicPage;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Http\StreamResponse;
use OCP\AppFramework\QueryException;
Expand All @@ -32,7 +35,6 @@
use OCP\Files\File;
use OCP\Files\Folder;
use OCP\Files\GenericFileException;
use OCP\Files\InvalidPathException;
use OCP\Files\IRootFolder;
use OCP\Files\Lock\ILock;
use OCP\Files\Lock\ILockManager;
Expand All @@ -41,7 +43,6 @@
use OCP\Files\Lock\OwnerLockedException;
use OCP\Files\Node;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\IRequest;
Expand Down Expand Up @@ -89,18 +90,12 @@ public function __construct(

/**
* Returns general info about a file.
*
* @NoAdminRequired
* @NoCSRFRequired
* @PublicPage
*
* @param string $fileId
* @param string $access_token
* @return JSONResponse
* @throws InvalidPathException
* @throws NotFoundException
*/
public function checkFileInfo($fileId, $access_token) {
#[NoAdminRequired]
#[NoCSRFRequired]
#[PublicPage]
#[FrontpageRoute(verb: 'GET', url: 'wopi/files/{fileId}')]
public function checkFileInfo(string $fileId, string $access_token): JSONResponse {
try {
[$fileId, , $version] = Helper::parseFileId($fileId);

Expand Down Expand Up @@ -218,7 +213,7 @@ public function checkFileInfo($fileId, $access_token) {
$response = array_merge($response, $this->appConfig->getWopiOverride());

$this->eventDispatcher->dispatchTyped(new DocumentOpenedEvent(
$user ? $user->getUID() : null,
$user?->getUID(),
$file
));

Expand Down Expand Up @@ -268,20 +263,12 @@ private function setFederationFileInfo(Wopi $wopi, $response) {

/**
* Given an access token and a fileId, returns the contents of the file.
* Expects a valid token in access_token parameter.
*
* @PublicPage
* @NoCSRFRequired
*
* @param string $fileId
* @param string $access_token
* @return Http\Response
* @throws NotFoundException
* @throws NotPermittedException
* @throws LockedException
*/
public function getFile($fileId,
$access_token) {
#[NoAdminRequired]
#[NoCSRFRequired]
#[PublicPage]
#[FrontpageRoute(verb: 'GET', url: 'wopi/files/{fileId}/contents')]
public function getFile(string $fileId, string $access_token): JSONResponse|StreamResponse {
[$fileId, , $version] = Helper::parseFileId($fileId);

try {
Expand Down Expand Up @@ -360,16 +347,12 @@ public function getFile($fileId,
/**
* Given an access token and a fileId, replaces the files with the request body.
* Expects a valid token in access_token parameter.
*
* @PublicPage
* @NoCSRFRequired
*
* @param string $fileId
* @param string $access_token
* @return JSONResponse
*/
public function putFile($fileId,
$access_token) {
#[NoAdminRequired]
#[NoCSRFRequired]
#[PublicPage]
#[FrontpageRoute(verb: 'POST', url: 'wopi/files/{fileId}/contents')]
public function putFile(string $fileId, string $access_token): JSONResponse {
[$fileId, , ] = Helper::parseFileId($fileId);
$isPutRelative = ($this->request->getHeader('X-WOPI-Override') === 'PUT_RELATIVE');

Expand Down Expand Up @@ -490,15 +473,11 @@ public function putFile($fileId,
* handles both saving and saving as.* Given an access token and a fileId, replaces the files with the request body.
*
* FIXME Cleanup this code as is a lot of shared logic between putFile and putRelativeFile
*
* @PublicPage
* @NoCSRFRequired
*
* @param string $fileId
* @param string $access_token
* @return JSONResponse
* @throws DoesNotExistException
*/
#[NoAdminRequired]
#[NoCSRFRequired]
#[PublicPage]
#[FrontpageRoute(verb: 'POST', url: 'wopi/files/{fileId}')]
public function postFile(string $fileId, string $access_token): JSONResponse {
try {
$wopiOverride = $this->request->getHeader('X-WOPI-Override');
Expand Down Expand Up @@ -805,15 +784,12 @@ private function getShareForWopiToken(Wopi $wopi): ?IShare {

/**
* Endpoint to return the template file that is requested by collabora to create a new document
*
* @PublicPage
* @NoCSRFRequired
*
* @param $fileId
* @param $access_token
* @return JSONResponse|StreamResponse
*/
public function getTemplate($fileId, $access_token) {
#[NoAdminRequired]
#[NoCSRFRequired]
#[PublicPage]
#[FrontpageRoute(verb: 'GET', url: 'wopi/template/{fileId}')]
public function getTemplate(string $fileId, string $access_token): JSONResponse|StreamResponse {
try {
$wopi = $this->wopiMapper->getWopiForToken($access_token);
} catch (UnknownTokenException $e) {
Expand Down

0 comments on commit f897059

Please sign in to comment.