Skip to content
Merged
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
35 changes: 34 additions & 1 deletion src/AttachmentManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,39 @@ public function upload(UploadedFile $file, ?string $desiredPath = null): Attachm
]);
}

/**
* Replace existing file on disk and update database entry.
*
* @throws DestinationAlreadyExistsException
* @throws DisallowedCharacterException
*/
public function replace(UploadedFile $file, Attachment $attachment): Attachment
{
$filename = new Filename($file);

$this->validateBasename($filename);

$disk = $this->getFilesystem();

$path = implode('/', array_filter([$attachment->path, $filename]));

if ($disk->exists($path) && $path !== $attachment->full_path) {
throw new DestinationAlreadyExistsException();
}

$disk->delete($attachment->full_path);
$disk->put($path, $file->getContent());

$attachment->update([
'name' => $filename->name,
'extension' => $filename->extension,
'mime_type' => $file->getMimeType(),
'size' => $file->getSize(),
]);

return $attachment;
}

/**
* Validate filename and throw exception if validation fails.
*
Expand Down Expand Up @@ -222,7 +255,7 @@ public function rename(Attachment $file, string $name): void
*
* @throws DestinationAlreadyExistsException if conflicting file exists in desired path.
*/
public function move(Attachment $file, string $desiredPath): void
public function move(Attachment $file, ?string $desiredPath): void
{
$disk = $this->getFilesystem();
$path = "{$desiredPath}/{$file->filename}";
Expand Down