Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to remove file for ReplacingFile #1462

Merged
merged 4 commits into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
40 changes: 39 additions & 1 deletion src/FileAbstraction/ReplacingFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,54 @@
namespace Vich\UploaderBundle\FileAbstraction;

use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
* This class can be used to signal that the given file should be "uploaded" into the Vich-abstraction
* in cases where it is not possible to construct an `UploadedFile`.
*/
class ReplacingFile extends File
{
protected bool $removeReplacedFile;
garak marked this conversation as resolved.
Show resolved Hide resolved
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
{
return $this->getFilename();
}

public function isRemoveReplacedFile(): bool
{
return $this->removeReplacedFile;
}

public function setRemoveReplacedFile(bool $removeReplacedFile): self
garak marked this conversation as resolved.
Show resolved Hide resolved
{
$this->removeReplacedFile = $removeReplacedFile;

return $this;
}

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

public function setRemoveReplacedFileOnError(bool $removeReplacedFileOnError): self
garak marked this conversation as resolved.
Show resolved Hide resolved
{
$this->removeReplacedFileOnError = $removeReplacedFileOnError;

return $this;
}
}
18 changes: 17 additions & 1 deletion src/Storage/AbstractStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,23 @@ 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()
) {
unlink($file->getPathname());
}
}

abstract protected function doRemove(PropertyMapping $mapping, ?string $dir, string $name): ?bool;
Expand Down
Loading