Use XString random string generation.

This commit is contained in:
flash 2023-11-07 21:47:35 +00:00
parent c546bf6da8
commit 0da6cd7fe5

View file

@ -6,13 +6,12 @@ use InvalidArgumentException;
use RuntimeException; use RuntimeException;
use Imagick; use Imagick;
use JsonSerializable; use JsonSerializable;
use Index\XString;
use Index\Data\IDbConnection; use Index\Data\IDbConnection;
use Index\Data\IDbResult; use Index\Data\IDbResult;
use Index\Data\DbType; use Index\Data\DbType;
final class Upload implements JsonSerializable { final class Upload implements JsonSerializable {
private const ID_CHARS = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789-_';
public function __construct( public function __construct(
private string $id = '', private string $id = '',
private int $userId = 0, private int $userId = 0,
@ -197,14 +196,6 @@ final class Upload implements JsonSerializable {
]; ];
} }
public static function generateId(int $length = 32): string {
$bytes = str_repeat("\0", $length);
$chars = strlen(self::ID_CHARS);
for($i = 0; $i < $length; ++$i)
$bytes[$i] = self::ID_CHARS[random_int(0, $chars - 1)];
return $bytes;
}
public static function create( public static function create(
IDbConnection $conn, IDbConnection $conn,
Application $app, User $user, Application $app, User $user,
@ -224,7 +215,7 @@ final class Upload implements JsonSerializable {
if($fileExpiry < 0) if($fileExpiry < 0)
throw new InvalidArgumentException('$fileExpiry must be a positive integer.'); throw new InvalidArgumentException('$fileExpiry must be a positive integer.');
$id = self::generateId(); $id = XString::random(32);
$create = $conn->prepare( $create = $conn->prepare(
'INSERT INTO `prm_uploads` (' 'INSERT INTO `prm_uploads` ('
. ' `upload_id`, `app_id`, `user_id`, `upload_name`,' . ' `upload_id`, `app_id`, `user_id`, `upload_name`,'