From f8a5812f9846fe9799eacd2f2231af3707016e36 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 8 Apr 2020 15:41:20 +0200 Subject: [PATCH] Try to use the display name of file transfers Signed-off-by: Joas Schilling --- .../lib/Service/OwnershipTransferService.php | 28 ++++++++++++++----- lib/private/Files/Filesystem.php | 2 +- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/apps/files/lib/Service/OwnershipTransferService.php b/apps/files/lib/Service/OwnershipTransferService.php index e1b26c6449..3415a2fd9e 100644 --- a/apps/files/lib/Service/OwnershipTransferService.php +++ b/apps/files/lib/Service/OwnershipTransferService.php @@ -36,6 +36,7 @@ use OCA\Files\Exception\TransferOwnershipException; use OCP\Encryption\IManager as IEncryptionManager; use OCP\Files\FileInfo; use OCP\Files\IHomeStorage; +use OCP\Files\InvalidPathException; use OCP\Files\Mount\IMountManager; use OCP\IUser; use OCP\Share\IManager as IShareManager; @@ -94,18 +95,31 @@ class OwnershipTransferService { throw new TransferOwnershipException("The target user is not ready to accept files. The user has at least to have logged in once.", 2); } - if ($move) { - $finalTarget = "$destinationUid/files/"; - } else { - $date = date('Y-m-d H-i-s'); - $finalTarget = "$destinationUid/files/transferred from $sourceUid on $date"; - } - // setup filesystem Filesystem::initMountPoints($sourceUid); Filesystem::initMountPoints($destinationUid); $view = new View(); + + if ($move) { + $finalTarget = "$destinationUid/files/"; + } else { + $date = date('Y-m-d H-i-s'); + + // Remove some characters which are prone to cause errors + $cleanUserName = str_replace(['\\', '/', ':', '.', '?', '#', '\'', '"'], '-', $sourceUser->getDisplayName()); + // Replace multiple dashes with one dash + $cleanUserName = preg_replace('/-{2,}/s', '-', $cleanUserName); + $cleanUserName = $cleanUserName ?: $sourceUid; + + $finalTarget = "$destinationUid/files/transferred from $cleanUserName on $date"; + try { + $view->verifyPath(dirname($finalTarget), basename($finalTarget)); + } catch (InvalidPathException $e) { + $finalTarget = "$destinationUid/files/transferred from $sourceUid on $date"; + } + } + if (!($view->is_dir($sourcePath) || $view->is_file($sourcePath))) { throw new TransferOwnershipException("Unknown path provided: $path", 1); } diff --git a/lib/private/Files/Filesystem.php b/lib/private/Files/Filesystem.php index 8fddbf7120..44da2e6f20 100644 --- a/lib/private/Files/Filesystem.php +++ b/lib/private/Files/Filesystem.php @@ -828,7 +828,7 @@ class Filesystem { $patterns = [ '/\\\\/s', // no windows style slashes '/\/\.(\/\.)?\//s', // remove '/./' - '/\/{2,}/s', // remove squence of slashes + '/\/{2,}/s', // remove sequence of slashes '/\/\.$/s', // remove trailing /. ];