/** * We need to test that the _versions folder isn't completed wiped by * {@link VersionedFileExtension::onBeforeDelete()} when there is more than the file currently being deleted. */ public function testOnBeforeDelete() { // Create the second file $file2 = $this->folder->getFullPath() . 'test-file2.txt'; file_put_contents($file2, 'first-version'); $file2Obj = new File(); $file2Obj->ParentID = $this->folder->ID; $file2Obj->Filename = $this->folder->getFilename() . 'test-file2.txt'; $file2Obj->write(); // Create a second version of the second file file_put_contents($file2Obj->getFullPath(), 'second-version'); $file2Obj->createVersion(); // Delete the second file $file2Obj->delete(); // Ensure the _versions folder still exists $this->assertTrue(is_dir($this->folder->getFullPath())); $this->assertTrue(is_dir($this->folder->getFullPath() . '/_versions')); // Now delete the first file, and ensure the _versions folder no longer exists $this->file->delete(); $this->assertTrue(is_dir($this->folder->getFullPath())); $this->assertFalse(is_dir($this->folder->getFullPath() . '/_versions')); // Now create another file to ensure that the _versions folder can be successfully re-created $file3 = $this->folder->getFullPath() . 'test-file3.txt'; file_put_contents($file3, 'first-version'); $file3Obj = new File(); $file3Obj->ParentID = $this->folder->ID; $file3Obj->Filename = $this->folder->getFilename() . 'test-file3.txt'; $file3Obj->write(); $this->assertTrue(is_file($file3Obj->getFullPath())); $this->assertTrue(is_dir($this->folder->getFullPath() . '/_versions')); }
/** * Find the given folder or create it both as {@link Folder} database records * and on the filesystem. If necessary, creates parent folders as well. * * @param $folderPath string Absolute or relative path to the file. * If path is relative, its interpreted relative to the "assets/" directory. * @return Folder */ public static function find_or_make($folderPath) { // Create assets directory, if it is missing if (!file_exists(ASSETS_PATH)) { Filesystem::makeFolder(ASSETS_PATH); } $folderPath = trim(Director::makeRelative($folderPath)); // replace leading and trailing slashes $folderPath = preg_replace('/^\\/?(.*)\\/?$/', '$1', $folderPath); $parts = explode("/", $folderPath); $parentID = 0; $item = null; foreach ($parts as $part) { if (!$part) { continue; } // happens for paths with a trailing slash $item = DataObject::get_one("Folder", sprintf("\"Name\" = '%s' AND \"ParentID\" = %d", Convert::raw2sql($part), (int) $parentID)); if (!$item) { $item = new Folder(); $item->ParentID = $parentID; $item->Name = $part; $item->Title = $part; $item->write(); } if (!file_exists($item->getFullPath())) { Filesystem::makeFolder($item->getFullPath()); } $parentID = $item->ID; } return $item; }
/** * This will return the full path string of the current folder object * * @author KnowledgeTree Team * @access public * @return string */ function get_full_path() { $path = $this->folder->getFullPath(); if (empty($path)) { $path = '/'; } return $path; }
static function findOrMake($folder) { $parts = explode("/", $folder); $parentID = 0; foreach ($parts as $part) { $item = DataObject::get_one("Folder", "Name = '{$part}' AND ParentID = {$parentID}"); if (!$item) { $item = new Folder(); $item->ParentID = $parentID; $item->Name = $part; $item->Title = $part; $item->write(); if (!file_exists($item->getFullPath())) { mkdir($item->getFullPath(), Filesystem::$folder_create_mask); } } $parentID = $item->ID; } return $item; }
static function findOrMake($folderPath) { $folderPath = trim(Director::makeRelative($folderPath)); // replace leading and trailing slashes $folderPath = preg_replace('/^\/?(.*)\/?$/', '$1', $folderPath); $parts = explode("/",$folderPath); $parentID = 0; foreach($parts as $part) { $item = DataObject::get_one("Folder", "Name = '$part' AND ParentID = $parentID"); if(!$item) { $item = new Folder(); $item->ParentID = $parentID; $item->Name = $part; $item->Title = $part; $item->write(); if(!file_exists($item->getFullPath())) mkdir($item->getFullPath(),Filesystem::$folder_create_mask); } $parentID = $item->ID; } return $item; }
/** * Find the given folder or create it both as {@link Folder} database records * and on the filesystem. If necessary, creates parent folders as well. If it's * unable to find or make the folder, it will return null (as /assets is unable * to be represented by a Folder DataObject) * * @param $folderPath string Absolute or relative path to the file. * If path is relative, its interpreted relative to the "assets/" directory. * @return Folder|null */ public static function find_or_make($folderPath) { // Create assets directory, if it is missing if (!file_exists(ASSETS_PATH)) { Filesystem::makeFolder(ASSETS_PATH); } $folderPath = trim(Director::makeRelative($folderPath)); // replace leading and trailing slashes $folderPath = preg_replace('/^\\/?(.*)\\/?$/', '$1', $folderPath); $parts = explode("/", $folderPath); $parentID = 0; $item = null; $filter = FileNameFilter::create(); foreach ($parts as $part) { if (!$part) { continue; } // happens for paths with a trailing slash // Ensure search includes folders with illegal characters removed, but // err in favour of matching existing folders if $folderPath // includes illegal characters itself. $partSafe = $filter->filter($part); $item = Folder::get()->filter(array('ParentID' => $parentID, 'Name' => array($partSafe, $part)))->first(); if (!$item) { $item = new Folder(); $item->ParentID = $parentID; $item->Name = $partSafe; $item->Title = $part; $item->write(); } if (!file_exists($item->getFullPath())) { Filesystem::makeFolder($item->getFullPath()); } $parentID = $item->ID; } return $item; }
/** * Creates a new folder at the level of {@link KickAssetAdmin::$currentFolder} * * @param SS_HTTPRequest * @return SS_HTTPResponse */ public function newfolder(SS_HTTPRequest $r) { $f = new Folder(); $f->ParentID = $this->currentFolder ? $this->currentFolder->ID : 0; $f->Name = "New-Folder"; $f->Title = "New-Folder"; $f->write(); if (!file_exists($f->getFullPath())) { Filesystem::makeFolder($f->getFullPath()); } return $this->browse($r); }