0.0.2
Make sure you have already installed laravel >= 5.2.*
Then, install package via composer
composer require "smadia/laravel-google-drive-connector:0.0.*"
Register Facade in config/app.php
'aliases' => [
.....
'LGD' => Smadia\LaravelGoogleDrive\Facades\LaravelGoogleDrive::class
.....
]
Register ServiceProvider in config/app.php
'providers' => [
.....
Smadia\LaravelGoogleDrive\Providers\LaravelGoogleDriveServiceProvider::class
.....
]
You can create your own Facade and ServiceProvider manually, and then register it.
Before using package, make sure you have added new disk in config/filesystem.php
'disks' => [
// ...
'googledrive' => [
'driver' => 'googledrive',
'clientId' => env('GOOGLE_DRIVE_CLIENT_ID'),
'clientSecret' => env('GOOGLE_DRIVE_CLIENT_SECRET'),
'refreshToken' => env('GOOGLE_DRIVE_REFRESH_TOKEN'),
'folderId' => env('GOOGLE_DRIVE_FOLDER_ID'),
],
// ...
],
Then, you should add new field in .env files
FILESYSTEM_CLOUD=googledrive GOOGLE_DRIVE_CLIENT_ID=xxx.apps.googleusercontent.com GOOGLE_DRIVE_CLIENT_SECRET=xxx GOOGLE_DRIVE_REFRESH_TOKEN=xxx GOOGLE_DRIVE_FOLDER_ID=null
How do I get the Google Drive API ? Follow these links :
You can use LGD Facade in non-object file, such as routes/web.php, blade file. Below is the example usage in non-object file
LGD::dir('mydir')->ls()->toObject();
If you are using the Facade in object context, you should use the Facade in Smadia\LaravelGoogleDrive\Facades\LaravelGoogleDrive after the namespace declaration like below
<?php namespace App\User; // other use declaration use Smadia\LaravelGoogleDrive\Facades\LaravelGoogleDrive; // other use declaration
After that, you can use LaravelGoogleDrive facade instead than LGD like below
LaravelGoogleDrive::ls()->toArray()
To use it only with LGD keyword, you can use as in use declaration like code below
<?php namespace App\User; // other use declaration use Smadia\LaravelGoogleDrive\Facades\LaravelGoogleDrive as LGD; // other use declaration
LGD::ls()->toArray()
LGD::dir('mydir')->ls()->toArray()
Index is started from 0 (zero)
LGD::dir('mydir, 2)->ls()->toArray()
LGD::dir('mydir')->file('youfile', 'txt')->name
LGD::dir('mydir')->file('yourfile', 'txt', 2)->name
Below is the list of file's property which you can get by adding ->(property) after file() method
- name
- extension
- path
- dirname
- basename
- size
- timestamp
- mimetype (only for file type)
- type
To filter the list contents of directory, you can use filter() method
LGD::ls(function($ls) {
return $ls->where('type', '=', 'dir'); // another option of 'type' is 'file'
})->toArray();
These are some example to store file in Google Drive
LGD::put('hello.txt', 'Hello World !');
If you want store the uploaded file in your controller, you can use this one
LGD::put('hello.txt', file_get_contents($request->file('file')->getRealPath()));
If you want the name is same with the orginal uploaded file, you can assign the $request->file() as parameter
LGD::put($request->file('file'));
Below is optional method to your directory
| Method | Usage | Description |
|---|---|---|
ls($filter = null) |
Get the list contents of the current directory | This method will return the Smadia/GoogleDriveLaravel/ListContent Class. $filter is a closure which return the $filter itself |
delete() |
Delete the current directory | - |
rename($newname) |
Rename the current directory | This method will return back the DirectoryHandler Class |
put($filename, $contents = null) |
Put a new file to the current directory | $contents can be null if the $filename is instance of FileHandler class. This method will return the FileHandler class of the created file. $filename can assign with UploadedFile class ($request->file() in Laravel) |
file($filename, $extension, $index = 0) |
Get the specified file | This method will return the FileHandler class |
isExist() |
Check is the directory exist | Return boolean type |
mkdir($dirname) |
Make a new directory inside the current directory | This method will return the DirectoryHandler class of the created directory |
dir($dirname, $index = 0) |
Go to specified directory | This method will return the DirectoryHandler class of the requested directory with certain index (default is 0) |
parent() |
Get the parent directory | This method will return the DirectoryHandler class |
| Method | Usage | Description |
|---|---|---|
rename($newname) |
Rename the current file | This method will return the same FileHandler class |
delete() |
Delete the current file | This method will not delete all files with the same name in the same level of directory |
isExist() |
Check whether the file is exist | - |
getDir() |
Get the directory where the file is located | This method will return the DirectoryHandler class |
show() |
Show the file in browser. Only work for jpeg, jpg, png, gif, and svg file extension | - |
download() |
Give a download response | - |
| Method | Usage | Description |
|---|---|---|
toArray() |
Get list contents as array | - |
toCollect() |
Get list contents as Laravel's collection | - |
toObject() |
Get list contents as array which contains FileHandler or DirectoryHandler as it's member | - |
dir($dirname, $index = 0) |
Get the directory where the file is located | This method will return the DirectoryHandler class |
file($filename, $extension, $index = 0) |
Get the specific file name and extension | This method will return the FileHandler class |
filter($filter) |
Get the directory where the file is located | This method will return the DirectoryHandler class. $filter is a closure which return the $filter itself |
getAllProperties() |
Get all properties of a file or directory | This method will return all properties in array |