Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,19 @@ You can pass it like this:

$converter = new Converter($config);
```


To Debug the file handling (upload and download) you can activate the debug mode. By default Debug mode is disabled.
You can pass it like this:

```php
use RebaseData\Config\Config;
use RebaseData\Converter\Converter;

$config = new Config();
$config->setDebugMode(true);

$converter = new Converter($config);
```

Tests
-----
Expand Down
30 changes: 21 additions & 9 deletions src/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,38 @@ class Config
private $cacheEnabled;
private $cacheDirectory;
private $useZipIfAvailable;
private $debugMode;

public function __construct()
{
$this->protocol = 'https';
$this->host = 'www.rebasedata.com';
$this->cacheEnabled = false;
$this->useZipIfAvailable = true;
$this->debugMode = false;
}

public function setProtocol(string $protocol)
{
$this->protocol = $protocol;
}

public function getProtocol() : string
public function getProtocol(): string
{
return $this->protocol;
}

public function setHost(string $host) : void
public function setHost(string $host): void
{
$this->host = $host;
}

public function getHost() : string
public function getHost(): string
{
return $this->host;
}

public function getWorkingDirectory()
public function getWorkingDirectory(): string
{
if ($this->workingDirectory === null) {
$defaultWorkingDirectory = sys_get_temp_dir().DIRECTORY_SEPARATOR.'rebasedata-working-dir';
Expand Down Expand Up @@ -78,10 +80,10 @@ public function setApiKey($apiKey)

public function setCacheEnabled($cacheEnabled)
{
$this->cacheEnabled = (bool) $cacheEnabled;
$this->cacheEnabled = (bool)$cacheEnabled;
}

public function getCacheEnabled()
public function getCacheEnabled(): bool
{
return $this->cacheEnabled;
}
Expand All @@ -95,7 +97,7 @@ public function setCacheDirectory($cacheDirectory)
$this->cacheDirectory = $cacheDirectory;
}

public function getCacheDirectory()
public function getCacheDirectory(): string
{
if ($this->cacheDirectory === null) {
$defaultCacheDirectory = $this->getWorkingDirectory().DIRECTORY_SEPARATOR.'cache';
Expand All @@ -110,13 +112,23 @@ public function getCacheDirectory()
return $this->cacheDirectory;
}

public function setUseZipIfAvailable(bool $useZipIfAvailable) : void
public function setUseZipIfAvailable(bool $useZipIfAvailable): void
{
$this->useZipIfAvailable = $useZipIfAvailable;
}

public function getUseZipIfAvailable() : bool
public function getUseZipIfAvailable(): bool
{
return $this->useZipIfAvailable;
}

public function setDebugMode(bool $debugMode): void
{
$this->debugMode = $debugMode;
}

public function getDebugMode(): bool
{
return $this->debugMode;
}
}
75 changes: 74 additions & 1 deletion src/Converter/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ public function convertAndSaveToZipFile(array $inputFiles, string $format, strin
$this->doConversion($inputFiles, $format, $zipFilePath, null, $options);
}

/**
* @throws LogicException
* @throws RuntimeException
* @throws InvalidArgumentException
*/
private function doConversion(array $inputFiles, string $format, ?string $zipFilePath, ?string $targetDirectory, array $options = [])
{
CheckInputFilesService::execute($inputFiles);
Expand Down Expand Up @@ -190,22 +195,44 @@ private function doConversion(array $inputFiles, string $format, ?string $zipFil
// Send request body
foreach ($inputFiles as $inputFile) {
$name = basename($inputFile->getName());
if ($this->config->getDebugMode()) {
$humanSize = $this->humanFileSize(filesize($inputFile->getPath()));
echo "\n\n[DEBUG] *** Processing file {$inputFile->getName()} [{$humanSize}] ***";
}

$nameLength = pack('J', strlen($name));

fwrite($socket, $nameLength);
fwrite($socket, $name);

$contentLength = pack('J', filesize($inputFile->getPath()));
$fileSize = filesize($inputFile->getPath());
$contentLength = pack('J', $fileSize);

fwrite ($socket, $contentLength);

$currentTransferSize = 0;
$inputFileHandle = fopen($inputFile->getPath(), 'r');
$chunkSize = $fileSize > 10000 ? round($fileSize / 1000) : 1;

while (!feof($inputFileHandle)) {
$chunk = fread($inputFileHandle, 2048);

if ($this->config->getDebugMode() && $chunkSize > 1) {
$currentTransferSize += 2048;
$percentage = round($currentTransferSize / $fileSize * 100);

if ($currentTransferSize % $chunkSize === 0 && $percentage < 100) {
echo "\n *** uploading {$percentage}% ***";
}
}

fwrite($socket, $chunk);
}

if ($this->config->getDebugMode()) {
echo "\n[DEBUG] *** File {$inputFile->getName()} uploaded. ***";
}

fclose($inputFileHandle);
}

Expand Down Expand Up @@ -246,6 +273,10 @@ private function doConversion(array $inputFiles, string $format, ?string $zipFil
throw new RuntimeException('Got error from API: '.$json['error']);
}

if ($this->config->getDebugMode()) {
echo "\n\n[DEBUG] *** Handling response from {$this->config->getHost()} ***";
}

// Handle response body
if ($zipFilePath !== null) {
// Read connection and write data to ZIP file
Expand Down Expand Up @@ -353,4 +384,46 @@ public function convertToFormatAndSaveAsZipFile(array $inputFiles, string $forma
{
$this->doConversion($inputFiles, $format, $zipFilePath, null, $options);
}

/**
* Converts bytes into human-readable file size.
*
* @param string $bytes
* @return string human-readable file size (2,87 МB)
*/
function humanFileSize(string $bytes): string
{
$bytes = floatval($bytes);
$unitMap = [
[
"unit" => "TB",
"value" => pow(1024, 4)
],
[
"unit" => "GB",
"value" => pow(1024, 3)
],
[
"unit" => "MB",
"value" => pow(1024, 2)
],
[
"unit" => "KB",
"value" => 1024
],
[
"unit" => "B",
"value" => 1
],
];

foreach ($unitMap as $item) {
if ($bytes >= $item["value"]) {
$result = round($bytes / $item["value"], 2). " " . $item["unit"];
break;
}
}

return $result ?? $bytes;
}
}