-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ben Thomson
committed
Feb 26, 2020
1 parent
c258e66
commit 3ee82fd
Showing
4 changed files
with
172 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters