diff --git a/README.md b/README.md index 01d0434..5d52ce3 100644 --- a/README.md +++ b/README.md @@ -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 ----- diff --git a/src/Config/Config.php b/src/Config/Config.php index f7338ee..73e6fa6 100644 --- a/src/Config/Config.php +++ b/src/Config/Config.php @@ -13,6 +13,7 @@ class Config private $cacheEnabled; private $cacheDirectory; private $useZipIfAvailable; + private $debugMode; public function __construct() { @@ -20,6 +21,7 @@ public function __construct() $this->host = 'www.rebasedata.com'; $this->cacheEnabled = false; $this->useZipIfAvailable = true; + $this->debugMode = false; } public function setProtocol(string $protocol) @@ -27,22 +29,22 @@ 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'; @@ -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; } @@ -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'; @@ -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; + } } diff --git a/src/Converter/Converter.php b/src/Converter/Converter.php index 92902d9..c49f7d6 100644 --- a/src/Converter/Converter.php +++ b/src/Converter/Converter.php @@ -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); @@ -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); } @@ -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 @@ -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; + } }