From 2c87dade030b7515d63735045c180cb80a12d3fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20D=C3=BChr?= Date: Wed, 13 Sep 2017 21:50:24 +0200 Subject: [PATCH] added route for accessing files --- src/Controller/ContentApiController.php | 16 +++++----- src/Resources/contao/classes/FileHelper.php | 31 ++++++++++++++++++-- src/Resources/contao/classes/FrontendApi.php | 6 ++++ 3 files changed, 43 insertions(+), 10 deletions(-) diff --git a/src/Controller/ContentApiController.php b/src/Controller/ContentApiController.php index 8f5e32e..e4e08f3 100644 --- a/src/Controller/ContentApiController.php +++ b/src/Controller/ContentApiController.php @@ -54,9 +54,9 @@ public function userAction(Request $request) /** * @return Response * - * @Route("/module", name="content_api_module") + * @Route("/file", name="content_api_file") */ - public function moduleAction(Request $request) + public function fileAction(Request $request) { return $this->handle($request); } @@ -64,9 +64,9 @@ public function moduleAction(Request $request) /** * @return Response * - * @Route("/", name="content_api_auto") + * @Route("/module", name="content_api_module") */ - public function indexAction(Request $request) + public function moduleAction(Request $request) { return $this->handle($request); } @@ -74,9 +74,9 @@ public function indexAction(Request $request) /** * @return Response * - * @Route("/{reader}", name="content_api_reader") + * @Route("/", name="content_api_auto") */ - public function readerAction(Request $request) + public function indexAction(Request $request) { return $this->handle($request); } @@ -84,9 +84,9 @@ public function readerAction(Request $request) /** * @return Response * - * @Route("/{text}", name="content_api_text") + * @Route("/{reader}", name="content_api_reader") */ - public function textAction(Request $request) + public function readerAction(Request $request) { return $this->handle($request); } diff --git a/src/Resources/contao/classes/FileHelper.php b/src/Resources/contao/classes/FileHelper.php index 79fc160..8fb1277 100644 --- a/src/Resources/contao/classes/FileHelper.php +++ b/src/Resources/contao/classes/FileHelper.php @@ -10,12 +10,12 @@ class FileHelper { public static function file($uuid, $size = null) { - $model = FilesModel::findOneBy('uuid', $uuid); + $model = FilesModel::findByUuid($uuid); if(!$model) return null; $result = Helper::toObj($model); unset($result->pid); unset($result->uuid); - $result->path = '/' + $result->path; + $result->path = '/' . $result->path; if($size && count($size) == 3){ if(is_numeric($size[2])){ $result->size = Helper::toObj(ImageSizeModel::findOneById($size[2])); @@ -32,4 +32,31 @@ public static function file($uuid, $size = null) } return $result; } + + private static function children($uuid, $depth = 0){ + $models = FilesModel::findByPid($uuid); + if(!$models) return []; + $children = Helper::toObj($models); + foreach($children as &$file){ + if($depth > 0){ + $file->children = static::children($file->uuid, $depth - 1); + } + unset($file->pid); + unset($file->uuid); + } + return $children; + } + + public static function get($path, $depth = 1) + { + $model = FilesModel::findByPath($path); + if(!$model) return null; + $result = Helper::toObj($model); + if($depth > 0){ + $result->children = static::children($result->uuid, $depth - 1); + } + unset($result->pid); + unset($result->uuid); + return $result; + } } diff --git a/src/Resources/contao/classes/FrontendApi.php b/src/Resources/contao/classes/FrontendApi.php index 32474a9..09ff92f 100644 --- a/src/Resources/contao/classes/FrontendApi.php +++ b/src/Resources/contao/classes/FrontendApi.php @@ -64,6 +64,12 @@ public function handle(Request $request) $request->query->get('lang', null) ); break; + case '/api/file': + $result = FileHelper::get( + $request->query->get('path', null), + $request->query->get('depth', 0) + ); + break; case '/api/user': if ($this->user) { $result = [