mirror of
https://github.com/Karaka-Management/oms-Media.git
synced 2026-01-11 17:08:40 +00:00
Improve performance
This commit is contained in:
parent
08ce435c7c
commit
1af1af6f86
|
|
@ -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];
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user