Skip to content

Commit

Permalink
make one function
Browse files Browse the repository at this point in the history
Signed-off-by: nabim777 <[email protected]>
  • Loading branch information
nabim777 committed Jan 22, 2025
1 parent 87b01e9 commit a2fd9b6
Showing 1 changed file with 55 additions and 27 deletions.
82 changes: 55 additions & 27 deletions tests/acceptance/bootstrap/TUSContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ public function uploadFileUsingTus(
array $uploadMetadata = [],
int $noOfChunks = 1,
int $bytes = null,
string $checksum = ''
string $checksum = '',
): ResponseInterface {
$user = $this->featureContext->getActualUsername($user);
$password = $this->featureContext->getUserPassword($user);
Expand All @@ -320,39 +320,36 @@ public function uploadFileUsingTus(
$headers = \array_merge($headers, $checksumHeader);
}

$tusClient = new TusClient(
$this->featureContext->getBaseUrl(),
[
'verify' => false,
'headers' => $headers
]
);

$davPathVersion = $this->featureContext->getDavPathVersion();
$suffixPath = $user;
if ($davPathVersion === WebDavHelper::DAV_VERSION_SPACES) {
$suffixPath = $spaceId ?: $this->featureContext->getPersonalSpaceIdForUser($user);
}

$tusClient->setChecksumAlgorithm('sha1');
$tusClient->setApiPath(WebDavHelper::getDavPath($davPathVersion, $suffixPath));
$tusClient->setMetadata($uploadMetadata);
$sourceFile = $this->featureContext->acceptanceTestsDirLocation() . $source;
$tusClient->setKey((string)rand())->file($sourceFile, $destination);

$client = $this->createTusClient(
$this->featureContext->getBaseUrl(),
$headers,
$sourceFile,
$destination,
WebDavHelper::getDavPath($davPathVersion, $suffixPath),
$uploadMetadata
);

$this->featureContext->pauseUploadDelete();
$response = null;

if ($bytes !== null) {
return $tusClient->file($sourceFile, $destination)
->createUploadWithResponse($tusClient->getKey(), $bytes);
return $client->file($sourceFile, $destination)
->createUploadWithResponse($client->getKey(), $bytes);
} elseif (\filesize($sourceFile) === 0) {
return $tusClient->file($sourceFile, $destination)->createUploadWithResponse($tusClient->getKey(), 0);
return $client->file($sourceFile, $destination)->createUploadWithResponse($client->getKey(), 0);
} elseif ($noOfChunks === 1) {
return $tusClient->file($sourceFile, $destination)->uploadWithResponse();
return $client->file($sourceFile, $destination)->uploadWithResponse();
} else {
$bytesPerChunk = (int)\ceil(\filesize($sourceFile) / $noOfChunks);
for ($i = 0; $i < $noOfChunks; $i++) {
$response = $tusClient->uploadWithResponse($bytesPerChunk);
$response = $client->uploadWithResponse($bytesPerChunk);
}
return $response;
}
Expand Down Expand Up @@ -383,19 +380,50 @@ public function publicUploadFileUsingTus(
];
$sourceFile = $this->featureContext->acceptanceTestsDirLocation() . $source;
$url = WebdavHelper::getDavPath(WebDavHelper::DAV_VERSION_SPACES, $token, "public-files");

$tusClient = new TusClient(
$client = $this->createTusClient(
$this->featureContext->getBaseUrl(),
$headers,
$sourceFile,
$destination,
$url
);
$response = $client->createUploadWithResponse("", 0);
return $response;
}

/**
* Creates the TusClient with API path, headers, metadata, upload key and checksum
*
* @param string $baseUrl
* @param array $headers
* @param string $sourceFile
* @param string $destination
* @param string $path
* @param array $metadata
*
* @return TusClient
* @throws ReflectionException
*/
private function createTusClient(
string $baseUrl,
array $headers,
string $sourceFile,
string $destination,
string $path,
array $metadata = []
): TusClient {
$client = new TusClient(
$baseUrl,
[
'verify' => false,
'headers' => $headers
'headers' => $headers,
]
);

$tusClient->setApiPath($url);
$tusClient->setKey((string)rand())->file($sourceFile, $destination);
$response = $tusClient->file($sourceFile, $destination)->createUploadWithResponse("", 0);
return $response;
$client->setApiPath($path);
$client->setMetadata($metadata);
$client->setChecksumAlgorithm('sha1');
$client->setKey((string)rand())->file($sourceFile, $destination);
return $client;
}

/**
Expand Down

0 comments on commit a2fd9b6

Please sign in to comment.