2012-05-02 06:54:31 -04:00
|
|
|
<?php
|
2025-06-30 09:04:05 -04:00
|
|
|
|
2012-05-02 06:54:31 -04:00
|
|
|
/**
|
2024-05-23 03:26:56 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2013-11-03 07:51:39 -05:00
|
|
|
*/
|
2012-08-29 02:38:33 -04:00
|
|
|
// use OCP namespace for all classes that are considered public.
|
2024-05-23 03:26:56 -04:00
|
|
|
// This means that they should be used by apps instead of the internal Nextcloud classes
|
2019-11-22 14:52:10 -05:00
|
|
|
|
2012-05-02 06:54:31 -04:00
|
|
|
namespace OCP;
|
|
|
|
|
|
2025-05-14 18:19:38 -04:00
|
|
|
use OCP\Files\IMimeTypeDetector;
|
|
|
|
|
|
2012-05-19 04:36:57 -04:00
|
|
|
/**
|
2013-02-11 11:44:02 -05:00
|
|
|
* This class provides access to the internal filesystem abstraction layer. Use
|
2023-05-10 05:56:34 -04:00
|
|
|
* this class exclusively if you want to access files
|
2015-04-16 11:00:08 -04:00
|
|
|
* @since 5.0.0
|
2018-03-21 09:24:22 -04:00
|
|
|
* @deprecated 14.0.0
|
2012-05-19 04:36:57 -04:00
|
|
|
*/
|
2012-05-02 06:54:31 -04:00
|
|
|
class Files {
|
|
|
|
|
/**
|
2023-05-10 05:56:34 -04:00
|
|
|
* Recursive deletion of folders
|
2025-05-14 05:52:35 -04:00
|
|
|
*
|
|
|
|
|
* @param string $dir path to the folder
|
|
|
|
|
* @param bool $deleteSelf if set to false only the content of the folder will be deleted
|
2013-01-06 17:54:18 -05:00
|
|
|
* @return bool
|
2015-04-16 11:00:08 -04:00
|
|
|
* @since 5.0.0
|
2025-05-14 05:52:35 -04:00
|
|
|
* @since 32.0.0 added the $deleteSelf parameter
|
2018-03-21 09:24:22 -04:00
|
|
|
* @deprecated 14.0.0
|
2012-05-02 06:54:31 -04:00
|
|
|
*/
|
2025-05-14 05:52:35 -04:00
|
|
|
public static function rmdirr($dir, bool $deleteSelf = true) {
|
|
|
|
|
if (is_dir($dir)) {
|
|
|
|
|
$files = new \RecursiveIteratorIterator(
|
|
|
|
|
new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS),
|
|
|
|
|
\RecursiveIteratorIterator::CHILD_FIRST
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
foreach ($files as $fileInfo) {
|
|
|
|
|
/** @var \SplFileInfo $fileInfo */
|
|
|
|
|
if ($fileInfo->isLink()) {
|
|
|
|
|
unlink($fileInfo->getPathname());
|
|
|
|
|
} elseif ($fileInfo->isDir()) {
|
|
|
|
|
rmdir($fileInfo->getRealPath());
|
|
|
|
|
} else {
|
|
|
|
|
unlink($fileInfo->getRealPath());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ($deleteSelf) {
|
|
|
|
|
rmdir($dir);
|
|
|
|
|
}
|
|
|
|
|
} elseif (file_exists($dir)) {
|
|
|
|
|
if ($deleteSelf) {
|
|
|
|
|
unlink($dir);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!$deleteSelf) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return !file_exists($dir);
|
2012-05-02 06:54:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2013-10-16 18:07:29 -04:00
|
|
|
* Get the mimetype form a local file
|
2014-02-08 05:47:55 -05:00
|
|
|
* @param string $path
|
2012-05-02 06:54:31 -04:00
|
|
|
* @return string
|
|
|
|
|
* does NOT work for ownClouds filesystem, use OC_FileSystem::getMimeType instead
|
2015-04-16 11:00:08 -04:00
|
|
|
* @since 5.0.0
|
2018-03-21 09:24:22 -04:00
|
|
|
* @deprecated 14.0.0
|
2012-05-02 06:54:31 -04:00
|
|
|
*/
|
2020-04-10 10:51:06 -04:00
|
|
|
public static function getMimeType($path) {
|
2025-05-14 18:19:38 -04:00
|
|
|
return Server::get(IMimeTypeDetector::class)->detect($path);
|
2012-05-02 06:54:31 -04:00
|
|
|
}
|
|
|
|
|
|
2013-03-19 09:27:02 -04:00
|
|
|
/**
|
2013-10-16 18:07:29 -04:00
|
|
|
* Search for files by mimetype
|
2014-02-08 05:47:55 -05:00
|
|
|
* @param string $mimetype
|
2013-03-19 09:27:02 -04:00
|
|
|
* @return array
|
2015-04-16 11:00:08 -04:00
|
|
|
* @since 6.0.0
|
2018-03-21 09:24:22 -04:00
|
|
|
* @deprecated 14.0.0
|
2013-03-19 09:27:02 -04:00
|
|
|
*/
|
2020-04-10 10:51:06 -04:00
|
|
|
public static function searchByMime($mimetype) {
|
2017-07-22 15:10:16 -04:00
|
|
|
return \OC\Files\Filesystem::searchByMime($mimetype);
|
2013-03-19 09:27:02 -04:00
|
|
|
}
|
|
|
|
|
|
2012-05-02 06:54:31 -04:00
|
|
|
/**
|
2013-10-16 18:07:29 -04:00
|
|
|
* Copy the contents of one stream to another
|
2025-05-15 18:07:33 -04:00
|
|
|
*
|
|
|
|
|
* @template T of null|true
|
2014-02-08 05:47:55 -05:00
|
|
|
* @param resource $source
|
|
|
|
|
* @param resource $target
|
2025-05-15 18:07:33 -04:00
|
|
|
* @param T $includeResult
|
|
|
|
|
* @return int|array
|
|
|
|
|
* @psalm-return (T is true ? array{0: int, 1: bool} : int)
|
2015-04-16 11:00:08 -04:00
|
|
|
* @since 5.0.0
|
2025-05-15 18:07:33 -04:00
|
|
|
* @since 32.0.0 added $includeResult parameter
|
2018-03-21 09:24:22 -04:00
|
|
|
* @deprecated 14.0.0
|
2012-05-02 06:54:31 -04:00
|
|
|
*/
|
2025-05-15 18:07:33 -04:00
|
|
|
public static function streamCopy($source, $target, ?bool $includeResult = null) {
|
|
|
|
|
if (!$source || !$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;
|
2012-05-02 06:54:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Adds a suffix to the name in case the file exists
|
2014-02-08 05:47:55 -05:00
|
|
|
* @param string $path
|
|
|
|
|
* @param string $filename
|
2012-05-02 06:54:31 -04:00
|
|
|
* @return string
|
2015-04-16 11:00:08 -04:00
|
|
|
* @since 5.0.0
|
2018-03-21 09:24:22 -04:00
|
|
|
* @deprecated 14.0.0 use getNonExistingName of the OCP\Files\Folder object
|
2012-05-02 06:54:31 -04:00
|
|
|
*/
|
2017-07-22 15:10:16 -04:00
|
|
|
public static function buildNotExistingFileName($path, $filename) {
|
|
|
|
|
return \OC_Helper::buildNotExistingFileName($path, $filename);
|
2012-05-02 06:54:31 -04:00
|
|
|
}
|
|
|
|
|
}
|