Skip to content

Commit

Permalink
Fix an issue with the file list pagination (#190)
Browse files Browse the repository at this point in the history
* Fix pagination

- add 'from' parameter to request;
- add 'getPageRequestParameters' method to generate a previous/next links

* Update changelog, update readme

Co-authored-by: Roman Sedykh <[email protected]>
  • Loading branch information
andrew72ru and rsedykh committed Mar 28, 2022
1 parent b320678 commit 6563d2d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ The format is based now on [Keep a
Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to
[Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [3.2.2]
### Fix for an issue with the file list pagination
- Added `getPageRequestParameters` method: use it to load the next page parameters from `$fileListResponse->getNext()`/`$fileListResponse->getPrevious()`.
- **Attention**: if you've implemented your own class for `FileApiInterface`, add `public getPageRequestParameters(?string $url): array` method to the implementation.

## [3.2.1]
### Secure delivery image processing fix
- Resolved issue with invalid signed urls being generated for image transformations.
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ After that, you can access to file operation methods:
$files = $page->getResults();
}
```
- `getPageRequestParameters(string | null $url)` — Get an array with next page request parameters. Use it to create links to the previous/next page of the list.
- `storeFile(string $id)` — Stores a single file by UUID. Returns the `Uploadcare\File` (`FileInfoInterface`).
Takes file UUID as an argument.
- `deleteFile(string $id)` — Removes individual files. Returns file info.
Expand Down
17 changes: 17 additions & 0 deletions src/Apis/FileApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@ final class FileApi extends AbstractApi implements FileApiInterface
{
public const UUID_REGEX = '[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89aAbB][a-f0-9]{3}-[a-f0-9]{12}';

public function getPageRequestParameters(?string $url): ?array
{
if ($url === null) {
return null;
}

$query = \parse_url($url, PHP_URL_QUERY);
if (!\is_string($query) || empty($query)) {
return null;
}
$parameters = null;
\parse_str($query, $parameters);

return $parameters;
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -67,6 +83,7 @@ public function listFiles(int $limit = 100, string $orderBy = 'datetime_uploaded
'ordering' => $orderBy,
'removed' => $removed,
'add_fields' => $addFields,
'from' => $from,
];
if (\is_bool($stored)) {
$parameters['stored'] = $stored;
Expand Down
9 changes: 9 additions & 0 deletions src/Interfaces/Api/FileApiInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@

interface FileApiInterface
{
/**
* Makes an array from next/previous url got from API. Use this method to generate links to next or previous pages.
*
* @param string|null $url
*
* @return array|null
*/
public function getPageRequestParameters(?string $url): ?array;

/**
* Get the next page from previous answer (if next page exists).
*
Expand Down

0 comments on commit 6563d2d

Please sign in to comment.