Upload: refactor file versioning

This commit is contained in:
Devlin 2014-03-04 13:43:08 +01:00
parent 7ecc563783
commit 0096ab0d3d
2 changed files with 77 additions and 16 deletions

View File

@ -134,9 +134,10 @@ class Upload extends Controller {
$file = $nameFilter->filter($tmpFile['name']); $file = $nameFilter->filter($tmpFile['name']);
$fileName = basename($file); $fileName = basename($file);
$relativeFilePath = $parentFolder $relativeFolderPath = $parentFolder
? $parentFolder->getRelativePath() . "$fileName" ? $parentFolder->getRelativePath()
: ASSETS_DIR . "/" . $fileName; : ASSETS_DIR . '/';
$relativeFilePath = $relativeFolderPath . $fileName;
// Create a new file record (or try to retrieve an existing one) // Create a new file record (or try to retrieve an existing one)
if(!$this->file) { if(!$this->file) {
@ -156,26 +157,33 @@ class Upload extends Controller {
} }
} }
// if filename already exists, version the filename (e.g. test.gif to test1.gif) // if filename already exists, version the filename (e.g. test.gif to test2.gif, test2.gif to test3.gif)
if(!$this->replaceFile) { if(!$this->replaceFile) {
$fileSuffixArray = explode('.', $fileName);
$fileTitle = array_shift($fileSuffixArray);
$fileSuffix = !empty($fileSuffixArray)
? '.' . implode('.', $fileSuffixArray)
: null;
// make sure files retain valid extensions
$oldFilePath = $relativeFilePath;
$relativeFilePath = $relativeFolderPath . $fileTitle . $fileSuffix;
if($oldFilePath !== $relativeFilePath) {
user_error("Couldn't fix $relativeFilePath", E_USER_ERROR);
}
while(file_exists("$base/$relativeFilePath")) { while(file_exists("$base/$relativeFilePath")) {
$i = isset($i) ? ($i+1) : 2; $i = isset($i) ? ($i+1) : 2;
$oldFilePath = $relativeFilePath; $pattern = '/([0-9]+$)/';
// make sure archives retain valid extensions if(preg_match($pattern, $fileTitle)) {
if(substr($relativeFilePath, strlen($relativeFilePath) - strlen('.tar.gz')) == '.tar.gz' || $fileTitle = preg_replace($pattern, $i, $fileTitle);
substr($relativeFilePath, strlen($relativeFilePath) - strlen('.tar.bz2')) == '.tar.bz2') {
$relativeFilePath = preg_replace('/[0-9]*(\.tar\.[^.]+$)/', $i . '\\1', $relativeFilePath);
} else if (strpos($relativeFilePath, '.') !== false) {
$relativeFilePath = preg_replace('/[0-9]*(\.[^.]+$)/', $i . '\\1', $relativeFilePath);
} else if (strpos($relativeFilePath, '_') !== false) {
$relativeFilePath = preg_replace('/_([^_]+$)/', '_'.$i, $relativeFilePath);
} else { } else {
$relativeFilePath .= '_'.$i; $fileTitle .= $i;
} }
$relativeFilePath = $relativeFolderPath . $fileTitle . $fileSuffix;
if($oldFilePath == $relativeFilePath && $i > 2) { if($oldFilePath == $relativeFilePath && $i > 2) {
user_error("Couldn't fix $relativeFilePath with $i tries", E_USER_ERROR); user_error("Couldn't fix $relativeFilePath with $i tries", E_USER_ERROR);
} }
} }
} else { } else {
//reset the ownerID to the current member when replacing files //reset the ownerID to the current member when replacing files
$this->file->OwnerID = (Member::currentUser() ? Member::currentUser()->ID : 0); $this->file->OwnerID = (Member::currentUser() ? Member::currentUser()->ID : 0);

View File

@ -258,6 +258,10 @@ class UploadTest extends SapphireTest {
$file->Name, $file->Name,
'File has a name without a number because it\'s not a duplicate' 'File has a name without a number because it\'s not a duplicate'
); );
$this->assertFileExists(
BASE_PATH . '/' . $file->getRelativePath(),
'File exists'
);
$u = new Upload(); $u = new Upload();
$u->load($tmpFile); $u->load($tmpFile);
@ -267,9 +271,37 @@ class UploadTest extends SapphireTest {
$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'
); );
$this->assertFileExists(
BASE_PATH . '/' . $file2->getRelativePath(),
'File exists'
);
$this->assertGreaterThan(
$file->ID,
$file2->ID,
'File database record is not the same'
);
$u = new Upload();
$u->load($tmpFile);
$file3 = $u->getFile();
$this->assertEquals(
'UploadTest-testUpload3.tar.gz',
$file3->Name,
'File receives a number attached to the end before the extension'
);
$this->assertFileExists(
BASE_PATH . '/' . $file3->getRelativePath(),
'File exists'
);
$this->assertGreaterThan(
$file2->ID,
$file3->ID,
'File database record is not the same'
);
$file->delete(); $file->delete();
$file2->delete(); $file2->delete();
$file3->delete();
} }
public function testUploadFileWithNoExtensionTwiceAppendsNumber() { public function testUploadFileWithNoExtensionTwiceAppendsNumber() {
@ -307,16 +339,29 @@ class UploadTest extends SapphireTest {
$file->Name, $file->Name,
'File is uploaded without extension' 'File is uploaded without extension'
); );
$this->assertFileExists(
BASE_PATH . '/' . $file->getRelativePath(),
'File exists'
);
$u = new Upload(); $u = new Upload();
$u->setValidator($v); $u->setValidator($v);
$u->load($tmpFile); $u->load($tmpFile);
$file2 = $u->getFile(); $file2 = $u->getFile();
$this->assertEquals( $this->assertEquals(
'UploadTest-testUpload-2', 'UploadTest-testUpload2',
$file2->Name, $file2->Name,
'File receives a number attached to the end' 'File receives a number attached to the end'
); );
$this->assertFileExists(
BASE_PATH . '/' . $file2->getRelativePath(),
'File exists'
);
$this->assertGreaterThan(
$file->ID,
$file2->ID,
'File database record is not the same'
);
$file->delete(); $file->delete();
$file2->delete(); $file2->delete();
@ -357,6 +402,10 @@ class UploadTest extends SapphireTest {
$file->Name, $file->Name,
'File is uploaded without extension' 'File is uploaded without extension'
); );
$this->assertFileExists(
BASE_PATH . '/' . $file->getRelativePath(),
'File exists'
);
$u = new Upload(); $u = new Upload();
$u->setValidator($v); $u->setValidator($v);
@ -368,6 +417,10 @@ class UploadTest extends SapphireTest {
$file2->Name, $file2->Name,
'File does not receive new name' 'File does not receive new name'
); );
$this->assertFileExists(
BASE_PATH . '/' . $file2->getRelativePath(),
'File exists'
);
$this->assertEquals( $this->assertEquals(
$file->ID, $file->ID,
$file2->ID, $file2->ID,