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) {
@ -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