diff --git a/src/flextype/Endpoints/files.php b/src/flextype/Endpoints/files.php index a47a7f01..b8c47450 100644 --- a/src/flextype/Endpoints/files.php +++ b/src/flextype/Endpoints/files.php @@ -75,9 +75,9 @@ flextype()->get('/api/files', function (Request $request, Response $response) us // Get list if file or files for specific folder if (is_dir(PATH['project'] . '/uploads/' . $path)) { - $files = flextype('media_files')->fetchCollection($path); + $files = flextype('media_files')->fetchCollection($path)->toArray(); } else { - $files = flextype('media_files')->fetchSingle($path); + $files = flextype('media_files')->fetchSingle($path)->toArray(); } // Write response data @@ -190,7 +190,7 @@ flextype()->post('/api/files', function (Request $request, Response $response) u $response_data['data'] = []; if ($create_file) { - $response_data['data'] = flextype('media_files')->fetch($folder . '/' . basename($create_file)); + $response_data['data'] = flextype('media_files')->fetchSingle($folder . '/' . basename($create_file)); } // Set response code @@ -300,7 +300,7 @@ flextype()->put('/api/files', function (Request $request, Response $response) us $response_data['data'] = []; if ($rename_file) { - $response_data['data'] = flextype('media_files')->fetch($new_path); + $response_data['data'] = flextype('media_files')->fetchSingle($new_path); } // Set response code @@ -409,7 +409,7 @@ flextype()->put('/api/files/copy', function (Request $request, Response $respons $response_data['data'] = []; if ($copy_file) { - $response_data['data'] = flextype('media_files')->fetch($new_path); + $response_data['data'] = flextype('media_files')->fetchSingle($new_path); } // Set response code @@ -621,7 +621,7 @@ flextype()->patch('/api/files/meta', function (Request $request, Response $respo $response_data['data'] = []; if ($update_file_meta) { - $response_data['data'] = flextype('media_files')->fetch($path); + $response_data['data'] = flextype('media_files')->fetchSingle($path); } // Set response code @@ -732,7 +732,7 @@ flextype()->post('/api/files/meta', function (Request $request, Response $respon $response_data['data'] = []; if ($add_file_meta) { - $response_data['data'] = flextype('media_files')->fetch($path); + $response_data['data'] = flextype('media_files')->fetchSingle($path); } // Set response code @@ -842,7 +842,7 @@ flextype()->delete('/api/files/meta', function (Request $request, Response $resp $response_data['data'] = []; if ($delete_file_meta) { - $response_data['data'] = flextype('media_files')->fetch($path); + $response_data['data'] = flextype('media_files')->fetchSingle($path); } // Set response code diff --git a/src/flextype/Foundation/Media/MediaFiles.php b/src/flextype/Foundation/Media/MediaFiles.php index e4ff8c2e..d6458690 100644 --- a/src/flextype/Foundation/Media/MediaFiles.php +++ b/src/flextype/Foundation/Media/MediaFiles.php @@ -9,12 +9,16 @@ declare(strict_types=1); namespace Flextype\Foundation\Media; + +use Atomastic\Arrays\Arrays; use ErrorException; use Intervention\Image\ImageManagerStatic as Image; use RuntimeException; use Slim\Http\Environment; use Slim\Http\Uri; +use function arrays; +use function filter; use function basename; use function chmod; use function exif_read_data; @@ -200,32 +204,14 @@ class MediaFiles } /** - * Fetch file(s) + * Fetch single file. * - * @param string $directory The directory to list. + * @param string $path The path to file. + * @param array $options Options array. * - * @return array A list of file(s) metadata. + * @access public */ - public function fetch(string $path): array - { - // Get list if file or files for specific folder - if (is_dir($path)) { - $files = $this->fetchCollection($path); - } else { - $files = $this->fetchSingle($path); - } - - return $files; - } - - /** - * Fetch single file - * - * @param string $path The path to file. - * - * @return array A file metadata. - */ - public function fetchSingle(string $path): array + public function fetchSingle(string $path, array $options = []): Arrays { $result = []; @@ -248,17 +234,20 @@ class MediaFiles $result['full_url'] = $full_url . '/project/uploads/' . $path; } - return $result; + $result = filter($result, $options); + + return arrays($result); } /** - * Fetch files collection + * Fetch files collection. * - * @param string $path The path to files collection. + * @param string $path Unique identifier of the files collecton. + * @param array $options Options array. * - * @return array A list of files metadata. + * @access public */ - public function fetchCollection(string $path): array + public function fetchCollection(string $path, array $options = []): Arrays { $result = []; @@ -281,7 +270,9 @@ class MediaFiles $result[$basename]['full_url'] = $full_url . '/project/uploads/' . $path . '/' . $basename; } - return $result; + $result = filter($result, $options); + + return arrays($result); } /**