mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-10 08:44:58 +00:00
Implement TempUrlLocalStorageGenerator and its tests
Added the full implementation for TempUrlLocalStorageGenerator, including methods to generate signed URLs and POST requests with expiration and signature logic. Introduced corresponding unit tests to validate functionality using mocked dependencies.
This commit is contained in:
@@ -14,16 +14,62 @@ namespace Chill\DocStoreBundle\AsyncUpload\Driver\LocalStorage;
|
||||
use Chill\DocStoreBundle\AsyncUpload\SignedUrl;
|
||||
use Chill\DocStoreBundle\AsyncUpload\SignedUrlPost;
|
||||
use Chill\DocStoreBundle\AsyncUpload\TempUrlGeneratorInterface;
|
||||
use Chill\DocStoreBundle\Entity\StoredObject;
|
||||
use Symfony\Component\Clock\ClockInterface;
|
||||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
||||
|
||||
class TempUrlLocalStorageGenerator implements TempUrlGeneratorInterface
|
||||
{
|
||||
private const SIGNATURE_DURATION = 180;
|
||||
|
||||
public function __construct(
|
||||
private readonly string $secret,
|
||||
private readonly ClockInterface $clock,
|
||||
private readonly UrlGeneratorInterface $urlGenerator,
|
||||
) {}
|
||||
|
||||
public function generate(string $method, string $object_name, ?int $expire_delay = null): SignedUrl
|
||||
{
|
||||
// TODO: Implement generate() method.
|
||||
$expiration = $this->clock->now()->getTimestamp() + min($expire_delay ?? self::SIGNATURE_DURATION, self::SIGNATURE_DURATION);
|
||||
|
||||
return new SignedUrl(
|
||||
$method,
|
||||
$this->urlGenerator->generate('chill_docstore_stored_object_operate', [
|
||||
'object_name' => $object_name,
|
||||
'exp' => $expiration,
|
||||
'sig' => $this->sign($method, $object_name, $expiration),
|
||||
], UrlGeneratorInterface::ABSOLUTE_URL),
|
||||
\DateTimeImmutable::createFromFormat('U', (string) $expiration),
|
||||
$object_name,
|
||||
);
|
||||
}
|
||||
|
||||
public function generatePost(?int $expire_delay = null, ?int $submit_delay = null, int $max_file_count = 1, ?string $object_name = null): SignedUrlPost
|
||||
{
|
||||
// TODO: Implement generatePost() method.
|
||||
$submitDelayComputed = min($submit_delay ?? self::SIGNATURE_DURATION, self::SIGNATURE_DURATION);
|
||||
$expireDelayComputed = min($expire_delay ?? self::SIGNATURE_DURATION, self::SIGNATURE_DURATION);
|
||||
$objectNameComputed = $object_name ?? StoredObject::generatePrefix();
|
||||
$expiration = $this->clock->now()->getTimestamp() + $expireDelayComputed + $submitDelayComputed;
|
||||
|
||||
return new SignedUrlPost(
|
||||
$this->urlGenerator->generate(
|
||||
'chill_docstore_storedobject_post',
|
||||
['prefix' => $objectNameComputed],
|
||||
UrlGeneratorInterface::ABSOLUTE_URL
|
||||
),
|
||||
\DateTimeImmutable::createFromFormat('U', (string) $expiration),
|
||||
$objectNameComputed,
|
||||
15_000_000,
|
||||
1,
|
||||
$submitDelayComputed,
|
||||
'',
|
||||
$objectNameComputed,
|
||||
$this->sign('POST', $object_name, $expiration),
|
||||
);
|
||||
}
|
||||
|
||||
private function sign(string $method, string $object_name, int $expiration): string
|
||||
{
|
||||
return hash_hmac('sha512', $method, sprintf('%s.%s.%s.%d', $method, $this->secret, $object_name, $expiration));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user