Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/config/doctrine.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ doctrine:
datetime: Common\Doctrine\Type\UTCDateTimeType
datetimetz: Common\Doctrine\Type\UTCDateTimeType
time: Common\Doctrine\Type\UTCTimeType
uuid: Ramsey\Uuid\Doctrine\UuidType
uuid: Common\Doctrine\Type\UuidType

orm:
auto_generate_proxy_classes: "%kernel.debug%"
Expand Down
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@
"matthiasmullie/minify": "~1.3",
"matthiasmullie/scrapbook": "^1.3",
"pimple/pimple": "^3.2",
"ramsey/uuid": "^4.0",
"ramsey/uuid-doctrine": "^2.0",
"spoon/library": "^4.0",
"symfony/monolog-bundle": "^3.1",
"symfony/console": "^5.4",
Expand All @@ -59,7 +57,8 @@
"twig/extra-bundle": "^3.23",
"twig/cssinliner-extra": "^3.23",
"twig/string-extra": "^3.23",
"twig/inky-extra": "^3.23"
"twig/inky-extra": "^3.23",
"symfony/uid": "^5.4"
},
"require-dev": {
"jdorn/sql-formatter": "1.2.17",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace Backend\Modules\MediaGalleries\Domain\MediaGallery;

use Doctrine\ORM\Mapping as ORM;
use Backend\Core\Engine\Model;
use Backend\Modules\MediaLibrary\Domain\MediaGroup\MediaGroup;
use Common\ModuleExtraType;
use Doctrine\ORM\Mapping as ORM;
use Ramsey\Uuid\Doctrine\UuidGenerator;
use Symfony\Component\Uid\Uuid;

/**
* @ORM\Entity(repositoryClass="Backend\Modules\MediaGalleries\Domain\MediaGallery\MediaGalleryRepository")
Expand All @@ -15,11 +15,10 @@
class MediaGallery
{
/**
* @var string
* @var Uuid
*
* @ORM\Id()
* @ORM\CustomIdGenerator(class=UuidGenerator::class)
* @ORM\Column(type="guid")
* @ORM\Column(type="uuid", unique=true)
*/
private $id;

Expand Down Expand Up @@ -106,6 +105,7 @@ private function __construct(
Status $status,
?string $text = null
) {
$this->id = Uuid::v4();
$this->userId = $userId;
$this->action = $action;
$this->title = $title;
Expand Down
4 changes: 4 additions & 0 deletions src/Backend/Modules/MediaLibrary/Ajax/MediaItemUpload.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public function execute(): void
{
parent::execute();

dump('starting upload');
$this->request = $this->getRequest();
$this->response = $this->createResponse();
$this->uploadHandler = $this->createUploader();
Expand All @@ -102,6 +103,7 @@ public function execute(): void
// Remove the old folder
$this->fileManager->deleteFolder($this->uploadDirectory . '/' . $result['uuid']);
}
dump($newName);

/** @var CreateMediaItemFromLocalStorageType $createMediaItem */
$createMediaItemFromLocalSource = new CreateMediaItemFromLocalStorageType(
Expand All @@ -112,6 +114,8 @@ public function execute(): void

$this->messageBus->dispatch($createMediaItemFromLocalSource);

dump('finished upload');

$this->sendResponseForResult(
array_merge(
$result,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
use Backend\Modules\MediaLibrary\Domain\MediaGroupMediaItem\MediaGroupMediaItem;
use Backend\Modules\MediaLibrary\Domain\MediaItem\Exception\MediaItemNotFound;
use Backend\Modules\MediaLibrary\Domain\MediaItem\MediaItemRepository;
use Symfony\Component\Uid\Uuid;
use Doctrine\ORM\EntityManagerInterface;
use Ramsey\Uuid\Uuid;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;

#[AsMessageHandler]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use JsonSerializable;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
use Symfony\Component\Uid\Uuid;

/**
* MediaGroup
Expand Down Expand Up @@ -54,7 +53,7 @@ private function __construct(
* @ORM\Id
* @ORM\Column(type="uuid")
*/
private UuidInterface $id,
private Uuid $id,
/**
* @ORM\Column(type="media_group_type")
*/
Expand All @@ -67,13 +66,13 @@ public static function create(
Type $type
): self {
return new self(
Uuid::uuid4(),
Uuid::v4(),
$type
);
}

public static function createFromId(
UuidInterface $id,
Uuid $id,
Type $type
): self {
return new self(
Expand Down Expand Up @@ -123,7 +122,7 @@ public function jsonSerialize(): array
];
}

public function getId(): UuidInterface
public function getId(): Uuid
{
return $this->id;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Backend\Modules\MediaLibrary\Domain\MediaGroup;

use Ramsey\Uuid\Uuid;
use Symfony\Component\Uid\Uuid;

class MediaGroupDataTransferObject
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Backend\Modules\MediaLibrary\Domain\MediaItem\StorageType;
use Backend\Modules\MediaLibrary\Domain\MediaItem\Type as MediaItemPossibleType;
use Backend\Modules\MediaLibrary\Domain\MediaGroup\Type as MediaGroupPossibleType;
use Ramsey\Uuid\Uuid;
use Symfony\Component\Uid\Uuid;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\CallbackTransformer;
use Symfony\Component\Form\Exception\LogicException;
Expand Down Expand Up @@ -183,7 +183,7 @@ private function getMediaGroupFromMediaGroupData(array $mediaGroupData): MediaGr
private function getMediaGroupTransformFunction(): callable
{
return static fn(MediaGroup $mediaGroup) => [
'id' => $mediaGroup->getId(),
'id' => $mediaGroup->getId()->toRfc4122(),
'type' => $mediaGroup->getType(),
'mediaIds' => implode(',', $mediaGroup->getIdsForConnectedItems()),
'mediaGroup' => $mediaGroup,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
use Backend\Modules\MediaLibrary\Domain\MediaItem\MediaItem;
use Backend\Modules\MediaLibrary\Domain\MediaGroup\MediaGroup;
use JsonSerializable;
use Ramsey\Uuid\Doctrine\UuidGenerator;
use Ramsey\Uuid\UuidInterface;
use Symfony\Component\Uid\Uuid;

/**
* MediaGroup MediaItem
Expand All @@ -18,11 +17,9 @@
class MediaGroupMediaItem implements JsonSerializable
{
/**
* @var UuidInterface
* @var Uuid
*
* @ORM\Id()
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class=UuidGenerator::class)
* @ORM\Column(type="uuid", unique=true)
*/
private $id;
Expand Down Expand Up @@ -81,6 +78,7 @@ private function __construct(
MediaItem $item,
int $sequence
) {
$this->id = Uuid::v4();
$this->group = $group;
$this->item = $item;
$this->createdOn = new DateTime();
Expand All @@ -102,14 +100,14 @@ public static function create(
public function jsonSerialize(): array
{
return [
'id' => $this->id,
'id' => $this->id->toRfc4122(),
'item' => $this->item,
'createdOn' => $this->createdOn->getTimestamp(),
'sequence' => $this->sequence,
];
}

public function getId(): int
public function getId(): Uuid
{
return $this->id;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public function __invoke(CreateMediaItemFromLocalStorageType $createMediaItemFro

$createMediaItemFromLocalStorageType->setMediaItem($mediaItem);

dump($mediaItem);
$this->entityManager->flush();
dump('flushed');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
use Doctrine\Common\Collections\ArrayCollection;
use Exception;
use JsonSerializable;
use Ramsey\Uuid\Doctrine\UuidGenerator;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
use Backend\Modules\MediaLibrary\Domain\MediaFolder\MediaFolder;
use Backend\Core\Engine\Model;
use Symfony\Component\Uid\Uuid;

/**
* MediaItem
Expand All @@ -26,12 +26,10 @@
class MediaItem implements JsonSerializable
{
/**
* @var string
* @var Uuid
*
* @ORM\Id()
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class=UuidGenerator::class)
* @ORM\Column(type="guid")
* @ORM\Column(type="uuid", unique=true)
*/
private $id;

Expand Down Expand Up @@ -163,6 +161,7 @@ private function __construct(
MediaFolder $folder,
int $userId
) {
$this->id = Uuid::v4();
$this->folder = $folder;
$this->userId = $userId;
$this->type = $type;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
doctrine:
dbal:
types:
uuid: Ramsey\Uuid\Doctrine\UuidType
uuid: Common\Doctrine\Type\UuidType
media_item_storage_type: Backend\Modules\MediaLibrary\Domain\MediaItem\StorageTypeDBALType
media_item_type: Backend\Modules\MediaLibrary\Domain\MediaItem\TypeDBALType
media_item_aspect_ratio: Backend\Modules\MediaLibrary\Domain\MediaItem\AspectRatioDBALType
Expand Down
68 changes: 68 additions & 0 deletions src/Common/Doctrine/Type/UuidType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Common\Doctrine\Type;

use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\GuidType;
use Doctrine\DBAL\Types\ConversionException;
use Throwable;
use Symfony\Component\Uid\Uuid;

final class UuidType extends GuidType
{
public const NAME = 'uuid';

public function convertToPHPValue($value, AbstractPlatform $platform): ?Uuid
{
if ($value instanceof Uuid) {
return $value;
}

if (!is_string($value) || $value === '') {
return null;
}

try {
return Uuid::fromString($value);
} catch (Throwable) {
throw ConversionException::conversionFailed($value, self::NAME);
}
}

public function convertToDatabaseValue($value, AbstractPlatform $platform): ?string
{
if ($value === null || $value === '') {
return null;
}

Comment on lines +32 to +37
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (bug_risk): Treating empty-string values as null in convertToDatabaseValue may mask invalid inputs.

Because convertToDatabaseValue maps both null and '' to null, an empty string ID from the application will be stored as NULL instead of failing conversion. This can hide invalid input and violate NOT NULL expectations. Consider only treating null as null and letting '' go through normal validation so it raises a ConversionException if it’s not a valid UUID.

Suggested change
public function convertToDatabaseValue($value, AbstractPlatform $platform): ?string
{
if ($value === null || $value === '') {
return null;
}
public function convertToDatabaseValue($value, AbstractPlatform $platform): ?string
{
if ($value === null) {
return null;
}

if ($value instanceof Uuid) {
return $value->toRfc4122();
}

if (is_string($value) || (is_object($value) && method_exists($value, '__toString'))) {
$string = (string) $value;

if ($string !== '' && Uuid::isValid($string)) {
return $string;
}
}

throw ConversionException::conversionFailed($value, self::NAME);
}

public function getName(): string
{
return self::NAME;
}

public function requiresSQLCommentHint(AbstractPlatform $platform): bool
{
return true;
}

/** @return string[] */
public function getMappedDatabaseTypes(AbstractPlatform $platform): array
{
return [self::NAME];
}
}
Loading