smadia/laravel-google-drive-connector

Connector for Laravel with Google Drive API

0.0.2 2017-10-29 13:55 UTC

This package is not auto-updated.

Last update: 2024-03-21 23:58:54 UTC


README

0.0.2

Installation

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.

Configuration

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 :

Usage

Usage in non-object file

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

Get list contents from your root directory

LGD::ls()->toArray()

Get the spesific directory

LGD::dir('mydir')->ls()->toArray()

Get the spesific index if you have two or more with the same name

Index is started from 0 (zero)

LGD::dir('mydir, 2)->ls()->toArray()

Get the spesific file in certain directory

LGD::dir('mydir')->file('youfile', 'txt')->name

You can add index to get the file (if you have two or more files with the same name)

LGD::dir('mydir')->file('yourfile', 'txt', 2)->name

Get other file's properties

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

Filter

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();

File Storing

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'));

Directory methods

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

File Methods

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 -

List Content Methods

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