diff --git a/README.md b/README.md index dbdf3c4..c672835 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,20 @@ If want to generate file in a temp directory and then move to main upload direct $uploadHandeler->setTempDirectory('path-to-temp'); ``` +## Max upload size + +ChunksUploader calculate uploaded file size (sum of chunks) and compare with upload_max_filesize ini config. If want to stop bigger file at first request for better ux can pass total main file size in bytes: + +```PHP +$uploadHandeler->setVideoTotalSize(213456); +``` + +And can overwrite upload_max_filesize by: + +```PHP +$uploadHandeler->setMaxUploadSize(213456); +``` + ## License hls-video-generater is licensed under the [GPLv3 License](http://opensource.org/licenses/GPL). \ No newline at end of file diff --git a/src/ChunksUploader.php b/src/ChunksUploader.php index bacb7f2..88bcbe6 100644 --- a/src/ChunksUploader.php +++ b/src/ChunksUploader.php @@ -11,6 +11,8 @@ class ChunksUploader private $fileName; private $fileTotalSize; private $inputName; + private $maxUploadSize; + private $videoTotalSize; protected $uploadName; @@ -26,6 +28,8 @@ class ChunksUploader public function __construct() { @umask(0002); + + // dd($_POST, $_FILES); } public function uploadChunk(string $chunkName) @@ -36,6 +40,13 @@ public function uploadChunk(string $chunkName) return $checkUploadStatus; } + if (!$this->checkUploadSize()) { + return [ + 'status' => false, + 'error' => 'Max upload size. uploaded file size is ' . $this->getVideoTotalSize() . ' bytes but max allowed size is ' . $this->getMaxUploadSize() .' bytes!', + ]; + } + if (!$this->makeSubDirectoryForChunks()) { return [ 'status' => false, @@ -364,6 +375,55 @@ private function getFileTempName() return $_FILES[$this->inputName]['tmp_name']; } + public function setVideoTotalSize(int $size) + { + $this->videoTotalSize = $size; + + return $this; + } + + private function getVideoTotalSize(): int + { + if ($this->videoTotalSize < 1) { + $uploadedChunksCount = count($this->getSuccessUploadedChunks()); + + $onePartSize = $_FILES[$this->inputName]['size']; + + $minTotalSize = ($uploadedChunksCount + 1) * $onePartSize; + + $this->setVideoTotalSize($minTotalSize); + } + + return $this->videoTotalSize; + } + + public function setMaxUploadSize(int $size) + { + $this->maxUploadSize = $size; + + return $this; + } + + public function getMaxUploadSize(): int + { + if ($this->maxUploadSize < 1) { + $maxUpload = $this->convertToBytes(ini_get('upload_max_filesize')); //select maximum upload size + + $this->setMaxUploadSize($maxUpload); // set the smallest of them, this defines the real limit + } + + return $this->maxUploadSize; + } + + private function checkUploadSize(): bool + { + if ($this->getVideoTotalSize() > $this->getMaxUploadSize()) { + return false; + } + + return true; + } + private function checkUploadStatus(): array { // Check $_FILES['upfile']['error'] value. @@ -440,4 +500,27 @@ private function checkAndGenerateUploadFolder(string $path) return true; } + + private function convertToBytes($stringSize): int + { + if ($stringSize == -1) { + return 9223372036854775807; + } + + $stringSize = trim($stringSize); + $last = strtolower($stringSize[strlen($stringSize) - 1]); + + $size = substr($stringSize, 0, strlen($stringSize) - 1); + + switch ($last) { + case 'g': + $size *= 1024; + case 'm': + $size *= 1024; + case 'k': + $size *= 1024; + } + + return $size; + } }