mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-20 17:15:51 +02:00
Filter out duplicate file DTO returned by the info providers
This commit is contained in:
parent
76295b73c8
commit
0f0adfcf36
3 changed files with 40 additions and 7 deletions
2
VERSION
2
VERSION
|
@ -1 +1 @@
|
|||
1.9.0
|
||||
1.9.1-dev
|
||||
|
|
|
@ -186,7 +186,8 @@ final class DTOtoEntityConverter
|
|||
}
|
||||
|
||||
//Add other images
|
||||
foreach ($dto->images ?? [] as $image) {
|
||||
$images = $this->files_unique($dto->images ?? []);
|
||||
foreach ($images as $image) {
|
||||
//Ensure that the image is not the same as the preview image
|
||||
if ($image->url === $dto->preview_image_url) {
|
||||
continue;
|
||||
|
@ -195,10 +196,10 @@ final class DTOtoEntityConverter
|
|||
$entity->addAttachment($this->convertFile($image, $image_type));
|
||||
}
|
||||
|
||||
|
||||
//Add datasheets
|
||||
$datasheet_type = $this->getDatasheetType();
|
||||
foreach ($dto->datasheets ?? [] as $datasheet) {
|
||||
$datasheets = $this->files_unique($dto->datasheets ?? []);
|
||||
foreach ($datasheets as $datasheet) {
|
||||
$entity->addAttachment($this->convertFile($datasheet, $datasheet_type));
|
||||
}
|
||||
|
||||
|
@ -210,6 +211,27 @@ final class DTOtoEntityConverter
|
|||
return $entity;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the given array of files with all duplicates removed.
|
||||
* @param FileDTO[] $files
|
||||
* @return FileDTO[]
|
||||
*/
|
||||
private function files_unique(array $files): array
|
||||
{
|
||||
$unique = [];
|
||||
//We use the URL and name as unique identifier. If two file DTO have the same URL and name, they are considered equal
|
||||
//and get filtered out, if it already exists in the array
|
||||
foreach ($files as $file) {
|
||||
//Skip already existing files, to preserve the order. The second condition ensure that we keep the version with a name over the one without a name
|
||||
if (isset($unique[$file->url]) && $unique[$file->url]->name !== null) {
|
||||
continue;
|
||||
}
|
||||
$unique[$file->url] = $file;
|
||||
}
|
||||
|
||||
return array_values($unique);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the existing entity of the given class with the given name or create it if it does not exist.
|
||||
* If the name is null, null is returned.
|
||||
|
|
|
@ -146,11 +146,11 @@ class DTOtoEntityConverterTest extends WebTestCase
|
|||
$this->assertEquals($type, $entity->getAttachmentType());
|
||||
}
|
||||
|
||||
public function testConvertPart()
|
||||
public function testConvertPart(): void
|
||||
{
|
||||
$parameters = [new ParameterDTO('Test', 'Test')];
|
||||
$datasheets = [new FileDTO('https://invalid.invalid/file.pdf')];
|
||||
$images = [new FileDTO('https://invalid.invalid/image.png')];
|
||||
$datasheets = [new FileDTO('https://invalid.invalid/file.pdf'), new FileDTO('https://invalid.invalid/file.pdf', name: 'TestFile')];
|
||||
$images = [new FileDTO('https://invalid.invalid/image.png'), new FileDTO('https://invalid.invalid/image2.png', name: 'TestImage2'), new FileDTO('https://invalid.invalid/image2.png')];
|
||||
$shopping_infos = [new PurchaseInfoDTO('TestDistributor', 'TestOrderNumber', [new PriceDTO(1, "10.0", 'EUR')])];
|
||||
|
||||
$dto = new PartDetailDTO(
|
||||
|
@ -179,5 +179,16 @@ class DTOtoEntityConverterTest extends WebTestCase
|
|||
//The actual content is tested in the corresponding tests
|
||||
$this->assertCount(count($parameters), $entity->getParameters());
|
||||
$this->assertCount(count($shopping_infos), $entity->getOrderdetails());
|
||||
|
||||
//Datasheets and images are stored as attachments and the duplicates, should be filtered out
|
||||
$this->assertCount(3, $entity->getAttachments());
|
||||
//The attachments should have the name of the named duplicate file
|
||||
$image1 = $entity->getAttachments()[0];
|
||||
$this->assertEquals('Main image', $image1->getName());
|
||||
|
||||
$image1 = $entity->getAttachments()[1];
|
||||
|
||||
$datasheet = $entity->getAttachments()[2];
|
||||
$this->assertEquals('TestFile', $datasheet->getName());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue