Merge branch 'pr/4396' into 3.2

* pr/4396:
  FIX Image_Cached exists method doesnt check for positive ID FIX Files should only report as "existing" if the file actually exists
This commit is contained in:
Damian Mooyman 2015-07-30 14:54:11 +12:00
commit f32fa4631a
4 changed files with 81 additions and 49 deletions

View File

@ -221,6 +221,15 @@ class File extends DataObject {
} }
} }
/**
* A file only exists if the file_exists() and is in the DB as a record
*
* @return bool
*/
public function exists() {
return parent::exists() && file_exists($this->getFullPath());
}
/** /**
* Find a File object by the given filename. * Find a File object by the given filename.
* *
@ -293,7 +302,7 @@ class File extends DataObject {
// ensure that the record is synced with the filesystem before deleting // ensure that the record is synced with the filesystem before deleting
$this->updateFilesystem(); $this->updateFilesystem();
if($this->Filename && $this->Name && file_exists($this->getFullPath()) && !is_dir($this->getFullPath())) { if($this->exists() && !is_dir($this->getFullPath())) {
unlink($this->getFullPath()); unlink($this->getFullPath());
} }
} }

View File

@ -122,18 +122,6 @@ class Image extends File implements Flushable {
return $fields; return $fields;
} }
/**
* An image exists if it has a filename.
* Does not do any filesystem checks.
*
* @return boolean
*/
public function exists() {
if(isset($this->record["Filename"])) {
return true;
}
}
/** /**
* Return an XHTML img tag for this Image, * Return an XHTML img tag for this Image,
* or NULL if the image file doesn't exist on the filesystem. * or NULL if the image file doesn't exist on the filesystem.
@ -141,7 +129,7 @@ class Image extends File implements Flushable {
* @return string * @return string
*/ */
public function getTag() { public function getTag() {
if(file_exists(Director::baseFolder() . '/' . $this->Filename)) { if($this->exists()) {
$url = $this->getURL(); $url = $this->getURL();
$title = ($this->Title) ? $this->Title : $this->Filename; $title = ($this->Title) ? $this->Title : $this->Filename;
if($this->Title) { if($this->Title) {
@ -229,7 +217,7 @@ class Image extends File implements Flushable {
// Check if image is already sized to the correct dimension // Check if image is already sized to the correct dimension
$widthRatio = $width / $this->getWidth(); $widthRatio = $width / $this->getWidth();
$heightRatio = $height / $this->getHeight(); $heightRatio = $height / $this->getHeight();
if( $widthRatio < $heightRatio ) { if( $widthRatio < $heightRatio ) {
// Target is higher aspect ratio than image, so check width // Target is higher aspect ratio than image, so check width
if($this->isWidth($width) && !Config::inst()->get('Image', 'force_resample')) return $this; if($this->isWidth($width) && !Config::inst()->get('Image', 'force_resample')) return $this;
@ -257,7 +245,7 @@ class Image extends File implements Flushable {
/** /**
* Proportionally scale down this image if it is wider or taller than the specified dimensions. * Proportionally scale down this image if it is wider or taller than the specified dimensions.
* Similar to Fit but without up-sampling. Use in templates with $FitMax. * Similar to Fit but without up-sampling. Use in templates with $FitMax.
* *
* @uses Image::Fit() * @uses Image::Fit()
* @param integer $width The maximum width of the output image * @param integer $width The maximum width of the output image
* @param integer $height The maximum height of the output image * @param integer $height The maximum height of the output image
@ -266,7 +254,7 @@ class Image extends File implements Flushable {
public function FitMax($width, $height) { public function FitMax($width, $height) {
// Temporary $force_resample support for 3.x, to be removed in 4.0 // Temporary $force_resample support for 3.x, to be removed in 4.0
if (Config::inst()->get('Image', 'force_resample') && $this->getWidth() <= $width && $this->getHeight() <= $height) return $this->Fit($this->getWidth(),$this->getHeight()); if (Config::inst()->get('Image', 'force_resample') && $this->getWidth() <= $width && $this->getHeight() <= $height) return $this->Fit($this->getWidth(),$this->getHeight());
return $this->getWidth() > $width || $this->getHeight() > $height return $this->getWidth() > $width || $this->getHeight() > $height
? $this->Fit($width,$height) ? $this->Fit($width,$height)
: $this; : $this;
@ -300,7 +288,7 @@ class Image extends File implements Flushable {
} }
/** /**
* Crop this image to the aspect ratio defined by the specified width and height, * Crop this image to the aspect ratio defined by the specified width and height,
* then scale down the image to those dimensions if it exceeds them. * then scale down the image to those dimensions if it exceeds them.
* Similar to Fill but without up-sampling. Use in templates with $FillMax. * Similar to Fill but without up-sampling. Use in templates with $FillMax.
* *
@ -312,13 +300,13 @@ class Image extends File implements Flushable {
public function FillMax($width, $height) { public function FillMax($width, $height) {
// Prevent divide by zero on missing/blank file // Prevent divide by zero on missing/blank file
if(!$this->getWidth() || !$this->getHeight()) return null; if(!$this->getWidth() || !$this->getHeight()) return null;
// Temporary $force_resample support for 3.x, to be removed in 4.0 // Temporary $force_resample support for 3.x, to be removed in 4.0
if (Config::inst()->get('Image', 'force_resample') && $this->isSize($width, $height)) return $this->Fill($width, $height); if (Config::inst()->get('Image', 'force_resample') && $this->isSize($width, $height)) return $this->Fill($width, $height);
// Is the image already the correct size? // Is the image already the correct size?
if ($this->isSize($width, $height)) return $this; if ($this->isSize($width, $height)) return $this;
// If not, make sure the image isn't upsampled // If not, make sure the image isn't upsampled
$imageRatio = $this->getWidth() / $this->getHeight(); $imageRatio = $this->getWidth() / $this->getHeight();
$cropRatio = $width / $height; $cropRatio = $width / $height;
@ -326,7 +314,7 @@ class Image extends File implements Flushable {
if ($cropRatio < $imageRatio && $this->getHeight() < $height) return $this->Fill($this->getHeight()*$cropRatio, $this->getHeight()); if ($cropRatio < $imageRatio && $this->getHeight() < $height) return $this->Fill($this->getHeight()*$cropRatio, $this->getHeight());
// Otherwise we're cropping on the y axis (or not cropping at all) so compare widths // Otherwise we're cropping on the y axis (or not cropping at all) so compare widths
if ($this->getWidth() < $width) return $this->Fill($this->getWidth(), $this->getWidth()/$cropRatio); if ($this->getWidth() < $width) return $this->Fill($this->getWidth(), $this->getWidth()/$cropRatio);
return $this->Fill($width, $height); return $this->Fill($width, $height);
} }
@ -379,7 +367,7 @@ class Image extends File implements Flushable {
} }
/** /**
* Proportionally scale down this image if it is wider than the specified width. * Proportionally scale down this image if it is wider than the specified width.
* Similar to ScaleWidth but without up-sampling. Use in templates with $ScaleMaxWidth. * Similar to ScaleWidth but without up-sampling. Use in templates with $ScaleMaxWidth.
* *
* @uses Image::ScaleWidth() * @uses Image::ScaleWidth()
@ -389,7 +377,7 @@ class Image extends File implements Flushable {
public function ScaleMaxWidth($width) { public function ScaleMaxWidth($width) {
// Temporary $force_resample support for 3.x, to be removed in 4.0 // Temporary $force_resample support for 3.x, to be removed in 4.0
if (Config::inst()->get('Image', 'force_resample') && $this->getWidth() <= $width) return $this->ScaleWidth($this->getWidth()); if (Config::inst()->get('Image', 'force_resample') && $this->getWidth() <= $width) return $this->ScaleWidth($this->getWidth());
return $this->getWidth() > $width return $this->getWidth() > $width
? $this->ScaleWidth($width) ? $this->ScaleWidth($width)
: $this; : $this;
@ -419,7 +407,7 @@ class Image extends File implements Flushable {
} }
/** /**
* Proportionally scale down this image if it is taller than the specified height. * Proportionally scale down this image if it is taller than the specified height.
* Similar to ScaleHeight but without up-sampling. Use in templates with $ScaleMaxHeight. * Similar to ScaleHeight but without up-sampling. Use in templates with $ScaleMaxHeight.
* *
* @uses Image::ScaleHeight() * @uses Image::ScaleHeight()
@ -429,7 +417,7 @@ class Image extends File implements Flushable {
public function ScaleMaxHeight($height) { public function ScaleMaxHeight($height) {
// Temporary $force_resample support for 3.x, to be removed in 4.0 // Temporary $force_resample support for 3.x, to be removed in 4.0
if (Config::inst()->get('Image', 'force_resample') && $this->getHeight() <= $height) return $this->ScaleHeight($this->getHeight()); if (Config::inst()->get('Image', 'force_resample') && $this->getHeight() <= $height) return $this->ScaleHeight($this->getHeight());
return $this->getHeight() > $height return $this->getHeight() > $height
? $this->ScaleHeight($height) ? $this->ScaleHeight($height)
: $this; : $this;
@ -446,7 +434,7 @@ class Image extends File implements Flushable {
public function CropWidth($width) { public function CropWidth($width) {
// Temporary $force_resample support for 3.x, to be removed in 4.0 // Temporary $force_resample support for 3.x, to be removed in 4.0
if (Config::inst()->get('Image', 'force_resample') && $this->getWidth() <= $width) return $this->Fill($this->getWidth(), $this->getHeight()); if (Config::inst()->get('Image', 'force_resample') && $this->getWidth() <= $width) return $this->Fill($this->getWidth(), $this->getHeight());
return $this->getWidth() > $width return $this->getWidth() > $width
? $this->Fill($width, $this->getHeight()) ? $this->Fill($width, $this->getHeight())
: $this; : $this;
@ -463,7 +451,7 @@ class Image extends File implements Flushable {
public function CropHeight($height) { public function CropHeight($height) {
// Temporary $force_resample support for 3.x, to be removed in 4.0 // Temporary $force_resample support for 3.x, to be removed in 4.0
if (Config::inst()->get('Image', 'force_resample') && $this->getHeight() <= $height) return $this->Fill($this->getWidth(), $this->getHeight()); if (Config::inst()->get('Image', 'force_resample') && $this->getHeight() <= $height) return $this->Fill($this->getWidth(), $this->getHeight());
return $this->getHeight() > $height return $this->getHeight() > $height
? $this->Fill($this->getWidth(), $height) ? $this->Fill($this->getWidth(), $height)
: $this; : $this;
@ -684,7 +672,7 @@ class Image extends File implements Flushable {
public function getFormattedImage($format) { public function getFormattedImage($format) {
$args = func_get_args(); $args = func_get_args();
if($this->ID && $this->Filename && Director::fileExists($this->Filename)) { if($this->exists()) {
$cacheFile = call_user_func_array(array($this, "cacheFilename"), $args); $cacheFile = call_user_func_array(array($this, "cacheFilename"), $args);
if(!file_exists(Director::baseFolder()."/".$cacheFile) || self::$flush) { if(!file_exists(Director::baseFolder()."/".$cacheFile) || self::$flush) {
@ -950,8 +938,8 @@ class Image extends File implements Flushable {
public function getDimensions($dim = "string") { public function getDimensions($dim = "string") {
if($this->getField('Filename')) { if($this->getField('Filename')) {
$imagefile = Director::baseFolder() . '/' . $this->getField('Filename'); $imagefile = $this->getFullPath();
if(file_exists($imagefile)) { if($this->exists()) {
$size = getimagesize($imagefile); $size = getimagesize($imagefile);
return ($dim === "string") ? "$size[0]x$size[1]" : $size[$dim]; return ($dim === "string") ? "$size[0]x$size[1]" : $size[$dim];
} else { } else {
@ -1029,6 +1017,16 @@ class Image_Cached extends Image {
$this->Filename = $filename; $this->Filename = $filename;
} }
/**
* Override the parent's exists method becuase the ID is explicitly set to -1 on a cached image we can't use the
* default check
*
* @return bool Whether the cached image exists
*/
public function exists() {
return file_exists($this->getFullPath());
}
public function getRelativePath() { public function getRelativePath() {
return $this->getField('Filename'); return $this->getField('Filename');
} }

View File

@ -253,23 +253,49 @@ class UploadFieldTest extends FunctionalTest {
// two should work and the third will fail. // two should work and the third will fail.
$record = $this->objFromFixture('UploadFieldTest_Record', 'record1'); $record = $this->objFromFixture('UploadFieldTest_Record', 'record1');
$record->HasManyFilesMaxTwo()->removeAll(); $record->HasManyFilesMaxTwo()->removeAll();
$this->assertCount(0, $record->HasManyFilesMaxTwo());
// Get references for each file to upload // Get references for each file to upload
$file1 = $this->objFromFixture('File', 'file1'); $file1 = $this->objFromFixture('File', 'file1');
$file2 = $this->objFromFixture('File', 'file2'); $file2 = $this->objFromFixture('File', 'file2');
$file3 = $this->objFromFixture('File', 'file3'); $file3 = $this->objFromFixture('File', 'file3');
$this->assertTrue($file1->exists());
$this->assertTrue($file2->exists());
$this->assertTrue($file3->exists());
// Write the first element, should be okay. // Write the first element, should be okay.
$response = $this->mockUploadFileIDs('HasManyFilesMaxTwo', array($file1->ID)); $response = $this->mockUploadFileIDs('HasManyFilesMaxTwo', array($file1->ID));
$this->assertEmpty($response['errors']); $this->assertEmpty($response['errors']);
$this->assertCount(1, $record->HasManyFilesMaxTwo());
$this->assertContains($file1->ID, $record->HasManyFilesMaxTwo()->getIDList());
$record->HasManyFilesMaxTwo()->removeAll();
$this->assertCount(0, $record->HasManyFilesMaxTwo());
$this->assertTrue($file1->exists());
$this->assertTrue($file2->exists());
$this->assertTrue($file3->exists());
// Write the second element, should be okay. // Write the second element, should be okay.
$response = $this->mockUploadFileIDs('HasManyFilesMaxTwo', array($file1->ID, $file2->ID)); $response = $this->mockUploadFileIDs('HasManyFilesMaxTwo', array($file1->ID, $file2->ID));
$this->assertEmpty($response['errors']); $this->assertEmpty($response['errors']);
$this->assertCount(2, $record->HasManyFilesMaxTwo());
$this->assertContains($file1->ID, $record->HasManyFilesMaxTwo()->getIDList());
$this->assertContains($file2->ID, $record->HasManyFilesMaxTwo()->getIDList());
$record->HasManyFilesMaxTwo()->removeAll();
$this->assertCount(0, $record->HasManyFilesMaxTwo());
$this->assertTrue($file1->exists());
$this->assertTrue($file2->exists());
$this->assertTrue($file3->exists());
// Write the third element, should result in error. // Write the third element, should result in error.
$response = $this->mockUploadFileIDs('HasManyFilesMaxTwo', array($file1->ID, $file2->ID, $file3->ID)); $response = $this->mockUploadFileIDs('HasManyFilesMaxTwo', array($file1->ID, $file2->ID, $file3->ID));
$this->assertNotEmpty($response['errors']); $this->assertNotEmpty($response['errors']);
$this->assertCount(0, $record->HasManyFilesMaxTwo());
} }
/** /**
@ -948,22 +974,21 @@ class UploadFieldTest extends FunctionalTest {
} }
public function setUp() { public function setUp() {
Config::inst()->update('File', 'update_filesystem', false);
parent::setUp(); parent::setUp();
if(!file_exists(ASSETS_PATH)) mkdir(ASSETS_PATH); if(!file_exists(ASSETS_PATH)) mkdir(ASSETS_PATH);
/* Create a test folders for each of the fixture references */ /* Create a test folders for each of the fixture references */
$folderIDs = $this->allFixtureIDs('Folder'); $folders = Folder::get()->byIDs($this->allFixtureIDs('Folder'));
foreach($folderIDs as $folderID) { foreach($folders as $folder) {
$folder = DataObject::get_by_id('Folder', $folderID); if(!file_exists($folder->getFullPath())) mkdir($folder->getFullPath());
if(!file_exists(BASE_PATH."/$folder->Filename")) mkdir(BASE_PATH."/$folder->Filename");
} }
/* Create a test files for each of the fixture references */ /* Create a test files for each of the fixture references */
$fileIDs = $this->allFixtureIDs('File'); $files = File::get()->byIDs($this->allFixtureIDs('File'));
foreach($fileIDs as $fileID) { foreach($files as $file) {
$file = DataObject::get_by_id('File', $fileID); $fh = fopen($file->getFullPath(), "w");
$fh = fopen(BASE_PATH."/$file->Filename", "w");
fwrite($fh, str_repeat('x',1000000)); fwrite($fh, str_repeat('x',1000000));
fclose($fh); fclose($fh);
} }

View File

@ -3,48 +3,48 @@ Folder:
Name: UploadFieldTest Name: UploadFieldTest
folder1-subfolder1: folder1-subfolder1:
Name: subfolder1 Name: subfolder1
ParentID: =>Folder.folder1 Parent: =>Folder.folder1
File: File:
file1: file1:
Title: File1 Title: File1
Filename: assets/UploadFieldTest/file1.txt Filename: assets/UploadFieldTest/file1.txt
ParentID: =>Folder.folder1 Parent: =>Folder.folder1
file2: file2:
Title: File2 Title: File2
Filename: assets/UploadFieldTest/file2.txt Filename: assets/UploadFieldTest/file2.txt
ParentID: =>Folder.folder1 Parent: =>Folder.folder1
file3: file3:
Title: File3 Title: File3
Filename: assets/UploadFieldTest/file3.txt Filename: assets/UploadFieldTest/file3.txt
ParentID: =>Folder.folder1 Parent: =>Folder.folder1
file4: file4:
Title: File4 Title: File4
Filename: assets/UploadFieldTest/file4.txt Filename: assets/UploadFieldTest/file4.txt
ParentID: =>Folder.folder1 Parent: =>Folder.folder1
file5: file5:
Title: File5 Title: File5
Filename: assets/UploadFieldTest/file5.txt Filename: assets/UploadFieldTest/file5.txt
ParentID: =>Folder.folder1 Parent: =>Folder.folder1
file-noview: file-noview:
Title: noview.txt Title: noview.txt
Name: noview.txt Name: noview.txt
Filename: assets/UploadFieldTest/noview.txt Filename: assets/UploadFieldTest/noview.txt
ParentID: =>Folder.folder1 Parent: =>Folder.folder1
file-noedit: file-noedit:
Title: noedit.txt Title: noedit.txt
Name: noedit.txt Name: noedit.txt
Filename: assets/UploadFieldTest/noedit.txt Filename: assets/UploadFieldTest/noedit.txt
ParentID: =>Folder.folder1 Parent: =>Folder.folder1
file-nodelete: file-nodelete:
Title: nodelete.txt Title: nodelete.txt
Name: nodelete.txt Name: nodelete.txt
Filename: assets/UploadFieldTest/nodelete.txt Filename: assets/UploadFieldTest/nodelete.txt
ParentID: =>Folder.folder1 Parent: =>Folder.folder1
file-subfolder: file-subfolder:
Title: file-subfolder.txt Title: file-subfolder.txt
Name: file-subfolder.txt Name: file-subfolder.txt
Filename: assets/UploadFieldTest/subfolder1/file-subfolder.txt Filename: assets/UploadFieldTest/subfolder1/file-subfolder.txt
ParentID: =>Folder.folder1-subfolder1 Parent: =>Folder.folder1-subfolder1
UploadFieldTest_Record: UploadFieldTest_Record:
record1: record1:
Title: Record 1 Title: Record 1