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
This commit is contained in:
Ingo Schommer 2010-06-29 04:59:46 +00:00 committed by Sam Minnee
parent a00dc63124
commit 2a5a00ced4

View File

@ -389,15 +389,10 @@ class File extends DataObject {
// If it's changed, check for duplicates // If it's changed, check for duplicates
if($oldName && $oldName != $name) { if($oldName && $oldName != $name) {
if($dotPos = strpos($name, '.')) { $base = pathinfo($name, PATHINFO_BASENAME);
$base = substr($name,0,$dotPos); $ext = self::get_file_extension($name);
$ext = substr($name,$dotPos);
} else {
$base = $name;
$ext = "";
}
$suffix = 1; $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++; $suffix++;
$name = "$base-$suffix$ext"; $name = "$base-$suffix$ext";
} }
@ -440,15 +435,15 @@ class File extends DataObject {
$to = Director::getAbsFile($newFilename); $to = Director::getAbsFile($newFilename);
// Error checking // Error checking
if(!file_exists($from)) user_error("Cannot move $oldFilename to $newFilename - $oldFilename doesn't exist", E_USER_WARNING); if(!file_exists($from)) throw new Exception("Cannot move $oldFilename to $newFilename - $oldFilename doesn't exist");
else if(!file_exists(dirname($to))) user_error("Cannot move $oldFilename to $newFilename - " . dirname($newFilename) . " doesn't exist", E_USER_WARNING); if(!file_exists(dirname($to))) throw new Exception("Cannot move $oldFilename to $newFilename - " . dirname($newFilename) . " doesn't exist");
else if(!rename($from, $to)) user_error("Cannot move $oldFilename to $newFilename", E_USER_WARNING);
else $this->updateLinks($oldFilename, $newFilename); // Rename file
$success = rename($from, $to);
} else { if(!$success) throw new Exception("Cannot move $oldFilename to $newFilename");
$this->updateLinks($oldFilename, $newFilename);
} }
$this->updateLinks($oldFilename, $newFilename);
} else { } else {
// If the old file doesn't exist, maybe it's already been renamed. // If the old file doesn't exist, maybe it's already been renamed.
if(file_exists(Director::getAbsFile($newFilename))) $this->updateLinks($oldFilename, $newFilename); if(file_exists(Director::getAbsFile($newFilename))) $this->updateLinks($oldFilename, $newFilename);