Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions apps/files/appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,16 @@
'url' => '/api/v1/templates/path',
'verb' => 'POST'
],
[
'name' => 'Template#getPath',
'url' => '/api/v1/templates/path',
'verb' => 'GET',
],
[
'name' => 'Template#setPath',
'url' => '/api/v1/templates/path',
'verb' => 'PUT',
],
[
'name' => 'TransferOwnership#transfer',
'url' => '/api/v1/transferownership',
Expand Down
66 changes: 66 additions & 0 deletions apps/files/lib/Controller/TemplateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCS\OCSBadRequestException;
use OCP\AppFramework\OCS\OCSForbiddenException;
use OCP\AppFramework\OCSController;
use OCP\Files\Folder;
use OCP\Files\GenericFileException;
use OCP\Files\InvalidPathException;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\Files\Template\ITemplateManager;
use OCP\Files\Template\Template;
use OCP\Files\Template\TemplateFileCreator;
Expand All @@ -33,10 +39,70 @@
$appName,
IRequest $request,
protected ITemplateManager $templateManager,
private IRootFolder $rootFolder,
private string $userId,
) {
parent::__construct($appName, $request);
}

/**
* Get the personal template directory
*
* @return DataResponse<Http::STATUS_OK, array{template_path: string, available: bool}, array{}>
*
* 200: Personal template directory returned
*/
#[NoAdminRequired]
public function getPath(): DataResponse {
$path = $this->templateManager->getTemplatePath();
$available = false;
if ($path !== '') {
try {
$folder = $this->rootFolder->getUserFolder($this->userId)->get($path);
$available = $folder instanceof Folder && $folder->isReadable();
} catch (NotFoundException|NotPermittedException|InvalidPathException $e) {
}
}
return new DataResponse(['template_path' => $path, 'available' => $available]);
}

/**
* Select an existing personal template directory, or clear the selection
*
* @param string $templatePath User-relative folder path, or an empty string to clear the selection
* @return DataResponse<Http::STATUS_OK, array{template_path: string, available: bool}, array{}>
* @throws OCSBadRequestException The path does not refer to an existing folder
* @throws OCSForbiddenException The folder is not readable
*
* 200: Personal template directory updated
*/
#[NoAdminRequired]
public function setPath(string $templatePath): DataResponse {
if ($templatePath !== '') {
try {
$userFolder = $this->rootFolder->getUserFolder($this->userId);
$folder = $userFolder->get($templatePath);
if (!$folder instanceof Folder) {
throw new OCSBadRequestException('The template path must be an existing folder');
}
if (!$folder->isReadable()) {
throw new OCSForbiddenException('The template folder must be readable');
}
$templatePath = $userFolder->getRelativePath($folder->getPath());
if ($templatePath === null) {
throw new OCSBadRequestException('Invalid template folder');
}
$templatePath = '/' . trim($templatePath, '/');
} catch (NotFoundException|InvalidPathException $e) {
throw new OCSBadRequestException('The template path must be an existing folder');
} catch (NotPermittedException $e) {
throw new OCSForbiddenException('The template folder must be readable');
}
}
$this->templateManager->setTemplatePath($templatePath);
return new DataResponse(['template_path' => $templatePath, 'available' => $templatePath !== '']);

Check failure on line 103 in apps/files/lib/Controller/TemplateController.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

RedundantCondition

apps/files/lib/Controller/TemplateController.php:103:77: RedundantCondition: '' can never contain non-falsy-string (see https://psalm.dev/122)
}

/**
* List the available templates
*
Expand Down
273 changes: 273 additions & 0 deletions apps/files/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -1821,6 +1821,279 @@
}
}
}
},
"get": {
"operationId": "template-get-path",
"summary": "Get the personal template directory",
"tags": [
"template"
],
"security": [
{
"bearer_auth": []
},
{
"basic_auth": []
}
],
"parameters": [
{
"name": "OCS-APIRequest",
"in": "header",
"description": "Required to be true for the API request to pass",
"required": true,
"schema": {
"type": "boolean",
"default": true
}
}
],
"responses": {
"200": {
"description": "Personal template directory returned",
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"ocs"
],
"properties": {
"ocs": {
"type": "object",
"required": [
"meta",
"data"
],
"properties": {
"meta": {
"$ref": "#/components/schemas/OCSMeta"
},
"data": {
"type": "object",
"required": [
"template_path",
"available"
],
"properties": {
"template_path": {
"type": "string"
},
"available": {
"type": "boolean"
}
}
}
}
}
}
}
}
}
},
"401": {
"description": "Current user is not logged in",
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"ocs"
],
"properties": {
"ocs": {
"type": "object",
"required": [
"meta",
"data"
],
"properties": {
"meta": {
"$ref": "#/components/schemas/OCSMeta"
},
"data": {}
}
}
}
}
}
}
}
}
},
"put": {
"operationId": "template-set-path",
"summary": "Select an existing personal template directory, or clear the selection",
"tags": [
"template"
],
"security": [
{
"bearer_auth": []
},
{
"basic_auth": []
}
],
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"templatePath"
],
"properties": {
"templatePath": {
"type": "string",
"description": "User-relative folder path, or an empty string to clear the selection"
}
}
}
}
}
},
"parameters": [
{
"name": "OCS-APIRequest",
"in": "header",
"description": "Required to be true for the API request to pass",
"required": true,
"schema": {
"type": "boolean",
"default": true
}
}
],
"responses": {
"200": {
"description": "Personal template directory updated",
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"ocs"
],
"properties": {
"ocs": {
"type": "object",
"required": [
"meta",
"data"
],
"properties": {
"meta": {
"$ref": "#/components/schemas/OCSMeta"
},
"data": {
"type": "object",
"required": [
"template_path",
"available"
],
"properties": {
"template_path": {
"type": "string"
},
"available": {
"type": "boolean"
}
}
}
}
}
}
}
}
}
},
"400": {
"description": "The path does not refer to an existing folder",
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"ocs"
],
"properties": {
"ocs": {
"type": "object",
"required": [
"meta",
"data"
],
"properties": {
"meta": {
"$ref": "#/components/schemas/OCSMeta"
},
"data": {}
}
}
}
}
}
}
},
"403": {
"description": "The folder is not readable",
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"ocs"
],
"properties": {
"ocs": {
"type": "object",
"required": [
"meta",
"data"
],
"properties": {
"meta": {
"$ref": "#/components/schemas/OCSMeta"
},
"data": {}
}
}
}
}
}
}
},
"401": {
"description": "Current user is not logged in",
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"ocs"
],
"properties": {
"ocs": {
"type": "object",
"required": [
"meta",
"data"
],
"properties": {
"meta": {
"$ref": "#/components/schemas/OCSMeta"
},
"data": {}
}
}
}
}
}
}
}
}
}
},
"/ocs/v2.php/apps/files/api/v1/transferownership": {
Expand Down
Loading
Loading