| 1 |
<?php |
| 2 |
/* |
| 3 |
* This file is part of the ManageWP Worker plugin. |
| 4 |
* |
| 5 |
* (c) ManageWP LLC <contact@managewp.com> |
| 6 |
* |
| 7 |
* For the full copyright and license information, please view the LICENSE |
| 8 |
* file that was distributed with this source code. |
| 9 |
*/ |
| 10 |
|
| 11 |
class MWP_Progress_Upload extends MWP_Progress_Abstract |
| 12 |
{ |
| 13 |
/** |
| 14 |
* @var int |
| 15 |
*/ |
| 16 |
private $fileSize; |
| 17 |
|
| 18 |
/** |
| 19 |
* @var Monolog_Psr_LoggerInterface |
| 20 |
*/ |
| 21 |
private $logger; |
| 22 |
|
| 23 |
/** |
| 24 |
* @var int |
| 25 |
*/ |
| 26 |
private $lastProgress = 0; |
| 27 |
|
| 28 |
public function __construct($fileSize, $threshold, Monolog_Psr_LoggerInterface $logger) |
| 29 |
{ |
| 30 |
$this->fileSize = $fileSize; |
| 31 |
$this->setThreshold($threshold); |
| 32 |
$this->logger = $logger; |
| 33 |
} |
| 34 |
|
| 35 |
public function callback(&$curl, $downloadSize, $downloadedSize, $uploadSize, $uploadedSize = 0) |
| 36 |
{ |
| 37 |
if (!$this->yieldCallback()) { |
| 38 |
return; |
| 39 |
} |
| 40 |
|
| 41 |
if (func_num_args() < 5) { |
| 42 |
$uploadedSize = $uploadSize; |
| 43 |
$uploadSize = $downloadedSize; |
| 44 |
$downloadedSize = $downloadSize; |
| 45 |
$downloadSize = $curl; |
| 46 |
} |
| 47 |
|
| 48 |
$offset = is_resource($curl) ? $this->calculateOffset($curl) : 0; |
| 49 |
$currentProgress = $uploadedSize + $offset; |
| 50 |
$speed = $this->formatBytes(($currentProgress - $this->lastProgress) / $this->getThreshold()); |
| 51 |
$this->lastProgress = $currentProgress; |
| 52 |
|
| 53 |
$progress = round($currentProgress / $this->fileSize * 100, 2); |
| 54 |
|
| 55 |
global $forkedRequest; |
| 56 |
if (!$forkedRequest) { |
| 57 |
echo " "; |
| 58 |
flush(); |
| 59 |
} |
| 60 |
|
| 61 |
$this->logger->info( |
| 62 |
'Upload progress: {progress}% (speed: {speed}/s)', |
| 63 |
array( |
| 64 |
'progress' => $progress, |
| 65 |
'speed' => $speed, |
| 66 |
) |
| 67 |
); |
| 68 |
} |
| 69 |
} |
| 70 |
|