diff options
author | Roeland Jago Douma <roeland@famdouma.nl> | 2020-04-03 17:30:04 +0300 |
---|---|---|
committer | Roeland Jago Douma <roeland@famdouma.nl> | 2020-04-03 17:30:04 +0300 |
commit | 3c4c88721a106d10b31de384a4e6b820d2ba5f03 (patch) | |
tree | ec5f67ca5643e24713d8cb681ee7c398d16cf621 | |
parent | b8958c4de59a9822eeeb6efddbf3f23f5b81f7a0 (diff) |
Make sure that the target we try to restore to doesn't exist yet
Else it may overwrite this
Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
-rw-r--r-- | lib/Trash/TrashBackend.php | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/lib/Trash/TrashBackend.php b/lib/Trash/TrashBackend.php index 3c01ea8b..eff7c5b2 100644 --- a/lib/Trash/TrashBackend.php +++ b/lib/Trash/TrashBackend.php @@ -111,6 +111,33 @@ class TrashBackend implements ITrashBackend { if ($parent !== '' && !$targetFolder->nodeExists($parent)) { $originalLocation = basename($originalLocation); } + + if ($targetFolder->nodeExists($originalLocation)) { + $info = pathinfo($originalLocation); + $i = 1; + + $gen = function($info, $i): string { + $target = $info['dirname']; + if ($target === '.') { + $target = ''; + } + + $target .= $info['filename']; + $target .= ' (' . $i . ')'; + + if (isset($info['extension'])) { + $target .= $info['extension']; + } + + return $target; + }; + + do { + $originalLocation = $gen($info, $i); + $i++; + } while($targetFolder->nodeExists($originalLocation)); + } + $targetLocation = $targetFolder->getInternalPath() . '/' . $originalLocation; $targetFolder->getStorage()->moveFromStorage($trashStorage, $node->getInternalPath(), $targetLocation); $targetFolder->getStorage()->getCache()->moveFromCache($trashStorage->getCache(), $node->getInternalPath(), $targetLocation); |