From 2a5a00ced43cf509e423198765f0b6faccc19bc7 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Tue, 29 Jun 2010 04:59:46 +0000 Subject: [PATCH] MINOR Using File::get_file_extension() instead of substr() magic in File->setName() MINOR Using exceptions instead of user_error() in File->setName() git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.4@107269 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- filesystem/File.php | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/filesystem/File.php b/filesystem/File.php index 4059dc02f..c687c768c 100755 --- a/filesystem/File.php +++ b/filesystem/File.php @@ -389,15 +389,10 @@ class File extends DataObject { // If it's changed, check for duplicates if($oldName && $oldName != $name) { - if($dotPos = strpos($name, '.')) { - $base = substr($name,0,$dotPos); - $ext = substr($name,$dotPos); - } else { - $base = $name; - $ext = ""; - } + $base = pathinfo($name, PATHINFO_BASENAME); + $ext = self::get_file_extension($name); $suffix = 1; - while(DataObject::get_one("File", "\"Name\" = '" . addslashes($name) . "' AND \"ParentID\" = " . (int)$this->ParentID)) { + while(DataObject::get_one("File", "\"Name\" = '" . Convert::raw2sql($name) . "' AND \"ParentID\" = " . (int)$this->ParentID)) { $suffix++; $name = "$base-$suffix$ext"; } @@ -440,15 +435,15 @@ class File extends DataObject { $to = Director::getAbsFile($newFilename); // Error checking - if(!file_exists($from)) user_error("Cannot move $oldFilename to $newFilename - $oldFilename doesn't exist", E_USER_WARNING); - else if(!file_exists(dirname($to))) user_error("Cannot move $oldFilename to $newFilename - " . dirname($newFilename) . " doesn't exist", E_USER_WARNING); - else if(!rename($from, $to)) user_error("Cannot move $oldFilename to $newFilename", E_USER_WARNING); - - else $this->updateLinks($oldFilename, $newFilename); - - } else { - $this->updateLinks($oldFilename, $newFilename); + if(!file_exists($from)) throw new Exception("Cannot move $oldFilename to $newFilename - $oldFilename doesn't exist"); + if(!file_exists(dirname($to))) throw new Exception("Cannot move $oldFilename to $newFilename - " . dirname($newFilename) . " doesn't exist"); + + // Rename file + $success = rename($from, $to); + if(!$success) throw new Exception("Cannot move $oldFilename to $newFilename"); } + + $this->updateLinks($oldFilename, $newFilename); } else { // If the old file doesn't exist, maybe it's already been renamed. if(file_exists(Director::getAbsFile($newFilename))) $this->updateLinks($oldFilename, $newFilename);