-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: nabim777 <[email protected]>
- Loading branch information
Showing
4 changed files
with
137 additions
and
178 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,25 @@ | ||
<?php | ||
<?php declare(strict_types=1); | ||
/** | ||
* ownCloud | ||
* | ||
* @author Nabin Magar <[email protected]> | ||
* @copyright Copyright (c) 2017 Nabin Magar [email protected] | ||
* | ||
* This code is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License, | ||
* as published by the Free Software Foundation; | ||
* either version 3 of the License, or any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/> | ||
* | ||
*/ | ||
|
||
namespace TestHelpers; | ||
|
||
use http\Env\Response; | ||
|
@@ -11,172 +32,102 @@ | |
use GuzzleHttp\ClientTrait; | ||
use GuzzleHttp\Client as GuzzleClient; | ||
use GuzzleHttp\Exception\ClientException; | ||
use Psr\Http\Message\ResponseInterface; | ||
use TusPhp\Exception\ConnectionException; | ||
use GuzzleHttp\Exception\GuzzleException; | ||
use GuzzleHttp\Exception\ConnectException; | ||
use Symfony\Component\HttpFoundation\Response as HttpResponse; | ||
use ReflectionException; | ||
use TusPhp\Tus\Client; | ||
|
||
class TusClientWrapper extends Client{ | ||
public $client; | ||
|
||
/** | ||
* Constructor for the TusClientWrapper. | ||
* | ||
* @param string $baseUri Base URI of the TUS server. | ||
* @param array $options Additional client options. | ||
* @throws ReflectionException | ||
*/ | ||
public function __construct(string $baseUri, array $options = []) { | ||
parent::__construct($baseUri,$options); | ||
$this->client = new Client($baseUri,$options); | ||
} | ||
|
||
public function setKey(string $key): self | ||
{ | ||
$this->client->setKey($key); | ||
return $this; | ||
} | ||
|
||
public function file(string $file, string $name = null): self | ||
{ | ||
$this->client->file($file,$name); | ||
return $this; | ||
} | ||
|
||
public function createWithUpload(string $key, int $bytes = -1): array { | ||
$bytes = $bytes < 0 ? $this->fileSize : $bytes; | ||
$headers = $this->headers + [ | ||
'Upload-Length' => $this->client->fileSize, | ||
'Upload-Key' => $key, | ||
'Upload-Checksum' => $this->client->getUploadChecksumHeader(), | ||
'Upload-Metadata' => $this->client->getUploadMetadataHeader(), | ||
]; | ||
|
||
$data = ''; | ||
if ($bytes > 0) { | ||
$data = $this->client->getData(0, $bytes); | ||
|
||
$headers += [ | ||
'Content-Type' => self::HEADER_CONTENT_TYPE, | ||
'Content-Length' => \strlen($data), | ||
]; | ||
} | ||
|
||
if ($this->isPartial()) { | ||
$headers += ['Upload-Concat' => 'partial']; | ||
} | ||
|
||
try { | ||
$response = $this->client->getClient()->post($this->apiPath, [ | ||
'body' => $data, | ||
'headers' => $headers, | ||
]); | ||
} catch (ClientException $e) { | ||
$response = $e->getResponse(); | ||
} | ||
|
||
$statusCode = $response->getStatusCode(); | ||
$header = $response->getHeaders(); | ||
|
||
|
||
if (HttpResponse::HTTP_CREATED !== $statusCode) { | ||
throw new FileException('Unable to create resource.'); | ||
} | ||
|
||
$uploadOffset = $bytes > 0 ? current($response->getHeader('upload-offset')) : 0; | ||
$uploadLocation = current($response->getHeader('location')); | ||
|
||
$this->getCache()->set($this->client->getKey(), [ | ||
'location' => $uploadLocation, | ||
'expires_at' => Carbon::now()->addSeconds($this->getCache()->getTtl())->format($this->getCache()::RFC_7231), | ||
]); | ||
|
||
return [ | ||
'location' => $uploadLocation, | ||
'offset' => $uploadOffset, | ||
]; | ||
} | ||
|
||
// /** | ||
// * Set API path. | ||
// * | ||
// * @param string $path | ||
// * | ||
// * @return self | ||
// */ | ||
// public function setApiPath(string $path): self | ||
// { | ||
// $this->client->apiPath = $path; | ||
// return $this; | ||
// } | ||
|
||
// public function setApiPath(string $path): self | ||
// { | ||
// $this->client->apiPath = $path; | ||
// return $this; | ||
// } | ||
|
||
// public function setKey(string $key): self | ||
// { | ||
// $this->client->setKey($key); | ||
// return $this; | ||
// } | ||
|
||
// public function file(string $file, string $name = null): self | ||
// { | ||
// $this->client->file($file,$name); | ||
// return $this; | ||
// } | ||
|
||
// public function createWithUpload(string $key, int $bytes = -1): void | ||
// { | ||
// $this->client->createWithUpload($key,$bytes); | ||
// } | ||
|
||
// public function setHeaders(array $headers): void { | ||
// $this->client->setHeaders($headers); | ||
// } | ||
// | ||
// public function upload(string $filePath): string { | ||
// $this->client->setFilePath($filePath); | ||
// return $this->client->upload(); | ||
// } | ||
// | ||
// public function getOffset(): int { | ||
// return $this->client->getOffset(); | ||
// } | ||
// | ||
// public function setChunkSize(int $bytes): void { | ||
// $this->client->setChunkSize($bytes); | ||
// } | ||
// | ||
// public function terminateUpload(string $uploadUrl): void { | ||
// $this->client->terminate($uploadUrl); | ||
// } | ||
// | ||
// public function setKey(string $key): self | ||
// { | ||
// $this->client->key = $key; | ||
// | ||
// return $this; | ||
// } | ||
// | ||
// public function file(string $file, string $name = null): self | ||
// { | ||
// $this->filePath = $file; | ||
// | ||
// if ( ! file_exists($file) || ! is_readable($file)) { | ||
// throw new FileException('Cannot read file: ' . $file); | ||
// } | ||
// | ||
// $this->fileName = $name ?? basename($this->filePath); | ||
// $this->fileSize = filesize($file); | ||
// | ||
// $this->addMetadata('filename', $this->fileName); | ||
// | ||
// return $this; | ||
// } | ||
|
||
/** | ||
* A helper class where TUS is wrapped and done API requests | ||
*/ | ||
|
||
class TusClientWrapper extends Client { | ||
public $client; | ||
|
||
/** | ||
* Constructor for the TusClientWrapper. | ||
* | ||
* @param string $baseUri | ||
* @param array $options | ||
* @throws ReflectionException | ||
*/ | ||
public function __construct(string $baseUri, array $options = []) { | ||
parent::__construct($baseUri, $options); | ||
$this->client = new Client($baseUri, $options); | ||
} | ||
|
||
public function setKey(string $key): self { | ||
$this->client->setKey($key); | ||
return $this; | ||
} | ||
|
||
public function file(string $file, string $name = null): self { | ||
$this->client->file($file, $name); | ||
return $this; | ||
} | ||
|
||
/** | ||
* @param string $key | ||
* @param int $bytes | ||
* | ||
* @return ResponseInterface | ||
* @throws GuzzleException | ||
*/ | ||
public function createWithUploadResponse(string $key, int $bytes = -1): ResponseInterface { | ||
$bytes = $bytes < 0 ? $this->fileSize : $bytes; | ||
$headers = $this->headers + [ | ||
'Upload-Length' => $this->client->fileSize, | ||
'Upload-Key' => $key, | ||
'Upload-Checksum' => $this->client->getUploadChecksumHeader(), | ||
'Upload-Metadata' => $this->client->getUploadMetadataHeader(), | ||
]; | ||
|
||
$data = ''; | ||
if ($bytes > 0) { | ||
$data = $this->client->getData(0, $bytes); | ||
|
||
$headers += [ | ||
'Content-Type' => self::HEADER_CONTENT_TYPE, | ||
'Content-Length' => \strlen($data), | ||
]; | ||
} | ||
|
||
if ($this->isPartial()) { | ||
$headers += ['Upload-Concat' => 'partial']; | ||
} | ||
|
||
try { | ||
$response = $this->client->getClient()->post( | ||
$this->apiPath, | ||
[ | ||
'body' => $data, | ||
'headers' => $headers, | ||
] | ||
); | ||
} catch (ClientException $e) { | ||
$response = $e->getResponse(); | ||
} | ||
|
||
$statusCode = $response->getStatusCode(); | ||
$header = $response->getHeaders(); | ||
|
||
if ($statusCode !== HttpResponse::HTTP_CREATED) { | ||
throw new FileException('Unable to create resource.'); | ||
} | ||
|
||
$uploadOffset = $bytes > 0 ? current($response->getHeader('upload-offset')) : 0; | ||
$uploadLocation = current($response->getHeader('location')); | ||
|
||
$this->getCache()->set( | ||
$this->client->getKey(), | ||
[ | ||
'location' => $uploadLocation, | ||
'expires_at' => Carbon::now()->addSeconds($this->getCache()->getTtl())->format($this->getCache()::RFC_7231), | ||
] | ||
); | ||
return $response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters