Commit d6412172 authored by Richard Weinhold's avatar Richard Weinhold 🎩

refactors moveTo methods to match new copyTo usage

parent 9579b8e2
Pipeline #5408 passed with stages
in 9 seconds
......@@ -126,7 +126,9 @@ class File extends FileSystem
} elseif (!$destination->doesSatisfyConstraints()) {
throw new AccessDeniedException('unable to open destination file', 403, $destination->getConstraintViolations());
} elseif ($destination->isFile() && !$destination->isWriteable()) {
throw new AccessDeniedException('unable to open destination file', 403);
throw new AccessDeniedException('unable to write to destination file', 403);
} elseif (!$destination->isFile() && !is_writable($destination->path()->directory)) {
throw new AccessDeniedException('unable to write to destination directory', 403);
}
// ensure the destination-path points to a filename
......@@ -154,11 +156,6 @@ class File extends FileSystem
{
$destination->setConstraints(($constraints !== null) ? $constraints : $this->storage->getConstraints());
// unable to move file from memory to disk, we use copy instead
if (!$this->storage instanceof Storage\Disk) {
return $this->copyTo($destination, $constraints);
}
$this->checkFileReadPermissions();
// validate constraints
......@@ -172,23 +169,11 @@ class File extends FileSystem
throw new AccessDeniedException('unable to write to destination directory', 403);
}
$this->storage->removeOnFree(false);
if ($this->storage instanceof Storage\Disk\Uploaded) {
// move uploaded file
if (!move_uploaded_file($this->storage->path()->real, $destination->path()->raw)) {
throw new AccessDeniedException('unable to move uploaded file', 403);
}
} else {
// move file
if (!rename($this->storage->path()->real, $destination->path()->raw)) {
throw new AccessDeniedException('unable to move file', 403);
}
// actual move file to file: use native functions if possible
if (!$this->storage->moveFileTo($destination)) {
throw new AccessDeniedException('unable to move file', 403);
}
$destination->path()->reload();
return new static($destination);
}
......
......@@ -502,7 +502,6 @@ class Disk extends Storage
$destination->path()->reload();
return true;
case $destination instanceof Flysystem:
$readStream = $this->getStream('r');
try {
......@@ -511,7 +510,6 @@ class Disk extends Storage
\fclose($readStream);
}
case $destination instanceof Memory:
default:
return $destination->writeFile($this->readFile());
......@@ -523,6 +521,25 @@ class Disk extends Storage
*/
public function moveFileTo(Storage $destination): bool
{
return true;
switch (true) {
// copy file from disk to disk
case $destination instanceof Disk:
if (!\rename($this->path->real, $destination->path()->raw)) {
return false;
}
$destination->path()->reload();
return true;
case $destination instanceof Flysystem:
case $destination instanceof Memory:
default:
if (!$this->copyFileTo($destination)) {
return false;
}
$this->removeFile();
return true;
}
}
}
......@@ -118,4 +118,31 @@ class Uploaded extends Disk
{
return parent::setConstraints($constraints & ~Constraint::IN_SAFEPATH);
}
/**
* @inheritDoc
*/
public function moveFileTo(Storage $destination): bool
{
switch (true) {
// use native safe-move function for uploaded files
case $destination instanceof Disk:
if (!\move_uploaded_file($this->path->real, $destination->path()->raw)) {
return false;
}
$destination->path()->reload();
return true;
// use a temp-file for safe-move before moving file into destination-storage
case $destination instanceof Storage\Flysystem:
case $destination instanceof Storage\Memory:
default:
$temp = new Storage\Disk\Temp();
if (!\move_uploaded_file($this->path->real, $temp->path()->raw)) {
return false;
}
return $temp->moveFileTo($destination);
}
}
}
......@@ -391,7 +391,11 @@ class Flysystem extends Storage
case $destination instanceof Disk:
$readStream = $this->getStream('r');
try {
return $destination->writeFromStream($readStream);
if (!$destination->writeFromStream($readStream)) {
return false;
}
$destination->path()->reload();
return true;
} finally {
\fclose($readStream);
}
......@@ -410,6 +414,19 @@ class Flysystem extends Storage
*/
public function moveFileTo(Storage $destination): bool
{
return true;
switch (true) {
case $destination instanceof Flysystem:
return $this->flysystem->rename($this->path, $destination->path());
case $destination instanceof Disk:
case $destination instanceof Memory:
default:
if (!$this->copyFileTo($destination)) {
return false;
}
$this->removeFile();
return true;
}
}
}
......@@ -235,7 +235,6 @@ class Memory extends Storage
switch (true) {
case $destination instanceof Disk:
if (!$destination->writeFile($this->readFile())) {
return false;
}
......@@ -254,6 +253,10 @@ class Memory extends Storage
*/
public function moveFileTo(Storage $destination): bool
{
return true;
if (!$this->copyFileTo($destination)) {
return false;
}
return $this->removeFile();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment