mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
BUG Upload: File versioning with existing files
reinsert oldFilePath = relativeFilePath in while loop
This commit is contained in:
parent
93b84358c8
commit
478edfa0c6
@ -173,6 +173,8 @@ class Upload extends Controller {
|
||||
}
|
||||
while(file_exists("$base/$relativeFilePath")) {
|
||||
$i = isset($i) ? ($i+1) : 2;
|
||||
$oldFilePath = $relativeFilePath;
|
||||
|
||||
$pattern = '/([0-9]+$)/';
|
||||
if(preg_match($pattern, $fileTitle)) {
|
||||
$fileTitle = preg_replace($pattern, $i, $fileTitle);
|
||||
@ -180,6 +182,7 @@ class Upload extends Controller {
|
||||
$fileTitle .= $i;
|
||||
}
|
||||
$relativeFilePath = $relativeFolderPath . $fileTitle . $fileSuffix;
|
||||
|
||||
if($oldFilePath == $relativeFilePath && $i > 2) {
|
||||
user_error("Couldn't fix $relativeFilePath with $i tries", E_USER_ERROR);
|
||||
}
|
||||
|
@ -560,6 +560,70 @@ class UploadTest extends SapphireTest {
|
||||
$image->delete();
|
||||
}
|
||||
|
||||
public function testFileVersioningWithAnExistingFile() {
|
||||
$upload = function($tmpFileName) {
|
||||
// create tmp file
|
||||
$tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName;
|
||||
$tmpFileContent = '';
|
||||
for ($i = 0; $i < 10000; $i++) {
|
||||
$tmpFileContent .= '0';
|
||||
}
|
||||
file_put_contents($tmpFilePath, $tmpFileContent);
|
||||
|
||||
// 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(false);
|
||||
$u->setValidator($v);
|
||||
$u->load($tmpFile);
|
||||
return $u->getFile();
|
||||
};
|
||||
|
||||
$file1 = $upload('UploadTest-testUpload.jpg');
|
||||
$this->assertEquals(
|
||||
'UploadTest-testUpload.jpg',
|
||||
$file1->Name,
|
||||
'File does not receive new name'
|
||||
);
|
||||
|
||||
$file2 = $upload('UploadTest-testUpload.jpg');
|
||||
$this->assertEquals(
|
||||
'UploadTest-testUpload2.jpg',
|
||||
$file2->Name,
|
||||
'File does receive new name'
|
||||
);
|
||||
|
||||
$file3 = $upload('UploadTest-testUpload.jpg');
|
||||
$this->assertEquals(
|
||||
'UploadTest-testUpload3.jpg',
|
||||
$file3->Name,
|
||||
'File does receive new name'
|
||||
);
|
||||
|
||||
$file4 = $upload('UploadTest-testUpload3.jpg');
|
||||
$this->assertEquals(
|
||||
'UploadTest-testUpload4.jpg',
|
||||
$file4->Name,
|
||||
'File does receive new name'
|
||||
);
|
||||
|
||||
$file1->delete();
|
||||
$file2->delete();
|
||||
$file3->delete();
|
||||
$file4->delete();
|
||||
}
|
||||
|
||||
}
|
||||
class UploadTest_Validator extends Upload_Validator implements TestOnly {
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user