157 lines
4.2 KiB
PHP
157 lines
4.2 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace App\Services;
|
||
|
|
||
|
use Illuminate\Support\Facades\Log;
|
||
|
|
||
|
class DropboxService
|
||
|
{
|
||
|
/**
|
||
|
* @var string
|
||
|
*/
|
||
|
private $accessToken;
|
||
|
|
||
|
/**
|
||
|
* Configuration related to the Backblaze B2 service.
|
||
|
* @var \Illuminate\Config\Repository|mixed
|
||
|
*/
|
||
|
private $config;
|
||
|
|
||
|
public function __construct()
|
||
|
{
|
||
|
$this->config = config('services.dropbox');
|
||
|
}
|
||
|
|
||
|
public function downloadFile($pathOnStorage)
|
||
|
{
|
||
|
$dropboxArgs = ['path' => $pathOnStorage];
|
||
|
|
||
|
return $this->sendRequest(
|
||
|
$this->config['download_url'],
|
||
|
'POST',
|
||
|
null,
|
||
|
[
|
||
|
'http_headers' => [
|
||
|
sprintf('Dropbox-API-Arg: %s', json_encode($dropboxArgs)),
|
||
|
'Content-Type: application/octet-stream'
|
||
|
],
|
||
|
'post_body_is_json' => false,
|
||
|
'response_body_is_json' => false
|
||
|
]
|
||
|
);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @param string $accessToken
|
||
|
*/
|
||
|
public function setAccessToken(string $accessToken)
|
||
|
{
|
||
|
$this->accessToken = $accessToken;
|
||
|
}
|
||
|
|
||
|
public function uploadFile($pathToFileToUpload, $pathToStorage)
|
||
|
{
|
||
|
$dropboxArgs = [
|
||
|
'path' => $pathToStorage,
|
||
|
'mode' => 'add',
|
||
|
'mute' => true
|
||
|
];
|
||
|
|
||
|
$uploadResult = $this->sendRequest(
|
||
|
$this->config['upload_url'],
|
||
|
'POST',
|
||
|
file_get_contents($pathToFileToUpload),
|
||
|
[
|
||
|
'http_headers' => [
|
||
|
sprintf('Dropbox-API-Arg: %s', json_encode($dropboxArgs)),
|
||
|
'Content-Type: application/octet-stream'
|
||
|
],
|
||
|
'post_body_is_json' => false
|
||
|
]
|
||
|
);
|
||
|
}
|
||
|
|
||
|
private function getBasicHttpClient($url, $method = 'GET', array $httpHeaders = [])
|
||
|
{
|
||
|
$httpHeaders = array_merge(
|
||
|
[
|
||
|
'Accept: application/json',
|
||
|
sprintf('Authorization: Bearer %s', $this->accessToken)
|
||
|
],
|
||
|
$httpHeaders
|
||
|
);
|
||
|
|
||
|
$ch = curl_init($url);
|
||
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeaders);
|
||
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||
|
|
||
|
switch (strtoupper($method))
|
||
|
{
|
||
|
case 'GET':
|
||
|
curl_setopt($ch, CURLOPT_HTTPGET, true);
|
||
|
break;
|
||
|
|
||
|
case 'POST':
|
||
|
curl_setopt($ch, CURLOPT_POST, true);
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
return $ch;
|
||
|
}
|
||
|
|
||
|
private function sendRequest($url, $method = 'GET', $postData = null, array $postOptions = [])
|
||
|
{
|
||
|
$postOptions = array_merge(
|
||
|
[
|
||
|
'http_headers' => [],
|
||
|
'post_body_is_json' => true,
|
||
|
'response_body_is_json' => true
|
||
|
],
|
||
|
$postOptions
|
||
|
);
|
||
|
$httpHeaders = $postOptions['http_headers'];
|
||
|
|
||
|
$ch = $this->getBasicHttpClient($url, $method, $httpHeaders);
|
||
|
|
||
|
if (!is_null($postData))
|
||
|
{
|
||
|
if ($postOptions['post_body_is_json'])
|
||
|
{
|
||
|
$postData = json_encode($postData);
|
||
|
}
|
||
|
|
||
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
|
||
|
}
|
||
|
|
||
|
Log::info(sprintf('%s: %s', strtoupper($method), $url));
|
||
|
Log::debug('HTTP headers:', $httpHeaders);
|
||
|
|
||
|
// Only log a post body if we have one and it's in JSON format (i.e. not a file upload)
|
||
|
if (!is_null($postData) && $postOptions['post_body_is_json'])
|
||
|
{
|
||
|
Log::debug($postData);
|
||
|
}
|
||
|
|
||
|
$result = curl_exec($ch);
|
||
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||
|
|
||
|
Log::info(sprintf('Received HTTP code %d', $httpCode));
|
||
|
|
||
|
// Only log a result if we have one and it's in JSON format (i.e. not a file download)
|
||
|
if (!is_null($result) && $result !== false && $postOptions['response_body_is_json'])
|
||
|
{
|
||
|
Log::debug($result);
|
||
|
}
|
||
|
|
||
|
if ($httpCode != 200 && $httpCode != 304)
|
||
|
{
|
||
|
throw new \Exception(sprintf('Exception from Dropbox: %s', $result));
|
||
|
}
|
||
|
|
||
|
curl_close($ch);
|
||
|
|
||
|
return $postOptions['response_body_is_json']
|
||
|
? json_decode($result)
|
||
|
: $result;
|
||
|
}
|
||
|
}
|