Skip to content

Commit

Permalink
Switch to Dropbox API 2.0 (kunalvarma05/dropbox-php-sdk)
Browse files Browse the repository at this point in the history
  • Loading branch information
VolCh authored and websviewer committed Oct 4, 2017
1 parent d384e2f commit ad2eefd
Show file tree
Hide file tree
Showing 5 changed files with 308 additions and 148 deletions.
43 changes: 15 additions & 28 deletions Fabric/ServiceFabric.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php

namespace Fabric;
use \Dropbox as dbx;

class ServiceFabric{

Expand All @@ -14,30 +13,23 @@ public static function auth($type, $code, $config) {

if ($code == 'code') {
switch ($type) {
case SELF::DROPBOX:
case self::DROPBOX:
$data = \HttpReceiver\HttpReceiver::get('userId','int');
return \UploadModels\DropBoxModel::auth($data,$config);
break;
case SELF::GOOGLEDRIVE:
case self::GOOGLEDRIVE:
$data = \HttpReceiver\HttpReceiver::get('userId','int');
return \UploadModels\GoogleDriveModel::auth($data,$config);
break;
case SELF::BOX:
case self::BOX:
$data = \HttpReceiver\HttpReceiver::get('userId','int');
return \UploadModels\BoxModel::auth($data,$config);
break;
case SELF::ONEDRIVE:
case self::ONEDRIVE:
$data = \HttpReceiver\HttpReceiver::get('userId','int');
return \UploadModels\OneDriveModel::auth($data,$config);
break;
}
}elseif($code == 'access_token'){
$result = self::getToken($type, $config);
$data = array('service' => $type, 'token_data' => $result);
return $data;

return array('service' => $type, 'token_data' => $result);
}

}


Expand All @@ -50,19 +42,17 @@ public static function uploadFile($type, $access_token, $uploadFile, $fileName,
}

switch($type){
case SELF::DROPBOX:
case self::DROPBOX:
if(!isset($access_token)) {
return array('status' => 'error', 'msg' => 'deniedByUser');
}
$result = array();
try {
$result = \UploadModels\DropBoxModel::uploadFile($access_token, $uploadFile, $fileName, $config);
}catch (dbx\Exception $e){
}catch (\Exception $e){
$result = array('status' => 'error', 'msg' => 'Cloud Error');
}
break;
case SELF::GOOGLEDRIVE:
$result = array();
case self::GOOGLEDRIVE:
if(!isset($access_token)) {
return array('status' => 'error', 'msg' => 'deniedByUser');
}
Expand All @@ -73,22 +63,20 @@ public static function uploadFile($type, $access_token, $uploadFile, $fileName,
$result = array('status' => 'error', 'msg' => 'Cloud Error');
}
break;
case SELF::BOX:
case self::BOX:
if(!isset($access_token)) {
return array('status' => 'error', 'msg' => 'deniedByUser');
}
$result = array();
try {
$result = \UploadModels\BoxModel::uploadFile($access_token, $uploadFile, $fileName, $config);
} catch(\Exception $e){
$result = array('status' => 'error', 'msg' => 'Cloud Error');
}
break;
case SELF::ONEDRIVE:
case self::ONEDRIVE:
if(!isset($access_token)) {
return array('status' => 'error', 'msg' => 'deniedByUser');
}
$result = array();
try {
$result = \UploadModels\OneDriveModel::uploadFile($access_token, $uploadFile, $fileName, $config);
} catch(\Exception $e){
Expand All @@ -103,20 +91,19 @@ public static function getToken($type, $config){

$result = '';
switch($type){
case SELF::DROPBOX:
case self::DROPBOX:
$result = \UploadModels\DropBoxModel::getToken($config);
break;
case SELF::GOOGLEDRIVE:
case self::GOOGLEDRIVE:
$result = \UploadModels\GoogleDriveModel::getToken($config);
break;
case SELF::BOX:
case self::BOX:
$result = \UploadModels\BoxModel::getToken($config);
break;
case SELF::ONEDRIVE:
case self::ONEDRIVE:
$result = \UploadModels\OneDriveModel::getToken($config);
break;
}
return $result;
}

}
}
237 changes: 178 additions & 59 deletions UploadModels/DropBoxModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,87 +2,206 @@

namespace UploadModels;

use \Dropbox as dbx;
use Dropbox\Exception;
use HttpReceiver\HttpRecieiver;


class DropBoxModel implements \Interfaces\UploadServiceInterface{
use Exception;
use HttpReceiver\HttpReceiver;
use Interfaces\UploadServiceInterface;
use Kunnu\Dropbox\Dropbox;
use Kunnu\Dropbox\DropboxApp;
use Kunnu\Dropbox\Exceptions\DropboxClientException;

class DropBoxModel implements UploadServiceInterface
{
const CONFIG_CLIENT_ID = 'DROPBOX_KEY';
const CONFIG_CLIENT_SECRET = 'DROPBOX_SECRET';
const CONFIG_REDIRECT_URL = 'DROPBOX_REDIRECT_URI';

/**
* @var DropboxApp
*/
protected $app;

/**
* @var Dropbox
*/
private $service;
/**
* @var string
*/
protected $redirectUrl;
/**
* @var string
*/
private $clientId;
/**
* @var string
*/
private $clientSecret;
/**
* @var null|string
*/
private $accessToken;

/**
* DropBoxModel constructor.
* @param string $clientId
* @param string $clientSecret
* @param string $redirectUrl
* @param string|null $accessToken
*/
private function __construct($clientId, $clientSecret, $redirectUrl, $accessToken = null)
{
$this->clientId = $clientId;
$this->clientSecret = $clientSecret;
$this->redirectUrl = $redirectUrl;
$this->accessToken = $accessToken;
}

public static function auth($state, $config) {
private function init(array $serviceConfig = []) {
$this->app = new DropboxApp($this->clientId, $this->clientSecret, $this->accessToken);
$this->service = new Dropbox($this->app, $serviceConfig);
}

$authorizeUrl = self::getDropBoxAuth($config)->start(\HttpReceiver\HttpReceiver::get('userId','string'));
return $authorizeUrl;
/**
* @param int $userId
* @param array $config
* @return string
*/
public static function auth($userId, $config)
{
return self::create($config)->getAuthUrl($userId);
}

public static function getToken($config) {
try {
list($accessToken, $userId, $urlState) = self::getDropBoxAuth($config)->finish($_GET);
if (isset($accessToken) && strlen($accessToken) > 0) {
return $accessToken;
}
}catch(dbx\Exception_BadRequest $e){
/**
* @param array $config
* @return string
*/
public static function getToken($config)
{
if (!isset($_GET['code']) || !isset($_GET['state'])) {
return '';
}
$code = $_GET['code'];
$state = $_GET['state'];

try {
return self::create($config)->getAccessToken($code, $state);
} catch (DropboxClientException $exception) {
return '';
}
return '';
}


public static function uploadFile($access_token, $uploadFile, $fileName, $config) {
if(!isset($access_token)){
return array('status' => 'error', 'msg' => 'refreshToken', 'url' => self::auth(\HttpReceiver\HttpReceiver::get('userId','int'), $config));
/**
* @param string $access_token
* @param string $uploadFile
* @param string $fileName
* @param array $config
* @return array
*/
public static function uploadFile($access_token, $uploadFile, $fileName, $config)
{
if (empty($access_token)) {
return [
'status' => 'error',
'msg' => 'refreshToken',
'url' => self::auth(HttpReceiver::get('userId', 'int'), $config)
];
}

if(!file_exists($uploadFile)) {
return array('status' => 'error', 'fileNotExist');
if (!file_exists($uploadFile)) {
return ['status' => 'error', 'fileNotExist'];
}


$dbxClient = new dbx\Client($access_token, "PHP-Example/1.0");


$f = fopen($uploadFile, "rb");
try {
if (!isset($fileName) || strlen($fileName) == 0 || $fileName == '0') {
$tmp = explode('/', $uploadFile);
$fileName = $tmp[sizeof($tmp) - 1];
}else{
$fileName .= '.'.self::getExtension($uploadFile);
}

$result = $dbxClient->uploadFile("/".$config['SAVE_FOLDER']."/". $fileName, dbx\WriteMode::add(), $f);
}catch(Exception $e){
return array('status' => 'error', 'msg' => 'refreshToken', 'url' => self::auth(\HttpReceiver\HttpReceiver::get('userId','int'), $config));
if (empty($fileName) && $fileName !== '0') {
$remoteFilename = self::generateRemoteFilename($uploadFile);
} else {
$remoteFilename = $fileName .'.' . self::getExtension($uploadFile);
}
$remotePath = "/" . $config['SAVE_FOLDER'] . "/" . $remoteFilename;

fclose($f);
if(!isset($result) || !isset($result['size'])){
return array('status' => 'error', 'msg' => 'refreshToken');
}else {
return array('status' => 'ok');
try {
self::create($config, $access_token)->upload($uploadFile, $remotePath);
return ['status' => 'ok'];
} catch (Exception $e) {
return [
'status' => 'error',
'msg' => 'refreshToken',
'url' => self::create($config, $access_token)->getAuthUrl(HttpReceiver::get('userId', 'int')),
];
}

}

/**
* @param int $userId
* @return string
*/
private function getAuthUrl($userId)
{
return $this->service->getAuthHelper()->getAuthUrl($this->redirectUrl, [], $userId);
}

private static function getDropBoxAuth($config) {

$data = array('key' => $config['DROPBOX_KEY'], 'secret' => $config['DROPBOX_SECRET']);
/**
* @param string $code
* @param string $state
* @return string
*/
private function getAccessToken($code, $state)
{
return $this->service->getAuthHelper()->getAccessToken($code, $state, $this->redirectUrl)->getToken();
}

$appInfo = dbx\AppInfo::loadFromJson($data);
$clientIdentifier = "my-app/1.0";
$redirectUri = $config['DROPBOX_REDIRECT_URI'];
$csrfTokenStore = new dbx\ArrayEntryStore($_ENV, 'dropbox-auth-csrf-token');
/**
* @param string $localFilename
* @param string $remoteFilename
*/

return new dbx\WebAuth($appInfo, $clientIdentifier, $redirectUri, $csrfTokenStore);
private function upload($localFilename, $remoteFilename)
{
$this->service->upload($localFilename, $remoteFilename, ['autorename' => true]);
}

private static function getExtension($path){
$ext = pathinfo($path,PATHINFO_EXTENSION);
if(strpos($path,'&') > -1){
$ext = reset(explode('&',$ext));
/**
* @param string $path
* @return string
*/
private static function getExtension($path)
{
$ext = pathinfo($path, PATHINFO_EXTENSION);
if (strpos($path, '&') > -1) {
$ext = reset(explode('&', $ext));
}
return $ext;
}

}
/**
* @param string $localPath
* @return string
*/
private static function generateRemoteFilename($localPath)
{
$parts = explode('/', $localPath);
return end($parts);
}

/**
* @param array $config
* @param string|null $accessToken
* @return self
*/
private static function create($config, $accessToken = null)
{
$self = new self(
$config[self::CONFIG_CLIENT_ID],
$config[self::CONFIG_CLIENT_SECRET],
$config[self::CONFIG_REDIRECT_URL],
$accessToken
);
/** @see \Kunnu\Dropbox\Dropbox::__construct() */
$serviceConfig = array_intersect_key($config, [
'http_client_handler' => null,
'random_string_generator' => null,
'persistent_data_store' => null
]);
$self->init($serviceConfig);

return $self;
}
}
Loading

0 comments on commit ad2eefd

Please sign in to comment.