Skip to content

Commit

Permalink
Support a different destination for cloned attachments
Browse files Browse the repository at this point in the history
Closes #5
  • Loading branch information
weotch committed Aug 23, 2016
1 parent 4da3b28 commit f0defae
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 14 deletions.
39 changes: 34 additions & 5 deletions src/Adapters/Upchuck.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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);
}

}
/**
* 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,
]);
}

}
21 changes: 12 additions & 9 deletions tests/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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));
}
}

0 comments on commit f0defae

Please sign in to comment.