Skip to content

Commit

Permalink
add enhancemenets
Browse files Browse the repository at this point in the history
  • Loading branch information
ArrayIterator committed Oct 19, 2023
1 parent 4a8e207 commit b3cbc16
Show file tree
Hide file tree
Showing 24 changed files with 138 additions and 117 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"symfony/console": "^6",
"symfony/filesystem": "^6",
"symfony/finder": "^6",
"symfony/polyfill-mbstring": "^1",
"symfony/polyfill-intl-idn": "^1",
"symfony/yaml": "^v6",
"symfony/http-client": "^6",
Expand Down
16 changes: 2 additions & 14 deletions src/Auth/Generator/HashIdentity.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,16 @@

use ArrayAccess\TrayDigita\Http\ServerRequest;
use ArrayAccess\TrayDigita\Util\Filter\Consolidation;
use ArrayAccess\TrayDigita\Util\Generator\RandomString;
use SensitiveParameter;
use Throwable;
use WhichBrowser\Parser;
use function chr;
use function dechex;
use function hash_equals;
use function hash_hmac;
use function hexdec;
use function implode;
use function mt_rand;
use function preg_match;
use function random_bytes;
use function sha1;
use function strlen;
use function strtolower;
use function strtotime;
use function substr;
Expand Down Expand Up @@ -87,15 +83,7 @@ public function generateUserAgentHash(
public function generate(int $id): string
{
$bytes = 16;
try {
$random = random_bytes($bytes);
} catch (Throwable) {
$random = '';
while (strlen($random) < $bytes) {
$random .= chr(mt_rand(0, 255));
}
}

$random = RandomString::bytes($bytes);
$randomKey = sha1($random);
// get last 10 hex chars
$randomHexNum = hexdec(substr($randomKey, -10));
Expand Down
19 changes: 6 additions & 13 deletions src/Auth/Google/Authenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace ArrayAccess\TrayDigita\Auth\Google;

use Throwable;
use ArrayAccess\TrayDigita\Util\Generator\RandomString;
use function chr;
use function floor;
use function hash_hmac;
Expand All @@ -12,11 +12,9 @@
use function is_numeric;
use function max;
use function min;
use function mt_rand;
use function ord;
use function pack;
use function pow;
use function random_bytes;
use function rawurlencode;
use function rtrim;
use function sprintf;
Expand Down Expand Up @@ -117,17 +115,12 @@ public static function randomString(

public static function generateRandomCode(int $length = 16, string|int|float $prefix = ''): string
{
try {
$random = random_bytes($length);
} catch (Throwable) {
$random = '';
for ($i = 0; $i < $length; ++$i) {
$random .= chr(mt_rand(0, 256));
}
}

$length += strlen($prefix);
return substr(rtrim(Converter::base32Encode($prefix . $random), '='), 0, $length);
return substr(
rtrim(Converter::base32Encode($prefix . RandomString::bytes($length)), '='),
0,
$length
);
}

/**
Expand Down
9 changes: 4 additions & 5 deletions src/ComposerCreateProject.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace ArrayAccess\TrayDigita;

use ArrayAccess\TrayDigita\Util\Generator\RandomString;
use DirectoryIterator;
use Exception;
use function chmod;
Expand All @@ -17,9 +18,7 @@
use function is_dir;
use function is_link;
use function preg_match;
use function random_bytes;
use function realpath;
use function sha1;
use function sprintf;
use function str_replace;
use const DIRECTORY_SEPARATOR;
Expand Down Expand Up @@ -362,9 +361,9 @@ public static function composerDoCreateProject($event)
'random_nonce_key',
],
[
sha1(random_bytes(16)),
sha1(random_bytes(16)),
sha1(random_bytes(16)),
RandomString::randomHex(64),
RandomString::randomHex(64),
RandomString::randomHex(64),
],
$configFile
);
Expand Down
1 change: 0 additions & 1 deletion src/Console/Command/DatabaseEventGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use ArrayAccess\TrayDigita\Event\Interfaces\ManagerAllocatorInterface;
use ArrayAccess\TrayDigita\Exceptions\InvalidArgument\InteractiveArgumentException;
use ArrayAccess\TrayDigita\HttpKernel\BaseKernel;
use ArrayAccess\TrayDigita\Kernel\Decorator;
use ArrayAccess\TrayDigita\Kernel\Interfaces\KernelInterface;
use ArrayAccess\TrayDigita\Traits\Container\ContainerAllocatorTrait;
use ArrayAccess\TrayDigita\Traits\Manager\ManagerAllocatorTrait;
Expand Down
1 change: 0 additions & 1 deletion src/Console/Command/MiddlewareGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use ArrayAccess\TrayDigita\Event\Interfaces\ManagerAllocatorInterface;
use ArrayAccess\TrayDigita\Exceptions\InvalidArgument\InteractiveArgumentException;
use ArrayAccess\TrayDigita\HttpKernel\BaseKernel;
use ArrayAccess\TrayDigita\Kernel\Decorator;
use ArrayAccess\TrayDigita\Kernel\Interfaces\KernelInterface;
use ArrayAccess\TrayDigita\Traits\Container\ContainerAllocatorTrait;
use ArrayAccess\TrayDigita\Traits\Manager\ManagerAllocatorTrait;
Expand Down
1 change: 0 additions & 1 deletion src/Console/Command/SchedulerAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Helper\TableCell;
use Symfony\Component\Console\Helper\TableCellStyle;
use Symfony\Component\Console\Helper\TableStyle;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down
1 change: 0 additions & 1 deletion src/Console/Command/SchedulerGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use ArrayAccess\TrayDigita\Event\Interfaces\ManagerAllocatorInterface;
use ArrayAccess\TrayDigita\Exceptions\InvalidArgument\InteractiveArgumentException;
use ArrayAccess\TrayDigita\HttpKernel\BaseKernel;
use ArrayAccess\TrayDigita\Kernel\Decorator;
use ArrayAccess\TrayDigita\Kernel\Interfaces\KernelInterface;
use ArrayAccess\TrayDigita\Traits\Container\ContainerAllocatorTrait;
use ArrayAccess\TrayDigita\Traits\Manager\ManagerAllocatorTrait;
Expand Down
1 change: 0 additions & 1 deletion src/Database/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
use SensitiveParameter;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Cache\Adapter\NullAdapter;
use function floor;
use function is_dir;
use function is_string;
use function is_subclass_of;
Expand Down
2 changes: 0 additions & 2 deletions src/Database/Wrapper/ConnectionWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@
use ArrayAccess\TrayDigita\Event\Interfaces\ManagerIndicateInterface;
use ArrayAccess\TrayDigita\Event\Interfaces\ManagerInterface;
use ArrayAccess\TrayDigita\Traits\Manager\ManagerDispatcherTrait;
use ArrayAccess\TrayDigita\Util\Filter\ContainerHelper;
use Doctrine\DBAL\Driver\Connection as DoctrineConnection;
use Doctrine\DBAL\Driver\Middleware\AbstractConnectionMiddleware;
use Doctrine\DBAL\Driver\Result;
use Doctrine\DBAL\Driver\Statement;
use Throwable;

class ConnectionWrapper extends AbstractConnectionMiddleware implements ManagerIndicateInterface
{
Expand Down
1 change: 0 additions & 1 deletion src/Database/Wrapper/EntityManagerWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Doctrine\ORM\Exception\ORMException;
use Doctrine\ORM\Internal\Hydration\AbstractHydrator;
use Doctrine\Persistence\ObjectRepository;
use Throwable;
use function get_object_vars;

class EntityManagerWrapper extends EntityManagerDecorator implements ManagerIndicateInterface
Expand Down
1 change: 0 additions & 1 deletion src/Database/Wrapper/EntityRepositoryWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Doctrine\Common\Collections\Selectable;
use Doctrine\ORM\EntityRepository;
use Doctrine\Persistence\ObjectRepository;
use Throwable;
use function func_get_args;

/**
Expand Down
2 changes: 0 additions & 2 deletions src/HttpKernel/Helper/KernelMiddlewareLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
use function ksort;
use function trim;
use function ucfirst;
use function var_dump;
use const SORT_ASC;
use const SORT_DESC;

class KernelMiddlewareLoader extends AbstractLoaderNameBased
{
Expand Down
2 changes: 0 additions & 2 deletions src/Kernel/HttpKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,10 @@
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Throwable;
use function ksort;
use function memory_get_usage;
use function microtime;
use function spl_object_hash;
use function strtoupper;
use const SORT_DESC;

/**
* @mixin RouterInterface
Expand Down
1 change: 0 additions & 1 deletion src/Middleware/AbstractMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Throwable;

abstract class AbstractMiddleware implements MiddlewareInterface, ManagerIndicateInterface, ContainerIndicateInterface
{
Expand Down
2 changes: 1 addition & 1 deletion src/Scheduler/Scheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ function (Task $a, Task $b) {
}

/**
* @param ?int $timeout in seconds. if after processed time greater than time scheduler will stopped
* @param ?int $timeout in seconds. if after processed time greater than time scheduler will stop
* @return int
*/
public function run(?int $timeout = null): int
Expand Down
6 changes: 0 additions & 6 deletions src/Traits/Manager/ManagerDispatcherTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,8 @@

namespace ArrayAccess\TrayDigita\Traits\Manager;

use ArrayAccess\TrayDigita\Event\Interfaces\ManagerIndicateInterface;
use ArrayAccess\TrayDigita\Event\Interfaces\ManagerInterface;
use ArrayAccess\TrayDigita\Util\Filter\ContainerHelper;
use ReflectionNamedType;
use ReflectionObject;
use ReflectionUnionType;
use function debug_backtrace;
use function is_subclass_of;
use function reset;
use function ucfirst;
use const DEBUG_BACKTRACE_IGNORE_ARGS;
Expand Down
12 changes: 12 additions & 0 deletions src/Uploader/Chunk.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ class Chunk implements ManagerAllocatorInterface, ContainerIndicateInterface

private int $maxUploadFileSize;

private bool $allowRevertPosition = true;

public function __construct(
protected ContainerInterface $container,
?string $storageDir = null
Expand Down Expand Up @@ -119,6 +121,16 @@ public function __construct(
$this->maxUploadFileSize = Consolidation::getMaxUploadSize();
}

public function isAllowRevertPosition(): bool
{
return $this->allowRevertPosition;
}

public function setAllowRevertPosition(bool $allowRevertPosition): void
{
$this->allowRevertPosition = $allowRevertPosition;
}

public function getContainer(): ?ContainerInterface
{
return $this->container;
Expand Down
48 changes: 29 additions & 19 deletions src/Uploader/ChunkHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use function file_get_contents;
use function file_put_contents;
use function filesize;
use function fseek;
use function is_array;
use function is_file;
use function is_float;
Expand Down Expand Up @@ -263,10 +264,11 @@ public function getSize(): int

/**
* @param string $mode
* @param int $offset
*
* @return int
*/
private function writeResource(string $mode) : int
private function writeResource(string $mode, int $offset) : int
{
if (file_exists($this->targetCacheFile) && !is_writable($this->targetCacheFile)) {
$this->status = self::STATUS_FAIL;
Expand Down Expand Up @@ -308,14 +310,16 @@ private function writeResource(string $mode) : int
if ($uploadedStream->isSeekable()) {
$uploadedStream->rewind();
}

fseek($this->cacheResource, $offset);
$this->written = 0;
while (!$uploadedStream->eof()) {
$this->written += (int) fwrite($this->cacheResource, $uploadedStream->read(2048));
}
$isFirst = $this->size === 0;
$stat = Consolidation::callbackReduceError(fn () => fstat($this->cacheResource));
$this->size = $stat ? (int) ($stat['size']??$this->size+$this->written) : ($this->size+$this->written);
$this->size = $stat ? (int) (
$stat['size']??($offset + $this->written)
) : ($offset + $this->written);
flock($this->cacheResource, LOCK_EX);
$written = null;
$time = $_SERVER['REQUEST_FLOAT_TIME']??null;
Expand Down Expand Up @@ -365,41 +369,47 @@ private function writeResource(string $mode) : int
json_encode($written, JSON_UNESCAPED_SLASHES)
));
}

return $this->written;
}

/**
* @param ?int $position
* @param ?int $offset
*
* @return int
*/
public function start(?int $position = null): int
public function start(?int $offset = null): int
{
if ($this->status === self::STATUS_WAITING) {
$this->check();
}

$position ??= $this->size;
$offset ??= $this->size;
if ($this->status !== self::STATUS_READY) {
return $this->written;
}

$mode = 'ab+';
if ($position === 0) {
$mode = 'wb+';
} elseif ($position !== $this->size) {
throw new InvalidOffsetPositionException(
$position,
$this->size,
$this->processor->chunk->translateContext(
'Offset upload position is invalid.',
'chunk-uploader'
)
);
$mode = 'wb+';
if ($offset > 0) {
$allowRevertPosition = $this->processor->chunk->isAllowRevertPosition();
if (!$allowRevertPosition && $offset !== $this->size
|| $allowRevertPosition && $offset > $this->size
) {
// do delete
$this->deletePartial();
throw new InvalidOffsetPositionException(
$offset,
$this->size,
$this->processor->chunk->translateContext(
'Offset upload position is invalid.',
'chunk-uploader'
)
);
}
}

$this->status = self::STATUS_RESUME;
return $this->writeResource($mode);
return $this->writeResource($mode, $offset);
}

/**
Expand Down
Loading

0 comments on commit b3cbc16

Please sign in to comment.