Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

listContents returns empty array #94

Closed
levkp opened this issue Feb 2, 2023 · 3 comments
Closed

listContents returns empty array #94

levkp opened this issue Feb 2, 2023 · 3 comments

Comments

@levkp
Copy link
Contributor

levkp commented Feb 2, 2023

I migrated to Laravel 9 which uses Flysystem 3, making the nao-pon/flysystem-google-drive repository incompatible. In search of an alternative, I was happy to find out this one was built on his adapter.

First, I tried using it through the Storage facade:

$disk = \Storage::disk('google');
$dirs = $disk->allDirectories();

Returns an empty array, even though I have a couple directories in the root folder. The very same code works with Naoki Sawada's adapter, then the array is populated with the IDs of the directories.

I tried bypassing the facade: $content = $disk->getAdapter()->listContents('', true);, and I get the same.

My configuration is straight from your documentation, but I'm going to paste it here anyway.

fileysytems.php:

'google' => [
            'driver' => 'google',
            'clientId' => env('GOOGLE_DRIVE_CLIENT_ID'),
            'clientSecret' => env('GOOGLE_DRIVE_CLIENT_SECRET'),
            'refreshToken' => env('GOOGLE_DRIVE_REFRESH_TOKEN'),
            'folderId' => env('GOOGLE_DRIVE_FOLDER_ID'),
            'teamDriveId' => env('GOOGLE_DRIVE_TEAM_DRIVE_ID'),
        ],

All values are set and correct in .env.

In the boot() of GoogleDriveServiceProvider:

\Storage::extend('google', function($app, $config) {
            $options = [];

            if (!empty($config['teamDriveId'] ?? null)) {
                $options['teamDriveId'] = $config['teamDriveId'];
            }

            $client = new \Google\Client();
            $client->setClientId($config['clientId']);
            $client->setClientSecret($config['clientSecret']);
            $client->refreshToken($config['refreshToken']);

            $service = new \Google\Service\Drive($client);
            $adapter = new \Masbug\Flysystem\GoogleDriveAdapter($service, $config['folderId'] ?? '/', $options);
            $driver = new \League\Flysystem\Filesystem($adapter);

            return new \Illuminate\Filesystem\FilesystemAdapter($driver, $adapter);
        });

Am I missing something obvious? What else should I include?

@levkp
Copy link
Contributor Author

levkp commented Feb 2, 2023

I forgot that I'm not using display paths but IDs only. It works if I pass $options = ['useDisplayPaths' => false]; to the adapter when I extend Storage in Laravel. 🤦

@levkp levkp closed this as completed Feb 2, 2023
@levkp
Copy link
Contributor Author

levkp commented Feb 27, 2023

Since then I re-encountered this issue, because I started using display paths in my application.
First, I adjusted my configuration: $options = [ 'useDisplayPaths' => true ];

My Laravel code:

$disk = \Storage::disk('google');
$contents = $disk->listContents('/', true);

will return a Generator that yields nothing.

I tried $contentsArr = iterator_to_array($contents);, but unsurprisingly it will result in an empty array.

The curious thing is that when I set useDisplayPaths to false, the listing works and I see the folders and files that sit in the root directory. This makes me think the environment variables I set to access Google Drive are correct.

I use Laravel 9, PHP 8.2 and version 2.2 of this project.

@levkp levkp reopened this Feb 27, 2023
@parallels999
Copy link

parallels999 commented Mar 8, 2023

if you use 'useDisplayPaths' => false you have to set folderId, if you use 'useDisplayPaths' => true you have to set folderPathName
Look at this demo: laravel-google-drive-ext-demo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants