/** * Retrieves the contents of a trash bin directory. * * @param string $dir path to the directory inside the trashbin * or empty to retrieve the root of the trashbin * @param string $user * @param string $sortAttribute attribute to sort on or empty to disable sorting * @param bool $sortDescending true for descending sort, false otherwise * @return \OCP\Files\FileInfo[] */ public static function getTrashFiles($dir, $user, $sortAttribute = '', $sortDescending = false) { $result = array(); $timestamp = null; $view = new \OC\Files\View('/' . $user . '/files_trashbin/files'); if (ltrim($dir, '/') !== '' && !$view->is_dir($dir)) { throw new \Exception('Directory does not exists'); } $dirContent = $view->opendir($dir); if ($dirContent === false) { return $result; } $mount = $view->getMount($dir); $storage = $mount->getStorage(); $absoluteDir = $view->getAbsolutePath($dir); $internalPath = $mount->getInternalPath($absoluteDir); if (is_resource($dirContent)) { $originalLocations = \OCA\Files_Trashbin\Trashbin::getLocations($user); while (($entryName = readdir($dirContent)) !== false) { if (!\OC\Files\Filesystem::isIgnoredDir($entryName)) { $id = $entryName; if ($dir === '' || $dir === '/') { $size = $view->filesize($id); $pathparts = pathinfo($entryName); $timestamp = substr($pathparts['extension'], 1); $id = $pathparts['filename']; } else { if ($timestamp === null) { // for subfolders we need to calculate the timestamp only once $size = $view->filesize($dir . '/' . $id); $parts = explode('/', ltrim($dir, '/')); $timestamp = substr(pathinfo($parts[0], PATHINFO_EXTENSION), 1); } } $originalPath = ''; if (isset($originalLocations[$id][$timestamp])) { $originalPath = $originalLocations[$id][$timestamp]; if (substr($originalPath, -1) === '/') { $originalPath = substr($originalPath, 0, -1); } } $i = array('name' => $id, 'mtime' => $timestamp, 'mimetype' => $view->is_dir($dir . '/' . $entryName) ? 'httpd/unix-directory' : \OC_Helper::getFileNameMimeType($id), 'type' => $view->is_dir($dir . '/' . $entryName) ? 'dir' : 'file', 'directory' => $dir === '/' ? '' : $dir, 'size' => $size); if ($originalPath) { $i['extraData'] = $originalPath . '/' . $id; } $result[] = new FileInfo($absoluteDir . '/' . $i['name'], $storage, $internalPath . '/' . $i['name'], $i, $mount); } } closedir($dirContent); } if ($sortAttribute !== '') { return \OCA\Files\Helper::sortFiles($result, $sortAttribute, $sortDescending); } return $result; }
/** * Retrieves the contents of a trash bin directory. * * @param string $dir path to the directory inside the trashbin * or empty to retrieve the root of the trashbin * @param string $user * @param string $sortAttribute attribute to sort on or empty to disable sorting * @param bool $sortDescending true for descending sort, false otherwise * @return \OCP\Files\FileInfo[] */ public static function getTrashFiles($dir, $user, $sortAttribute = '', $sortDescending = false) { $result = array(); $timestamp = null; $view = new \OC\Files\View('/' . $user . '/files_trashbin/files'); if (ltrim($dir, '/') !== '' && !$view->is_dir($dir)) { throw new \Exception('Directory does not exists'); } $mount = $view->getMount($dir); $storage = $mount->getStorage(); $absoluteDir = $view->getAbsolutePath($dir); $internalPath = $mount->getInternalPath($absoluteDir); $originalLocations = \OCA\Files_Trashbin\Trashbin::getLocations($user); $dirContent = $storage->getCache()->getFolderContents($mount->getInternalPath($view->getAbsolutePath($dir))); foreach ($dirContent as $entry) { $entryName = $entry->getName(); $id = $entry->getId(); $name = $entryName; if ($dir === '' || $dir === '/') { $pathparts = pathinfo($entryName); $timestamp = substr($pathparts['extension'], 1); $name = $pathparts['filename']; } else { if ($timestamp === null) { // for subfolders we need to calculate the timestamp only once $parts = explode('/', ltrim($dir, '/')); $timestamp = substr(pathinfo($parts[0], PATHINFO_EXTENSION), 1); } } $originalPath = ''; if (isset($originalLocations[$id][$timestamp])) { $originalPath = $originalLocations[$id][$timestamp]; if (substr($originalPath, -1) === '/') { $originalPath = substr($originalPath, 0, -1); } } $i = array('name' => $name, 'mtime' => $timestamp, 'mimetype' => $entry->getMimeType(), 'type' => $entry->getMimeType() === ICacheEntry::DIRECTORY_MIMETYPE ? 'dir' : 'file', 'directory' => $dir === '/' ? '' : $dir, 'size' => $entry->getSize(), 'etag' => '', 'permissions' => Constants::PERMISSION_ALL - Constants::PERMISSION_SHARE); if ($originalPath) { $i['extraData'] = $originalPath . '/' . $id; } $result[] = new FileInfo($absoluteDir . '/' . $i['name'], $storage, $internalPath . '/' . $i['name'], $i, $mount); } if ($sortAttribute !== '') { return \OCA\Files\Helper::sortFiles($result, $sortAttribute, $sortDescending); } return $result; }
public function testNameConflict() { self::loginHelper(self::TEST_FILES_SHARING_API_USER1); $view1 = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER1 . '/files'); $view1->mkdir('foo'); $folderInfo1 = $view1->getFileInfo('foo'); self::loginHelper(self::TEST_FILES_SHARING_API_USER3); $view3 = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER3 . '/files'); $view3->mkdir('foo'); $folderInfo2 = $view3->getFileInfo('foo'); // share a folder with the same name from two different users to the same user self::loginHelper(self::TEST_FILES_SHARING_API_USER1); \OCP\Share::shareItem('folder', $folderInfo1['fileid'], \OCP\Share::SHARE_TYPE_GROUP, self::TEST_FILES_SHARING_API_GROUP1, 31); self::loginHelper(self::TEST_FILES_SHARING_API_USER2); self::loginHelper(self::TEST_FILES_SHARING_API_USER3); \OCP\Share::shareItem('folder', $folderInfo2['fileid'], \OCP\Share::SHARE_TYPE_GROUP, self::TEST_FILES_SHARING_API_GROUP1, 31); self::loginHelper(self::TEST_FILES_SHARING_API_USER2); $view2 = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files'); $this->assertTrue($view2->file_exists('/foo')); $this->assertTrue($view2->file_exists('/foo (2)')); $mount = $view2->getMount('/foo'); $this->assertInstanceOf('\\OCA\\Files_Sharing\\SharedMount', $mount); /** @var \OC\Files\Storage\Shared $storage */ $storage = $mount->getStorage(); $source = $storage->getFile(''); $this->assertEquals(self::TEST_FILES_SHARING_API_USER1, $source['uid_owner']); }
public function testNameConflict() { self::loginHelper(self::TEST_FILES_SHARING_API_USER1); $view1 = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER1 . '/files'); $view1->mkdir('foo'); self::loginHelper(self::TEST_FILES_SHARING_API_USER3); $view3 = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER3 . '/files'); $view3->mkdir('foo'); // share a folder with the same name from two different users to the same user self::loginHelper(self::TEST_FILES_SHARING_API_USER1); $share1 = $this->share(\OCP\Share::SHARE_TYPE_GROUP, 'foo', self::TEST_FILES_SHARING_API_USER1, self::TEST_FILES_SHARING_API_GROUP1, \OCP\Constants::PERMISSION_ALL); self::loginHelper(self::TEST_FILES_SHARING_API_USER2); self::loginHelper(self::TEST_FILES_SHARING_API_USER3); $share2 = $this->share(\OCP\Share::SHARE_TYPE_GROUP, 'foo', self::TEST_FILES_SHARING_API_USER3, self::TEST_FILES_SHARING_API_GROUP1, \OCP\Constants::PERMISSION_ALL); self::loginHelper(self::TEST_FILES_SHARING_API_USER2); $view2 = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files'); $this->assertTrue($view2->file_exists('/foo')); $this->assertTrue($view2->file_exists('/foo (2)')); $mount = $view2->getMount('/foo'); $this->assertInstanceOf('\\OCA\\Files_Sharing\\SharedMount', $mount); /** @var \OC\Files\Storage\Shared $storage */ $storage = $mount->getStorage(); $this->assertEquals(self::TEST_FILES_SHARING_API_USER1, $storage->getOwner('')); $this->shareManager->deleteShare($share1); $this->shareManager->deleteShare($share2); }