2015-09-03 07:46:08 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
use Filesystem as SS_Filesystem;
|
|
|
|
use League\Flysystem\Filesystem;
|
|
|
|
use SilverStripe\Filesystem\Flysystem\AssetAdapter;
|
|
|
|
use SilverStripe\Filesystem\Flysystem\FlysystemAssetStore;
|
|
|
|
use SilverStripe\Filesystem\Flysystem\FlysystemUrlPlugin;
|
2015-09-15 04:52:02 +02:00
|
|
|
use SilverStripe\Filesystem\Storage\AssetContainer;
|
2015-09-03 07:46:08 +02:00
|
|
|
use SilverStripe\Filesystem\Storage\AssetStore;
|
2015-11-26 03:19:43 +01:00
|
|
|
use SilverStripe\Filesystem\Storage\FlysystemGeneratedAssetHandler;
|
2015-09-03 07:46:08 +02:00
|
|
|
|
|
|
|
class AssetStoreTest extends SapphireTest {
|
|
|
|
|
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
// Set backend and base url
|
|
|
|
AssetStoreTest_SpyStore::activate('DBFileTest');
|
2015-09-03 07:46:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown() {
|
2015-09-15 04:52:02 +02:00
|
|
|
AssetStoreTest_SpyStore::reset();
|
2015-09-03 07:46:08 +02:00
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-09-15 04:52:02 +02:00
|
|
|
* @return AssetStoreTest_SpyStore
|
2015-09-03 07:46:08 +02:00
|
|
|
*/
|
|
|
|
protected function getBackend() {
|
|
|
|
return Injector::inst()->get('AssetStore');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test different storage methods
|
|
|
|
*/
|
|
|
|
public function testStorageMethods() {
|
|
|
|
$backend = $this->getBackend();
|
|
|
|
|
|
|
|
// Test setFromContent
|
|
|
|
$puppies1 = 'puppies';
|
|
|
|
$puppies1Tuple = $backend->setFromString($puppies1, 'pets/my-puppy.txt');
|
|
|
|
$this->assertEquals(
|
|
|
|
array (
|
|
|
|
'Hash' => '2a17a9cb4be918774e73ba83bd1c1e7d000fdd53',
|
|
|
|
'Filename' => 'pets/my-puppy.txt',
|
|
|
|
'Variant' => '',
|
|
|
|
),
|
|
|
|
$puppies1Tuple
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test setFromStream (seekable)
|
2015-09-15 04:52:02 +02:00
|
|
|
$fish1 = realpath(__DIR__ .'/../model/testimages/test-image-high-quality.jpg');
|
2015-09-03 07:46:08 +02:00
|
|
|
$fish1Stream = fopen($fish1, 'r');
|
|
|
|
$fish1Tuple = $backend->setFromStream($fish1Stream, 'parent/awesome-fish.jpg');
|
|
|
|
fclose($fish1Stream);
|
|
|
|
$this->assertEquals(
|
|
|
|
array (
|
|
|
|
'Hash' => 'a870de278b475cb75f5d9f451439b2d378e13af1',
|
|
|
|
'Filename' => 'parent/awesome-fish.jpg',
|
|
|
|
'Variant' => '',
|
|
|
|
),
|
|
|
|
$fish1Tuple
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test with non-seekable streams
|
|
|
|
AssetStoreTest_SpyStore::$seekable_override = false;
|
2015-09-15 04:52:02 +02:00
|
|
|
$fish2 = realpath(__DIR__ .'/../model/testimages/test-image-low-quality.jpg');
|
2015-09-03 07:46:08 +02:00
|
|
|
$fish2Stream = fopen($fish2, 'r');
|
|
|
|
$fish2Tuple = $backend->setFromStream($fish2Stream, 'parent/mediocre-fish.jpg');
|
|
|
|
fclose($fish2Stream);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
array (
|
|
|
|
'Hash' => '33be1b95cba0358fe54e8b13532162d52f97421c',
|
|
|
|
'Filename' => 'parent/mediocre-fish.jpg',
|
|
|
|
'Variant' => '',
|
|
|
|
),
|
|
|
|
$fish2Tuple
|
|
|
|
);
|
|
|
|
AssetStoreTest_SpyStore::$seekable_override = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that the backend correctly resolves conflicts
|
|
|
|
*/
|
|
|
|
public function testConflictResolution() {
|
|
|
|
$backend = $this->getBackend();
|
|
|
|
|
|
|
|
// Put a file in
|
2015-09-15 04:52:02 +02:00
|
|
|
$fish1 = realpath(__DIR__ .'/../model/testimages/test-image-high-quality.jpg');
|
2015-09-03 07:46:08 +02:00
|
|
|
$this->assertFileExists($fish1);
|
|
|
|
$fish1Tuple = $backend->setFromLocalFile($fish1, 'directory/lovely-fish.jpg');
|
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
|
|
|
'Hash' => 'a870de278b475cb75f5d9f451439b2d378e13af1',
|
|
|
|
'Filename' => 'directory/lovely-fish.jpg',
|
|
|
|
'Variant' => '',
|
|
|
|
),
|
|
|
|
$fish1Tuple
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2015-09-15 04:52:02 +02:00
|
|
|
'/assets/DBFileTest/directory/a870de278b/lovely-fish.jpg',
|
|
|
|
$backend->getAsURL($fish1Tuple['Filename'], $fish1Tuple['Hash'])
|
2015-09-03 07:46:08 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
// Write a different file with same name. Should not detect duplicates since sha are different
|
2015-09-15 04:52:02 +02:00
|
|
|
$fish2 = realpath(__DIR__ .'/../model/testimages/test-image-low-quality.jpg');
|
2015-09-03 07:46:08 +02:00
|
|
|
try {
|
2015-09-15 04:52:02 +02:00
|
|
|
$fish2Tuple = $backend->setFromLocalFile($fish2, 'directory/lovely-fish.jpg', null, null, AssetStore::CONFLICT_EXCEPTION);
|
2015-09-03 07:46:08 +02:00
|
|
|
} catch(Exception $ex) {
|
|
|
|
return $this->fail('Writing file with different sha to same location failed with exception');
|
|
|
|
}
|
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
|
|
|
'Hash' => '33be1b95cba0358fe54e8b13532162d52f97421c',
|
|
|
|
'Filename' => 'directory/lovely-fish.jpg',
|
|
|
|
'Variant' => '',
|
|
|
|
),
|
|
|
|
$fish2Tuple
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2015-09-15 04:52:02 +02:00
|
|
|
'/assets/DBFileTest/directory/33be1b95cb/lovely-fish.jpg',
|
|
|
|
$backend->getAsURL($fish2Tuple['Filename'], $fish2Tuple['Hash'])
|
2015-09-03 07:46:08 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
// Write original file back with rename
|
|
|
|
$this->assertFileExists($fish1);
|
2015-09-15 04:52:02 +02:00
|
|
|
$fish3Tuple = $backend->setFromLocalFile($fish1, 'directory/lovely-fish.jpg', null, null, AssetStore::CONFLICT_RENAME);
|
2015-09-03 07:46:08 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
|
|
|
'Hash' => 'a870de278b475cb75f5d9f451439b2d378e13af1',
|
|
|
|
'Filename' => 'directory/lovely-fish-v2.jpg',
|
|
|
|
'Variant' => '',
|
|
|
|
),
|
|
|
|
$fish3Tuple
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2015-09-15 04:52:02 +02:00
|
|
|
'/assets/DBFileTest/directory/a870de278b/lovely-fish-v2.jpg',
|
|
|
|
$backend->getAsURL($fish3Tuple['Filename'], $fish3Tuple['Hash'])
|
2015-09-03 07:46:08 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
// Write another file should increment to -v3
|
2015-09-15 04:52:02 +02:00
|
|
|
$fish4Tuple = $backend->setFromLocalFile($fish1, 'directory/lovely-fish-v2.jpg', null, null, AssetStore::CONFLICT_RENAME);
|
2015-09-03 07:46:08 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
|
|
|
'Hash' => 'a870de278b475cb75f5d9f451439b2d378e13af1',
|
|
|
|
'Filename' => 'directory/lovely-fish-v3.jpg',
|
|
|
|
'Variant' => '',
|
|
|
|
),
|
|
|
|
$fish4Tuple
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2015-09-15 04:52:02 +02:00
|
|
|
'/assets/DBFileTest/directory/a870de278b/lovely-fish-v3.jpg',
|
|
|
|
$backend->getAsURL($fish4Tuple['Filename'], $fish4Tuple['Hash'])
|
2015-09-03 07:46:08 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
// Test conflict use existing file
|
2015-09-15 04:52:02 +02:00
|
|
|
$fish5Tuple = $backend->setFromLocalFile($fish1, 'directory/lovely-fish.jpg', null, null, AssetStore::CONFLICT_USE_EXISTING);
|
2015-09-03 07:46:08 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
|
|
|
'Hash' => 'a870de278b475cb75f5d9f451439b2d378e13af1',
|
|
|
|
'Filename' => 'directory/lovely-fish.jpg',
|
|
|
|
'Variant' => '',
|
|
|
|
),
|
|
|
|
$fish5Tuple
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2015-09-15 04:52:02 +02:00
|
|
|
'/assets/DBFileTest/directory/a870de278b/lovely-fish.jpg',
|
|
|
|
$backend->getAsURL($fish5Tuple['Filename'], $fish5Tuple['Hash'])
|
2015-09-03 07:46:08 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
// Test conflict use existing file
|
2015-09-15 04:52:02 +02:00
|
|
|
$fish6Tuple = $backend->setFromLocalFile($fish1, 'directory/lovely-fish.jpg', null, null, AssetStore::CONFLICT_OVERWRITE);
|
2015-09-03 07:46:08 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
|
|
|
'Hash' => 'a870de278b475cb75f5d9f451439b2d378e13af1',
|
|
|
|
'Filename' => 'directory/lovely-fish.jpg',
|
|
|
|
'Variant' => '',
|
|
|
|
),
|
|
|
|
$fish6Tuple
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2015-09-15 04:52:02 +02:00
|
|
|
'/assets/DBFileTest/directory/a870de278b/lovely-fish.jpg',
|
|
|
|
$backend->getAsURL($fish6Tuple['Filename'], $fish6Tuple['Hash'])
|
2015-09-03 07:46:08 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that flysystem can regenerate the original filename from fileID
|
|
|
|
*/
|
|
|
|
public function testGetOriginalFilename() {
|
|
|
|
$store = new AssetStoreTest_SpyStore();
|
|
|
|
$this->assertEquals(
|
|
|
|
'directory/lovely-fish.jpg',
|
|
|
|
$store->getOriginalFilename('directory/a870de278b/lovely-fish.jpg', $variant)
|
|
|
|
);
|
|
|
|
$this->assertEmpty($variant);
|
|
|
|
$this->assertEquals(
|
|
|
|
'directory/lovely-fish.jpg',
|
|
|
|
$store->getOriginalFilename('directory/a870de278b/lovely-fish__variant.jpg', $variant)
|
|
|
|
);
|
|
|
|
$this->assertEquals('variant', $variant);
|
|
|
|
$this->assertEquals(
|
|
|
|
'directory/lovely_fish.jpg',
|
|
|
|
$store->getOriginalFilename('directory/a870de278b/lovely_fish__vari_ant.jpg', $variant)
|
|
|
|
);
|
|
|
|
$this->assertEquals('vari_ant', $variant);
|
|
|
|
$this->assertEquals(
|
|
|
|
'directory/lovely_fish.jpg',
|
|
|
|
$store->getOriginalFilename('directory/a870de278b/lovely_fish.jpg', $variant)
|
|
|
|
);
|
|
|
|
$this->assertEmpty($variant);
|
|
|
|
$this->assertEquals(
|
|
|
|
'lovely-fish.jpg',
|
|
|
|
$store->getOriginalFilename('a870de278b/lovely-fish.jpg', $variant)
|
|
|
|
);
|
|
|
|
$this->assertEmpty($variant);
|
|
|
|
$this->assertEquals(
|
|
|
|
'lovely-fish.jpg',
|
|
|
|
$store->getOriginalFilename('a870de278b/lovely-fish__variant.jpg', $variant)
|
|
|
|
);
|
|
|
|
$this->assertEquals('variant', $variant);
|
|
|
|
$this->assertEquals(
|
|
|
|
'lovely_fish.jpg',
|
|
|
|
$store->getOriginalFilename('a870de278b/lovely_fish__vari__ant.jpg', $variant)
|
|
|
|
);
|
|
|
|
$this->assertEquals('vari__ant', $variant);
|
|
|
|
$this->assertEquals(
|
|
|
|
'lovely_fish.jpg',
|
|
|
|
$store->getOriginalFilename('a870de278b/lovely_fish.jpg', $variant)
|
|
|
|
);
|
|
|
|
$this->assertEmpty($variant);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test internal file Id generation
|
|
|
|
*/
|
|
|
|
public function testGetFileID() {
|
|
|
|
$store = new AssetStoreTest_SpyStore();
|
|
|
|
$this->assertEquals(
|
|
|
|
'directory/2a17a9cb4b/file.jpg',
|
2015-09-15 04:52:02 +02:00
|
|
|
$store->getFileID('directory/file.jpg', sha1('puppies'))
|
2015-09-03 07:46:08 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'2a17a9cb4b/file.jpg',
|
2015-09-15 04:52:02 +02:00
|
|
|
$store->getFileID('file.jpg', sha1('puppies'))
|
2015-09-03 07:46:08 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'dir_ectory/2a17a9cb4b/fil_e.jpg',
|
2015-09-15 04:52:02 +02:00
|
|
|
$store->getFileID('dir__ectory/fil__e.jpg', sha1('puppies'))
|
2015-09-03 07:46:08 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'directory/2a17a9cb4b/file_variant.jpg',
|
2015-09-15 04:52:02 +02:00
|
|
|
$store->getFileID('directory/file__variant.jpg', sha1('puppies'), null)
|
2015-09-03 07:46:08 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'directory/2a17a9cb4b/file__variant.jpg',
|
2015-09-15 04:52:02 +02:00
|
|
|
$store->getFileID('directory/file.jpg', sha1('puppies'), 'variant')
|
2015-09-03 07:46:08 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'2a17a9cb4b/file__var__iant.jpg',
|
2015-09-15 04:52:02 +02:00
|
|
|
$store->getFileID('file.jpg', sha1('puppies'), 'var__iant')
|
2015-09-03 07:46:08 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetMetadata() {
|
|
|
|
$backend = $this->getBackend();
|
|
|
|
|
|
|
|
// jpg
|
2015-09-15 04:52:02 +02:00
|
|
|
$fish = realpath(__DIR__ .'/../model/testimages/test-image-high-quality.jpg');
|
2015-09-03 07:46:08 +02:00
|
|
|
$fishTuple = $backend->setFromLocalFile($fish, 'parent/awesome-fish.jpg');
|
|
|
|
$this->assertEquals(
|
|
|
|
'image/jpeg',
|
2015-09-15 04:52:02 +02:00
|
|
|
$backend->getMimeType($fishTuple['Filename'], $fishTuple['Hash'])
|
2015-09-03 07:46:08 +02:00
|
|
|
);
|
2015-09-15 04:52:02 +02:00
|
|
|
$fishMeta = $backend->getMetadata($fishTuple['Filename'], $fishTuple['Hash']);
|
2015-09-03 07:46:08 +02:00
|
|
|
$this->assertEquals(151889, $fishMeta['size']);
|
|
|
|
$this->assertEquals('file', $fishMeta['type']);
|
|
|
|
$this->assertNotEmpty($fishMeta['timestamp']);
|
|
|
|
|
|
|
|
|
|
|
|
// text
|
|
|
|
$puppies = 'puppies';
|
|
|
|
$puppiesTuple = $backend->setFromString($puppies, 'pets/my-puppy.txt');
|
|
|
|
$this->assertEquals(
|
|
|
|
'text/plain',
|
2015-09-15 04:52:02 +02:00
|
|
|
$backend->getMimeType($puppiesTuple['Filename'], $puppiesTuple['Hash'])
|
2015-09-03 07:46:08 +02:00
|
|
|
);
|
2015-09-15 04:52:02 +02:00
|
|
|
$puppiesMeta = $backend->getMetadata($puppiesTuple['Filename'], $puppiesTuple['Hash']);
|
2015-09-03 07:46:08 +02:00
|
|
|
$this->assertEquals(7, $puppiesMeta['size']);
|
|
|
|
$this->assertEquals('file', $puppiesMeta['type']);
|
|
|
|
$this->assertNotEmpty($puppiesMeta['timestamp']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that legacy filenames work as expected
|
|
|
|
*/
|
|
|
|
public function testLegacyFilenames() {
|
|
|
|
Config::inst()->update(get_class(new FlysystemAssetStore()), 'legacy_filenames', true);
|
|
|
|
|
|
|
|
$backend = $this->getBackend();
|
|
|
|
|
|
|
|
// Put a file in
|
2015-09-15 04:52:02 +02:00
|
|
|
$fish1 = realpath(__DIR__ .'/../model/testimages/test-image-high-quality.jpg');
|
2015-09-03 07:46:08 +02:00
|
|
|
$this->assertFileExists($fish1);
|
|
|
|
$fish1Tuple = $backend->setFromLocalFile($fish1, 'directory/lovely-fish.jpg');
|
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
|
|
|
'Hash' => 'a870de278b475cb75f5d9f451439b2d378e13af1',
|
|
|
|
'Filename' => 'directory/lovely-fish.jpg',
|
|
|
|
'Variant' => '',
|
|
|
|
),
|
|
|
|
$fish1Tuple
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2015-09-15 04:52:02 +02:00
|
|
|
'/assets/DBFileTest/directory/lovely-fish.jpg',
|
|
|
|
$backend->getAsURL($fish1Tuple['Filename'], $fish1Tuple['Hash'])
|
2015-09-03 07:46:08 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
// Write a different file with same name.
|
|
|
|
// Since we are using legacy filenames, this should generate a new filename
|
2015-09-15 04:52:02 +02:00
|
|
|
$fish2 = realpath(__DIR__ .'/../model/testimages/test-image-low-quality.jpg');
|
2015-09-03 07:46:08 +02:00
|
|
|
try {
|
2015-09-15 04:52:02 +02:00
|
|
|
$backend->setFromLocalFile($fish2, 'directory/lovely-fish.jpg', null, null, AssetStore::CONFLICT_EXCEPTION);
|
2015-09-03 07:46:08 +02:00
|
|
|
return $this->fail('Writing file with different sha to same location should throw exception');
|
|
|
|
} catch(Exception $ex) {
|
|
|
|
// Success
|
|
|
|
}
|
|
|
|
|
|
|
|
// Re-attempt this file write with conflict_rename
|
2015-09-15 04:52:02 +02:00
|
|
|
$fish3Tuple = $backend->setFromLocalFile($fish2, 'directory/lovely-fish.jpg', null, null, AssetStore::CONFLICT_RENAME);
|
2015-09-03 07:46:08 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
|
|
|
'Hash' => '33be1b95cba0358fe54e8b13532162d52f97421c',
|
|
|
|
'Filename' => 'directory/lovely-fish-v2.jpg',
|
|
|
|
'Variant' => '',
|
|
|
|
),
|
|
|
|
$fish3Tuple
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2015-09-15 04:52:02 +02:00
|
|
|
'/assets/DBFileTest/directory/lovely-fish-v2.jpg',
|
|
|
|
$backend->getAsURL($fish3Tuple['Filename'], $fish3Tuple['Hash'])
|
2015-09-03 07:46:08 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
// Write back original file, but with CONFLICT_EXISTING. The file should not change
|
2015-09-15 04:52:02 +02:00
|
|
|
$fish4Tuple = $backend->setFromLocalFile($fish1, 'directory/lovely-fish-v2.jpg', null, null, AssetStore::CONFLICT_USE_EXISTING);
|
2015-09-03 07:46:08 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
|
|
|
'Hash' => '33be1b95cba0358fe54e8b13532162d52f97421c',
|
|
|
|
'Filename' => 'directory/lovely-fish-v2.jpg',
|
|
|
|
'Variant' => '',
|
|
|
|
),
|
|
|
|
$fish4Tuple
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2015-09-15 04:52:02 +02:00
|
|
|
'/assets/DBFileTest/directory/lovely-fish-v2.jpg',
|
|
|
|
$backend->getAsURL($fish4Tuple['Filename'], $fish4Tuple['Hash'])
|
2015-09-03 07:46:08 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
// Write back original file with CONFLICT_OVERWRITE. The file sha should now be updated
|
2015-09-15 04:52:02 +02:00
|
|
|
$fish5Tuple = $backend->setFromLocalFile($fish1, 'directory/lovely-fish-v2.jpg', null, null, AssetStore::CONFLICT_OVERWRITE);
|
2015-09-03 07:46:08 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
|
|
|
'Hash' => 'a870de278b475cb75f5d9f451439b2d378e13af1',
|
|
|
|
'Filename' => 'directory/lovely-fish-v2.jpg',
|
|
|
|
'Variant' => '',
|
|
|
|
),
|
|
|
|
$fish5Tuple
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2015-09-15 04:52:02 +02:00
|
|
|
'/assets/DBFileTest/directory/lovely-fish-v2.jpg',
|
|
|
|
$backend->getAsURL($fish5Tuple['Filename'], $fish5Tuple['Hash'])
|
2015-09-03 07:46:08 +02:00
|
|
|
);
|
|
|
|
}
|
2015-09-15 04:52:02 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test default conflict resolution
|
|
|
|
*/
|
|
|
|
public function testDefaultConflictResolution() {
|
|
|
|
$store = $this->getBackend();
|
|
|
|
|
|
|
|
// Disable legacy filenames
|
|
|
|
Config::inst()->update(get_class(new FlysystemAssetStore()), 'legacy_filenames', false);
|
|
|
|
$this->assertEquals(AssetStore::CONFLICT_OVERWRITE, $store->getDefaultConflictResolution(null));
|
|
|
|
$this->assertEquals(AssetStore::CONFLICT_OVERWRITE, $store->getDefaultConflictResolution('somevariant'));
|
|
|
|
|
|
|
|
// Enable legacy filenames
|
|
|
|
Config::inst()->update(get_class(new FlysystemAssetStore()), 'legacy_filenames', true);
|
|
|
|
$this->assertEquals(AssetStore::CONFLICT_RENAME, $store->getDefaultConflictResolution(null));
|
|
|
|
$this->assertEquals(AssetStore::CONFLICT_OVERWRITE, $store->getDefaultConflictResolution('somevariant'));
|
|
|
|
}
|
2015-09-03 07:46:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Spy!
|
|
|
|
*/
|
|
|
|
class AssetStoreTest_SpyStore extends FlysystemAssetStore {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set to true|false to override all isSeekableStream calls
|
2015-09-15 04:52:02 +02:00
|
|
|
*
|
2015-09-03 07:46:08 +02:00
|
|
|
* @var null|bool
|
|
|
|
*/
|
|
|
|
public static $seekable_override = null;
|
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
/**
|
|
|
|
* Base dir of current file
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public static $basedir = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set this store as the new asset backend
|
|
|
|
*
|
|
|
|
* @param string $basedir Basedir to store assets, which will be placed beneath 'assets' folder
|
|
|
|
*/
|
|
|
|
public static function activate($basedir) {
|
|
|
|
// Assign this as the new store
|
|
|
|
$adapter = new AssetAdapter(ASSETS_PATH . '/' . $basedir);
|
|
|
|
$filesystem = new Filesystem($adapter);
|
|
|
|
$filesystem->addPlugin(new FlysystemUrlPlugin());
|
|
|
|
$backend = new AssetStoreTest_SpyStore();
|
|
|
|
$backend->setFilesystem($filesystem);
|
|
|
|
Injector::inst()->registerService($backend, 'AssetStore');
|
|
|
|
|
2015-11-26 03:19:43 +01:00
|
|
|
// Assign flysystem backend to generated asset handler at the same time
|
|
|
|
$generated = new FlysystemGeneratedAssetHandler();
|
|
|
|
$generated->setFilesystem($filesystem);
|
|
|
|
Injector::inst()->registerService($generated, 'GeneratedAssetHandler');
|
2015-11-30 03:03:46 +01:00
|
|
|
Requirements::backend()->setAssetHandler($generated);
|
2015-11-26 03:19:43 +01:00
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
// Disable legacy and set defaults
|
|
|
|
Config::inst()->remove(get_class(new FlysystemAssetStore()), 'legacy_filenames');
|
|
|
|
Config::inst()->update('Director', 'alternate_base_url', '/');
|
|
|
|
DBFile::config()->force_resample = false;
|
|
|
|
File::config()->force_resample = false;
|
|
|
|
self::reset();
|
|
|
|
self::$basedir = $basedir;
|
|
|
|
|
|
|
|
// Ensure basedir exists
|
2015-10-15 00:09:04 +02:00
|
|
|
SS_Filesystem::makeFolder(self::base_path());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get absolute path to basedir
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function base_path() {
|
|
|
|
if(self::$basedir) {
|
|
|
|
return ASSETS_PATH . '/' . self::$basedir;
|
|
|
|
}
|
2015-09-15 04:52:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset defaults for this store
|
|
|
|
*/
|
|
|
|
public static function reset() {
|
2015-10-23 00:46:58 +02:00
|
|
|
// Remove all files in this store
|
2015-09-15 04:52:02 +02:00
|
|
|
if(self::$basedir) {
|
2015-10-15 00:09:04 +02:00
|
|
|
$path = self::base_path();
|
2015-09-15 04:52:02 +02:00
|
|
|
if(file_exists($path)) {
|
|
|
|
SS_Filesystem::removeFolder($path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self::$seekable_override = null;
|
|
|
|
self::$basedir = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper method to get local filesystem path for this file
|
|
|
|
*
|
|
|
|
* @param AssetContainer $asset
|
|
|
|
*/
|
|
|
|
public static function getLocalPath(AssetContainer $asset) {
|
|
|
|
if($asset instanceof Folder) {
|
2015-10-15 00:09:04 +02:00
|
|
|
return self::base_path() . '/' . $asset->getFilename();
|
2015-09-15 04:52:02 +02:00
|
|
|
}
|
2015-10-23 00:46:58 +02:00
|
|
|
if($asset instanceof File) {
|
|
|
|
$asset = $asset->File;
|
|
|
|
}
|
|
|
|
if($asset instanceof DBFile) {
|
|
|
|
return BASE_PATH . $asset->getSourceURL();
|
|
|
|
}
|
2015-09-15 04:52:02 +02:00
|
|
|
return BASE_PATH . $asset->getUrl();
|
|
|
|
}
|
|
|
|
|
2015-09-03 07:46:08 +02:00
|
|
|
public function cleanFilename($filename) {
|
|
|
|
return parent::cleanFilename($filename);
|
|
|
|
}
|
2015-09-15 04:52:02 +02:00
|
|
|
|
|
|
|
public function getFileID($filename, $hash, $variant = null) {
|
|
|
|
return parent::getFileID($filename, $hash, $variant);
|
2015-09-03 07:46:08 +02:00
|
|
|
}
|
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
|
2015-09-03 07:46:08 +02:00
|
|
|
public function getOriginalFilename($fileID, &$variant = '') {
|
|
|
|
return parent::getOriginalFilename($fileID, $variant);
|
|
|
|
}
|
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
public function getDefaultConflictResolution($variant) {
|
|
|
|
return parent::getDefaultConflictResolution($variant);
|
|
|
|
}
|
|
|
|
|
2015-09-03 07:46:08 +02:00
|
|
|
protected function isSeekableStream($stream) {
|
|
|
|
if(isset(self::$seekable_override)) {
|
|
|
|
return self::$seekable_override;
|
|
|
|
}
|
|
|
|
return parent::isSeekableStream($stream);
|
|
|
|
}
|
|
|
|
}
|