|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Codesleeve\Stapler\Storage; |
| 4 | + |
| 5 | +use Codesleeve\Stapler\Interfaces\Storage as StorageInterface; |
| 6 | +use Codesleeve\Stapler\Attachment; |
| 7 | +use League\Flysystem\Filesystem; |
| 8 | + |
| 9 | +class GCS implements StorageInterface |
| 10 | +{ |
| 11 | + /** |
| 12 | + * The current attachedFile object being processed. |
| 13 | + * |
| 14 | + * @var \Codesleeve\Stapler\Attachment |
| 15 | + */ |
| 16 | + public $attachedFile; |
| 17 | + |
| 18 | + /** |
| 19 | + * The AWS S3Client instance. |
| 20 | + * |
| 21 | + * @var Filesystem |
| 22 | + */ |
| 23 | + protected $filesystem; |
| 24 | + |
| 25 | + /** |
| 26 | + * Constructor method. |
| 27 | + * |
| 28 | + * @param Attachment $attachedFile |
| 29 | + * @param Filesystem $filesystem |
| 30 | + */ |
| 31 | + public function __construct(Attachment $attachedFile, Filesystem $filesystem) |
| 32 | + { |
| 33 | + $this->attachedFile = $attachedFile; |
| 34 | + $this->filesystem = $filesystem; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Return the url for a file upload. |
| 39 | + * |
| 40 | + * @param string $styleName |
| 41 | + * |
| 42 | + * @return string |
| 43 | + */ |
| 44 | + public function url($styleName) |
| 45 | + { |
| 46 | + return $this->filesystem->getAdapter()->getUrl($this->path($styleName)); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Return the key the uploaded file object is stored under within a bucket. |
| 51 | + * |
| 52 | + * @param string $styleName |
| 53 | + * |
| 54 | + * @return string |
| 55 | + */ |
| 56 | + public function path($styleName) |
| 57 | + { |
| 58 | + return $this->attachedFile->getInterpolator()->interpolate($this->attachedFile->path, $this->attachedFile, $styleName); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Remove an attached file. |
| 63 | + * |
| 64 | + * @param array $filePaths |
| 65 | + */ |
| 66 | + public function remove(array $filePaths) |
| 67 | + { |
| 68 | + if ($filePaths) { |
| 69 | + $this->filesystem->delete($filePaths); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Move an uploaded file to it's intended destination. |
| 75 | + * |
| 76 | + * @param string $file |
| 77 | + * @param string $filePath |
| 78 | + */ |
| 79 | + public function move($file, $filePath) |
| 80 | + { |
| 81 | + $this->filesystem->put($filePath, fopen($file, 'r+')); |
| 82 | + } |
| 83 | +} |
0 commit comments