diff options
26 files changed, 237 insertions, 266 deletions
diff --git a/apps/dav/lib/Connector/Sabre/File.php b/apps/dav/lib/Connector/Sabre/File.php index 045b9d7e784..98e0f2e9e4b 100644 --- a/apps/dav/lib/Connector/Sabre/File.php +++ b/apps/dav/lib/Connector/Sabre/File.php @@ -19,6 +19,7 @@ use OCA\DAV\Connector\Sabre\Exception\Forbidden as DAVForbiddenException; use OCA\DAV\Connector\Sabre\Exception\UnsupportedMediaType; use OCP\App\IAppManager; use OCP\Encryption\Exceptions\GenericEncryptionException; +use OCP\Files; use OCP\Files\EntityTooLargeException; use OCP\Files\FileInfo; use OCP\Files\ForbiddenException; @@ -229,7 +230,7 @@ class File extends Node implements IFile { // because we have no clue about the cause we can only throw back a 500/Internal Server Error throw new Exception($this->l10n->t('Could not write file contents')); } - [$count, $result] = \OC_Helper::streamCopy($data, $target); + [$count, $result] = Files::streamCopy($data, $target, true); fclose($target); } diff --git a/apps/files_versions/lib/Storage.php b/apps/files_versions/lib/Storage.php index 19e7dd598ae..4b58d721583 100644 --- a/apps/files_versions/lib/Storage.php +++ b/apps/files_versions/lib/Storage.php @@ -23,6 +23,7 @@ use OCA\Files_Versions\Versions\IVersionManager; use OCP\AppFramework\Db\DoesNotExistException; use OCP\Command\IBus; use OCP\EventDispatcher\IEventDispatcher; +use OCP\Files; use OCP\Files\FileInfo; use OCP\Files\Folder; use OCP\Files\IMimeTypeDetector; @@ -32,6 +33,7 @@ use OCP\Files\NotFoundException; use OCP\Files\NotPermittedException; use OCP\Files\Search\ISearchBinaryOperator; use OCP\Files\Search\ISearchComparison; +use OCP\Files\Storage\IWriteStreamStorage; use OCP\Files\StorageInvalidException; use OCP\Files\StorageNotAvailableException; use OCP\IURLGenerator; @@ -416,12 +418,25 @@ class Storage { try { // TODO add a proper way of overwriting a file while maintaining file ids - if ($storage1->instanceOfStorage('\OC\Files\ObjectStore\ObjectStoreStorage') || $storage2->instanceOfStorage('\OC\Files\ObjectStore\ObjectStoreStorage')) { + if ($storage1->instanceOfStorage(\OC\Files\ObjectStore\ObjectStoreStorage::class) + || $storage2->instanceOfStorage(\OC\Files\ObjectStore\ObjectStoreStorage::class) + ) { $source = $storage1->fopen($internalPath1, 'r'); - $target = $storage2->fopen($internalPath2, 'w'); - [, $result] = \OC_Helper::streamCopy($source, $target); - fclose($source); - fclose($target); + $result = $source !== false; + if ($result) { + if ($storage2->instanceOfStorage(IWriteStreamStorage::class)) { + /** @var IWriteStreamStorage $storage2 */ + $storage2->writeStream($internalPath2, $source); + } else { + $target = $storage2->fopen($internalPath2, 'w'); + $result = $target !== false; + if ($target !== false) { + [, $result] = Files::streamCopy($source, $target, true); + fclose($target); + } + } + fclose($source); + } if ($result !== false) { $storage1->unlink($internalPath1); diff --git a/build/psalm-baseline.xml b/build/psalm-baseline.xml index c0f3d35e57f..f1107ecb4a1 100644 --- a/build/psalm-baseline.xml +++ b/build/psalm-baseline.xml @@ -675,6 +675,12 @@ </ParamNameMismatch> </file> <file src="apps/dav/lib/Connector/Sabre/File.php"> + <DeprecatedClass> + <code><![CDATA[Files::streamCopy($data, $target, true)]]></code> + </DeprecatedClass> + <DeprecatedMethod> + <code><![CDATA[Files::streamCopy($data, $target, true)]]></code> + </DeprecatedMethod> <LessSpecificReturnStatement> <code><![CDATA[$this->node]]></code> </LessSpecificReturnStatement> @@ -1964,14 +1970,13 @@ </file> <file src="apps/files_versions/lib/Storage.php"> <DeprecatedClass> + <code><![CDATA[Files::streamCopy($source, $target, true)]]></code> <code><![CDATA[\OC_Util::setupFS($uid)]]></code> </DeprecatedClass> <DeprecatedMethod> + <code><![CDATA[Files::streamCopy($source, $target, true)]]></code> <code><![CDATA[dispatch]]></code> </DeprecatedMethod> - <RedundantCondition> - <code><![CDATA[$storage1->instanceOfStorage('\OC\Files\ObjectStore\ObjectStoreStorage')]]></code> - </RedundantCondition> </file> <file src="apps/oauth2/lib/Controller/OauthApiController.php"> <NoInterfaceProperties> diff --git a/lib/private/Files/Storage/Common.php b/lib/private/Files/Storage/Common.php index ca0e38774c8..b1e02cf2e6c 100644 --- a/lib/private/Files/Storage/Common.php +++ b/lib/private/Files/Storage/Common.php @@ -19,6 +19,7 @@ use OC\Files\ObjectStore\ObjectStoreStorage; use OC\Files\Storage\Wrapper\Encryption; use OC\Files\Storage\Wrapper\Jail; use OC\Files\Storage\Wrapper\Wrapper; +use OCP\Files; use OCP\Files\Cache\ICache; use OCP\Files\Cache\IPropagator; use OCP\Files\Cache\IScanner; @@ -205,7 +206,7 @@ abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage, } else { $sourceStream = $this->fopen($source, 'r'); $targetStream = $this->fopen($target, 'w'); - [, $result] = \OC_Helper::streamCopy($sourceStream, $targetStream); + [, $result] = Files::streamCopy($sourceStream, $targetStream, true); if (!$result) { Server::get(LoggerInterface::class)->warning("Failed to write data while copying $source to $target"); } @@ -734,7 +735,7 @@ abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage, throw new GenericFileException("Failed to open $path for writing"); } try { - [$count, $result] = \OC_Helper::streamCopy($stream, $target); + [$count, $result] = Files::streamCopy($stream, $target, true); if (!$result) { throw new GenericFileException('Failed to copy stream'); } diff --git a/lib/private/Files/Storage/LocalTempFileTrait.php b/lib/private/Files/Storage/LocalTempFileTrait.php index c8e3588f2e8..fffc3e789f3 100644 --- a/lib/private/Files/Storage/LocalTempFileTrait.php +++ b/lib/private/Files/Storage/LocalTempFileTrait.php @@ -7,6 +7,8 @@ */ namespace OC\Files\Storage; +use OCP\Files; + /** * Storage backend class for providing common filesystem operation methods * which are not storage-backend specific. @@ -45,7 +47,7 @@ trait LocalTempFileTrait { } $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($extension); $target = fopen($tmpFile, 'w'); - \OC_Helper::streamCopy($source, $target); + Files::streamCopy($source, $target); fclose($target); return $tmpFile; } diff --git a/lib/private/Files/Storage/Wrapper/Encryption.php b/lib/private/Files/Storage/Wrapper/Encryption.php index f4ab4754cff..4d38d2d37aa 100644 --- a/lib/private/Files/Storage/Wrapper/Encryption.php +++ b/lib/private/Files/Storage/Wrapper/Encryption.php @@ -21,6 +21,7 @@ use OCP\Encryption\Exceptions\InvalidHeaderException; use OCP\Encryption\IFile; use OCP\Encryption\IManager; use OCP\Encryption\Keys\IStorage; +use OCP\Files; use OCP\Files\Cache\ICacheEntry; use OCP\Files\Mount\IMountPoint; use OCP\Files\Storage; @@ -684,7 +685,7 @@ class Encryption extends Wrapper { try { $source = $sourceStorage->fopen($sourceInternalPath, 'r'); $target = $this->fopen($targetInternalPath, 'w'); - [, $result] = \OC_Helper::streamCopy($source, $target); + [, $result] = Files::streamCopy($source, $target, true); } finally { if (is_resource($source)) { fclose($source); @@ -893,7 +894,7 @@ class Encryption extends Wrapper { public function writeStream(string $path, $stream, ?int $size = null): int { // always fall back to fopen $target = $this->fopen($path, 'w'); - [$count, $result] = \OC_Helper::streamCopy($stream, $target); + [$count, $result] = Files::streamCopy($stream, $target, true); fclose($stream); fclose($target); diff --git a/lib/private/Files/Storage/Wrapper/Jail.php b/lib/private/Files/Storage/Wrapper/Jail.php index c8db0e94e59..38b113cef88 100644 --- a/lib/private/Files/Storage/Wrapper/Jail.php +++ b/lib/private/Files/Storage/Wrapper/Jail.php @@ -11,6 +11,7 @@ use OC\Files\Cache\Wrapper\CacheJail; use OC\Files\Cache\Wrapper\JailPropagator; use OC\Files\Cache\Wrapper\JailWatcher; use OC\Files\Filesystem; +use OCP\Files; use OCP\Files\Cache\ICache; use OCP\Files\Cache\IPropagator; use OCP\Files\Cache\IWatcher; @@ -253,7 +254,7 @@ class Jail extends Wrapper { return $storage->writeStream($this->getUnjailedPath($path), $stream, $size); } else { $target = $this->fopen($path, 'w'); - [$count, $result] = \OC_Helper::streamCopy($stream, $target); + $count = Files::streamCopy($stream, $target); fclose($stream); fclose($target); return $count; diff --git a/lib/private/Files/Storage/Wrapper/Wrapper.php b/lib/private/Files/Storage/Wrapper/Wrapper.php index 6dea439fe25..7af11dd5ef7 100644 --- a/lib/private/Files/Storage/Wrapper/Wrapper.php +++ b/lib/private/Files/Storage/Wrapper/Wrapper.php @@ -9,6 +9,7 @@ namespace OC\Files\Storage\Wrapper; use OC\Files\Storage\FailedStorage; use OC\Files\Storage\Storage; +use OCP\Files; use OCP\Files\Cache\ICache; use OCP\Files\Cache\IPropagator; use OCP\Files\Cache\IScanner; @@ -322,7 +323,7 @@ class Wrapper implements \OC\Files\Storage\Storage, ILockingStorage, IWriteStrea return $storage->writeStream($path, $stream, $size); } else { $target = $this->fopen($path, 'w'); - [$count, $result] = \OC_Helper::streamCopy($stream, $target); + $count = Files::streamCopy($stream, $target); fclose($stream); fclose($target); return $count; diff --git a/lib/private/Files/View.php b/lib/private/Files/View.php index e49043355e8..6832b4e1551 100644 --- a/lib/private/Files/View.php +++ b/lib/private/Files/View.php @@ -17,6 +17,7 @@ use OC\User\Manager as UserManager; use OC\User\User; use OCA\Files_Sharing\SharedMount; use OCP\Constants; +use OCP\Files; use OCP\Files\Cache\ICacheEntry; use OCP\Files\ConnectionLostException; use OCP\Files\EmptyFileNameException; @@ -629,7 +630,7 @@ class View { [$storage, $internalPath] = $this->resolvePath($path); $target = $storage->fopen($internalPath, 'w'); if ($target) { - [, $result] = \OC_Helper::streamCopy($data, $target); + [, $result] = Files::streamCopy($data, $target, true); fclose($target); fclose($data); diff --git a/lib/private/legacy/OC_Helper.php b/lib/private/legacy/OC_Helper.php index 6caab38b784..a06b15573cb 100644 --- a/lib/private/legacy/OC_Helper.php +++ b/lib/private/legacy/OC_Helper.php @@ -12,6 +12,7 @@ use OCP\Files\Mount\IMountPoint; use OCP\IBinaryFinder; use OCP\ICacheFactory; use OCP\IUser; +use OCP\Server; use OCP\Util; use Psr\Log\LoggerInterface; @@ -36,32 +37,6 @@ class OC_Helper { private static ?bool $quotaIncludeExternalStorage = null; /** - * Make a human file size - * @param int|float $bytes file size in bytes - * @return string a human readable file size - * @deprecated 4.0.0 replaced with \OCP\Util::humanFileSize - * - * Makes 2048 to 2 kB. - */ - public static function humanFileSize(int|float $bytes): string { - return \OCP\Util::humanFileSize($bytes); - } - - /** - * Make a computer file size - * @param string $str file size in human readable format - * @return false|int|float a file size in bytes - * @deprecated 4.0.0 Use \OCP\Util::computerFileSize - * - * Makes 2kB to 2048. - * - * Inspired by: https://www.php.net/manual/en/function.filesize.php#92418 - */ - public static function computerFileSize(string $str): false|int|float { - return \OCP\Util::computerFileSize($str); - } - - /** * Recursive copying of folders * @param string $src source folder * @param string $dest target folder @@ -92,17 +67,6 @@ class OC_Helper { } /** - * Recursive deletion of folders - * @param string $dir path to the folder - * @param bool $deleteSelf if set to false only the content of the folder will be deleted - * @return bool - * @deprecated 5.0.0 use \OCP\Files::rmdirr instead - */ - public static function rmdirr($dir, $deleteSelf = true) { - return \OCP\Files::rmdirr($dir, $deleteSelf); - } - - /** * @deprecated 18.0.0 * @return \OC\Files\Type\TemplateManager */ @@ -159,31 +123,10 @@ class OC_Helper { * @param resource $source * @param resource $target * @return array the number of bytes copied and result + * @deprecated 5.0.0 - Use \OCP\Files::streamCopy */ public static function streamCopy($source, $target) { - if (!$source or !$target) { - return [0, false]; - } - $bufSize = 8192; - $result = true; - $count = 0; - while (!feof($source)) { - $buf = fread($source, $bufSize); - $bytesWritten = fwrite($target, $buf); - if ($bytesWritten !== false) { - $count += $bytesWritten; - } - // note: strlen is expensive so only use it when necessary, - // on the last block - if ($bytesWritten === false - || ($bytesWritten < $bufSize && $bytesWritten < strlen($buf)) - ) { - // write error, could be disk full ? - $result = false; - break; - } - } - return [$count, $result]; + return \OCP\Files::streamCopy($source, $target, true); } /** @@ -246,73 +189,12 @@ class OC_Helper { } /** - * Returns an array with all keys from input lowercased or uppercased. Numbered indices are left as is. - * Based on https://www.php.net/manual/en/function.array-change-key-case.php#107715 - * - * @param array $input The array to work on - * @param int $case Either MB_CASE_UPPER or MB_CASE_LOWER (default) - * @param string $encoding The encoding parameter is the character encoding. Defaults to UTF-8 - * @return array - * @deprecated 4.5.0 use \OCP\Util::mb_array_change_key_case instead - */ - public static function mb_array_change_key_case($input, $case = MB_CASE_LOWER, $encoding = 'UTF-8') { - return \OCP\Util::mb_array_change_key_case($input, $case, $encoding); - } - - /** - * Performs a search in a nested array. - * Taken from https://www.php.net/manual/en/function.array-search.php#97645 - * - * @param array $haystack the array to be searched - * @param string $needle the search string - * @param mixed $index optional, only search this key name - * @return mixed the key of the matching field, otherwise false - * @deprecated 4.5.0 - use \OCP\Util::recursiveArraySearch - */ - public static function recursiveArraySearch($haystack, $needle, $index = null) { - return \OCP\Util::recursiveArraySearch($haystack, $needle, $index); - } - - /** - * calculates the maximum upload size respecting system settings, free space and user quota - * - * @param string $dir the current folder where the user currently operates - * @param int|float $freeSpace the number of bytes free on the storage holding $dir, if not set this will be received from the storage directly - * @return int|float number of bytes representing - * @deprecated 5.0.0 - use \OCP\Util::maxUploadFilesize - */ - public static function maxUploadFilesize($dir, $freeSpace = null) { - return \OCP\Util::maxUploadFilesize($dir, $freeSpace); - } - - /** - * Calculate free space left within user quota - * - * @param string $dir the current folder where the user currently operates - * @return int|float number of bytes representing - * @deprecated 7.0.0 - use \OCP\Util::freeSpace - */ - public static function freeSpace($dir) { - return \OCP\Util::freeSpace($dir); - } - - /** - * Calculate PHP upload limit - * - * @return int|float PHP upload file size limit - * @deprecated 7.0.0 - use \OCP\Util::uploadLimit - */ - public static function uploadLimit() { - return \OCP\Util::uploadLimit(); - } - - /** * Checks if a function is available * * @deprecated 25.0.0 use \OCP\Util::isFunctionEnabled instead */ public static function is_function_enabled(string $function_name): bool { - return \OCP\Util::isFunctionEnabled($function_name); + return Util::isFunctionEnabled($function_name); } /** @@ -320,7 +202,7 @@ class OC_Helper { * @deprecated 25.0.0 Use \OC\BinaryFinder directly */ public static function findBinaryPath(string $program): ?string { - $result = \OCP\Server::get(IBinaryFinder::class)->findBinaryPath($program); + $result = Server::get(IBinaryFinder::class)->findBinaryPath($program); return $result !== false ? $result : null; } @@ -340,7 +222,7 @@ class OC_Helper { */ public static function getStorageInfo($path, $rootInfo = null, $includeMountPoints = true, $useCache = true) { if (!self::$cacheFactory) { - self::$cacheFactory = \OC::$server->get(ICacheFactory::class); + self::$cacheFactory = Server::get(ICacheFactory::class); } $memcache = self::$cacheFactory->createLocal('storage_info'); diff --git a/lib/public/Files.php b/lib/public/Files.php index b12aa463f1a..3df3152b0ef 100644 --- a/lib/public/Files.php +++ b/lib/public/Files.php @@ -85,15 +85,45 @@ class Files { /** * Copy the contents of one stream to another + * + * @template T of null|true * @param resource $source * @param resource $target - * @return int the number of bytes copied + * @param T $includeResult + * @return int|array + * @psalm-return (T is true ? array{0: int, 1: bool} : int) * @since 5.0.0 + * @since 32.0.0 added $includeResult parameter * @deprecated 14.0.0 */ - public static function streamCopy($source, $target) { - [$count, ] = \OC_Helper::streamCopy($source, $target); - return $count; + public static function streamCopy($source, $target, ?bool $includeResult = null) { + if (!$source or !$target) { + return $includeResult ? [0, false] : 0; + } + + $bufSize = 8192; + $count = 0; + $result = true; + while (!feof($source)) { + $buf = fread($source, $bufSize); + if ($buf === false) { + $result = false; + break; + } + + $bytesWritten = fwrite($target, $buf); + if ($bytesWritten !== false) { + $count += $bytesWritten; + } + + if ($bytesWritten === false + || ($bytesWritten < $bufSize && $bytesWritten < strlen($buf)) + ) { + $result = false; + break; + } + } + return $includeResult ? [$count, $result] : $count; } /** diff --git a/lib/public/Template.php b/lib/public/Template.php index 715115bc635..c29de52db4f 100644 --- a/lib/public/Template.php +++ b/lib/public/Template.php @@ -23,7 +23,7 @@ require_once __DIR__ . '/../private/Template/functions.php'; */ class Template extends \OC_Template implements ITemplate { /** - * Make OC_Helper::imagePath available as a simple function + * Make \OCP\IURLGenerator::imagePath available as a simple function * * @see \OCP\IURLGenerator::imagePath * @@ -39,7 +39,7 @@ class Template extends \OC_Template implements ITemplate { /** - * Make OC_Helper::mimetypeIcon available as a simple function + * Make IMimeTypeDetector->mimeTypeIcon available as a simple function * * @param string $mimetype * @return string to the image of this file type. diff --git a/package-lock.json b/package-lock.json index 2677107e22b..4fb743eb1f6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -112,7 +112,7 @@ "@vitest/coverage-v8": "^3.1.3", "@vue/test-utils": "^1.3.5", "@vue/tsconfig": "^0.5.1", - "@zip.js/zip.js": "^2.7.60", + "@zip.js/zip.js": "^2.7.61", "babel-loader-exclude-node-modules-except": "^1.2.1", "babel-plugin-module-resolver": "^5.0.2", "colord": "^2.9.3", @@ -7645,9 +7645,9 @@ "license": "Apache-2.0" }, "node_modules/@zip.js/zip.js": { - "version": "2.7.60", - "resolved": "https://registry.npmjs.org/@zip.js/zip.js/-/zip.js-2.7.60.tgz", - "integrity": "sha512-vA3rLyqdxBrVo1FWSsbyoecaqWTV+vgPRf0QKeM7kVDG0r+lHUqd7zQDv1TO9k4BcAoNzNDSNrrel24Mk6addA==", + "version": "2.7.61", + "resolved": "https://registry.npmjs.org/@zip.js/zip.js/-/zip.js-2.7.61.tgz", + "integrity": "sha512-+tZvY10nkW0pJoU88XFWLBd2O9PJPvEnDhSY/jQHfIroN5W5qGfPgFHKC4lkx0+9Vw/0IAkNHf1XBVInBkM9Vw==", "dev": true, "license": "BSD-3-Clause", "engines": { diff --git a/package.json b/package.json index 318a5e75434..46035e1196c 100644 --- a/package.json +++ b/package.json @@ -143,7 +143,7 @@ "@vitest/coverage-v8": "^3.1.3", "@vue/test-utils": "^1.3.5", "@vue/tsconfig": "^0.5.1", - "@zip.js/zip.js": "^2.7.60", + "@zip.js/zip.js": "^2.7.61", "babel-loader-exclude-node-modules-except": "^1.2.1", "babel-plugin-module-resolver": "^5.0.2", "colord": "^2.9.3", diff --git a/tests/lib/Files/FilesystemTest.php b/tests/lib/Files/FilesystemTest.php index 4a3543474a1..966f6f34cee 100644 --- a/tests/lib/Files/FilesystemTest.php +++ b/tests/lib/Files/FilesystemTest.php @@ -10,6 +10,7 @@ namespace Test\Files; use OC\Files\Mount\MountPoint; use OC\Files\Storage\Temporary; use OC\User\NoUserException; +use OCP\Files; use OCP\Files\Config\IMountProvider; use OCP\Files\Storage\IStorageFactory; use OCP\IUser; @@ -74,7 +75,7 @@ class FilesystemTest extends \Test\TestCase { protected function tearDown(): void { foreach ($this->tmpDirs as $dir) { - \OC_Helper::rmdirr($dir); + Files::rmdirr($dir); } $this->logout(); diff --git a/tests/lib/Files/Storage/CommonTest.php b/tests/lib/Files/Storage/CommonTest.php index b51b35be8f9..529615f3733 100644 --- a/tests/lib/Files/Storage/CommonTest.php +++ b/tests/lib/Files/Storage/CommonTest.php @@ -9,6 +9,7 @@ namespace Test\Files\Storage; use OC\Files\Storage\Wrapper\Jail; use OC\Files\Storage\Wrapper\Wrapper; +use OCP\Files; use OCP\Files\IFilenameValidator; use OCP\Files\InvalidCharacterInPathException; use OCP\Files\InvalidPathException; @@ -37,7 +38,7 @@ class CommonTest extends Storage { } protected function tearDown(): void { - \OC_Helper::rmdirr($this->tmpDir); + Files::rmdirr($this->tmpDir); $this->restoreService(IFilenameValidator::class); parent::tearDown(); } diff --git a/tests/lib/Files/Storage/HomeTest.php b/tests/lib/Files/Storage/HomeTest.php index b6d0f1aa85a..26b81c6f1a0 100644 --- a/tests/lib/Files/Storage/HomeTest.php +++ b/tests/lib/Files/Storage/HomeTest.php @@ -8,6 +8,7 @@ namespace Test\Files\Storage; use OC\User\User; +use OCP\Files; class DummyUser extends User { private $home; @@ -62,7 +63,7 @@ class HomeTest extends Storage { } protected function tearDown(): void { - \OC_Helper::rmdirr($this->tmpDir); + Files::rmdirr($this->tmpDir); parent::tearDown(); } diff --git a/tests/lib/Files/Storage/LocalTest.php b/tests/lib/Files/Storage/LocalTest.php index 65bc538ef17..13317fad7bb 100644 --- a/tests/lib/Files/Storage/LocalTest.php +++ b/tests/lib/Files/Storage/LocalTest.php @@ -8,6 +8,7 @@ namespace Test\Files\Storage; use OC\Files\Storage\Wrapper\Jail; +use OCP\Files; /** * Class LocalTest @@ -30,7 +31,7 @@ class LocalTest extends Storage { } protected function tearDown(): void { - \OC_Helper::rmdirr($this->tmpDir); + Files::rmdirr($this->tmpDir); parent::tearDown(); } diff --git a/tests/lib/Files/Storage/Wrapper/QuotaTest.php b/tests/lib/Files/Storage/Wrapper/QuotaTest.php index aebae9b3c62..791e8047f7f 100644 --- a/tests/lib/Files/Storage/Wrapper/QuotaTest.php +++ b/tests/lib/Files/Storage/Wrapper/QuotaTest.php @@ -10,6 +10,7 @@ namespace Test\Files\Storage\Wrapper; //ensure the constants are loaded use OC\Files\Cache\CacheEntry; use OC\Files\Storage\Local; +use OCP\Files; \OC::$loader->load('\OC\Files\Filesystem'); @@ -35,7 +36,7 @@ class QuotaTest extends \Test\Files\Storage\Storage { } protected function tearDown(): void { - \OC_Helper::rmdirr($this->tmpDir); + Files::rmdirr($this->tmpDir); parent::tearDown(); } diff --git a/tests/lib/Files/Storage/Wrapper/WrapperTest.php b/tests/lib/Files/Storage/Wrapper/WrapperTest.php index 1d0f41bf3ed..4cbae1762fc 100644 --- a/tests/lib/Files/Storage/Wrapper/WrapperTest.php +++ b/tests/lib/Files/Storage/Wrapper/WrapperTest.php @@ -7,6 +7,8 @@ namespace Test\Files\Storage\Wrapper; +use OCP\Files; + class WrapperTest extends \Test\Files\Storage\Storage { /** * @var string tmpDir @@ -22,7 +24,7 @@ class WrapperTest extends \Test\Files\Storage\Storage { } protected function tearDown(): void { - \OC_Helper::rmdirr($this->tmpDir); + Files::rmdirr($this->tmpDir); parent::tearDown(); } diff --git a/tests/lib/FilesTest.php b/tests/lib/FilesTest.php new file mode 100644 index 00000000000..2ba6ce2666b --- /dev/null +++ b/tests/lib/FilesTest.php @@ -0,0 +1,42 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +namespace Test; + +use OCP\Files; +use OCP\ITempManager; +use OCP\Server; + +class FilesTest extends TestCase { + + /** + * Tests recursive folder deletion with rmdirr() + */ + public function testRecursiveFolderDeletion(): void { + $baseDir = Server::get(ITempManager::class)->getTemporaryFolder() . '/'; + mkdir($baseDir . 'a/b/c/d/e', 0777, true); + mkdir($baseDir . 'a/b/c1/d/e', 0777, true); + mkdir($baseDir . 'a/b/c2/d/e', 0777, true); + mkdir($baseDir . 'a/b1/c1/d/e', 0777, true); + mkdir($baseDir . 'a/b2/c1/d/e', 0777, true); + mkdir($baseDir . 'a/b3/c1/d/e', 0777, true); + mkdir($baseDir . 'a1/b', 0777, true); + mkdir($baseDir . 'a1/c', 0777, true); + file_put_contents($baseDir . 'a/test.txt', 'Hello file!'); + file_put_contents($baseDir . 'a/b1/c1/test one.txt', 'Hello file one!'); + file_put_contents($baseDir . 'a1/b/test two.txt', 'Hello file two!'); + Files::rmdirr($baseDir . 'a'); + + $this->assertFalse(file_exists($baseDir . 'a')); + $this->assertTrue(file_exists($baseDir . 'a1')); + + Files::rmdirr($baseDir); + $this->assertFalse(file_exists($baseDir)); + } +} diff --git a/tests/lib/HelperStorageTest.php b/tests/lib/HelperStorageTest.php index e4e877ab2a8..628e77e935a 100644 --- a/tests/lib/HelperStorageTest.php +++ b/tests/lib/HelperStorageTest.php @@ -94,6 +94,7 @@ class HelperStorageTest extends \Test\TestCase { $this->assertEquals(5, $storageInfo['used']); $this->assertEquals(17, $storageInfo['total']); } + private function getIncludeExternalStorage(): bool { $class = new \ReflectionClass(\OC_Helper::class); $prop = $class->getProperty('quotaIncludeExternalStorage'); diff --git a/tests/lib/LegacyHelperTest.php b/tests/lib/LegacyHelperTest.php index 15af5ca06e8..0938640b892 100644 --- a/tests/lib/LegacyHelperTest.php +++ b/tests/lib/LegacyHelperTest.php @@ -23,85 +23,6 @@ class LegacyHelperTest extends \Test\TestCase { \OC::$WEBROOT = $this->originalWebRoot; } - /** - * @dataProvider humanFileSizeProvider - */ - public function testHumanFileSize($expected, $input): void { - $result = OC_Helper::humanFileSize($input); - $this->assertEquals($expected, $result); - } - - public static function humanFileSizeProvider(): array { - return [ - ['0 B', 0], - ['1 KB', 1024], - ['9.5 MB', 10000000], - ['1.3 GB', 1395864371], - ['465.7 GB', 500000000000], - ['454.7 TB', 500000000000000], - ['444.1 PB', 500000000000000000], - ]; - } - - /** - * @dataProvider providesComputerFileSize - */ - public function testComputerFileSize($expected, $input): void { - $result = OC_Helper::computerFileSize($input); - $this->assertEquals($expected, $result); - } - - public static function providesComputerFileSize(): array { - return [ - [0.0, '0 B'], - [1024.0, '1 KB'], - [1395864371.0, '1.3 GB'], - [9961472.0, '9.5 MB'], - [500041567437.0, '465.7 GB'], - [false, '12 GB etfrhzui'] - ]; - } - - public function testMb_array_change_key_case(): void { - $arrayStart = [ - 'Foo' => 'bar', - 'Bar' => 'foo', - ]; - $arrayResult = [ - 'foo' => 'bar', - 'bar' => 'foo', - ]; - $result = OC_Helper::mb_array_change_key_case($arrayStart); - $expected = $arrayResult; - $this->assertEquals($result, $expected); - - $arrayStart = [ - 'foo' => 'bar', - 'bar' => 'foo', - ]; - $arrayResult = [ - 'FOO' => 'bar', - 'BAR' => 'foo', - ]; - $result = OC_Helper::mb_array_change_key_case($arrayStart, MB_CASE_UPPER); - $expected = $arrayResult; - $this->assertEquals($result, $expected); - } - - public function testRecursiveArraySearch(): void { - $haystack = [ - 'Foo' => 'own', - 'Bar' => 'Cloud', - ]; - - $result = OC_Helper::recursiveArraySearch($haystack, 'own'); - $expected = 'Foo'; - $this->assertEquals($result, $expected); - - $result = OC_Helper::recursiveArraySearch($haystack, 'NotFound'); - $this->assertFalse($result); - } - public function testBuildNotExistingFileNameForView(): void { $viewMock = $this->createMock(View::class); $this->assertEquals('/filename', OC_Helper::buildNotExistingFileNameForView('/', 'filename', $viewMock)); @@ -227,29 +148,4 @@ class LegacyHelperTest extends \Test\TestCase { [3670, true, \OC::$SERVERROOT . '/tests/data/testimage.png', \OC::$SERVERROOT . '/tests/data/testimage-copy.png'], ]; } - - /** - * Tests recursive folder deletion with rmdirr() - */ - public function testRecursiveFolderDeletion(): void { - $baseDir = \OC::$server->getTempManager()->getTemporaryFolder() . '/'; - mkdir($baseDir . 'a/b/c/d/e', 0777, true); - mkdir($baseDir . 'a/b/c1/d/e', 0777, true); - mkdir($baseDir . 'a/b/c2/d/e', 0777, true); - mkdir($baseDir . 'a/b1/c1/d/e', 0777, true); - mkdir($baseDir . 'a/b2/c1/d/e', 0777, true); - mkdir($baseDir . 'a/b3/c1/d/e', 0777, true); - mkdir($baseDir . 'a1/b', 0777, true); - mkdir($baseDir . 'a1/c', 0777, true); - file_put_contents($baseDir . 'a/test.txt', 'Hello file!'); - file_put_contents($baseDir . 'a/b1/c1/test one.txt', 'Hello file one!'); - file_put_contents($baseDir . 'a1/b/test two.txt', 'Hello file two!'); - \OC_Helper::rmdirr($baseDir . 'a'); - - $this->assertFalse(file_exists($baseDir . 'a')); - $this->assertTrue(file_exists($baseDir . 'a1')); - - \OC_Helper::rmdirr($baseDir); - $this->assertFalse(file_exists($baseDir)); - } } diff --git a/tests/lib/Preview/OfficeTest.php b/tests/lib/Preview/OfficeTest.php index c9019d017a3..167c442dd34 100644 --- a/tests/lib/Preview/OfficeTest.php +++ b/tests/lib/Preview/OfficeTest.php @@ -7,6 +7,9 @@ namespace Test\Preview; +use OCP\IBinaryFinder; +use OCP\Server; + /** * Class OfficeTest * @@ -16,10 +19,11 @@ namespace Test\Preview; */ class OfficeTest extends Provider { protected function setUp(): void { - $libreofficeBinary = \OC_Helper::findBinaryPath('libreoffice'); - $openofficeBinary = ($libreofficeBinary) ? null : \OC_Helper::findBinaryPath('openoffice'); + $binaryFinder = Server::get(IBinaryFinder::class); + $libreofficeBinary = $binaryFinder->findBinaryPath('libreoffice'); + $openofficeBinary = $libreofficeBinary === false ? $binaryFinder->findBinaryPath('openoffice') : false; - if ($libreofficeBinary || $openofficeBinary) { + if ($libreofficeBinary !== false || $openofficeBinary !== false) { parent::setUp(); $fileName = 'testimage.odt'; diff --git a/tests/lib/TempManagerTest.php b/tests/lib/TempManagerTest.php index 63cffc0dcae..b607772f5c3 100644 --- a/tests/lib/TempManagerTest.php +++ b/tests/lib/TempManagerTest.php @@ -9,6 +9,7 @@ namespace Test; use bantu\IniGetWrapper\IniGetWrapper; +use OCP\Files; use OCP\IConfig; use Psr\Log\LoggerInterface; @@ -26,7 +27,7 @@ class TempManagerTest extends \Test\TestCase { protected function tearDown(): void { if ($this->baseDir !== null) { - \OC_Helper::rmdirr($this->baseDir); + Files::rmdirr($this->baseDir); } $this->baseDir = null; parent::tearDown(); diff --git a/tests/lib/UtilTest.php b/tests/lib/UtilTest.php index 0063a991e48..e124ea687d8 100644 --- a/tests/lib/UtilTest.php +++ b/tests/lib/UtilTest.php @@ -334,4 +334,84 @@ class UtilTest extends \Test\TestCase { // each of the characters is 12 bytes $this->assertEquals('🙈', Util::shortenMultibyteString('🙈🙊🙉', 16, 2)); } + + /** + * @dataProvider humanFileSizeProvider + */ + public function testHumanFileSize($expected, $input): void { + $result = Util::humanFileSize($input); + $this->assertEquals($expected, $result); + } + + public static function humanFileSizeProvider(): array { + return [ + ['0 B', 0], + ['1 KB', 1024], + ['9.5 MB', 10000000], + ['1.3 GB', 1395864371], + ['465.7 GB', 500000000000], + ['454.7 TB', 500000000000000], + ['444.1 PB', 500000000000000000], + ]; + } + + /** + * @dataProvider providesComputerFileSize + */ + public function testComputerFileSize($expected, $input): void { + $result = Util::computerFileSize($input); + $this->assertEquals($expected, $result); + } + + public static function providesComputerFileSize(): array { + return [ + [0.0, '0 B'], + [1024.0, '1 KB'], + [1395864371.0, '1.3 GB'], + [9961472.0, '9.5 MB'], + [500041567437.0, '465.7 GB'], + [false, '12 GB etfrhzui'] + ]; + } + + public function testMb_array_change_key_case(): void { + $arrayStart = [ + 'Foo' => 'bar', + 'Bar' => 'foo', + ]; + $arrayResult = [ + 'foo' => 'bar', + 'bar' => 'foo', + ]; + $result = Util::mb_array_change_key_case($arrayStart); + $expected = $arrayResult; + $this->assertEquals($result, $expected); + + $arrayStart = [ + 'foo' => 'bar', + 'bar' => 'foo', + ]; + $arrayResult = [ + 'FOO' => 'bar', + 'BAR' => 'foo', + ]; + $result = Util::mb_array_change_key_case($arrayStart, MB_CASE_UPPER); + $expected = $arrayResult; + $this->assertEquals($result, $expected); + } + + public function testRecursiveArraySearch(): void { + $haystack = [ + 'Foo' => 'own', + 'Bar' => 'Cloud', + ]; + + $result = Util::recursiveArraySearch($haystack, 'own'); + $expected = 'Foo'; + $this->assertEquals($result, $expected); + + $result = Util::recursiveArraySearch($haystack, 'NotFound'); + $this->assertFalse($result); + } + } |