mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
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:
commit
f32fa4631a
@ -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.
|
||||
*
|
||||
@ -293,7 +302,7 @@ class File extends DataObject {
|
||||
// ensure that the record is synced with the filesystem before deleting
|
||||
$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());
|
||||
}
|
||||
}
|
||||
|
@ -122,18 +122,6 @@ class Image extends File implements Flushable {
|
||||
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,
|
||||
* or NULL if the image file doesn't exist on the filesystem.
|
||||
@ -141,7 +129,7 @@ class Image extends File implements Flushable {
|
||||
* @return string
|
||||
*/
|
||||
public function getTag() {
|
||||
if(file_exists(Director::baseFolder() . '/' . $this->Filename)) {
|
||||
if($this->exists()) {
|
||||
$url = $this->getURL();
|
||||
$title = ($this->Title) ? $this->Title : $this->Filename;
|
||||
if($this->Title) {
|
||||
@ -684,7 +672,7 @@ class Image extends File implements Flushable {
|
||||
public function getFormattedImage($format) {
|
||||
$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);
|
||||
|
||||
if(!file_exists(Director::baseFolder()."/".$cacheFile) || self::$flush) {
|
||||
@ -950,8 +938,8 @@ class Image extends File implements Flushable {
|
||||
public function getDimensions($dim = "string") {
|
||||
if($this->getField('Filename')) {
|
||||
|
||||
$imagefile = Director::baseFolder() . '/' . $this->getField('Filename');
|
||||
if(file_exists($imagefile)) {
|
||||
$imagefile = $this->getFullPath();
|
||||
if($this->exists()) {
|
||||
$size = getimagesize($imagefile);
|
||||
return ($dim === "string") ? "$size[0]x$size[1]" : $size[$dim];
|
||||
} else {
|
||||
@ -1029,6 +1017,16 @@ class Image_Cached extends Image {
|
||||
$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() {
|
||||
return $this->getField('Filename');
|
||||
}
|
||||
|
@ -253,23 +253,49 @@ class UploadFieldTest extends FunctionalTest {
|
||||
// two should work and the third will fail.
|
||||
$record = $this->objFromFixture('UploadFieldTest_Record', 'record1');
|
||||
$record->HasManyFilesMaxTwo()->removeAll();
|
||||
$this->assertCount(0, $record->HasManyFilesMaxTwo());
|
||||
|
||||
// Get references for each file to upload
|
||||
$file1 = $this->objFromFixture('File', 'file1');
|
||||
$file2 = $this->objFromFixture('File', 'file2');
|
||||
$file3 = $this->objFromFixture('File', 'file3');
|
||||
$this->assertTrue($file1->exists());
|
||||
$this->assertTrue($file2->exists());
|
||||
$this->assertTrue($file3->exists());
|
||||
|
||||
// Write the first element, should be okay.
|
||||
$response = $this->mockUploadFileIDs('HasManyFilesMaxTwo', array($file1->ID));
|
||||
$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.
|
||||
$response = $this->mockUploadFileIDs('HasManyFilesMaxTwo', array($file1->ID, $file2->ID));
|
||||
$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.
|
||||
$response = $this->mockUploadFileIDs('HasManyFilesMaxTwo', array($file1->ID, $file2->ID, $file3->ID));
|
||||
$this->assertNotEmpty($response['errors']);
|
||||
$this->assertCount(0, $record->HasManyFilesMaxTwo());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -948,22 +974,21 @@ class UploadFieldTest extends FunctionalTest {
|
||||
}
|
||||
|
||||
public function setUp() {
|
||||
Config::inst()->update('File', 'update_filesystem', false);
|
||||
parent::setUp();
|
||||
|
||||
if(!file_exists(ASSETS_PATH)) mkdir(ASSETS_PATH);
|
||||
|
||||
/* Create a test folders for each of the fixture references */
|
||||
$folderIDs = $this->allFixtureIDs('Folder');
|
||||
foreach($folderIDs as $folderID) {
|
||||
$folder = DataObject::get_by_id('Folder', $folderID);
|
||||
if(!file_exists(BASE_PATH."/$folder->Filename")) mkdir(BASE_PATH."/$folder->Filename");
|
||||
$folders = Folder::get()->byIDs($this->allFixtureIDs('Folder'));
|
||||
foreach($folders as $folder) {
|
||||
if(!file_exists($folder->getFullPath())) mkdir($folder->getFullPath());
|
||||
}
|
||||
|
||||
/* Create a test files for each of the fixture references */
|
||||
$fileIDs = $this->allFixtureIDs('File');
|
||||
foreach($fileIDs as $fileID) {
|
||||
$file = DataObject::get_by_id('File', $fileID);
|
||||
$fh = fopen(BASE_PATH."/$file->Filename", "w");
|
||||
$files = File::get()->byIDs($this->allFixtureIDs('File'));
|
||||
foreach($files as $file) {
|
||||
$fh = fopen($file->getFullPath(), "w");
|
||||
fwrite($fh, str_repeat('x',1000000));
|
||||
fclose($fh);
|
||||
}
|
||||
|
@ -3,48 +3,48 @@ Folder:
|
||||
Name: UploadFieldTest
|
||||
folder1-subfolder1:
|
||||
Name: subfolder1
|
||||
ParentID: =>Folder.folder1
|
||||
Parent: =>Folder.folder1
|
||||
File:
|
||||
file1:
|
||||
Title: File1
|
||||
Filename: assets/UploadFieldTest/file1.txt
|
||||
ParentID: =>Folder.folder1
|
||||
Parent: =>Folder.folder1
|
||||
file2:
|
||||
Title: File2
|
||||
Filename: assets/UploadFieldTest/file2.txt
|
||||
ParentID: =>Folder.folder1
|
||||
Parent: =>Folder.folder1
|
||||
file3:
|
||||
Title: File3
|
||||
Filename: assets/UploadFieldTest/file3.txt
|
||||
ParentID: =>Folder.folder1
|
||||
Parent: =>Folder.folder1
|
||||
file4:
|
||||
Title: File4
|
||||
Filename: assets/UploadFieldTest/file4.txt
|
||||
ParentID: =>Folder.folder1
|
||||
Parent: =>Folder.folder1
|
||||
file5:
|
||||
Title: File5
|
||||
Filename: assets/UploadFieldTest/file5.txt
|
||||
ParentID: =>Folder.folder1
|
||||
Parent: =>Folder.folder1
|
||||
file-noview:
|
||||
Title: noview.txt
|
||||
Name: noview.txt
|
||||
Filename: assets/UploadFieldTest/noview.txt
|
||||
ParentID: =>Folder.folder1
|
||||
Parent: =>Folder.folder1
|
||||
file-noedit:
|
||||
Title: noedit.txt
|
||||
Name: noedit.txt
|
||||
Filename: assets/UploadFieldTest/noedit.txt
|
||||
ParentID: =>Folder.folder1
|
||||
Parent: =>Folder.folder1
|
||||
file-nodelete:
|
||||
Title: nodelete.txt
|
||||
Name: nodelete.txt
|
||||
Filename: assets/UploadFieldTest/nodelete.txt
|
||||
ParentID: =>Folder.folder1
|
||||
Parent: =>Folder.folder1
|
||||
file-subfolder:
|
||||
Title: file-subfolder.txt
|
||||
Name: file-subfolder.txt
|
||||
Filename: assets/UploadFieldTest/subfolder1/file-subfolder.txt
|
||||
ParentID: =>Folder.folder1-subfolder1
|
||||
Parent: =>Folder.folder1-subfolder1
|
||||
UploadFieldTest_Record:
|
||||
record1:
|
||||
Title: Record 1
|
||||
|
Loading…
Reference in New Issue
Block a user