-
-
Notifications
You must be signed in to change notification settings - Fork 199
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
Showing
15 changed files
with
661 additions
and
0 deletions.
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
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
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
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,32 @@ | ||
<?php | ||
|
||
namespace App\Storage; | ||
|
||
use League\Glide\Responses\ResponseFactoryInterface; | ||
use League\Glide\Server; | ||
use Symfony\Contracts\HttpClient\ResponseInterface; | ||
|
||
class Cache implements CacheInterface | ||
{ | ||
private Server $glide; | ||
|
||
public function __construct(Server $glide) | ||
{ | ||
$this->glide = $glide; | ||
} | ||
|
||
public function deleteCache(string $path): bool | ||
{ | ||
return $this->glide->deleteCache($path); | ||
} | ||
|
||
public function setResponseFactory(ResponseFactoryInterface $responseFactory): void | ||
{ | ||
$this->glide->setResponseFactory($responseFactory); | ||
} | ||
|
||
public function getImageResponse(string $path, array $params): ResponseInterface | ||
{ | ||
return $this->glide->getImageResponse($path, $params); | ||
} | ||
} |
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,12 @@ | ||
<?php | ||
|
||
namespace App\Storage; | ||
|
||
use Symfony\Contracts\HttpClient\ResponseInterface; | ||
|
||
interface CacheInterface | ||
{ | ||
public function deleteCache(string $path): bool; | ||
|
||
public function getImageResponse(string $path, array $params): ResponseInterface; | ||
} |
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,21 @@ | ||
<?php | ||
|
||
namespace App\Storage; | ||
|
||
/** | ||
* This service is here so that the injected services | ||
* are not removed from the container after compilation. | ||
* | ||
* To be removed. | ||
*/ | ||
class DummyService | ||
{ | ||
private StorageInterface $publicStorage; | ||
private StorageInterface $privateStorage; | ||
|
||
public function __construct(StorageInterface $publicStorage, StorageInterface $privateStorage) | ||
{ | ||
$this->publicStorage = $publicStorage; | ||
$this->privateStorage = $privateStorage; | ||
} | ||
} |
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,9 @@ | ||
<?php | ||
|
||
namespace App\Storage\Exception; | ||
|
||
use League\Flysystem\FileExistsException as BaseFileExistsException; | ||
|
||
class FileExistsException extends BaseFileExistsException | ||
{ | ||
} |
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,9 @@ | ||
<?php | ||
|
||
namespace App\Storage\Exception; | ||
|
||
use League\Flysystem\FileNotFoundException as BaseFileNotFoundException; | ||
|
||
class FileNotFoundException extends BaseFileNotFoundException | ||
{ | ||
} |
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,34 @@ | ||
<?php | ||
|
||
namespace App\Storage; | ||
|
||
use Google\Cloud\Storage\StorageClient; | ||
use League\Flysystem\Adapter\Local; | ||
use League\Flysystem\AdapterInterface; | ||
use League\Flysystem\Cached\CachedAdapter; | ||
use League\Flysystem\Cached\Storage\Memory; | ||
use League\Flysystem\Filesystem; | ||
use Superbalist\Flysystem\GoogleStorage\GoogleStorageAdapter; | ||
|
||
class FilesystemFactory | ||
{ | ||
public static function createLocal(string $localPath): Filesystem | ||
{ | ||
return self::create(new Local($localPath)); | ||
} | ||
|
||
public static function createGoogleStorage(string $gcloudBucket, string $pathPrefix = null): Filesystem | ||
{ | ||
$storage = new StorageClient(); | ||
|
||
return self::create(new CachedAdapter( | ||
new GoogleStorageAdapter($storage, $storage->bucket($gcloudBucket), $pathPrefix), | ||
new Memory(), | ||
)); | ||
} | ||
|
||
private static function create(AdapterInterface $adapter): Filesystem | ||
{ | ||
return new Filesystem($adapter); | ||
} | ||
} |
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,94 @@ | ||
<?php | ||
|
||
namespace App\Storage; | ||
|
||
use App\Storage\Exception\FileExistsException; | ||
use App\Storage\Exception\FileNotFoundException; | ||
use League\Flysystem\FileExistsException as LeagueFileExistsException; | ||
use League\Flysystem\FileNotFoundException as LeagueFileNotFoundException; | ||
use League\Flysystem\FilesystemInterface; | ||
use League\Glide\Server; | ||
|
||
class Storage implements StorageInterface | ||
{ | ||
private FilesystemInterface $filesystem; | ||
private Server $glide; | ||
|
||
public function __construct(FilesystemInterface $filesystem) | ||
{ | ||
$this->filesystem = $filesystem; | ||
$this->glide = $glide; | ||
} | ||
|
||
public function has(string $path): bool | ||
{ | ||
return $this->filesystem->has($path); | ||
} | ||
|
||
public function getMimetype(string $path): string | ||
{ | ||
try { | ||
return $this->filesystem->getMimetype($path); | ||
} catch (LeagueFileNotFoundException $exception) { | ||
throw $this->createFileNotFoundException($path); | ||
} | ||
} | ||
|
||
public function read(string $path): string | ||
{ | ||
try { | ||
return $this->filesystem->read($path); | ||
} catch (LeagueFileNotFoundException $exception) { | ||
throw $this->createFileNotFoundException($path); | ||
} | ||
} | ||
|
||
public function readStream(string $path) | ||
{ | ||
try { | ||
return $this->filesystem->readStream($path); | ||
} catch (LeagueFileNotFoundException $exception) { | ||
throw $this->createFileNotFoundException($path); | ||
} | ||
} | ||
|
||
public function put(string $path, string $contents, array $config = []): bool | ||
{ | ||
return $this->filesystem->put($path, $contents, $config); | ||
} | ||
|
||
public function copy(string $path, string $newPath): bool | ||
{ | ||
try { | ||
return $this->filesystem->copy($path, $newPath); | ||
} catch (LeagueFileNotFoundException $exception) { | ||
throw $this->createFileNotFoundException($path); | ||
} catch (LeagueFileExistsException $exception) { | ||
throw $this->createFileExistsException($newPath); | ||
} | ||
} | ||
|
||
public function delete(string $path): bool | ||
{ | ||
try { | ||
return $this->filesystem->delete($path); | ||
} catch (LeagueFileNotFoundException $exception) { | ||
throw $this->createFileNotFoundException($path); | ||
} | ||
} | ||
|
||
public function listContents(string $directory, bool $recursive = false): array | ||
{ | ||
return $this->filesystem->listContents($directory, $recursive); | ||
} | ||
|
||
private function createFileNotFoundException(string $path): FileNotFoundException | ||
{ | ||
return new FileNotFoundException($path); | ||
} | ||
|
||
private function createFileExistsException(string $path): FileExistsException | ||
{ | ||
return new FileExistsException($path); | ||
} | ||
} |
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,43 @@ | ||
<?php | ||
|
||
namespace App\Storage; | ||
|
||
use App\Storage\Exception\FileExistsException; | ||
use App\Storage\Exception\FileNotFoundException; | ||
|
||
interface StorageInterface | ||
{ | ||
public function has(string $path): bool; | ||
|
||
/** | ||
* @throws FileNotFoundException | ||
*/ | ||
public function getMimetype(string $path): string; | ||
|
||
/** | ||
* @throws FileNotFoundException Thrown if $path does not exist | ||
*/ | ||
public function read(string $path): string; | ||
|
||
/** | ||
* @throws FileNotFoundException Thrown if $path does not exist | ||
* | ||
* @return resource | ||
*/ | ||
public function readStream(string $path); | ||
|
||
public function put(string $path, string $contents, array $config = []): bool; | ||
|
||
/** | ||
* @throws FileExistsException Thrown if $newpath exists | ||
* @throws FileNotFoundException Thrown if $path does not exist | ||
*/ | ||
public function copy(string $path, string $newpath): bool; | ||
|
||
/** | ||
* @throws FileNotFoundException | ||
*/ | ||
public function delete(string $path): bool; | ||
|
||
public function listContents(string $directory, bool $recursive = false): array; | ||
} |
Oops, something went wrong.