Skip to content

Commit

Permalink
added route for accessing files
Browse files Browse the repository at this point in the history
  • Loading branch information
saibotd committed Sep 13, 2017
1 parent 62dd4b8 commit 2c87dad
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 10 deletions.
16 changes: 8 additions & 8 deletions src/Controller/ContentApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,39 +54,39 @@ 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);
}

/**
* @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);
}

/**
* @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);
}

/**
* @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);
}
Expand Down
31 changes: 29 additions & 2 deletions src/Resources/contao/classes/FileHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]));
Expand All @@ -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;
}
}
6 changes: 6 additions & 0 deletions src/Resources/contao/classes/FrontendApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down

0 comments on commit 2c87dad

Please sign in to comment.