From 3830d4af3382bb4856a4cb7fdd98f684e31d5b06 Mon Sep 17 00:00:00 2001 From: yaza Date: Thu, 1 Dec 2022 17:15:12 +0800 Subject: [PATCH 01/11] add merge config filesystem --- config/filesystems.php | 14 -------------- config/google-drive-storage.php | 17 ++++++++++------- ...LaravelGoogleDriveStorageServiceProvider.php | 5 +++++ 3 files changed, 15 insertions(+), 21 deletions(-) delete mode 100644 config/filesystems.php diff --git a/config/filesystems.php b/config/filesystems.php deleted file mode 100644 index 1920b94..0000000 --- a/config/filesystems.php +++ /dev/null @@ -1,14 +0,0 @@ - [ - 'google' => [ - 'driver' => 'google', - 'clientId' => env('GOOGLE_DRIVE_CLIENT_ID'), - 'clientSecret' => env('GOOGLE_DRIVE_CLIENT_SECRET'), - 'refreshToken' => env('GOOGLE_DRIVE_REFRESH_TOKEN'), - 'folder' => env('GOOGLE_DRIVE_FOLDER_ID'), // without folder is root of drive or team drive - //'teamDriveId' => env('GOOGLE_DRIVE_TEAM_DRIVE_ID'), - ], - ], -]; diff --git a/config/google-drive-storage.php b/config/google-drive-storage.php index 91765bd..1920b94 100644 --- a/config/google-drive-storage.php +++ b/config/google-drive-storage.php @@ -1,11 +1,14 @@ [ - 'driver' => 'google', - 'client_id' => env('GOOGLE_DRIVE_CLIENT_ID'), - 'client_secret' => env('GOOGLE_DRIVE_CLIENT_SECRET'), - 'refresh_token' => env('GOOGLE_DRIVE_REFRESH_TOKEN'), - 'folder_id' => env('GOOGLE_DRIVE_FOLDER_ID') - ] + 'disks' => [ + 'google' => [ + 'driver' => 'google', + 'clientId' => env('GOOGLE_DRIVE_CLIENT_ID'), + 'clientSecret' => env('GOOGLE_DRIVE_CLIENT_SECRET'), + 'refreshToken' => env('GOOGLE_DRIVE_REFRESH_TOKEN'), + 'folder' => env('GOOGLE_DRIVE_FOLDER_ID'), // without folder is root of drive or team drive + //'teamDriveId' => env('GOOGLE_DRIVE_TEAM_DRIVE_ID'), + ], + ], ]; diff --git a/src/LaravelGoogleDriveStorageServiceProvider.php b/src/LaravelGoogleDriveStorageServiceProvider.php index 4edcbc9..c8cbce1 100644 --- a/src/LaravelGoogleDriveStorageServiceProvider.php +++ b/src/LaravelGoogleDriveStorageServiceProvider.php @@ -29,6 +29,11 @@ public function configurePackage(Package $package): void public function bootingPackage() { try { + if (count(@config('filesystems.disks.google')) == 0) { + $this->mergeConfigFrom( + __DIR__.'/config/google-drive-storage.php', 'filesystems' + ); + } Storage::extend('google', function($app, $config) { $options = []; From ad587f2d639ffcf0f3375439a591cb9bd7f51f16 Mon Sep 17 00:00:00 2001 From: yaza-putu Date: Thu, 1 Dec 2022 09:15:43 +0000 Subject: [PATCH 02/11] Fix styling --- src/LaravelGoogleDriveStorageServiceProvider.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/LaravelGoogleDriveStorageServiceProvider.php b/src/LaravelGoogleDriveStorageServiceProvider.php index c8cbce1..5643da2 100644 --- a/src/LaravelGoogleDriveStorageServiceProvider.php +++ b/src/LaravelGoogleDriveStorageServiceProvider.php @@ -3,11 +3,10 @@ namespace Yaza\LaravelGoogleDriveStorage; use Illuminate\Support\Facades\Log; +use Illuminate\Support\Facades\Storage; use Spatie\LaravelPackageTools\Package; use Spatie\LaravelPackageTools\PackageServiceProvider; use Yaza\LaravelGoogleDriveStorage\Commands\LaravelGoogleDriveStorageCommand; -use Illuminate\Support\Facades\Storage; -use Illuminate\Support\ServiceProvider; class LaravelGoogleDriveStorageServiceProvider extends PackageServiceProvider { @@ -34,10 +33,10 @@ public function bootingPackage() __DIR__.'/config/google-drive-storage.php', 'filesystems' ); } - Storage::extend('google', function($app, $config) { + Storage::extend('google', function ($app, $config) { $options = []; - if (!empty($config['teamDriveId'] ?? null)) { + if (! empty($config['teamDriveId'] ?? null)) { $options['teamDriveId'] = $config['teamDriveId']; } From b1ee81e1321221ae1eaad2ed46f6dd4e77b5df9f Mon Sep 17 00:00:00 2001 From: yaza Date: Thu, 1 Dec 2022 22:38:17 +0800 Subject: [PATCH 03/11] fix config directory --- src/LaravelGoogleDriveStorageServiceProvider.php | 2 +- {config => src/config}/google-drive-storage.php | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename {config => src/config}/google-drive-storage.php (100%) diff --git a/src/LaravelGoogleDriveStorageServiceProvider.php b/src/LaravelGoogleDriveStorageServiceProvider.php index c8cbce1..2ce67cb 100644 --- a/src/LaravelGoogleDriveStorageServiceProvider.php +++ b/src/LaravelGoogleDriveStorageServiceProvider.php @@ -29,7 +29,7 @@ public function configurePackage(Package $package): void public function bootingPackage() { try { - if (count(@config('filesystems.disks.google')) == 0) { + if (@config('filesystems.disks.google') == null || count(@config('filesystems.disks.google')) == 0) { $this->mergeConfigFrom( __DIR__.'/config/google-drive-storage.php', 'filesystems' ); diff --git a/config/google-drive-storage.php b/src/config/google-drive-storage.php similarity index 100% rename from config/google-drive-storage.php rename to src/config/google-drive-storage.php From 4c1149bd735c361558ac9bf7296c76cd3d600649 Mon Sep 17 00:00:00 2001 From: yaza Date: Thu, 1 Dec 2022 22:52:55 +0800 Subject: [PATCH 04/11] restore config folder --- {src/config => config}/google-drive-storage.php | 0 src/LaravelGoogleDriveStorageServiceProvider.php | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename {src/config => config}/google-drive-storage.php (100%) diff --git a/src/config/google-drive-storage.php b/config/google-drive-storage.php similarity index 100% rename from src/config/google-drive-storage.php rename to config/google-drive-storage.php diff --git a/src/LaravelGoogleDriveStorageServiceProvider.php b/src/LaravelGoogleDriveStorageServiceProvider.php index 5bdbc16..72669b9 100644 --- a/src/LaravelGoogleDriveStorageServiceProvider.php +++ b/src/LaravelGoogleDriveStorageServiceProvider.php @@ -30,7 +30,7 @@ public function bootingPackage() try { if (@config('filesystems.disks.google') == null || count(@config('filesystems.disks.google')) == 0) { $this->mergeConfigFrom( - __DIR__.'/config/google-drive-storage.php', 'filesystems' + __DIR__.'/../config/google-drive-storage.php', 'filesystems.disk' ); } Storage::extend('google', function ($app, $config) { From a56859f4b0442fe9451a4d861d3c9860fb0e1684 Mon Sep 17 00:00:00 2001 From: yaza Date: Fri, 2 Dec 2022 10:21:57 +0800 Subject: [PATCH 05/11] add command --- config/google-drive-storage.php | 13 +-------- .../LaravelGoogleDriveStorageCommand.php | 28 ++++++++++++++++--- ...ravelGoogleDriveStorageServiceProvider.php | 5 ---- 3 files changed, 25 insertions(+), 21 deletions(-) diff --git a/config/google-drive-storage.php b/config/google-drive-storage.php index 1920b94..0b67a5f 100644 --- a/config/google-drive-storage.php +++ b/config/google-drive-storage.php @@ -1,14 +1,3 @@ [ - 'google' => [ - 'driver' => 'google', - 'clientId' => env('GOOGLE_DRIVE_CLIENT_ID'), - 'clientSecret' => env('GOOGLE_DRIVE_CLIENT_SECRET'), - 'refreshToken' => env('GOOGLE_DRIVE_REFRESH_TOKEN'), - 'folder' => env('GOOGLE_DRIVE_FOLDER_ID'), // without folder is root of drive or team drive - //'teamDriveId' => env('GOOGLE_DRIVE_TEAM_DRIVE_ID'), - ], - ], -]; +return []; diff --git a/src/Commands/LaravelGoogleDriveStorageCommand.php b/src/Commands/LaravelGoogleDriveStorageCommand.php index fd9a446..95a302e 100644 --- a/src/Commands/LaravelGoogleDriveStorageCommand.php +++ b/src/Commands/LaravelGoogleDriveStorageCommand.php @@ -6,14 +6,34 @@ class LaravelGoogleDriveStorageCommand extends Command { - public $signature = 'laravel-google-drive-storage'; + public $signature = ' + gdrive:config {name : The name of command} + '; - public $description = 'My command'; + public $description = 'Publish Config Gdrive'; public function handle(): int { - $this->comment('All done'); + if ($this->argument("name") == "publish") { + try { + $config = config('filesystems.disk'); + array_push($config, [ + 'google' => [ + 'driver' => 'google', + 'clientId' => env('GOOGLE_DRIVE_CLIENT_ID'), + 'clientSecret' => env('GOOGLE_DRIVE_CLIENT_SECRET'), + 'refreshToken' => env('GOOGLE_DRIVE_REFRESH_TOKEN'), + 'folder' => env('GOOGLE_DRIVE_FOLDER_ID'), // without folder is root of drive or team drive + //'teamDriveId' => env('GOOGLE_DRIVE_TEAM_DRIVE_ID'), + ] + ]); - return self::SUCCESS; + return self::SUCCESS; + } catch (\Exception $exception) { + return self::FAILURE; + } + } else { + $this->comment("Mungkin yg dimagsud adalah `publish`"); + } } } diff --git a/src/LaravelGoogleDriveStorageServiceProvider.php b/src/LaravelGoogleDriveStorageServiceProvider.php index 72669b9..01fa6dd 100644 --- a/src/LaravelGoogleDriveStorageServiceProvider.php +++ b/src/LaravelGoogleDriveStorageServiceProvider.php @@ -28,11 +28,6 @@ public function configurePackage(Package $package): void public function bootingPackage() { try { - if (@config('filesystems.disks.google') == null || count(@config('filesystems.disks.google')) == 0) { - $this->mergeConfigFrom( - __DIR__.'/../config/google-drive-storage.php', 'filesystems.disk' - ); - } Storage::extend('google', function ($app, $config) { $options = []; From bd27376e3000eb3431b14c2a7cdc3019bbc81daa Mon Sep 17 00:00:00 2001 From: yaza-putu Date: Fri, 2 Dec 2022 02:22:30 +0000 Subject: [PATCH 06/11] Fix styling --- .../LaravelGoogleDriveStorageCommand.php | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/Commands/LaravelGoogleDriveStorageCommand.php b/src/Commands/LaravelGoogleDriveStorageCommand.php index 95a302e..5a865e4 100644 --- a/src/Commands/LaravelGoogleDriveStorageCommand.php +++ b/src/Commands/LaravelGoogleDriveStorageCommand.php @@ -14,26 +14,26 @@ class LaravelGoogleDriveStorageCommand extends Command public function handle(): int { - if ($this->argument("name") == "publish") { + if ($this->argument('name') == 'publish') { try { - $config = config('filesystems.disk'); - array_push($config, [ - 'google' => [ - 'driver' => 'google', - 'clientId' => env('GOOGLE_DRIVE_CLIENT_ID'), - 'clientSecret' => env('GOOGLE_DRIVE_CLIENT_SECRET'), - 'refreshToken' => env('GOOGLE_DRIVE_REFRESH_TOKEN'), - 'folder' => env('GOOGLE_DRIVE_FOLDER_ID'), // without folder is root of drive or team drive - //'teamDriveId' => env('GOOGLE_DRIVE_TEAM_DRIVE_ID'), - ] - ]); + $config = config('filesystems.disk'); + array_push($config, [ + 'google' => [ + 'driver' => 'google', + 'clientId' => env('GOOGLE_DRIVE_CLIENT_ID'), + 'clientSecret' => env('GOOGLE_DRIVE_CLIENT_SECRET'), + 'refreshToken' => env('GOOGLE_DRIVE_REFRESH_TOKEN'), + 'folder' => env('GOOGLE_DRIVE_FOLDER_ID'), // without folder is root of drive or team drive + //'teamDriveId' => env('GOOGLE_DRIVE_TEAM_DRIVE_ID'), + ], + ]); return self::SUCCESS; } catch (\Exception $exception) { return self::FAILURE; } } else { - $this->comment("Mungkin yg dimagsud adalah `publish`"); + $this->comment('Mungkin yg dimagsud adalah `publish`'); } } } From c85f7c2e232129780c429cb4916a265070f98e4f Mon Sep 17 00:00:00 2001 From: yaza Date: Fri, 2 Dec 2022 11:04:09 +0800 Subject: [PATCH 07/11] add filesystem on service provider --- .../LaravelGoogleDriveStorageCommand.php | 24 ++++--------------- ...ravelGoogleDriveStorageServiceProvider.php | 9 +++++++ 2 files changed, 13 insertions(+), 20 deletions(-) diff --git a/src/Commands/LaravelGoogleDriveStorageCommand.php b/src/Commands/LaravelGoogleDriveStorageCommand.php index 5a865e4..7440bd0 100644 --- a/src/Commands/LaravelGoogleDriveStorageCommand.php +++ b/src/Commands/LaravelGoogleDriveStorageCommand.php @@ -14,26 +14,10 @@ class LaravelGoogleDriveStorageCommand extends Command public function handle(): int { - if ($this->argument('name') == 'publish') { - try { - $config = config('filesystems.disk'); - array_push($config, [ - 'google' => [ - 'driver' => 'google', - 'clientId' => env('GOOGLE_DRIVE_CLIENT_ID'), - 'clientSecret' => env('GOOGLE_DRIVE_CLIENT_SECRET'), - 'refreshToken' => env('GOOGLE_DRIVE_REFRESH_TOKEN'), - 'folder' => env('GOOGLE_DRIVE_FOLDER_ID'), // without folder is root of drive or team drive - //'teamDriveId' => env('GOOGLE_DRIVE_TEAM_DRIVE_ID'), - ], - ]); - - return self::SUCCESS; - } catch (\Exception $exception) { - return self::FAILURE; - } - } else { - $this->comment('Mungkin yg dimagsud adalah `publish`'); + try { + return self::SUCCESS; + } catch (\Exception $exception) { + return self::FAILURE; } } } diff --git a/src/LaravelGoogleDriveStorageServiceProvider.php b/src/LaravelGoogleDriveStorageServiceProvider.php index 01fa6dd..7057a59 100644 --- a/src/LaravelGoogleDriveStorageServiceProvider.php +++ b/src/LaravelGoogleDriveStorageServiceProvider.php @@ -28,6 +28,15 @@ public function configurePackage(Package $package): void public function bootingPackage() { try { + app()->config['filesystems.disks.google'] = [ + 'driver' => 'google', + 'clientId' => env('GOOGLE_DRIVE_CLIENT_ID'), + 'clientSecret' => env('GOOGLE_DRIVE_CLIENT_SECRET'), + 'refreshToken' => env('GOOGLE_DRIVE_REFRESH_TOKEN'), + 'folder' => env('GOOGLE_DRIVE_FOLDER_ID'), // without folder is root of drive or team drive + //'teamDriveId' => env('GOOGLE_DRIVE_TEAM_DRIVE_ID'), + ]; + Storage::extend('google', function ($app, $config) { $options = []; From 42b9de44cd3a71ef6cf9398d7a6e5ef417d973cc Mon Sep 17 00:00:00 2001 From: yaza Date: Fri, 2 Dec 2022 11:19:35 +0800 Subject: [PATCH 08/11] add readme md --- README.md | 66 ++++++------------- ...ravelGoogleDriveStorageServiceProvider.php | 2 +- 2 files changed, 21 insertions(+), 47 deletions(-) diff --git a/README.md b/README.md index 19cdb94..fe95af8 100644 --- a/README.md +++ b/README.md @@ -1,19 +1,6 @@ -# This is my package laravel-google-drive-storage +# laravel-google-drive-storage -[![Latest Version on Packagist](https://img.shields.io/packagist/v/yaza/laravel-google-drive-storage.svg?style=flat-square)](https://packagist.org/packages/yaza/laravel-google-drive-storage) -[![GitHub Tests Action Status](https://img.shields.io/github/workflow/status/yaza/laravel-google-drive-storage/run-tests?label=tests)](https://github.com/yaza/laravel-google-drive-storage/actions?query=workflow%3Arun-tests+branch%3Amain) -[![GitHub Code Style Action Status](https://img.shields.io/github/workflow/status/yaza/laravel-google-drive-storage/Fix%20PHP%20code%20style%20issues?label=code%20style)](https://github.com/yaza/laravel-google-drive-storage/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain) -[![Total Downloads](https://img.shields.io/packagist/dt/yaza/laravel-google-drive-storage.svg?style=flat-square)](https://packagist.org/packages/yaza/laravel-google-drive-storage) - -This is where your description should go. Limit it to a paragraph or two. Consider adding a small example. - -## Support us - -[](https://spatie.be/github-ad-click/laravel-google-drive-storage) - -We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us). - -We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards). +This package allow to store and get data from google drive like S3 AWS in laravel ## Installation @@ -23,43 +10,28 @@ You can install the package via composer: composer require yaza/laravel-google-drive-storage ``` -You can publish and run the migrations with: - -```bash -php artisan vendor:publish --tag="laravel-google-drive-storage-migrations" -php artisan migrate +copy to .env +```env +GOOGLE_DRIVE_CLIENT_ID=xxx.apps.googleusercontent.com +GOOGLE_DRIVE_CLIENT_SECRET=xxx +GOOGLE_DRIVE_REFRESH_TOKEN=xxx +GOOGLE_DRIVE_FOLDER= ``` - -You can publish the config file with: - -```bash -php artisan vendor:publish --tag="laravel-google-drive-storage-config" -``` - -This is the contents of the published config file: - -```php -return [ -]; -``` - -Optionally, you can publish the views using - -```bash -php artisan vendor:publish --tag="laravel-google-drive-storage-views" +example +```env +GOOGLE_DRIVE_CLIENT_ID=xxx.apps.googleusercontent.com +GOOGLE_DRIVE_CLIENT_SECRET=xxx +GOOGLE_DRIVE_REFRESH_TOKEN=xxx +GOOGLE_DRIVE_FOLDER=backups ``` +## Setup Google Keys + - [Getting your Client ID and Secret](https://github.com/ivanvermeyen/laravel-google-drive-demo/blob/master/README/1-getting-your-dlient-id-and-secret.md) + - [Getting your Refresh Token](https://github.com/ivanvermeyen/laravel-google-drive-demo/blob/master/README/2-getting-your-refresh-token.md) ## Usage ```php -$laravelGoogleDriveStorage = new Yaza\LaravelGoogleDriveStorage(); -echo $laravelGoogleDriveStorage->echoPhrase('Hello, Yaza!'); -``` - -## Testing - -```bash -composer test + Storage::disk('google') ``` ## Changelog @@ -79,6 +51,8 @@ Please review [our security policy](../../security/policy) on how to report secu - [yaza](https://github.com/yaza-putu) - [All Contributors](../../contributors) +Thanks to [Masbug](https://github.com/masbug/flysystem-google-drive-ext) + ## License The MIT License (MIT). Please see [License File](LICENSE.md) for more information. diff --git a/src/LaravelGoogleDriveStorageServiceProvider.php b/src/LaravelGoogleDriveStorageServiceProvider.php index 7057a59..2b59240 100644 --- a/src/LaravelGoogleDriveStorageServiceProvider.php +++ b/src/LaravelGoogleDriveStorageServiceProvider.php @@ -33,7 +33,7 @@ public function bootingPackage() 'clientId' => env('GOOGLE_DRIVE_CLIENT_ID'), 'clientSecret' => env('GOOGLE_DRIVE_CLIENT_SECRET'), 'refreshToken' => env('GOOGLE_DRIVE_REFRESH_TOKEN'), - 'folder' => env('GOOGLE_DRIVE_FOLDER_ID'), // without folder is root of drive or team drive + 'folder' => env('GOOGLE_DRIVE_FOLDER'), // without folder is root of drive or team drive //'teamDriveId' => env('GOOGLE_DRIVE_TEAM_DRIVE_ID'), ]; From 4de8bc957d32b7f4a32fd5a5cafba6b8447d300b Mon Sep 17 00:00:00 2001 From: yaza Date: Fri, 2 Dec 2022 11:55:43 +0800 Subject: [PATCH 09/11] change readme md --- README.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index fe95af8..0efe02d 100644 --- a/README.md +++ b/README.md @@ -29,11 +29,15 @@ GOOGLE_DRIVE_FOLDER=backups - [Getting your Client ID and Secret](https://github.com/ivanvermeyen/laravel-google-drive-demo/blob/master/README/1-getting-your-dlient-id-and-secret.md) - [Getting your Refresh Token](https://github.com/ivanvermeyen/laravel-google-drive-demo/blob/master/README/2-getting-your-refresh-token.md) ## Usage - +you can use storage driver function by laravel
+example : ```php - Storage::disk('google') + Storage::disk('google')->put($filename, File::get($filepath)); ``` +## Limitations +Using display paths as identifiers for folders and files requires them to be unique. Unfortunately Google Drive allows users to create files and folders with same (displayed) names. In such cases when unique path cannot be determined this adapter chooses the oldest (first) instance. In case the newer duplicate is a folder and user puts a unique file or folder inside the adapter will be able to reach it properly (because full path is unique). + ## Changelog Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. From 21f3c549bcded12d38000be943e7aaef69edb4d5 Mon Sep 17 00:00:00 2001 From: yaza Date: Fri, 2 Dec 2022 11:57:46 +0800 Subject: [PATCH 10/11] fixing readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0efe02d..5f6e2aa 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # laravel-google-drive-storage - +![gdrive](https://is4-ssl.mzstatic.com/image/thumb/Purple122/v4/d9/cb/a8/d9cba8b1-85a0-723a-3f03-bdc6b76476d5/logo_drive_2020q4_color-0-1x_U007emarketing-0-0-0-6-0-0-0-85-220.png/1200x630wa.png) This package allow to store and get data from google drive like S3 AWS in laravel ## Installation From 7f2e624b4ec0b2700ff4879bb4a1803a42051932 Mon Sep 17 00:00:00 2001 From: yaza Date: Fri, 2 Dec 2022 12:05:55 +0800 Subject: [PATCH 11/11] add readme md --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 5f6e2aa..412f0e3 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,10 @@ GOOGLE_DRIVE_REFRESH_TOKEN=xxx GOOGLE_DRIVE_FOLDER=backups ``` +## Support Laravel +- php 8.1 +- now only support laravel 9 + ## Setup Google Keys - [Getting your Client ID and Secret](https://github.com/ivanvermeyen/laravel-google-drive-demo/blob/master/README/1-getting-your-dlient-id-and-secret.md) - [Getting your Refresh Token](https://github.com/ivanvermeyen/laravel-google-drive-demo/blob/master/README/2-getting-your-refresh-token.md)