Skip to content

Commit

Permalink
Remove cache dependency from Client (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
ankitpokhrel authored Sep 21, 2018
1 parent 9db38aa commit 8d87815
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 91 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ $server = new \TusPhp\Tus\Server('redis');
The client can be used for creating, resuming and/or deleting uploads.

```php
$client = new \TusPhp\Tus\Client($baseUrl, 'redis'); // Leave second parameter empty for file based cache
$client = new \TusPhp\Tus\Client($baseUrl);

// Optional. If a key is not set explicitly, the system will generate a unique uuid.
$key = 'your unique key';
Expand Down
2 changes: 1 addition & 1 deletion example/basic/upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use TusPhp\Exception\ConnectionException;
use TusPhp\Exception\Exception as TusException;

$client = new \TusPhp\Tus\Client('http://tus-php-server', 'redis');
$client = new \TusPhp\Tus\Client('http://tus-php-server');

// Alert: Sanitize all inputs properly in production code
if ( ! empty($_FILES)) {
Expand Down
2 changes: 1 addition & 1 deletion example/basic/verify.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use TusPhp\Exception\ConnectionException;
use GuzzleHttp\Exception\ConnectException;

$client = new \TusPhp\Tus\Client('http://tus-php-server', 'redis');
$client = new \TusPhp\Tus\Client('http://tus-php-server');

// Alert: Sanitize all inputs properly in production code
if ( ! empty($_FILES)) {
Expand Down
2 changes: 1 addition & 1 deletion example/partial/partial.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use TusPhp\Exception\ConnectionException;
use TusPhp\Exception\Exception as TusException;

$client = new \TusPhp\Tus\Client('http://tus-php-server', 'redis');
$client = new \TusPhp\Tus\Client('http://tus-php-server');

// Alert: Sanitize all inputs properly in production code
if ( ! empty($_FILES)) {
Expand Down
39 changes: 15 additions & 24 deletions src/Tus/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace TusPhp\Tus;

use TusPhp\File;
use TusPhp\Cache\Cacheable;
use TusPhp\Exception\Exception;
use TusPhp\Exception\FileException;
use GuzzleHttp\Client as GuzzleClient;
Expand Down Expand Up @@ -44,16 +43,13 @@ class Client extends AbstractTus
/**
* Client constructor.
*
* @param string $baseUrl
* @param Cacheable|string $cacheAdapter
* @param string $baseUrl
*/
public function __construct(string $baseUrl, $cacheAdapter = 'file')
public function __construct(string $baseUrl)
{
$this->client = new GuzzleClient([
'base_uri' => $baseUrl,
]);

$this->setCache($cacheAdapter);
}

/**
Expand Down Expand Up @@ -241,20 +237,21 @@ public function seek(int $offset) : self
*/
public function upload(int $bytes = -1) : int
{
$bytes = $bytes < 0 ? $this->getFileSize() : $bytes;
$key = $this->getKey();
$key = $this->getKey();
$bytes = $bytes < 0 ? $this->getFileSize() : $bytes;
$offset = $this->partialOffset < 0 ? 0 : $this->partialOffset;

try {
// Check if this upload exists with HEAD request.
$this->sendHeadRequest($key);
$offset = $this->sendHeadRequest($key);
} catch (FileException | ClientException $e) {
$this->create($key);
} catch (ConnectException $e) {
throw new ConnectionException("Couldn't connect to server.");
}

// Now, resume upload with PATCH request.
return $this->sendPatchRequest($key, $bytes);
return $this->sendPatchRequest($bytes, $offset);
}

/**
Expand Down Expand Up @@ -413,18 +410,18 @@ protected function sendHeadRequest(string $key) : int
/**
* Send PATCH request.
*
* @param string $key
* @param int $bytes
* @param int $bytes
* @param int $offset
*
* @throws Exception
* @throws FileException
* @throws ConnectionException
*
* @return int
*/
protected function sendPatchRequest(string $key, int $bytes) : int
protected function sendPatchRequest(int $bytes, int $offset) : int
{
$data = $this->getData($key, $bytes);
$data = $this->getData($offset, $bytes);
$headers = [
'Content-Type' => 'application/offset+octet-stream',
'Content-Length' => strlen($data),
Expand All @@ -436,7 +433,7 @@ protected function sendPatchRequest(string $key, int $bytes) : int
}

try {
$response = $this->getClient()->patch($this->apiPath . '/' . $key, [
$response = $this->getClient()->patch($this->apiPath . '/' . $this->getKey(), [
'body' => $data,
'headers' => $headers,
]);
Expand All @@ -462,21 +459,15 @@ protected function sendPatchRequest(string $key, int $bytes) : int
/**
* Get X bytes of data from file.
*
* @param string $key
* @param int $bytes
* @param int $offset
* @param int $bytes
*
* @return string
*/
protected function getData(string $key, int $bytes) : string
protected function getData(int $offset, int $bytes) : string
{
$file = new File;
$handle = $file->open($this->getFilePath(), $file::READ_BINARY);
$offset = $this->partialOffset;

if ($offset < 0) {
$fileMeta = $this->getCache()->get($key);
$offset = $fileMeta['offset'];
}

$file->seek($handle, $offset);

Expand Down
Loading

0 comments on commit 8d87815

Please sign in to comment.