Skip to content

Commit

Permalink
check max upload size
Browse files Browse the repository at this point in the history
  • Loading branch information
Mehrdad-Dadkhah committed Sep 26, 2017
1 parent 00e52e9 commit b4d8600
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
83 changes: 83 additions & 0 deletions src/ChunksUploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class ChunksUploader
private $fileName;
private $fileTotalSize;
private $inputName;
private $maxUploadSize;
private $videoTotalSize;

protected $uploadName;

Expand All @@ -26,6 +28,8 @@ class ChunksUploader
public function __construct()
{
@umask(0002);

// dd($_POST, $_FILES);
}

public function uploadChunk(string $chunkName)
Expand All @@ -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,
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
}

0 comments on commit b4d8600

Please sign in to comment.