ソースを参照

split out some path manipulation logic

Signed-off-by: Robin Appelman <robin@icewind.nl>
tags/v24.0.0beta3
Robin Appelman 2年前
コミット
700444e218
コミッターのメールアドレスに関連付けられたアカウントが存在しません

+ 1
- 0
lib/composer/composer/autoload_classmap.php ファイルの表示

@@ -1209,6 +1209,7 @@ return array(
'OC\\Files\\Type\\Detection' => $baseDir . '/lib/private/Files/Type/Detection.php',
'OC\\Files\\Type\\Loader' => $baseDir . '/lib/private/Files/Type/Loader.php',
'OC\\Files\\Type\\TemplateManager' => $baseDir . '/lib/private/Files/Type/TemplateManager.php',
'OC\\Files\\Utils\\PathHelper' => $baseDir . '/lib/private/Files/Utils/PathHelper.php',
'OC\\Files\\Utils\\Scanner' => $baseDir . '/lib/private/Files/Utils/Scanner.php',
'OC\\Files\\View' => $baseDir . '/lib/private/Files/View.php',
'OC\\ForbiddenException' => $baseDir . '/lib/private/ForbiddenException.php',

+ 1
- 0
lib/composer/composer/autoload_static.php ファイルの表示

@@ -1238,6 +1238,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OC\\Files\\Type\\Detection' => __DIR__ . '/../../..' . '/lib/private/Files/Type/Detection.php',
'OC\\Files\\Type\\Loader' => __DIR__ . '/../../..' . '/lib/private/Files/Type/Loader.php',
'OC\\Files\\Type\\TemplateManager' => __DIR__ . '/../../..' . '/lib/private/Files/Type/TemplateManager.php',
'OC\\Files\\Utils\\PathHelper' => __DIR__ . '/../../..' . '/lib/private/Files/Utils/PathHelper.php',
'OC\\Files\\Utils\\Scanner' => __DIR__ . '/../../..' . '/lib/private/Files/Utils/Scanner.php',
'OC\\Files\\View' => __DIR__ . '/../../..' . '/lib/private/Files/View.php',
'OC\\ForbiddenException' => __DIR__ . '/../../..' . '/lib/private/ForbiddenException.php',

+ 2
- 11
lib/private/Files/Node/Folder.php ファイルの表示

@@ -36,6 +36,7 @@ use OC\Files\Cache\Wrapper\CacheJail;
use OC\Files\Search\SearchComparison;
use OC\Files\Search\SearchOrder;
use OC\Files\Search\SearchQuery;
use OC\Files\Utils\PathHelper;
use OCP\Files\Cache\ICacheEntry;
use OCP\Files\FileInfo;
use OCP\Files\Mount\IMountPoint;
@@ -76,17 +77,7 @@ class Folder extends Node implements \OCP\Files\Folder {
* @return string|null
*/
public function getRelativePath($path) {
if ($this->path === '' or $this->path === '/') {
return $this->normalizePath($path);
}
if ($path === $this->path) {
return '/';
} elseif (strpos($path, $this->path . '/') !== 0) {
return null;
} else {
$path = substr($path, strlen($this->path));
return $this->normalizePath($path);
}
return PathHelper::getRelativePath($this->getPath(), $path);
}

/**

+ 2
- 17
lib/private/Files/Node/Node.php ファイルの表示

@@ -31,6 +31,7 @@ namespace OC\Files\Node;

use OC\Files\Filesystem;
use OC\Files\Mount\MoveableMount;
use OC\Files\Utils\PathHelper;
use OCP\Files\FileInfo;
use OCP\Files\InvalidPathException;
use OCP\Files\NotFoundException;
@@ -298,23 +299,7 @@ class Node implements \OCP\Files\Node {
* @return string
*/
protected function normalizePath($path) {
if ($path === '' or $path === '/') {
return '/';
}
//no windows style slashes
$path = str_replace('\\', '/', $path);
//add leading slash
if ($path[0] !== '/') {
$path = '/' . $path;
}
//remove duplicate slashes
while (strpos($path, '//') !== false) {
$path = str_replace('//', '/', $path);
}
//remove trailing slash
$path = rtrim($path, '/');

return $path;
return PathHelper::normalizePath($path);
}

/**

+ 2
- 0
lib/private/Files/Node/Root.php ファイルの表示

@@ -33,8 +33,10 @@
namespace OC\Files\Node;

use OC\Cache\CappedMemoryCache;
use OC\Files\FileInfo;
use OC\Files\Mount\Manager;
use OC\Files\Mount\MountPoint;
use OC\Files\Utils\PathHelper;
use OC\Files\View;
use OC\Hooks\PublicEmitter;
use OC\User\NoUserException;

+ 71
- 0
lib/private/Files/Utils/PathHelper.php ファイルの表示

@@ -0,0 +1,71 @@
<?php

declare(strict_types=1);
/**
* @copyright Copyright (c) 2022 Robin Appelman <robin@icewind.nl>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OC\Files\Utils;

class PathHelper {
/**
* Make a path relative to a root path, or return null if the path is outside the root
*
* @param string $root
* @param string $path
* @return ?string
*/
public static function getRelativePath(string $root, string $path) {
if ($root === '' or $root === '/') {
return self::normalizePath($path);
}
if ($path === $root) {
return '/';
} elseif (strpos($path, $root . '/') !== 0) {
return null;
} else {
$path = substr($path, strlen($root));
return self::normalizePath($path);
}
}

/**
* @param string $path
* @return string
*/
public static function normalizePath(string $path): string {
if ($path === '' or $path === '/') {
return '/';
}
//no windows style slashes
$path = str_replace('\\', '/', $path);
//add leading slash
if ($path[0] !== '/') {
$path = '/' . $path;
}
//remove duplicate slashes
while (strpos($path, '//') !== false) {
$path = str_replace('//', '/', $path);
}
//remove trailing slash
$path = rtrim($path, '/');

return $path;
}
}

読み込み中…
キャンセル
保存