1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-08 22:26:46 +02:00

Standardize Media Folders API fetch. #488

BREAKING CHANGES

method fetch() removed and instead of it should be used fetchSingle() and fetchCollection()
methods fetchSingle() and fetchCollection() return an instance of Atomastic Arrays.
This commit is contained in:
Awilum
2020-12-04 20:52:32 +03:00
parent fc61551b22
commit e687a1898f
2 changed files with 28 additions and 37 deletions

View File

@@ -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

View File

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