Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/richdocuments.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorRoeland Jago Douma <roeland@famdouma.nl>2018-10-08 15:52:25 +0300
committerRoeland Jago Douma <roeland@famdouma.nl>2018-12-20 09:38:44 +0300
commit34106609ea391d267c441c46492627745c314429 (patch)
treec5c8df41ab859876a5c5ce29b2f7414ab6b6d583 /lib
parentbb4956b87bb959407d349dc76a3c3b573bd49de5 (diff)
Move templates over to use fileids as well
Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
Diffstat (limited to 'lib')
-rw-r--r--lib/Controller/TemplatesController.php33
-rw-r--r--lib/TemplateManager.php151
2 files changed, 111 insertions, 73 deletions
diff --git a/lib/Controller/TemplatesController.php b/lib/Controller/TemplatesController.php
index 4262d82f..b83b8245 100644
--- a/lib/Controller/TemplatesController.php
+++ b/lib/Controller/TemplatesController.php
@@ -88,23 +88,28 @@ class TemplatesController extends Controller {
*
* Get preview for a specific template
*
- * @param string $templateName The template id
+ * @param int $fileId The template id
+ * @param int $x
+ * @param int $y
+ * @param bool $a
+ * @param bool $forceIcon
+ * @param string $mode
* @return DataResponse
* @throws NotFoundResponse
*/
- public function getPreview(string $templateName,
- int $x = 150,
- int $y = 150,
- bool $a = false,
- bool $forceIcon = true,
- string $mode = 'fill') {
-
- if ($templateName === '' || $x === 0 || $y === 0) {
+ public function getPreview($fileId,
+ $x = 150,
+ $y = 150,
+ $a = false,
+ $forceIcon = true,
+ $mode = 'fill') {
+
+ if ($fileId === '' || $x === 0 || $y === 0) {
return new DataResponse([], Http::STATUS_BAD_REQUEST);
}
try {
- $template = $this->manager->get($templateName);
+ $template = $this->manager->get($fileId);
} catch (NotFoundException $e) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
}
@@ -121,7 +126,7 @@ class TemplatesController extends Controller {
*
* @return JSONResponse
*/
- public function add(): JSONResponse {
+ public function add() {
$files = $this->request->getUploadedFile('files');
if (!is_null($files)) {
@@ -160,12 +165,12 @@ class TemplatesController extends Controller {
/**
* Delete a global template
*
- * @param string $templateName
+ * @param int $fileId
* @return JSONResponse
*/
- public function delete(string $templateName): JSONResponse {
+ public function delete($fileId) {
try {
- $this->manager->delete($templateName);
+ $this->manager->delete($fileId);
return new JSONResponse(
['data' => ['status' => 'success']],
diff --git a/lib/TemplateManager.php b/lib/TemplateManager.php
index 483e8ce4..07bafbec 100644
--- a/lib/TemplateManager.php
+++ b/lib/TemplateManager.php
@@ -24,8 +24,8 @@ declare (strict_types = 1);
namespace OCA\Richdocuments;
+use OCP\Files\File;
use OCP\Files\Folder;
-use OCP\Files\IAppData;
use OCP\Files\IRootFolder;
use OCP\Files\Node;
use OCP\Files\NotFoundException;
@@ -47,9 +47,6 @@ class TemplateManager {
/** @var IConfig */
private $config;
- /** @var IAppData */
- private $appData;
-
/** @var IURLGenerator */
private $urlGenerator;
@@ -59,8 +56,8 @@ class TemplateManager {
/** @var Folder */
private $userFolder;
- /** @var IPreview */
- private $previewManager;
+ /** @var IRootFolder */
+ private $rootFolder;
/** Accepted templates mime types */
const MIMES_DOCUMENTS = [
@@ -96,44 +93,55 @@ class TemplateManager {
IConfig $config,
Factory $appDataFactory,
IURLGenerator $urlGenerator,
- IRootFolder $rootFolder,
- IPreview $previewManager) {
+ IRootFolder $rootFolder) {
$this->appName = $appName;
$this->userId = $userId;
$this->config = $config;
- $this->appData = $appDataFactory->get($appName);
+ $this->rootFolder = $rootFolder;
$this->urlGenerator = $urlGenerator;
- $this->userFolder = $rootFolder->getUserFolder($userId);
- $this->previewManager = $previewManager;
+ /*
+ * Init the appdata folder
+ * We need an actual folder for the fileid and previews.
+ * TODO: Fix this at some point
+ */
+ $appData = $appDataFactory->get($appName);
try {
- $this->folder = $this->appData->getFolder('templates');
+ $appData->getFolder('templates');
} catch (NotFoundException $e) {
- $this->folder = $this->appData->newFolder('templates');
+ $appData->newFolder('templates');
}
+
+
+ $this->userFolder = $rootFolder->getUserFolder($userId);
+
+
}
/**
* Get template ISimpleFile|Node
*
- * @param string $templateName
- * @return ISimpleFile|Node
+ * @param int $fileId
+ * @return File
*/
- public function get(string $templateName) {
- try {
- // is this a global template ?
- $templateFile = $this->folder->getFile($templateName);
- } catch (NotFoundException $e) {
- $templateDir = $this->getUserTemplateDir();
- // finally get the template file
- try {
- $templateFile = $templateDir->get($templateName);
- } catch (NotFoundException $e) {
- throw new NotFoundException($e->getMessage());
+ public function get($fileId) {
+ // is this a global template ?
+ $files = $this->getSystemTemplateDir()->getDirectoryListing();
+
+ foreach ($files as $file) {
+ if ($file->getId() === $fileId) {
+ return $file;
}
}
- return $templateFile;
+ $templateDir = $this->getUserTemplateDir();
+ // finally get the template file
+ $files = $templateDir->getById($fileId);
+ if ($files !== []) {
+ return $files[0];
+ }
+
+ throw new NotFoundException();
}
/**
@@ -141,12 +149,16 @@ class TemplateManager {
*
* @return array
*/
- public function getSystem(): array{
- $templateFiles = $this->folder->getDirectoryListing();
+ public function getSystem() {
+ $folder = $this->getSystemTemplateDir();
- return array_map(function (ISimpleFile $templateFile) {
- return $this->formatNodeReturn($templateFile);
- }, $templateFiles);
+ $templateFiles = $folder->getDirectoryListing();
+ return array_filter(array_map(function (Node $templateFile) {
+ if ($templateFile instanceof File) {
+ return $this->formatNodeReturn($templateFile);
+ }
+ return null;
+ }, $templateFiles));
}
/**
@@ -154,14 +166,17 @@ class TemplateManager {
*
* @return array
*/
- public function getUser(): array{
+ public function getUser() {
try {
$templateDir = $this->getUserTemplateDir();
$templateFiles = $templateDir->getDirectoryListing();
- return array_map(function (Node $templateFile) {
- return $this->formatNodeReturn($templateFile);
- }, $templateFiles);
+ return array_filter(array_map(function (Node $templateFile) {
+ if ($templateFile instanceof File) {
+ return $this->formatNodeReturn($templateFile);
+ }
+ return null;
+ }, $templateFiles));
} catch(NotFoundException $e) {
return [];
}
@@ -172,7 +187,7 @@ class TemplateManager {
*
* @return array
*/
- public function getAll(string $type = 'document'): array{
+ public function getAll($type = 'document'): array{
$system = $this->getSystem();
$user = $this->getUser();
@@ -188,33 +203,36 @@ class TemplateManager {
* @param string $templateFile
* @return array
*/
- public function add(string $templateName, string $templateFile): array{
+ public function add($templateName, $templateFile) {
+ $folder = $this->getSystemTemplateDir();
+
try {
- $template = $this->folder->getFile($templateName);
+ $template = $folder->get($templateName);
} catch (NotFoundException $e) {
- $template = $this->folder->newFile($templateName);
+ $template = $folder->newFile($templateName);
}
$template->putContent($templateFile);
- return $this->formatNodeReturn($this->get($templateName));
+ return $this->formatNodeReturn($this->get($template->getId()));
}
/**
* Delete a template to the global template folder
*
- * @param string $templateName
+ * @param int $fileId
* @return boolean
* @throws NotFoundException
*/
- public function delete(string $templateName): bool {
- try {
- $template = $this->get($templateName);
- $template->delete();
- } catch (NotFoundException $e) {
- throw new NotFoundException($e->getMessage());
+ public function delete($fileId) {
+ $files = $this->getSystemTemplateDir()->getDirectoryListing();
+ foreach ($files as $file) {
+ if ($file->getId() === $fileId) {
+ $file->delete();
+ return true;
+ }
}
- return true;
+ throw new NotFoundException();
}
/**
@@ -222,8 +240,8 @@ class TemplateManager {
*
* @return array
*/
- private function flipTypes(): array{
- $result = array();
+ private function flipTypes() {
+ $result = [];
foreach ($this::$tplTypes as $type => $mime) {
$result = array_merge($result, array_fill_keys($mime, $type));
}
@@ -234,40 +252,55 @@ class TemplateManager {
/**
* Get the user template directory
*
- * @return Node
+ * @return Folder
* @throws NotFoundException
*/
- private function getUserTemplateDir(): Node {
+ private function getUserTemplateDir() {
// has the user manually set a directory as the default template dir ?
$templateDirID = $this->config->getUserValue($this->userId, $this->appName, 'template_dir', false);
+ $userFolder = $this->rootFolder->getUserFolder($this->userId);
if ($templateDirID !== false) {
- $templateDir = $this->userFolder->getById($templateDirID);
+ $templateDir = $userFolder->getById($templateDirID);
} else {
// fallback to default template dir
try {
- $templateDir = $this->userFolder->get('Templates');
+ $templateDir = $userFolder->get('Templates');
} catch (NotFoundException $e) {
throw new NotFoundException($e->getMessage());
}
}
+ if (!($templateDir instanceof Folder)) {
+ throw new NotFoundException('Template dir points to a file');
+ }
+
return $templateDir;
}
/**
+ * @return Folder
+ */
+ private function getSystemTemplateDir() {
+ return $this->rootFolder->get('appdata_' . $this->config->getSystemValue('instanceid', null))
+ ->get('richdocuments')
+ ->get('templates');
+ }
+
+ /**
* Format template file for json return object
*
- * @param ISimpleFile|Node $template
+ * @param File $template
* @return array
*/
- private function formatNodeReturn($template): array{
+ private function formatNodeReturn($template) {
return [
+ 'id' => $template->getId(),
'name' => $template->getName(),
- 'preview' => $this->urlGenerator->linkToRoute('richdocuments.templates.getPreview', ['templateName' => $template->getName()]),
+ 'preview' => $this->urlGenerator->linkToRoute('richdocuments.templates.getPreview', ['fileId' => $template->getId()]),
'ext' => $this->flipTypes()[$template->getMimeType()],
'etag' => $template->getETag(),
- 'delete' => $this->urlGenerator->linkToRoute('richdocuments.templates.delete', ['templateName' => $template->getName()])
+ 'delete' => $this->urlGenerator->linkToRoute('richdocuments.templates.delete', ['fileId' => $template->getId()])
];
}
}