Skip to content

Commit a466426

Browse files
committed
Fix delete old file when overwriging
1 parent 069151e commit a466426

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

src/Flysystem/FilesystemProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function __construct(private readonly ServiceLocator $filesystems)
3535
* @throws ContainerExceptionInterface
3636
* @throws NotFoundExceptionInterface
3737
*/
38-
public function getFilesystem(string $name): ?Filesystem
38+
public function getFilesystem(string $name): Filesystem
3939
{
4040
return $this->filesystems->get($name);
4141
}

src/Helper/Uploadable/UploadableFileManager.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace Silverback\ApiComponentsBundle\Helper\Uploadable;
1515

1616
use Doctrine\Common\Collections\ArrayCollection;
17+
use Doctrine\ORM\Mapping\ClassMetadata;
1718
use Doctrine\Persistence\ManagerRegistry;
1819
use Liip\ImagineBundle\Service\FilterService;
1920
use Silverback\ApiComponentsBundle\Annotation\UploadableField;
@@ -135,22 +136,18 @@ public function persistFiles(object $object): void
135136
if (!$file) {
136137
// so we need to know if it was a deleted field from the denormalizer
137138
if ($this->deletedFields->contains($fieldConfiguration->property)) {
138-
// this will not have been updated yet, original database value - string file path
139-
$currentFilepath = $classMetadata->getFieldValue($object, $fieldConfiguration->property);
140-
if ($currentFilepath) {
141-
$this->removeFilepath($object, $fieldConfiguration);
142-
// file path set to null
143-
$classMetadata->setFieldValue($object, $fieldConfiguration->property, null);
144-
}
139+
$this->deleteFileForField($object, $classMetadata, $fieldConfiguration);
140+
$classMetadata->setFieldValue($object, $fieldConfiguration->property, null);
145141
}
146142
continue;
147143
}
148144

145+
$this->deleteFileForField($object, $classMetadata, $fieldConfiguration);
149146
$filesystem = $this->filesystemProvider->getFilesystem($fieldConfiguration->adapter);
150147

151148
$path = $fieldConfiguration->prefix ?? '';
152149
$path .= $file->getFilename();
153-
$stream = fopen($file->getRealPath(), 'r');
150+
$stream = fopen($file->getRealPath(), 'rb');
154151
$filesystem->writeStream(
155152
$path,
156153
$stream,
@@ -169,10 +166,15 @@ public function deleteFiles(object $object): void
169166

170167
$configuredProperties = $this->annotationReader->getConfiguredProperties($object, true);
171168
foreach ($configuredProperties as $fileProperty => $fieldConfiguration) {
172-
$currentFilepath = $classMetadata->getFieldValue($object, $fieldConfiguration->property);
173-
if ($currentFilepath) {
174-
$this->removeFilepath($object, $fieldConfiguration);
175-
}
169+
$this->deleteFileForField($object, $classMetadata, $fieldConfiguration);
170+
}
171+
}
172+
173+
private function deleteFileForField(object $object, ClassMetadata $classMetadata, UploadableField $fieldConfiguration): void
174+
{
175+
$currentFilepath = $classMetadata->getFieldValue($object, $fieldConfiguration->property);
176+
if ($currentFilepath) {
177+
$this->removeFilepath($object, $fieldConfiguration);
176178
}
177179
}
178180

0 commit comments

Comments
 (0)