From 4bd9bc7b06c143ad0db8f11dfa31cec8842e34f0 Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Wed, 17 Apr 2024 23:50:18 +0200 Subject: [PATCH] feat(files): Use receiving users language for the ownership transfer target folder Signed-off-by: Ferdinand Thiessen --- .../lib/Service/OwnershipTransferService.php | 21 ++++++++++++------- .../features/bootstrap/CommandLineContext.php | 2 +- .../files_features/transfer-ownership.feature | 4 ++-- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/apps/files/lib/Service/OwnershipTransferService.php b/apps/files/lib/Service/OwnershipTransferService.php index bbb8b15794150..a624df4a92458 100644 --- a/apps/files/lib/Service/OwnershipTransferService.php +++ b/apps/files/lib/Service/OwnershipTransferService.php @@ -23,6 +23,7 @@ use OCP\Files\Mount\IMountManager; use OCP\IUser; use OCP\IUserManager; +use OCP\L10N\IFactory; use OCP\Share\IManager as IShareManager; use OCP\Share\IShare; use Symfony\Component\Console\Helper\ProgressBar; @@ -45,6 +46,7 @@ public function __construct( private IMountManager $mountManager, private IUserMountCache $userMountCache, private IUserManager $userManager, + private IFactory $l10nFactory, ) { $this->encryptionManager = $encryptionManager; } @@ -93,19 +95,15 @@ public function transfer( if ($move) { $finalTarget = "$destinationUid/files/"; } else { + $l = $this->l10nFactory->get('files', $this->l10nFactory->getUserLanguage($destinationUser)); $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"; + $cleanUserName = $this->sanitizeFolderName($sourceUser->getDisplayName()) ?: $sourceUid; + $finalTarget = "$destinationUid/files/" . $this->sanitizeFolderName($l->t('Transferred from %1$s on %2$s', [$cleanUserName, $date])); try { $view->verifyPath(dirname($finalTarget), basename($finalTarget)); } catch (InvalidPathException $e) { - $finalTarget = "$destinationUid/files/transferred from $sourceUid on $date"; + $finalTarget = "$destinationUid/files/" . $this->sanitizeFolderName($l->t('Transferred from %1$s on %2$s', [$sourceUid, $date])); } } @@ -185,6 +183,13 @@ public function transfer( } } + private function sanitizeFolderName(string $name): string { + // Remove some characters which are prone to cause errors + $name = str_replace(['\\', '/', ':', '.', '?', '#', '\'', '"'], '-', $name); + // Replace multiple dashes with one dash + return preg_replace('/-{2,}/s', '-', $name); + } + private function walkFiles(View $view, $path, Closure $callBack) { foreach ($view->getDirectoryContent($path) as $fileInfo) { if (!$callBack($fileInfo)) { diff --git a/build/integration/features/bootstrap/CommandLineContext.php b/build/integration/features/bootstrap/CommandLineContext.php index 38bdb54c38daa..47a85885ce414 100644 --- a/build/integration/features/bootstrap/CommandLineContext.php +++ b/build/integration/features/bootstrap/CommandLineContext.php @@ -51,7 +51,7 @@ private function findLastTransferFolderForUser($sourceUser, $targetUser) { foreach ($results as $path => $data) { $path = rawurldecode($path); $parts = explode(' ', $path); - if (basename($parts[0]) !== 'transferred') { + if (basename($parts[0]) !== 'Transferred') { continue; } if (isset($parts[2]) && $parts[2] === $sourceUser) { diff --git a/build/integration/files_features/transfer-ownership.feature b/build/integration/files_features/transfer-ownership.feature index 22e34dcf7aff0..44860c195b9e9 100644 --- a/build/integration/files_features/transfer-ownership.feature +++ b/build/integration/files_features/transfer-ownership.feature @@ -39,7 +39,7 @@ Feature: transfer-ownership And As an "user1" And using received transfer folder of "user1" as dav path Then Downloaded content when downloading file "/test/somefile.txt" with range "bytes=0-6" should be "This is" - And transfer folder name contains "transferred from user0 -risky- ヂspḷay -na|-|e- on" + And transfer folder name contains "Transferred from user0 -risky- ヂspḷay -na|-|e- on" And using old dav path And as "user0" the folder "/test" does not exist And using received transfer folder of "user1" as dav path @@ -345,7 +345,7 @@ Feature: transfer-ownership And As an "user1" And using received transfer folder of "user1" as dav path Then Downloaded content when downloading file "/test/somefile.txt" with range "bytes=0-6" should be "This is" - And transfer folder name contains "transferred from user0 -risky- ヂspḷay -na|-|e- on" + And transfer folder name contains "Transferred from user0 -risky- ヂspḷay -na|-|e- on" And using old dav path And as "user0" the folder "/test" does not exist And using received transfer folder of "user1" as dav path