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']);
$fileName = basename($file);
$relativeFilePath = $parentFolder
? $parentFolder->getRelativePath() . "$fileName"
: ASSETS_DIR . "/" . $fileName;
$relativeFolderPath = $parentFolder
? $parentFolder->getRelativePath()
: ASSETS_DIR . '/';
$relativeFilePath = $relativeFolderPath . $fileName;
// Create a new file record (or try to retrieve an existing one)
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) {
$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")) {
$i = isset($i) ? ($i+1) : 2;
$oldFilePath = $relativeFilePath;
// make sure archives retain valid extensions
if(substr($relativeFilePath, strlen($relativeFilePath) - strlen('.tar.gz')) == '.tar.gz' ||
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);
$pattern = '/([0-9]+$)/';
if(preg_match($pattern, $fileTitle)) {
$fileTitle = preg_replace($pattern, $i, $fileTitle);
} else {
$relativeFilePath .= '_'.$i;
$fileTitle .= $i;
}
$relativeFilePath = $relativeFolderPath . $fileTitle . $fileSuffix;
if($oldFilePath == $relativeFilePath && $i > 2) {
user_error("Couldn't fix $relativeFilePath with $i tries", E_USER_ERROR);
}
}
}
} else {
//reset the ownerID to the current member when replacing files
$this->file->OwnerID = (Member::currentUser() ? Member::currentUser()->ID : 0);

View File

@ -258,6 +258,10 @@ class UploadTest extends SapphireTest {
$file->Name,
'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->load($tmpFile);
@ -267,9 +271,37 @@ class UploadTest extends SapphireTest {
$file2->Name,
'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();
$file2->delete();
$file3->delete();
}
public function testUploadFileWithNoExtensionTwiceAppendsNumber() {
@ -307,16 +339,29 @@ class UploadTest extends SapphireTest {
$file->Name,
'File is uploaded without extension'
);
$this->assertFileExists(
BASE_PATH . '/' . $file->getRelativePath(),
'File exists'
);
$u = new Upload();
$u->setValidator($v);
$u->load($tmpFile);
$file2 = $u->getFile();
$this->assertEquals(
'UploadTest-testUpload-2',
'UploadTest-testUpload2',
$file2->Name,
'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();
$file2->delete();
@ -357,6 +402,10 @@ class UploadTest extends SapphireTest {
$file->Name,
'File is uploaded without extension'
);
$this->assertFileExists(
BASE_PATH . '/' . $file->getRelativePath(),
'File exists'
);
$u = new Upload();
$u->setValidator($v);
@ -368,6 +417,10 @@ class UploadTest extends SapphireTest {
$file2->Name,
'File does not receive new name'
);
$this->assertFileExists(
BASE_PATH . '/' . $file2->getRelativePath(),
'File exists'
);
$this->assertEquals(
$file->ID,
$file2->ID,