Skip to content

Commit

Permalink
Ability to remove file for ReplacingFile on error
Browse files Browse the repository at this point in the history
  • Loading branch information
iksmail committed Aug 24, 2024
1 parent 12294d2 commit 3c03cb4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
24 changes: 21 additions & 3 deletions src/FileAbstraction/ReplacingFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,18 @@
class ReplacingFile extends File
{
protected bool $removeReplacedFile;

public function __construct(string $path, bool $checkPath = true, bool $removeReplacedFile = false)
{
protected bool $removeReplacedFileOnError;

public function __construct(
string $path,
bool $checkPath = true,
bool $removeReplacedFile = false,
bool $removeReplacedFileOnError = false
) {
parent::__construct($path, $checkPath);

$this->removeReplacedFile = $removeReplacedFile;
$this->removeReplacedFileOnError = $removeReplacedFileOnError;
}

public function getClientOriginalName(): string
Expand All @@ -37,4 +43,16 @@ public function setRemoveReplacedFile(bool $removeReplacedFile): self

return $this;
}

public function isRemoveReplacedFileOnError(): bool
{
return $this->removeReplacedFileOnError;
}

public function setRemoveReplacedFileOnError(bool $removeReplacedFileOnError): self
{
$this->removeReplacedFileOnError = $removeReplacedFileOnError;

return $this;
}
}
12 changes: 11 additions & 1 deletion src/Storage/AbstractStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,17 @@ public function upload(object $obj, PropertyMapping $mapping): void

$dir = $mapping->getUploadDir($obj);

$this->doUpload($mapping, $file, $dir, $name);
try {
$this->doUpload($mapping, $file, $dir, $name);
} catch (\Exception $e) {
if ($file instanceof ReplacingFile
&& $file->isRemoveReplacedFileOnError()
) {
unlink($file->getPathname());
}

throw $e;
}

if ($file instanceof ReplacingFile
&& $file->isRemoveReplacedFile()
Expand Down

0 comments on commit 3c03cb4

Please sign in to comment.