Skip to content

Commit

Permalink
wopi: add delete setting file route
Browse files Browse the repository at this point in the history
Signed-off-by: codewithvk <[email protected]>
  • Loading branch information
codewithvk committed Jan 23, 2025
1 parent 91d2cad commit 6b7754c
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
36 changes: 36 additions & 0 deletions lib/Controller/WopiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,42 @@ public function uploadSettingsFile(string $fileId, string $access_token): JSONRe
}
}

#[NoAdminRequired]
#[NoCSRFRequired]
#[PublicPage]
#[FrontpageRoute(verb: 'DELETE', url: 'wopi/settings')]
public function deleteSettingsFile(string $fileId, string $access_token): JSONResponse {
try {
$wopi = $this->wopiMapper->getWopiForToken($access_token);
if ($wopi->getTokenType() !== Wopi::TOKEN_TYPE_SETTING_AUTH) {
return new JSONResponse(['error' => 'Invalid token type'], Http::STATUS_FORBIDDEN);
}

// Parse the dynamic file path from `fileId`, e.g. "/settings/systemconfig/wordbook/en_US (1).dic"
$settingsUrl = new SettingsUrl($fileId);
$type = $settingsUrl->getType();
$category = $settingsUrl->getCategory();
$fileName = $settingsUrl->getFileName();

$this->settingsService->deleteSettingsFile($type, $category, $fileName);

return new JSONResponse([
'status' => 'success',
'message' => "File '$fileName' deleted from '$category' of type '$type'."
], Http::STATUS_OK);
} catch (UnknownTokenException $e) {
$this->logger->debug($e->getMessage(), ['exception' => $e]);
return new JSONResponse(['error' => 'Invalid token'], Http::STATUS_FORBIDDEN);
} catch (NotFoundException $e) {
return new JSONResponse(['error' => 'File not found'], Http::STATUS_NOT_FOUND);
} catch (NotPermittedException $e) {
return new JSONResponse(['error' => 'Not permitted'], Http::STATUS_FORBIDDEN);
} catch (\Exception $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
return new JSONResponse(['error' => 'Internal Server Error'], Http::STATUS_INTERNAL_SERVER_ERROR);
}
}


/**
* Given an access token and a fileId, replaces the files with the request body.
Expand Down
35 changes: 33 additions & 2 deletions lib/Service/SettingsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use OCP\ICacheFactory;
use OCP\IConfig;
use OCP\IURLGenerator;
use \OCP\Files\NotPermittedException;

/**
* A generic service to manage "system-wide" files
Expand Down Expand Up @@ -200,8 +201,38 @@ public function getSettingsFile(string $type, string $category, string $name): I
throw new NotFoundException("File '{$name}' not found in category '{$category}' for type '{$type}'.");
}
}

// TODO: add route for delete setting config file?

/**
* Delete a specific settings file from the type/category directory.
*
* @param string $type
* @param string $category
* @param string $name
*/
public function deleteSettingsFile(string $type, string $category, string $name): void {
try {
$baseFolder = $this->appData->getFolder($type);
} catch (NotFoundException $e) {
throw new NotFoundException("Type folder '{$type}' not found.");
}

try {
$categoryFolder = $baseFolder->getFolder($category);
} catch (NotFoundException $e) {
throw new NotFoundException("Category folder '{$category}' not found in type '{$type}'.");
}

try {
if (!$categoryFolder->fileExists($name)) {
throw new NotFoundException("File '{$name}' not found in category '{$category}' for type '{$type}'.");
}
$categoryFolder->getFile($name)->delete();
} catch (NotFoundException $e) {
throw $e;
} catch (NotPermittedException $e) {
throw $e;
}
}

// TODO: Handle installDefaultSystemFiles setting

Expand Down

0 comments on commit 6b7754c

Please sign in to comment.