mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
mlanthaler: Bugfix: *size* is not a constant, added quotes.
(merged from branches/gsoc) git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@41790 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
17202b859e
commit
6f21a7cea9
@ -1,29 +1,29 @@
|
||||
<?php
|
||||
/**
|
||||
* This class handles the representation of a File within Sapphire
|
||||
* Note: The files are stored in the "/assets/" directory, but sapphire
|
||||
* Note: The files are stored in the "/assets/" directory, but sapphire
|
||||
* looks at the db object to gather information about a file such as URL
|
||||
*
|
||||
* It then uses this for all processing functions (like image manipulation)
|
||||
*
|
||||
* It then uses this for all processing functions (like image manipulation)
|
||||
*/
|
||||
class File extends DataObject {
|
||||
static $default_sort = "Name";
|
||||
|
||||
|
||||
/**
|
||||
* @var array Key is the extension, which has an array of MaxSize and WarnSize,
|
||||
* e.g. array("jpg" => array("MaxSize"=>1000, "WarnSize=>500"))
|
||||
*/
|
||||
static $file_size_restrictions = array();
|
||||
|
||||
|
||||
/**
|
||||
* @var array Collection of extensions, e.g. array("jpg","gif")
|
||||
*/
|
||||
static $allowed_file_types = array();
|
||||
|
||||
|
||||
static $singular_name = "File";
|
||||
|
||||
|
||||
static $plural_name = "Files";
|
||||
|
||||
|
||||
static $db = array(
|
||||
"Name" => "Varchar(255)",
|
||||
"Title" => "Varchar(255)",
|
||||
@ -40,14 +40,14 @@ class File extends DataObject {
|
||||
);
|
||||
static $extensions = array(
|
||||
"Hierarchy",
|
||||
);
|
||||
);
|
||||
static $belongs_many_many = array(
|
||||
"BackLinkTracking" => "SiteTree",
|
||||
);
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Set the maximum
|
||||
* Set the maximum
|
||||
*/
|
||||
static function setMaxFileSize( $maxSize, $warningSize, $extension = '*' ) {
|
||||
self::$file_size_restrictions[$extension]['MaxSize'] = $maxSize;
|
||||
@ -62,21 +62,21 @@ class File extends DataObject {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return array( self::$file_size_restrictions[$extension]['MaxSize'], self::$file_size_restrictions[$extension]['WarnSize'] );
|
||||
}
|
||||
|
||||
|
||||
static function allowedFileType( $extension ) {
|
||||
return !in_array($extension, self::$allowed_file_types);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Find the given file
|
||||
* Find the given file
|
||||
*/
|
||||
static function find($filename) {
|
||||
// Get the base file if $filename points to a resampled file
|
||||
$filename = ereg_replace('_resampled/[^-]+-','',$filename);
|
||||
|
||||
|
||||
$parts = explode("/",$filename);
|
||||
$parentID = 0;
|
||||
|
||||
@ -88,7 +88,7 @@ class File extends DataObject {
|
||||
}
|
||||
return $item;
|
||||
}
|
||||
|
||||
|
||||
public function appCategory() {
|
||||
$ext = $this->Extension;
|
||||
switch($ext) {
|
||||
@ -110,12 +110,12 @@ class File extends DataObject {
|
||||
return "image";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function CMSThumbnail() {
|
||||
$filename = $this->Icon();
|
||||
return "<div style=\"text-align:center;width: 100px;padding-top: 15px;\"><a target=\"_blank\" href=\"$this->URL\" title=\"Download: $this->URL\"><img src=\"$filename\" alt=\"$filename\" /></a><br /><br /><a style=\"color: #0074C6;\"target=\"_blank\" href=\"$this->URL\" title=\"Download: $this->URL\">Download</a><br /><em>$this->Size</e></div>";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return the URL of an icon for the file type
|
||||
*/
|
||||
@ -126,13 +126,13 @@ class File extends DataObject {
|
||||
case "aif": case "au": case "mid": case "midi": case "mp3": case "ra": case "ram": case "rm":
|
||||
case "mp3": case "wav": case "m4a":
|
||||
$ext = "audio"; break;
|
||||
|
||||
|
||||
case "arc": case "rar": case "tar": case "gz": case "tgz": case "bz2": case "dmg":
|
||||
$ext = "zip"; break;
|
||||
|
||||
|
||||
case "bmp": case "gif": case "jpg": case "jpeg": case "pcx": case "tif": case "png":
|
||||
$ext = "image"; break;
|
||||
|
||||
|
||||
}*/
|
||||
|
||||
$ext = $this->appCategory();
|
||||
@ -141,7 +141,7 @@ class File extends DataObject {
|
||||
if(!Director::fileExists("sapphire/images/app_icons/{$ext}_32.gif")) {
|
||||
$ext = "generic";
|
||||
}
|
||||
|
||||
|
||||
return "sapphire/images/app_icons/{$ext}_32.gif";
|
||||
}
|
||||
|
||||
@ -159,22 +159,22 @@ class File extends DataObject {
|
||||
$class = $this->class;
|
||||
$parentFolder = Folder::findOrMake("Uploads");
|
||||
|
||||
// Create a folder for uploading.
|
||||
// Create a folder for uploading.
|
||||
if(!file_exists("$base/assets")){
|
||||
mkdir("$base/assets", 02775);
|
||||
mkdir("$base/assets", 02775);
|
||||
}
|
||||
if(!file_exists("$base/assets/Uploads")){
|
||||
mkdir("$base/assets/Uploads", 02775);
|
||||
}
|
||||
mkdir("$base/assets/Uploads", 02775);
|
||||
}
|
||||
|
||||
// Generate default filename
|
||||
$file = str_replace(' ', '-',$tmpFile['name']);
|
||||
$file = ereg_replace('[^A-Za-z0-9+.-]+','',$file);
|
||||
$file = ereg_replace('-+', '-',$file);
|
||||
$file = basename($file);
|
||||
|
||||
|
||||
$file = "assets/Uploads/$file";
|
||||
|
||||
|
||||
while(file_exists("$base/$file")) {
|
||||
$i = isset($i) ? ($i+1) : 2;
|
||||
$oldFile = $file;
|
||||
@ -186,12 +186,12 @@ class File extends DataObject {
|
||||
}
|
||||
if($oldFile == $file && $i > 2) user_error("Couldn't fix $file with $i", E_USER_ERROR);
|
||||
}
|
||||
|
||||
|
||||
if(file_exists($tmpFile['tmp_name']) && copy($tmpFile['tmp_name'], "$base/$file")) {
|
||||
// Update with the new image
|
||||
/*$this->Filename = */ // $this->Name = null;
|
||||
// $this->Filename = $file;
|
||||
|
||||
|
||||
// This is to prevent it from trying to rename the file
|
||||
$this->record['Name'] = null;
|
||||
$this->ParentID = $parentFolder->ID;
|
||||
@ -203,7 +203,7 @@ class File extends DataObject {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* This function ensures the file table is correct with the files in the assets folder.
|
||||
*/
|
||||
@ -219,24 +219,24 @@ class File extends DataObject {
|
||||
$file = DataObject::get_by_id("File", $orphan['ID']);
|
||||
$file->deleteDatabaseOnly();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Help to load the content of different type of files to File Table Content Field
|
||||
*/
|
||||
function test() {
|
||||
Debug::show(get_defined_functions());
|
||||
}
|
||||
|
||||
|
||||
function loadallcontent() {
|
||||
ini_set("max_execution_time", 50000);
|
||||
// get all file objects(not folders)
|
||||
$start = (int)$_GET[start];
|
||||
$allFiles = DataObject::get("File", "ClassName = 'File' AND Filename LIKE '%.pdf'", "", "", "$start, 5");
|
||||
$total = $allFiles->TotalItems();
|
||||
|
||||
|
||||
$i = $start;
|
||||
foreach($allFiles as $file) {
|
||||
$i++;
|
||||
@ -245,48 +245,48 @@ class File extends DataObject {
|
||||
$file->loadContent();
|
||||
}
|
||||
Director::redirect(HTTP::setGetVar("start", $start + 5));
|
||||
|
||||
|
||||
// run loadcontent on each one
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets the content of this file and puts it in the field Content
|
||||
*/
|
||||
function loadContent() {
|
||||
|
||||
|
||||
switch(strtolower($this->getExtension())){
|
||||
case 'pdf':
|
||||
case 'pdf':
|
||||
$filename = escapeshellarg($this->getFullPath());
|
||||
|
||||
|
||||
$content = `pstotext $filename`;
|
||||
|
||||
|
||||
//echo("<pre>Content for $this->Filename:\n$content</pre>");
|
||||
$this->Content = $content;
|
||||
$this->write();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function Link($action = null) {
|
||||
return Director::baseURL() . $this->RelativeLink($action);
|
||||
}
|
||||
|
||||
|
||||
function RelativeLink($action = null){
|
||||
return $this->Filename;
|
||||
}
|
||||
|
||||
|
||||
function TreeTitle() {
|
||||
return $this->Title;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Event handler called before deleting from the database.
|
||||
* You can overload this to clean up or otherwise process data before delete this
|
||||
* You can overload this to clean up or otherwise process data before delete this
|
||||
* record. Don't forget to call parent::onBeforeDelete(), though!
|
||||
*/
|
||||
protected function onBeforeDelete() {
|
||||
parent::onBeforeDelete();
|
||||
|
||||
|
||||
$this->autosetFilename();
|
||||
if($this->Filename && $this->Name && file_exists($this->getFullPath()) && !is_dir($this->getFullPath())) unlink($this->getFullPath());
|
||||
|
||||
@ -308,7 +308,7 @@ class File extends DataObject {
|
||||
|
||||
/**
|
||||
* Event handler called before deleting from the database.
|
||||
* You can overload this to clean up or otherwise process data before delete this
|
||||
* You can overload this to clean up or otherwise process data before delete this
|
||||
* record. Don't forget to call parent::onBeforeDelete(), though!
|
||||
*/
|
||||
protected function onBeforeWrite() {
|
||||
@ -324,9 +324,9 @@ class File extends DataObject {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Collate selected descendants of this page.
|
||||
* Collate selected descendants of this page.
|
||||
* $condition will be evaluated on each descendant, and if it is succeeds, that item will be added
|
||||
* to the $collator array.
|
||||
* @param condition The PHP condition to be evaluated. The page will be called $item
|
||||
@ -340,15 +340,15 @@ class File extends DataObject {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter function for Name.
|
||||
* Automatically sets a default title.
|
||||
*/
|
||||
function setName($name) {
|
||||
$oldName = $this->Name;
|
||||
|
||||
|
||||
// It can't be blank
|
||||
if(!$name) $name = $this->Title;
|
||||
|
||||
@ -362,8 +362,8 @@ 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);
|
||||
$base = substr($name,0,$dotPos);
|
||||
$ext = substr($name,$dotPos);
|
||||
} else {
|
||||
$base = $name;
|
||||
$ext = "";
|
||||
@ -374,21 +374,21 @@ class File extends DataObject {
|
||||
$name = "$base-$suffix$ext";
|
||||
}
|
||||
}
|
||||
|
||||
if(!$this->getField('Title')) $this->__set('Title', str_replace(array('-','_'),' ',ereg_replace('\.[^.]+$','',$name)));
|
||||
|
||||
if(!$this->getField('Title')) $this->__set('Title', str_replace(array('-','_'),' ',ereg_replace('\.[^.]+$','',$name)));
|
||||
$this->setField('Name', $name);
|
||||
|
||||
|
||||
|
||||
|
||||
if($oldName && $oldName != $this->Name) {
|
||||
$this->resetFilename();
|
||||
} else {
|
||||
$this->autosetFilename();
|
||||
$this->autosetFilename();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
return $this->getField('Name');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Change a filename, moving the file if appropriate.
|
||||
* @param $renamePhysicalFile Set this to false if you don't want to rename the physical file. Used when calling resetFilename() on the children of a folder.
|
||||
@ -396,19 +396,19 @@ class File extends DataObject {
|
||||
protected function resetFilename($renamePhysicalFile = true) {
|
||||
$oldFilename = $this->getField('Filename');
|
||||
$newFilename = $this->getRelativePath();
|
||||
|
||||
|
||||
if($this->Name && $this->Filename && file_exists(Director::getAbsFile($oldFilename)) && strpos($oldFilename, '//') === false) {
|
||||
if($renamePhysicalFile) {
|
||||
$from = Director::getAbsFile($oldFilename);
|
||||
$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);
|
||||
}
|
||||
@ -416,27 +416,27 @@ class File extends DataObject {
|
||||
// If the old file doesn't exist, maybe it's already been renamed.
|
||||
if(file_exists(Director::getAbsFile($newFilename))) $this->updateLinks($oldFilename, $newFilename);
|
||||
}
|
||||
|
||||
|
||||
$this->setField('Filename', $newFilename);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set the Filename field without manipulating the filesystem.
|
||||
*/
|
||||
protected function autosetFilename() {
|
||||
$this->setField('Filename', $this->getRelativePath());
|
||||
}
|
||||
|
||||
|
||||
function setField( $field, $value ) {
|
||||
parent::setField( $field, $value );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Rewrite links to the $old file to now point to the $new file
|
||||
*/
|
||||
protected function updateLinks($old, $new) {
|
||||
$pages = $this->BackLinkTracking();
|
||||
|
||||
|
||||
if($pages) {
|
||||
foreach($pages as $page) {
|
||||
$fieldName = $page->FieldName; // extracted from the many-many join
|
||||
@ -448,61 +448,61 @@ class File extends DataObject {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function setParentID($parentID) {
|
||||
$this->setField('ParentID', $parentID);
|
||||
|
||||
if($this->Name) $this->resetFilename();
|
||||
else $this->autosetFilename();
|
||||
|
||||
|
||||
return $this->getField('ParentID');
|
||||
}
|
||||
|
||||
|
||||
function getURL() {
|
||||
return Director::absoluteBaseURL() . $this->getFilename();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return the last 50 characters of the URL
|
||||
*/
|
||||
function getLinkedURL() {
|
||||
return "$this->Name";
|
||||
}
|
||||
|
||||
|
||||
function getFullPath() {
|
||||
return Director::baseFolder() . '/' . $this->getFilename();
|
||||
}
|
||||
|
||||
|
||||
function getRelativePath() {
|
||||
|
||||
|
||||
if($this->ParentID) {
|
||||
$p = $this->Parent();
|
||||
|
||||
|
||||
if($p->ID) return $p->getRelativePath() . $this->getField("Name");
|
||||
else return "assets/" . $this->getField("Name");
|
||||
|
||||
} else if($this->getField("Name")) {
|
||||
return "assets/" . $this->getField("Name");
|
||||
|
||||
|
||||
} else {
|
||||
return "assets";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function DeleteLink() {
|
||||
return Director::absoluteBaseURL()."admin/assets/removefile/".$this->ID;
|
||||
}
|
||||
|
||||
|
||||
function getFilename() {
|
||||
if($this->getField('Name')) return $this->getField('Filename');
|
||||
else return 'assets/';
|
||||
}
|
||||
|
||||
|
||||
function setFilename($val) {
|
||||
$this->setField('Filename', $val);
|
||||
$this->setField('Name', basename($val));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* FIXME This overrides getExtension() in DataObject, but it does something completely different.
|
||||
* This should be renamed to getFileExtension(), but has not been yet as it may break
|
||||
@ -527,7 +527,7 @@ class File extends DataObject {
|
||||
$ext = $this->getExtension();
|
||||
return isset($types[$ext]) ? $types[$ext] : 'unknown';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the size of the file type in an appropriate format.
|
||||
*/
|
||||
@ -539,9 +539,9 @@ class File extends DataObject {
|
||||
if($size < 1024*1024) return round($size/1024) . ' KB';
|
||||
if($size < 1024*1024*10) return (round(($size/1024)/1024*10)/10) . ' MB';
|
||||
if($size < 1024*1024*1024) return round(($size/1024)/1024) . ' MB';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* returns the size in bytes with no extensions for calculations.
|
||||
*/
|
||||
@ -553,7 +553,7 @@ class File extends DataObject {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------//
|
||||
// Helper control functions
|
||||
function moverootfilesto() {
|
||||
@ -567,7 +567,7 @@ class File extends DataObject {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Cleanup function to reset all the Filename fields. Visit File/fixfiles to call.
|
||||
*/
|
||||
@ -583,7 +583,7 @@ class File extends DataObject {
|
||||
|
||||
/**
|
||||
* We've overridden the DataObject::get function for File so that the very large content field
|
||||
* is excluded!
|
||||
* is excluded!
|
||||
*
|
||||
* @todo Admittedly this is a bit of a hack; but we need a way of ensuring that large
|
||||
* TEXT fields don't stuff things up for the rest of us. Perhaps a separate search table would
|
||||
@ -600,7 +600,7 @@ class File extends DataObject {
|
||||
|
||||
return $ret;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Stub, overridden by Folder
|
||||
*/
|
||||
@ -608,6 +608,6 @@ class File extends DataObject {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
?>
|
Loading…
Reference in New Issue
Block a user