tomshaw/laravel-dropbox

A Laravel Dropbox API client library.

v1.1.0 2024-04-18 15:00 UTC

README

A Laravel Dropbox API 2.0 client library.

GitHub Workflow Status issues forks stars GitHub license

Features

  • Customizable Token Storage: User definable token storage adapters to suit your apps needs providing flexibility in how and where you store your tokens.
  • Token Refresh Middleware: Automatically handles token refreshing, ensuring your application maintains a valid API connection without manual intervention.
  • Laravel Facades Integration: Built using Laravel Facades offering a familiar and simple interface that promotes readability, flexibility, testing and ease of use.

Installation

You can install the package via composer:

composer require tomshaw/laravel-dropbox

Next publish the configuration file:

php artisan vendor:publish --provider="TomShaw\Dropbox\Providers\DropboxServiceProvider" --tag=config

Run the migration if you wish to use database storage adapter:

php artisan migrate

Configuration

Here's a breakdown of each configuration option:

The following variables should be set in your .env file

  • DROPBOX_CLIENT_ID: The client ID for your Dropbox application.

  • DROPBOX_CLIENT_SECRET: The client secret for your Dropbox application.

  • DROPBOX_REDIRECT_URI: The URI to redirect to after Dropbox authentication.

  • DROPBOX_ACCESS_TOKEN: This is the access token for Dropbox API requests.

  • DROPBOX_ACCESS_TYPE: This is the access type for the Dropbox application.

  • DROPBOX_ACCESS_SCOPES: If omitted will request all scopes selected on the Permissions tab.

Developers should review the Dropbox Developer Platform and SDK Documentation for further information.

Basic Usage

Below is a cursory explanation of this repository's usage. Please refer to the appropriate Facade Resource for additional methods and usage.

Verify your apps credentials utilizing the check accessor app method.

namespace App\Http\Controllers;

use TomShaw\Dropbox\Dropbox;
use TomShaw\Dropbox\DropboxClient;

class DropboxController extends Controller
{
    public function check()
    {
        Dropbox::check()->app(['query' => 'foo']);
    }
}

Authorizing the application and persisting the token.

namespace App\Http\Controllers;

use TomShaw\Dropbox\Dropbox;

class DropboxController extends Controller
{
    public function connect()
    {
        if (request()->has('code')) {

            Dropbox::connect(request('code'));

            return redirect()->route('dashboard');
        }

        return redirect()->away(Dropbox::getAuthUrl());
    }
}

Revoking access using the revoke accessor.

namespace App\Http\Controllers;

use TomShaw\Dropbox\Dropbox;

class DropboxController extends Controller
{
    public function revoke()
    {
        Dropbox::revoke();

        return redirect()->route('dashboard');
    }
}

Requesting account information using the users accessor.

namespace App\Http\Controllers;

use TomShaw\Dropbox\Dropbox;

class DropboxController extends Controller
{
    public function account()
    {
        Dropbox::users()->getCurrentAccount();
    }
}

Creating folders using the files accessor.

namespace App\Http\Controllers;

use TomShaw\Dropbox\Dropbox;

class DropboxController extends Controller
{
    public function createfolder(string $path)
    {
        Dropbox::files()->createFolder($path, true);
    }
}

Downloading files using the files accessor.

namespace App\Http\Controllers;

use TomShaw\Dropbox\Dropbox;

class DropboxController extends Controller
{
    public function download($id)
    {
        $item = Dropbox::files()->getMetadata($id);

        if ($item) {
            $fileContents = Dropbox::files()->download($item['path_lower']);

            return response($fileContents, 200, [
                'Content-Type' => 'application/octet-stream',
                'Content-Disposition' => 'attachment; filename="' . $item['name'] . '"',
            ]);
        }

        return abort(404);
    }
}

Uploading files using the files accessor.

namespace App\Http\Controllers;

use TomShaw\Dropbox\Dropbox;

class DropboxController extends Controller
{
    public function upload(string $path)
    {
        Dropbox::files($client)->upload($destinationPath, $sourceFilePath, mode: 'add', autorename: false, mute: false, strictConflict: false);
    }
}

Sharing a link using the sharing accessor.

namespace App\Http\Controllers;

use TomShaw\Dropbox\Dropbox;
use TomShaw\Dropbox\DropboxClient;

class DropboxController extends Controller
{
    public function sharelink(string $path)
    {
        Dropbox::sharing()->createSharedLinkWithSettings($path, ['requested_visibility' => 'public']);
    }
}

Middleware

Add the included Dropbox middleware to any routes that require API access.

Route::group(['middleware' => ['web', 'auth', 'dropbox']], function () {
  /* Grouped routes */
});

Requirements

The package is compatible with PHP 8 or later.

Contributing

Please see CONTRIBUTING for details.

Changelog

For changes made to the project, see the Changelog.

License

The MIT License (MIT). See License File for more information.