Improve performance

This commit is contained in:
Dennis Eichhorn 2023-05-25 12:19:55 +00:00
parent 08ce435c7c
commit 1af1af6f86
2 changed files with 15 additions and 12 deletions

View File

@ -367,7 +367,7 @@ final class ApiController extends Controller
*/
public static function createMediaPath(string $basePath = '/Modules/Media/Files') : string
{
$rndPath = \str_pad(\dechex(\mt_rand(0, 4294967295)), 8, '0', \STR_PAD_LEFT);
$rndPath = \bin2hex(\random_bytes(4));
return $basePath . '/_' . $rndPath[0] . $rndPath[1] . $rndPath[2] . $rndPath[3] . '/_' . $rndPath[4] . $rndPath[5] . $rndPath[6] . $rndPath[7];
}

View File

@ -245,9 +245,8 @@ class UploadFile
*/
private function createFileName(string $path, string $tempName, string $extension) : string
{
$rnd = '';
$limit = -1;
$fileName = '';
$rnd = '';
$limit = -1;
$nameWithoutExtension = empty($tempName)
? ''
@ -256,19 +255,23 @@ class UploadFile
: \substr($tempName, 0, -\strlen($extension) - 1)
);
do {
$fileName = $tempName;
while (\is_file($path . '/' . $fileName)) {
if ($limit >= self::PATH_GENERATION_LIMIT) {
throw new \Exception('No file path could be found. Potential attack!');
}
++$limit;
$tempName = empty($nameWithoutExtension)
? \sha1($tempName . $rnd)
: $nameWithoutExtension . (empty($rnd) ? '' : '_' . $rnd);
: $nameWithoutExtension . ($limit === 1 ? '' : '_' . $rnd);
$tempName .= !empty($extension) ? '.' . $extension : '';
$fileName = $tempName;
$rnd = (string) \mt_rand();
} while (\is_file($path . '/' . $fileName) && $limit < self::PATH_GENERATION_LIMIT);
$fileName = empty($extension)
? $tempName
: $tempName . '.' . $extension;
if ($limit >= self::PATH_GENERATION_LIMIT) {
throw new \Exception('No file path could be found. Potential attack!');
$rnd = \bin2hex(\random_bytes(3));
}
return $fileName;