Flex storages: added support for custom key length

This commit is contained in:
Matias Griese
2020-03-06 10:21:49 +02:00
parent 5aefc60f2e
commit d80a29b34d
3 changed files with 5 additions and 1 deletions

View File

@@ -34,6 +34,8 @@ abstract class AbstractFilesystemStorage implements FlexStorageInterface
protected $dataFormatter;
/** @var string */
protected $keyField = 'storage_key';
/** @var string */
protected $keyLen = '32';
/**
* @return bool
@@ -191,7 +193,7 @@ abstract class AbstractFilesystemStorage implements FlexStorageInterface
*/
protected function generateKey(): string
{
return substr(hash('sha256', random_bytes(32)), 0, 32);
return substr(hash('sha256', random_bytes($this->keyLen)), 0, $this->keyLen);
}
/**

View File

@@ -641,6 +641,7 @@ class FolderStorage extends AbstractFilesystemStorage
$this->prefixed = (bool)($options['prefixed'] ?? strpos($pattern, '/{KEY:2}/'));
$this->indexed = (bool)($options['indexed'] ?? false);
$this->keyField = $options['key'] ?? 'storage_key';
$this->keyLen = (int)($options['key_len'] ?? 32);
$variables = ['FOLDER' => '%1$s', 'KEY' => '%2$s', 'KEY:2' => '%3$s', 'FILE' => '%4$s', 'EXT' => '%5$s'];
$pattern = Utils::simpleTemplate($pattern, $variables);

View File

@@ -54,6 +54,7 @@ class SimpleStorage extends AbstractFilesystemStorage
$this->dataPattern = basename($pattern, $extension) . $extension;
$this->dataFolder = $filesystem->dirname($options['folder']);
$this->keyField = $options['key'] ?? 'storage_key';
$this->keyLen = (int)($options['key_len'] ?? 32);
$this->prefix = $options['prefix'] ?? null;
// Make sure that the data folder exists.