mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Merge pull request #3004 from stecman/delete-resampled-after-image-upload
Delete formatted images after image upload
This commit is contained in:
commit
3620d01998
@ -186,6 +186,7 @@ class Upload extends Controller {
|
|||||||
// This is to prevent it from trying to rename the file
|
// This is to prevent it from trying to rename the file
|
||||||
$this->file->Name = basename($relativeFilePath);
|
$this->file->Name = basename($relativeFilePath);
|
||||||
$this->file->write();
|
$this->file->write();
|
||||||
|
$this->file->onAfterUpload();
|
||||||
$this->extend('onAfterLoad', $this->file); //to allow extensions to e.g. create a version after an upload
|
$this->extend('onAfterLoad', $this->file); //to allow extensions to e.g. create a version after an upload
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
|
@ -640,6 +640,11 @@ class Image extends File {
|
|||||||
return self::ORIENTATION_SQUARE;
|
return self::ORIENTATION_SQUARE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function onAfterUpload() {
|
||||||
|
$this->deleteFormattedImages();
|
||||||
|
parent::onAfterUpload();
|
||||||
|
}
|
||||||
|
|
||||||
protected function onBeforeDelete() {
|
protected function onBeforeDelete() {
|
||||||
parent::onBeforeDelete();
|
parent::onBeforeDelete();
|
||||||
|
@ -465,6 +465,48 @@ class UploadTest extends SapphireTest {
|
|||||||
$file3->delete();
|
$file3->delete();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testDeleteResampledImagesOnUpload() {
|
||||||
|
$tmpFileName = 'UploadTest-testUpload.jpg';
|
||||||
|
$tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName;
|
||||||
|
|
||||||
|
$uploadImage = function() use ($tmpFileName, $tmpFilePath) {
|
||||||
|
copy(__DIR__ . '/gdtest/test_jpg.jpg', $tmpFilePath);
|
||||||
|
|
||||||
|
// emulates the $_FILES array
|
||||||
|
$tmpFile = array(
|
||||||
|
'name' => $tmpFileName,
|
||||||
|
'type' => 'text/plaintext',
|
||||||
|
'size' => filesize($tmpFilePath),
|
||||||
|
'tmp_name' => $tmpFilePath,
|
||||||
|
'extension' => 'jpg',
|
||||||
|
'error' => UPLOAD_ERR_OK,
|
||||||
|
);
|
||||||
|
|
||||||
|
$v = new UploadTest_Validator();
|
||||||
|
|
||||||
|
// test upload into default folder
|
||||||
|
$u = new Upload();
|
||||||
|
$u->setReplaceFile(true);
|
||||||
|
$u->setValidator($v);
|
||||||
|
$u->load($tmpFile);
|
||||||
|
return $u->getFile();
|
||||||
|
};
|
||||||
|
|
||||||
|
// Image upload and generate a resampled image
|
||||||
|
$image = $uploadImage();
|
||||||
|
$resampled = $image->ResizedImage(123, 456);
|
||||||
|
$resampledPath = $resampled->getFullPath();
|
||||||
|
$this->assertTrue(file_exists($resampledPath));
|
||||||
|
|
||||||
|
// Re-upload the image, overwriting the original
|
||||||
|
// Resampled images should removed when their parent file is overwritten
|
||||||
|
$image = $uploadImage();
|
||||||
|
$this->assertFalse(file_exists($resampledPath));
|
||||||
|
|
||||||
|
unlink($tmpFilePath);
|
||||||
|
$image->delete();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
class UploadTest_Validator extends Upload_Validator implements TestOnly {
|
class UploadTest_Validator extends Upload_Validator implements TestOnly {
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user