mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-09-07 10:11:17 +02:00
Merge pull request #4251 from jonom/file-versions-prefix-master
NEW Configurable file version prefix
This commit is contained in:
commit
358b7bfa05
@ -72,6 +72,17 @@ class Upload extends Controller {
|
|||||||
*/
|
*/
|
||||||
private static $uploads_folder = "Uploads";
|
private static $uploads_folder = "Uploads";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A prefix for the version number added to an uploaded file
|
||||||
|
* when a file with the same name already exists.
|
||||||
|
* Example using no prefix: IMG001.jpg becomes IMG2.jpg
|
||||||
|
* Example using '-v' prefix: IMG001.jpg becomes IMG001-v2.jpg
|
||||||
|
*
|
||||||
|
* @config
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private static $version_prefix = '-v';
|
||||||
|
|
||||||
public function __construct() {
|
public function __construct() {
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
$this->validator = Injector::inst()->create('Upload_Validator');
|
$this->validator = Injector::inst()->create('Upload_Validator');
|
||||||
@ -157,7 +168,7 @@ class Upload extends Controller {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// if filename already exists, version the filename (e.g. test.gif to test2.gif, test2.gif to test3.gif)
|
// if filename already exists, version the filename (e.g. test.gif to test-v2.gif, test-v2.gif to test-v3.gif)
|
||||||
if(!$this->replaceFile) {
|
if(!$this->replaceFile) {
|
||||||
$fileSuffixArray = explode('.', $fileName);
|
$fileSuffixArray = explode('.', $fileName);
|
||||||
$fileTitle = array_shift($fileSuffixArray);
|
$fileTitle = array_shift($fileSuffixArray);
|
||||||
@ -175,11 +186,12 @@ class Upload extends Controller {
|
|||||||
$i = isset($i) ? ($i+1) : 2;
|
$i = isset($i) ? ($i+1) : 2;
|
||||||
$oldFilePath = $relativeFilePath;
|
$oldFilePath = $relativeFilePath;
|
||||||
|
|
||||||
$pattern = '/([0-9]+$)/';
|
$prefix = $this->config()->version_prefix;
|
||||||
if(preg_match($pattern, $fileTitle)) {
|
$pattern = '/' . preg_quote($prefix) . '([0-9]+$)/';
|
||||||
$fileTitle = preg_replace($pattern, $i, $fileTitle);
|
if(preg_match($pattern, $fileTitle, $matches)) {
|
||||||
|
$fileTitle = preg_replace($pattern, $prefix . ($matches[1] + 1), $fileTitle);
|
||||||
} else {
|
} else {
|
||||||
$fileTitle .= $i;
|
$fileTitle .= $prefix . $i;
|
||||||
}
|
}
|
||||||
$relativeFilePath = $relativeFolderPath . $fileTitle . $fileSuffix;
|
$relativeFilePath = $relativeFolderPath . $fileTitle . $fileSuffix;
|
||||||
|
|
||||||
|
@ -352,7 +352,7 @@ class UploadTest extends SapphireTest {
|
|||||||
$u->load($tmpFile);
|
$u->load($tmpFile);
|
||||||
$file2 = $u->getFile();
|
$file2 = $u->getFile();
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
'UploadTest-testUpload2.tar.gz',
|
'UploadTest-testUpload-v2.tar.gz',
|
||||||
$file2->Name,
|
$file2->Name,
|
||||||
'File receives a number attached to the end before the extension'
|
'File receives a number attached to the end before the extension'
|
||||||
);
|
);
|
||||||
@ -370,7 +370,7 @@ class UploadTest extends SapphireTest {
|
|||||||
$u->load($tmpFile);
|
$u->load($tmpFile);
|
||||||
$file3 = $u->getFile();
|
$file3 = $u->getFile();
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
'UploadTest-testUpload3.tar.gz',
|
'UploadTest-testUpload-v3.tar.gz',
|
||||||
$file3->Name,
|
$file3->Name,
|
||||||
'File receives a number attached to the end before the extension'
|
'File receives a number attached to the end before the extension'
|
||||||
);
|
);
|
||||||
@ -434,7 +434,7 @@ class UploadTest extends SapphireTest {
|
|||||||
$u->load($tmpFile);
|
$u->load($tmpFile);
|
||||||
$file2 = $u->getFile();
|
$file2 = $u->getFile();
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
'UploadTest-testUpload2',
|
'UploadTest-testUpload-v2',
|
||||||
$file2->Name,
|
$file2->Name,
|
||||||
'File receives a number attached to the end'
|
'File receives a number attached to the end'
|
||||||
);
|
);
|
||||||
@ -584,7 +584,7 @@ class UploadTest extends SapphireTest {
|
|||||||
$u->loadIntoFile($tmpFile, new File());
|
$u->loadIntoFile($tmpFile, new File());
|
||||||
$file3 = $u->getFile();
|
$file3 = $u->getFile();
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
'UploadTest-testUpload2.txt',
|
'UploadTest-testUpload-v2.txt',
|
||||||
$file3->Name,
|
$file3->Name,
|
||||||
'File does receive new name'
|
'File does receive new name'
|
||||||
);
|
);
|
||||||
@ -675,30 +675,34 @@ class UploadTest extends SapphireTest {
|
|||||||
return $u->getFile();
|
return $u->getFile();
|
||||||
};
|
};
|
||||||
|
|
||||||
$file1 = $upload('UploadTest-testUpload.jpg');
|
// test empty file version prefix
|
||||||
|
$originalVersionPrefix = Config::inst()->get('Upload', 'version_prefix');
|
||||||
|
Config::inst()->update('Upload', 'version_prefix', '');
|
||||||
|
|
||||||
|
$file1 = $upload('UploadTest-IMG001.jpg');
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
'UploadTest-testUpload.jpg',
|
'UploadTest-IMG001.jpg',
|
||||||
$file1->Name,
|
$file1->Name,
|
||||||
'File does not receive new name'
|
'File does not receive new name'
|
||||||
);
|
);
|
||||||
|
|
||||||
$file2 = $upload('UploadTest-testUpload.jpg');
|
$file2 = $upload('UploadTest-IMG001.jpg');
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
'UploadTest-testUpload2.jpg',
|
'UploadTest-IMG2.jpg',
|
||||||
$file2->Name,
|
$file2->Name,
|
||||||
'File does receive new name'
|
'File does receive new name'
|
||||||
);
|
);
|
||||||
|
|
||||||
$file3 = $upload('UploadTest-testUpload.jpg');
|
$file3 = $upload('UploadTest-IMG001.jpg');
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
'UploadTest-testUpload3.jpg',
|
'UploadTest-IMG3.jpg',
|
||||||
$file3->Name,
|
$file3->Name,
|
||||||
'File does receive new name'
|
'File does receive new name'
|
||||||
);
|
);
|
||||||
|
|
||||||
$file4 = $upload('UploadTest-testUpload3.jpg');
|
$file4 = $upload('UploadTest-IMG3.jpg');
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
'UploadTest-testUpload4.jpg',
|
'UploadTest-IMG4.jpg',
|
||||||
$file4->Name,
|
$file4->Name,
|
||||||
'File does receive new name'
|
'File does receive new name'
|
||||||
);
|
);
|
||||||
@ -707,6 +711,44 @@ class UploadTest extends SapphireTest {
|
|||||||
$file2->delete();
|
$file2->delete();
|
||||||
$file3->delete();
|
$file3->delete();
|
||||||
$file4->delete();
|
$file4->delete();
|
||||||
|
|
||||||
|
// test '-v' file version prefix
|
||||||
|
Config::inst()->update('Upload', 'version_prefix', '-v');
|
||||||
|
|
||||||
|
$file1 = $upload('UploadTest2-IMG001.jpg');
|
||||||
|
$this->assertEquals(
|
||||||
|
'UploadTest2-IMG001.jpg',
|
||||||
|
$file1->Name,
|
||||||
|
'File does not receive new name'
|
||||||
|
);
|
||||||
|
|
||||||
|
$file2 = $upload('UploadTest2-IMG001.jpg');
|
||||||
|
$this->assertEquals(
|
||||||
|
'UploadTest2-IMG001-v2.jpg',
|
||||||
|
$file2->Name,
|
||||||
|
'File does receive new name'
|
||||||
|
);
|
||||||
|
|
||||||
|
$file3 = $upload('UploadTest2-IMG001.jpg');
|
||||||
|
$this->assertEquals(
|
||||||
|
'UploadTest2-IMG001-v3.jpg',
|
||||||
|
$file3->Name,
|
||||||
|
'File does receive new name'
|
||||||
|
);
|
||||||
|
|
||||||
|
$file4 = $upload('UploadTest2-IMG001-v3.jpg');
|
||||||
|
$this->assertEquals(
|
||||||
|
'UploadTest2-IMG001-v4.jpg',
|
||||||
|
$file4->Name,
|
||||||
|
'File does receive new name'
|
||||||
|
);
|
||||||
|
|
||||||
|
$file1->delete();
|
||||||
|
$file2->delete();
|
||||||
|
$file3->delete();
|
||||||
|
$file4->delete();
|
||||||
|
|
||||||
|
Config::inst()->update('Upload', 'version_prefix', $originalVersionPrefix);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user