Skip to content

Commit

Permalink
Re-implement support for Rackspace
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Thomson committed Feb 26, 2020
1 parent c258e66 commit 3ee82fd
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 1 deletion.
104 changes: 104 additions & 0 deletions src/Filesystem/FilesystemAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php namespace October\Rain\Filesystem;

use Carbon\Carbon;
use League\Flysystem\Adapter\Ftp;
use League\Flysystem\Adapter\Local as LocalAdapter;
use League\Flysystem\AwsS3v3\AwsS3Adapter;
use League\Flysystem\Cached\CachedAdapter;
use League\Flysystem\Rackspace\RackspaceAdapter;
use Illuminate\Filesystem\FilesystemAdapter as BaseFilesystemAdapter;

class FilesystemAdapter extends BaseFilesystemAdapter
{
/**
* Get the URL for the file at the given path.
*
* @param string $path
* @return string
*
* @throws \RuntimeException
*/
public function url($path)
{
$adapter = $this->driver->getAdapter();

if ($adapter instanceof CachedAdapter) {
$adapter = $adapter->getAdapter();
}

if (method_exists($adapter, 'getUrl')) {
return $adapter->getUrl($path);
} elseif (method_exists($this->driver, 'getUrl')) {
return $this->driver->getUrl($path);
} elseif ($adapter instanceof AwsS3Adapter) {
return $this->getAwsUrl($adapter, $path);
} elseif ($adapter instanceof RackspaceAdapter) {
return $this->getRackspaceUrl($adapter, $path);
} elseif ($adapter instanceof Ftp) {
return $this->getFtpUrl($path);
} elseif ($adapter instanceof LocalAdapter) {
return $this->getLocalUrl($path);
} else {
throw new RuntimeException('This driver does not support retrieving URLs.');
}
}

/**
* Get the URL for the file at the given path.
*
* @param \League\Flysystem\Rackspace\RackspaceAdapter $adapter
* @param string $path
* @return string
*/
protected function getRackspaceUrl($adapter, $path)
{
return (string) $adapter->getContainer()->getObject($path)->getPublicUrl();
}

/**
* Get a temporary URL for the file at the given path.
*
* @param string $path
* @param \DateTimeInterface $expiration
* @param array $options
* @return string
*
* @throws \RuntimeException
*/
public function temporaryUrl($path, $expiration, array $options = [])
{
$adapter = $this->driver->getAdapter();

if ($adapter instanceof CachedAdapter) {
$adapter = $adapter->getAdapter();
}

if (method_exists($adapter, 'getTemporaryUrl')) {
return $adapter->getTemporaryUrl($path, $expiration, $options);
} elseif ($adapter instanceof AwsS3Adapter) {
return $this->getAwsTemporaryUrl($adapter, $path, $expiration, $options);
} elseif ($adapter instanceof RackspaceAdapter) {
return $this->getRackspaceTemporaryUrl($adapter, $path, $expiration, $options);
} else {
throw new RuntimeException('This driver does not support creating temporary URLs.');
}
}

/**
* Get a temporary URL for the file at the given path.
*
* @param \League\Flysystem\Rackspace\RackspaceAdapter $adapter
* @param string $path
* @param \DateTimeInterface $expiration
* @param array $options
* @return string
*/
public function getRackspaceTemporaryUrl($adapter, $path, $expiration, $options)
{
return $adapter->getContainer()->getObject($path)->getTemporaryUrl(
Carbon::now()->diffInSeconds($expiration),
$options['method'] ?? 'GET',
$options['forcePublicUrl'] ?? true
);
}
}
55 changes: 55 additions & 0 deletions src/Filesystem/FilesystemManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php namespace October\Rain\Filesystem;

use OpenCloud\Rackspace;
use League\Flysystem\FilesystemInterface;
use League\Flysystem\Rackspace\RackspaceAdapter;
use Illuminate\Filesystem\FilesystemManager as BaseFilesystemManager;

class FilesystemManager extends BaseFilesystemManager
{
/**
* Adapt the filesystem implementation.
*
* @param \League\Flysystem\FilesystemInterface $filesystem
* @return \Illuminate\Contracts\Filesystem\Filesystem
*/
protected function adapt(FilesystemInterface $filesystem)
{
return new FilesystemAdapter($filesystem);
}

/**
* Create an instance of the Rackspace driver.
*
* @param array $config
* @return \Illuminate\Contracts\Filesystem\Cloud
*/
public function createRackspaceDriver(array $config)
{
$client = new Rackspace($config['endpoint'], [
'username' => $config['username'], 'apiKey' => $config['key'],
], $config['options'] ?? []);

$root = $config['root'] ?? null;

return $this->adapt($this->createFlysystem(
new RackspaceAdapter($this->getRackspaceContainer($client, $config), $root), $config
));
}

/**
* Get the Rackspace Cloud Files container.
*
* @param \OpenCloud\Rackspace $client
* @param array $config
* @return \OpenCloud\ObjectStore\Resource\Container
*/
protected function getRackspaceContainer(Rackspace $client, array $config)
{
$urlType = $config['url_type'] ?? null;

$store = $client->objectStoreService('cloudFiles', $config['region'], $urlType);

return $store->getContainer($config['container']);
}
}
12 changes: 12 additions & 0 deletions src/Filesystem/FilesystemServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ protected function registerNativeFilesystem()
});
}

/**
* Register the filesystem manager.
*
* @return void
*/
protected function registerManager()
{
$this->app->singleton('filesystem', function () {
return new FilesystemManager($this->app);
});
}

/**
* Get the services provided by the provider.
* @return array
Expand Down
2 changes: 1 addition & 1 deletion src/Foundation/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ public function registerCoreContainerAliases()
'db.connection' => [\Illuminate\Database\Connection::class, \Illuminate\Database\ConnectionInterface::class],
'events' => [\Illuminate\Events\Dispatcher::class, \Illuminate\Contracts\Events\Dispatcher::class],
'files' => [\Illuminate\Filesystem\Filesystem::class],
'filesystem' => [\Illuminate\Filesystem\FilesystemManager::class, \Illuminate\Contracts\Filesystem\Factory::class],
'filesystem' => [\October\Rain\Filesystem\FilesystemManager::class, \Illuminate\Contracts\Filesystem\Factory::class],
'filesystem.disk' => [\Illuminate\Contracts\Filesystem\Filesystem::class],
'filesystem.cloud' => [\Illuminate\Contracts\Filesystem\Cloud::class],
'hash' => [\Illuminate\Contracts\Hashing\Hasher::class],
Expand Down

0 comments on commit 3ee82fd

Please sign in to comment.