Skip to content

Commit

Permalink
review resolve
Browse files Browse the repository at this point in the history
Signed-off-by: nabim777 <[email protected]>
  • Loading branch information
nabim777 committed Jan 23, 2025
1 parent a2fd9b6 commit 1a16898
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 36 deletions.
52 changes: 21 additions & 31 deletions tests/acceptance/TestHelpers/TusClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class TusClient extends Client {
* @return ResponseInterface
* @throws GuzzleException
*/
public function createUploadWithResponse(string $key, int $bytes = -1): ResponseInterface {
public function createWithUploadR(string $key, int $bytes = -1): ResponseInterface {
$bytes = $bytes < 0 ? $this->fileSize : $bytes;
$headers = $this->headers + [
'Upload-Length' => $this->fileSize,
Expand Down Expand Up @@ -77,18 +77,16 @@ public function createUploadWithResponse(string $key, int $bytes = -1): Response
} catch (ClientException $e) {
$response = $e->getResponse();
}
$statusCode = $response->getStatusCode();
if ($statusCode !== HttpResponse::HTTP_CREATED) {
return $response;
if ($response->getStatusCode() === HttpResponse::HTTP_CREATED) {
$uploadLocation = current($response->getHeader('location'));
$this->getCache()->set(
$this->getKey(),
[
'location' => $uploadLocation,
'expires_at' => Carbon::now()->addSeconds($this->getCache()->getTtl())->format($this->getCache()::RFC_7231),
]
);
}
$uploadLocation = current($response->getHeader('location'));
$this->getCache()->set(
$this->getKey(),
[
'location' => $uploadLocation,
'expires_at' => Carbon::now()->addSeconds($this->getCache()->getTtl())->format($this->getCache()::RFC_7231),
]
);
return $response;
}

Expand All @@ -99,23 +97,19 @@ public function createUploadWithResponse(string $key, int $bytes = -1): Response
* @throws GuzzleException
* @throws TusException | ConnectionException
*/
public function uploadWithResponse(int $bytes = -1): ResponseInterface {
public function uploadR(int $bytes = -1): ResponseInterface {
$bytes = $bytes < 0 ? $this->getFileSize() : $bytes;
$offset = $this->partialOffset < 0 ? 0 : $this->partialOffset;
try {
// Check if this upload exists with HEAD request.
$offset = $this->sendHeadRequest();
} catch (FileException | ClientException $e) {
// Create a new upload.
$this->url = $this->createUploadWithResponse($this->getKey(), 0);
if ($this->url->getStatusCode() !== HttpResponse::HTTP_CREATED) {
return $this->url;
$response = $this->createWithUploadR($this->getKey(), 0);
if ($response->getStatusCode() !== HttpResponse::HTTP_CREATED) {
return $response;
}
}
// Verify that upload is not yet expired.
if ($this->isExpired()) {
throw new TusException('Upload expired.');
}
$data = $this->getData($offset, $bytes);
$headers = $this->headers + [
'Content-Type' => self::HEADER_CONTENT_TYPE,
Expand All @@ -127,17 +121,13 @@ public function uploadWithResponse(int $bytes = -1): ResponseInterface {
} else {
$headers += ['Upload-Offset' => $offset];
}
try {
$response = $this->getClient()->patch(
$this->getUrl(),
[
'body' => $data,
'headers' => $headers,
]
);
} catch (ClientException $e) {
throw $this->handleClientException($e);
}
$response = $this->getClient()->patch(
$this->getUrl(),
[
'body' => $data,
'headers' => $headers,
]
);
return $response;
}
}
10 changes: 5 additions & 5 deletions tests/acceptance/bootstrap/TUSContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,15 +341,15 @@ public function uploadFileUsingTus(

if ($bytes !== null) {
return $client->file($sourceFile, $destination)
->createUploadWithResponse($client->getKey(), $bytes);
->createWithUploadR($client->getKey(), $bytes);
} elseif (\filesize($sourceFile) === 0) {
return $client->file($sourceFile, $destination)->createUploadWithResponse($client->getKey(), 0);
return $client->file($sourceFile, $destination)->createWithUploadR($client->getKey(), 0);
} elseif ($noOfChunks === 1) {
return $client->file($sourceFile, $destination)->uploadWithResponse();
return $client->file($sourceFile, $destination)->uploadR();
} else {
$bytesPerChunk = (int)\ceil(\filesize($sourceFile) / $noOfChunks);
for ($i = 0; $i < $noOfChunks; $i++) {
$response = $client->uploadWithResponse($bytesPerChunk);
$response = $client->uploadR($bytesPerChunk);
}
return $response;
}
Expand Down Expand Up @@ -387,7 +387,7 @@ public function publicUploadFileUsingTus(
$destination,
$url
);
$response = $client->createUploadWithResponse("", 0);
$response = $client->createWithUploadR("", 0);
return $response;
}

Expand Down

0 comments on commit 1a16898

Please sign in to comment.