spatie / flysystem-dropbox

A flysystem driver for Dropbox that uses the v2 API
https://freek.dev/734-dropbox-will-turn-off-v1-of-their-api-soon-its-time-to-update-your-php-application
MIT License
342 stars 50 forks source link

DropboxAdapter::listContents() doesn't take "has_more" value into account #26

Closed joe-walker closed 6 years ago

joe-walker commented 6 years ago

Currently, DropboxAdapter::listContents() calls Client::listFolder(), does some processing on $result['entries'] if any are present, and returns the entries.

If I understand correctly, I think that this needs to also take into account the possibility that not all results are returned with the first request and instead $result['has_more'] may be TRUE, which means that Client::listFolderContinue($result['cursor']) should be called in a loop, collecting all entries until $result['has_more'] is FALSE.

The code for listContents() could be changed to something like this:

public function listContents($directory = '', $recursive = false): array
{
    $location = $this->applyPathPrefix($directory);

    $result = $this->client->listFolder($location, $recursive);
    $entries = $result['entries'];

    while ($result['has_more']) {
        $result = $this->client->listFolderContinue($result['cursor']);
        $entries = array_merge($entries, $result['entries']);
    }

    if (! count($entries)) {
        return [];
    }

    return array_map(function ($entry) {
        $path = $this->removePathPrefix($entry['path_display']);

        return $this->normalizeResponse($entry, $path);
    }, $entries);
}
freekmurze commented 6 years ago

I'd accept a fully tested PR that improves the function.