diff --git a/src/Adapters/Upchuck.php b/src/Adapters/Upchuck.php index fdb24da..2e16f84 100644 --- a/src/Adapters/Upchuck.php +++ b/src/Adapters/Upchuck.php @@ -5,6 +5,7 @@ use Bkwld\Upchuck\Helpers; use Bkwld\Upchuck\Storage; use League\Flysystem\Filesystem; +use League\Flysystem\MountManager; /** * File attachment adpater for https://github.com/BKWLD/upchuck @@ -26,11 +27,16 @@ class Upchuck implements AttachmentAdapter { */ private $disk; + /** + * @var League\Flysystem\MountManager + */ + private $disks; + /** * DI */ - public function __construct(Helpers $helpers, - Storage $storage, + public function __construct(Helpers $helpers, + Storage $storage, Filesystem $disk) { $this->helpers = $helpers; $this->storage = $storage; @@ -39,14 +45,37 @@ public function __construct(Helpers $helpers, /** * Duplicate a file given it's URL + * * @param string $url * @return string */ public function duplicate($url) { + + // Make the destination path $current_path = $this->helpers->path($url); - $new_path = $this->storage->makeNestedAndUniquePath(basename($current_path)); - $this->disk->copy($current_path, $new_path); + $filename = basename($current_path); + $dst_disk = $this->disks ? $this->disks->getFilesystem('dst') : $this->disk; + $new_path = $this->storage->makeNestedAndUniquePath($filename, $dst_disk); + + // Copy, supporting alternative destination disks + if ($this->disks) $this->disks->copy('src://'.$current_path, 'dst://'.$new_path); + else $this->disk->copy($current_path, $new_path); + + // Return the Upchuck URL return $this->helpers->url($new_path); } -} \ No newline at end of file + /** + * Set a different destination for cloned items. In doing so, create a + * MountManager instance that will be used to do the copying + * + * @param League\Flysystem\Filesystem $dst + */ + public function setDestination(Filesystem $dst) { + $this->disks = new MountManager([ + 'src' => $this->disk, + 'dst' => $dst, + ]); + } + +} diff --git a/tests/FunctionalTest.php b/tests/FunctionalTest.php index 162e0c2..d80f7ff 100644 --- a/tests/FunctionalTest.php +++ b/tests/FunctionalTest.php @@ -37,7 +37,7 @@ protected function initUpchuck() { $storage = new Storage($manager, $this->helpers); - $this->upchuck = new Upchuck( + $this->upchuck_adapter = new Upchuck( $this->helpers, $storage, $this->disk @@ -124,7 +124,7 @@ function testExists() { $this->migrateTables(); $this->seed(); - $cloner = new Cloner($this->upchuck, $this->mockEvents()); + $cloner = new Cloner($this->upchuck_adapter, $this->mockEvents()); $clone = $cloner->duplicate($this->article); // Test that the new article was created @@ -160,19 +160,23 @@ function testExists() { // use eloquent because Laravel has issues with relationships on models in // a different connection // https://github.com/laravel/framework/issues/9355 - function testExistsInAltDatabase() { + function testExistsInAltDatabaseAndFilesystem() { $this->initUpchuck(); $this->setUpDatabase(); $this->migrateTables(); $this->migrateTables('alt'); $this->seed(); + // ADd the remote disk to upchuck adapter + $this->remoteDisk = new Filesystem(new Adapter(new Vfs)); + $this->upchuck_adapter->setDestination($this->remoteDisk); + // Make sure that the alt databse is empty $this->assertEquals(0, DB::connection('alt')->table('articles')->count()); $this->assertEquals(0, DB::connection('alt')->table('authors')->count()); $this->assertEquals(0, DB::connection('alt')->table('photos')->count()); - $cloner = new Cloner($this->upchuck, $this->mockEvents()); + $cloner = new Cloner($this->upchuck_adapter, $this->mockEvents()); $clone = $cloner->duplicateTo($this->article, 'alt'); // Test that the new article was created @@ -197,12 +201,11 @@ function testExistsInAltDatabase() { // Test callbacks $this->assertNotEquals(1, $photo->uid); - // Test the file was created in a different place - $this->assertNotEquals('/uploads/test.jpg', $photo->image); + // Test the file was created on the remote disk + $path = $this->helpers->path($photo->image); + $this->assertTrue($this->remoteDisk->has($path)); // Test that the file is the same - $path = $this->helpers->path($photo->image); - $this->assertTrue($this->disk->has($path)); - $this->assertEquals('contents', $this->disk->read($path)); + $this->assertEquals('contents', $this->remoteDisk->read($path)); } }