public static function getFiles($dir, $sortAttribute = 'name', $sortDescending = false, $mimetypeFilter = '') { $contents = \OC\Files\Filesystem::getDirectoryContent($dir, $mimetypeFilter); if (isset($_SESSION['targetType']) && $_SESSION['targetType'] == TargetType::PROJECT) { /** @var FileInfo[] $subContents */ $subContents = []; foreach ($contents as $content) { /** @var FileInfo $content */ if ($content && $content->getMimetype() === 'httpd/unix-directory') { $subDir = $dir . "/" . $content->getName(); $subContents = array_merge($subContents, \OC\Files\Filesystem::getDirectoryContent($subDir, $mimetypeFilter)); } } } if (isset($subContents) && count($subContents) != 0) { $contents = array_merge($contents, $subContents); } $excludeFolder = []; foreach ($contents as $content) { /** @var FileInfo $content */ if ($content && $content->getMimetype() === 'httpd/unix-directory') { continue; } array_push($excludeFolder, $content); } return self::sortFiles($excludeFolder, $sortAttribute, $sortDescending); }
/** * Retrieves the contents of the given directory and * returns it as a sorted array. * @param string $dir path to the directory * @return array of files */ public static function getFiles($dir) { $content = \OC\Files\Filesystem::getDirectoryContent($dir); $files = array(); foreach ($content as $i) { $i['date'] = \OCP\Util::formatDate($i['mtime']); if ($i['type'] === 'file') { $fileinfo = pathinfo($i['name']); $i['basename'] = $fileinfo['filename']; if (!empty($fileinfo['extension'])) { $i['extension'] = '.' . $fileinfo['extension']; } else { $i['extension'] = ''; } } $i['directory'] = $dir; $i['isPreviewAvailable'] = \OC::$server->getPreviewManager()->isMimeSupported($i['mimetype']); $i['icon'] = \OCA\Files\Helper::determineIcon($i); $files[] = $i; } usort($files, array('\\OCA\\Files\\Helper', 'fileCmp')); return $files; }
/** * Stream directory recursively * @param string $dir * @param string $internalDir */ public function addDirRecursive($dir, $internalDir = '') { $dirname = basename($dir); $rootDir = $internalDir . $dirname; if (!empty($rootDir)) { $this->streamerInstance->addEmptyDir($rootDir); } $internalDir .= $dirname . '/'; // prevent absolute dirs $internalDir = ltrim($internalDir, '/'); $files = \OC\Files\Filesystem::getDirectoryContent($dir); foreach ($files as $file) { $filename = $file['name']; $file = $dir . '/' . $filename; if (\OC\Files\Filesystem::is_file($file)) { $filesize = \OC\Files\Filesystem::filesize($file); $fh = \OC\Files\Filesystem::fopen($file, 'r'); $this->addFileFromStream($fh, $internalDir . $filename, $filesize); fclose($fh); } elseif (\OC\Files\Filesystem::is_dir($file)) { $this->addDirRecursive($file, $internalDir); } } }
/** * @param string $dir * @param ZipStreamer $zip * @param string $internalDir */ public static function zipAddDir($dir, ZipStreamer $zip, $internalDir = '') { $dirname = basename($dir); $rootDir = $internalDir . $dirname; if (!empty($rootDir)) { $zip->addEmptyDir($rootDir); } $internalDir .= $dirname .= '/'; // prevent absolute dirs $internalDir = ltrim($internalDir, '/'); $files = \OC\Files\Filesystem::getDirectoryContent($dir); foreach ($files as $file) { $filename = $file['name']; $file = $dir . '/' . $filename; if (\OC\Files\Filesystem::is_file($file)) { $fh = \OC\Files\Filesystem::fopen($file, 'r'); $zip->addFileFromStream($fh, $internalDir . $filename); fclose($fh); } elseif (\OC\Files\Filesystem::is_dir($file)) { self::zipAddDir($file, $zip, $internalDir); } } }
/** * Retrieves the contents of the given directory and * returns it as a sorted array of FileInfo. * * @param string $dir path to the directory * @param string $sortAttribute attribute to sort on * @param bool $sortDescending true for descending sort, false otherwise * @return \OCP\Files\FileInfo[] files */ public static function getFiles($dir, $sortAttribute = 'name', $sortDescending = false) { $content = \OC\Files\Filesystem::getDirectoryContent($dir); return self::sortFiles($content, $sortAttribute, $sortDescending); }
<?php OCP\JSON::checkLoggedIn(); OCP\JSON::callCheck(); \OC::$server->getSession()->close(); // Get data $dir = stripslashes($_POST["dir"]); $allFiles = isset($_POST["allfiles"]) ? $_POST["allfiles"] : false; // delete all files in dir ? if ($allFiles === 'true') { $files = array(); $fileList = \OC\Files\Filesystem::getDirectoryContent($dir); foreach ($fileList as $fileInfo) { $files[] = $fileInfo['name']; } } else { $files = isset($_POST["file"]) ? $_POST["file"] : $_POST["files"]; $files = json_decode($files); } $filesWithError = ''; $success = true; //Now delete foreach ($files as $file) { if (\OC\Files\Filesystem::file_exists($dir . '/' . $file) && !\OC\Files\Filesystem::unlink($dir . '/' . $file)) { $filesWithError .= $file . "\n"; $success = false; } } // get array with updated storage stats (e.g. max file size) after upload $storageStats = \OCA\Files\Helper::buildFileStorageStatistics($dir); if ($success) {
/** * user1 share file to a group and to a user2 in the same group. Then user2 * unshares the file from self. Afterwards user1 should no longer see the * single user share to user2. If he re-shares the file to user2 the same target * then the group share should be used to group the item */ public function testShareAndUnshareFromSelf() { $fileinfo = $this->view->getFileInfo($this->filename); // share the file to group1 (user2 is a member of this group) and explicitely to user2 \OCP\Share::shareItem('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_GROUP, self::TEST_FILES_SHARING_API_GROUP1, \OCP\Constants::PERMISSION_ALL); \OCP\Share::shareItem('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER, self::TEST_FILES_SHARING_API_USER2, \OCP\Constants::PERMISSION_ALL); // user1 should have to shared files $shares = \OCP\Share::getItemsShared('file'); $this->assertSame(2, count($shares)); // user2 should have two files "welcome.txt" and the shared file, // both the group share and the single share of the same file should be // grouped to one file \Test_Files_Sharing::loginHelper(self::TEST_FILES_SHARING_API_USER2); $dirContent = \OC\Files\Filesystem::getDirectoryContent('/'); $this->assertSame(2, count($dirContent)); $this->verifyDirContent($dirContent, array('welcome.txt', ltrim($this->filename, '/'))); // now user2 deletes the share (= unshare from self) \OC\Files\Filesystem::unlink($this->filename); // only welcome.txt should exists $dirContent = \OC\Files\Filesystem::getDirectoryContent('/'); $this->assertSame(1, count($dirContent)); $this->verifyDirContent($dirContent, array('welcome.txt')); // login as user1... \Test_Files_Sharing::loginHelper(self::TEST_FILES_SHARING_API_USER1); // ... now user1 should have only one shared file, the group share $shares = \OCP\Share::getItemsShared('file'); $this->assertSame(1, count($shares)); // user1 shares a gain the file directly to user2 \OCP\Share::shareItem('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER, self::TEST_FILES_SHARING_API_USER2, \OCP\Constants::PERMISSION_ALL); // user2 should see again welcome.txt and the shared file \Test_Files_Sharing::loginHelper(self::TEST_FILES_SHARING_API_USER2); $dirContent = \OC\Files\Filesystem::getDirectoryContent('/'); $this->assertSame(2, count($dirContent)); $this->verifyDirContent($dirContent, array('welcome.txt', ltrim($this->filename, '/'))); }
* 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 library. If not, see <http://www.gnu.org/licenses/>. * */ // Check if we are a user OCP\User::checkLoggedIn(); // Load the files we need OCP\Util::addStyle("files", "files"); OCP\Util::addscript("files", "files"); // Load the files $dir = isset($_GET['dir']) ? $_GET['dir'] : ''; $files = array(); foreach (\OC\Files\Filesystem::getDirectoryContent($dir) as $i) { $i["date"] = date($CONFIG_DATEFORMAT, $i["mtime"]); $files[] = $i; } // Make breadcrumb $breadcrumb = array(); $pathtohere = "/"; foreach (explode("/", $dir) as $i) { if ($i != "") { $pathtohere .= "{$i}/"; $breadcrumb[] = array("dir" => $pathtohere, "name" => $i); } } // return template $tmpl = new OCP\Template("files", "index", "user"); $tmpl->assign('files', $files);
/** * Returns an array with all the child nodes * * @return Sabre_DAV_INode[] */ public function getChildren() { $folder_content = \OC\Files\Filesystem::getDirectoryContent($this->path); $paths = array(); foreach ($folder_content as $info) { $paths[] = $this->path . '/' . $info['name']; $properties[$this->path . '/' . $info['name']][self::GETETAG_PROPERTYNAME] = '"' . $info['etag'] . '"'; } if (count($paths) > 0) { // // the number of arguments within IN conditions are limited in most databases // we chunk $paths into arrays of 200 items each to meet this criteria // $chunks = array_chunk($paths, 200, false); foreach ($chunks as $pack) { $placeholders = join(',', array_fill(0, count($pack), '?')); $query = OC_DB::prepare('SELECT * FROM `*PREFIX*properties`' . ' WHERE `userid` = ?' . ' AND `propertypath` IN (' . $placeholders . ')'); array_unshift($pack, OC_User::getUser()); // prepend userid $result = $query->execute($pack); while ($row = $result->fetchRow()) { $propertypath = $row['propertypath']; $propertyname = $row['propertyname']; $propertyvalue = $row['propertyvalue']; if ($propertyname !== self::GETETAG_PROPERTYNAME) { $properties[$propertypath][$propertyname] = $propertyvalue; } } } } $nodes = array(); foreach ($folder_content as $info) { $node = $this->getChild($info['name'], $info); $node->setPropertyCache($properties[$this->path . '/' . $info['name']]); $nodes[] = $node; } return $nodes; }
/** * checks if the selected files are within the size constraint. If not, outputs an error page. * * @param dir $dir * @param files $files */ static function validateZipDownload($dir, $files) { if (!OC_Config::getValue('allowZipDownload', true)) { $l = OC_L10N::get('lib'); header("HTTP/1.0 409 Conflict"); OC_Template::printErrorPage($l->t('ZIP download is turned off.'), $l->t('Files need to be downloaded one by one.') . '<br/><a href="javascript:history.back()">' . $l->t('Back to Files') . '</a>'); exit; } $zipLimit = OC_Config::getValue('maxZipInputSize', OC_Helper::computerFileSize('800 MB')); if ($zipLimit > 0) { $totalsize = 0; if (!is_array($files)) { $files = array($files); } foreach ($files as $file) { $path = $dir . '/' . $file; if (\OC\Files\Filesystem::is_dir($path)) { foreach (\OC\Files\Filesystem::getDirectoryContent($path) as $i) { $totalsize += $i['size']; } } else { $totalsize += \OC\Files\Filesystem::filesize($path); } } if ($totalsize > $zipLimit) { $l = OC_L10N::get('lib'); header("HTTP/1.0 409 Conflict"); OC_Template::printErrorPage($l->t('Selected files too large to generate zip file.'), $l->t('Please download the files separately in smaller chunks or kindly ask your administrator.') . '<br/><a href="javascript:history.back()">' . $l->t('Back to Files') . '</a>'); exit; } } }
<?php // only need filesystem apps $RUNTIME_APPTYPES = array('filesystem'); // Init owncloud require_once 'lib/template.php'; OCP\JSON::checkLoggedIn(); // Load the files $dir = isset($_GET['dir']) ? $_GET['dir'] : ''; $mimetype = isset($_GET['mimetype']) ? $_GET['mimetype'] : ''; // make filelist $files = array(); foreach (\OC\Files\Filesystem::getDirectoryContent($dir, $mimetype) as $i) { $i["date"] = OCP\Util::formatDate($i["mtime"]); $i['mimetype_icon'] = $i['type'] == 'dir' ? \mimetype_icon('dir') : \mimetype_icon($i['mimetype']); $files[] = $i; } OCP\JSON::success(array('data' => $files));