From d12e33cdbcb6e2eb2e7534e1074faaac6cb14c2c Mon Sep 17 00:00:00 2001 From: Luizao Date: Wed, 15 Dec 2021 14:38:02 -0300 Subject: [PATCH 1/3] changing Abstract Duffs download to use a interface instead a hard-coded class --- src/AbstractDuf.php | 53 ++++++++++-------- src/Duffer/GCSDuffer.php | 18 ++++-- src/{ => Files}/File.php | 6 +- src/Files/Fileable.php | 19 +++++++ src/Files/GCSFile.php | 116 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 182 insertions(+), 30 deletions(-) rename src/{ => Files}/File.php (97%) create mode 100644 src/Files/Fileable.php create mode 100644 src/Files/GCSFile.php diff --git a/src/AbstractDuf.php b/src/AbstractDuf.php index 2277cc4..c925d40 100644 --- a/src/AbstractDuf.php +++ b/src/AbstractDuf.php @@ -11,6 +11,8 @@ use GuzzleHttp\Psr7\Response; use JeanSouzaK\Duf\Download\DownloadOptions; use JeanSouzaK\Duf\Prepare\WebResource; +use JeanSouzaK\Duf\Files\Fileable; +use JeanSouzaK\Duf\Files\File; abstract class AbstractDuf implements Dufable { @@ -81,38 +83,41 @@ public function prepare(array $resources) } /** - * Download prepared files + * Download Processes files read for download * * @return void */ - public function download(DownloadOptions $options = null) + abstract public function download(); + + /** + * Download a file + * + * @return void + */ + protected function downloadFile(Fileable $file, Resource $fileResource, DownloadOptions $options = null) { /** @var Resource $fileResource */ - foreach ($this->fileResources as $fileResource) { - $file = new File(); - try { - $response = $fileResource->download($options); - if (!$response) { - $file->setStatus(File::ERROR); - $file->setErrorMessage('Error on download file'); - } else if ($fileResource instanceof WebResource) { - //$fileResource->processHeaderFilters($response->getHeaders()); - $body = $response->getBody(); - while (!$body->eof()) { - $file->addBytes($body->read(1024)); - } - } else { - $fileResource->processPathFilters(array_merge(pathinfo($fileResource->getUrl()), ['size' => filesize($fileResource->getUrl())])); - $file->addBytes($response); - } - } catch (\Exception $e) { + try { + $response = $fileResource->download($options); + if (!$response) { $file->setStatus(File::ERROR); - $file->setErrorMessage($e->getMessage()); + $file->setErrorMessage('Error on download file'); + } else if ($fileResource instanceof WebResource) { + //$fileResource->processHeaderFilters($response->getHeaders()); + $body = $response->getBody(); + while (!$body->eof()) { + $file->addBytes($body->read(1024)); + } + } else { + $fileResource->processPathFilters(array_merge(pathinfo($fileResource->getUrl()), ['size' => filesize($fileResource->getUrl())])); + $file->addBytes($response); } - $file->setName($fileResource->getName()); - $this->downloadedFiles[] = $file; + } catch (\Exception $e) { + $file->setStatus(File::ERROR); + $file->setErrorMessage($e->getMessage()); } - return $this; + $file->setName($fileResource->getName()); + $this->downloadedFiles[] = $file; } diff --git a/src/Duffer/GCSDuffer.php b/src/Duffer/GCSDuffer.php index 9abc829..e7f9b24 100644 --- a/src/Duffer/GCSDuffer.php +++ b/src/Duffer/GCSDuffer.php @@ -4,9 +4,11 @@ namespace JeanSouzaK\Duf\Duffer; use Google\Cloud\Storage\StorageClient; -use JeanSouzaK\Duf\File; +use JeanSouzaK\Duf\Files\GCSFile; use JeanSouzaK\Duf\DownloadUploadFile; use JeanSouzaK\Duf\AbstractDuf; +use JeanSouzaK\Duf\Download\DownloadOptions; +use JeanSouzaK\Duf\Files\File; class GCSDuffer extends AbstractDuf { @@ -39,6 +41,12 @@ public function addBucket($bucketName) return $this; } + public function download(DownloadOptions $options = null){ + foreach ($this->fileResources as $fileResource) { + parent::downloadFile(new GCSFile(), $fileResource, $options); + } + return $this; + } public function upload() { @@ -65,11 +73,13 @@ public function upload() $bucketObj = $bucket->upload($fileToUpload->getBytes(), [ 'name' => $fileName ]); - $fileToUpload->setResultPath(self::STORAGE_URI . $this->bucketName . '/' . ($bucketObj->name() ? $bucketObj->name() : $fileName)); - $fileToUpload->setStatus(FILE::FINISHED); + $fileToUpload->setSelfLink($bucketObj->info()['selfLink']); + //self::STORAGE_URI . $this->bucketName . '/' . ($bucketObj->name() ? $bucketObj->name() : $fileName) + $fileToUpload->setResultPath($bucketObj->info()['mediaLink']); + $fileToUpload->setStatus(File::FINISHED); } catch (\Exception $e) { $fileToUpload->setErrorMessage($e->getMessage()); - $fileToUpload->setStatus(FILE::ERROR); + $fileToUpload->setStatus(File::ERROR); throw $e; } } diff --git a/src/File.php b/src/Files/File.php similarity index 97% rename from src/File.php rename to src/Files/File.php index 4c5597a..2ea308a 100644 --- a/src/File.php +++ b/src/Files/File.php @@ -1,9 +1,11 @@ id; + } + + /** + * Set file id + * + * @return string + */ + public function setId(string $id) + { + $this->id = $id; + } + + /** + * Get files info selfLink + * + * @return string + */ + public function getSelfLink() + { + return $this->selfLink; + } + + /** + * Get files info selfLink + * + * @return string + */ + public function setSelfLink(string $selfLink) + { + $this->selfLink = $selfLink; + } + + /** + * Get files bucket name + * + * @return string + */ + public function getBucket() + { + return $this->bucket; + } + + /** + * Get files bucket name + * + * @return string + */ + public function setBucket(string $name) + { + $this->bucket = $bucket; + } +} \ No newline at end of file From 2134e6cbd98637d022a9969932ac419ef973c5be Mon Sep 17 00:00:00 2001 From: Luizao Date: Fri, 17 Dec 2021 12:26:40 -0300 Subject: [PATCH 2/3] adding a method to abstractDuff in order to decide which file type is going to up uploaded --- src/AbstractDuf.php | 60 +++++++++++++++++++++++----------------- src/Duffer/GCSDuffer.php | 7 ++--- 2 files changed, 36 insertions(+), 31 deletions(-) diff --git a/src/AbstractDuf.php b/src/AbstractDuf.php index c925d40..466f395 100644 --- a/src/AbstractDuf.php +++ b/src/AbstractDuf.php @@ -57,6 +57,13 @@ abstract class AbstractDuf implements Dufable */ protected $filters; + /** + * Sets which type of file class will be used to manipulate downloaded files + * + * @var string + */ + protected $fileType; + public function __construct() { $this->client = new Client(); @@ -78,49 +85,50 @@ public function prepare(array $resources) } $this->fileResources[] = $resource; }, ARRAY_FILTER_USE_BOTH); + $this->setFileType(); return $this; } - /** - * Download Processes files read for download - * - * @return void - */ - abstract public function download(); + protected function setFileType(){ + $this->fileType = File::class; + } /** * Download a file * * @return void */ - protected function downloadFile(Fileable $file, Resource $fileResource, DownloadOptions $options = null) + public function download(DownloadOptions $options = null) { /** @var Resource $fileResource */ - try { - $response = $fileResource->download($options); - if (!$response) { - $file->setStatus(File::ERROR); - $file->setErrorMessage('Error on download file'); - } else if ($fileResource instanceof WebResource) { - //$fileResource->processHeaderFilters($response->getHeaders()); - $body = $response->getBody(); - while (!$body->eof()) { - $file->addBytes($body->read(1024)); + foreach ($this->fileResources as $fileResource) { + $file = new $this->fileType(); + try { + $response = $fileResource->download($options); + if (!$response) { + $file->setStatus(File::ERROR); + $file->setErrorMessage('Error on download file'); + } else if ($fileResource instanceof WebResource) { + //$fileResource->processHeaderFilters($response->getHeaders()); + $body = $response->getBody(); + while (!$body->eof()) { + $file->addBytes($body->read(1024)); + } + } else { + $fileResource->processPathFilters(array_merge(pathinfo($fileResource->getUrl()), ['size' => filesize($fileResource->getUrl())])); + $file->addBytes($response); } - } else { - $fileResource->processPathFilters(array_merge(pathinfo($fileResource->getUrl()), ['size' => filesize($fileResource->getUrl())])); - $file->addBytes($response); + } catch (\Exception $e) { + $file->setStatus(File::ERROR); + $file->setErrorMessage($e->getMessage()); } - } catch (\Exception $e) { - $file->setStatus(File::ERROR); - $file->setErrorMessage($e->getMessage()); + $file->setName($fileResource->getName()); + $this->downloadedFiles[] = $file; } - $file->setName($fileResource->getName()); - $this->downloadedFiles[] = $file; + return $this; } - public function upload() { $this->filesToUpload = array_filter($this->downloadedFiles, function ($downloadedFile) { diff --git a/src/Duffer/GCSDuffer.php b/src/Duffer/GCSDuffer.php index e7f9b24..54e4626 100644 --- a/src/Duffer/GCSDuffer.php +++ b/src/Duffer/GCSDuffer.php @@ -41,11 +41,8 @@ public function addBucket($bucketName) return $this; } - public function download(DownloadOptions $options = null){ - foreach ($this->fileResources as $fileResource) { - parent::downloadFile(new GCSFile(), $fileResource, $options); - } - return $this; + protected function setFileType() { + $this->fileType = GCSFile::class; } public function upload() From 9c8b8c091cc5ea622f193c6e3d68d155efa4ae11 Mon Sep 17 00:00:00 2001 From: Luizao Date: Fri, 17 Dec 2021 12:37:09 -0300 Subject: [PATCH 3/3] setting new fields on GCPFile for bucket info --- src/Duffer/GCSDuffer.php | 6 +++- src/Files/GCSFile.php | 62 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/src/Duffer/GCSDuffer.php b/src/Duffer/GCSDuffer.php index 54e4626..1046a55 100644 --- a/src/Duffer/GCSDuffer.php +++ b/src/Duffer/GCSDuffer.php @@ -70,8 +70,12 @@ public function upload() $bucketObj = $bucket->upload($fileToUpload->getBytes(), [ 'name' => $fileName ]); + + $fileToUpload->setId($bucketObj->info()['id']); $fileToUpload->setSelfLink($bucketObj->info()['selfLink']); - //self::STORAGE_URI . $this->bucketName . '/' . ($bucketObj->name() ? $bucketObj->name() : $fileName) + $fileToUpload->setContentType($bucketObj->info()['contentType']); + $fileToUpload->setSize($bucketObj->info()['size']); + $fileToUpload->setTimeCreated($bucketObj->info()['timeCreated']); $fileToUpload->setResultPath($bucketObj->info()['mediaLink']); $fileToUpload->setStatus(File::FINISHED); } catch (\Exception $e) { diff --git a/src/Files/GCSFile.php b/src/Files/GCSFile.php index af07b05..d217861 100644 --- a/src/Files/GCSFile.php +++ b/src/Files/GCSFile.php @@ -105,7 +105,7 @@ public function getBucket() } /** - * Get files bucket name + * Set files bucket name * * @return string */ @@ -113,4 +113,64 @@ public function setBucket(string $name) { $this->bucket = $bucket; } + + /** + * Get file content-type + * + * @return string + */ + public function getContentType() + { + return $this->contentType; + } + + /** + * Set file content-type + * + * @return string + */ + public function setContentType(string $contentType) + { + $this->contentType = $contentType; + } + + /** + * Get file size + * + * @return string + */ + public function getSize() + { + return $this->size; + } + + /** + * Get file size + * + * @return string + */ + public function setSize(string $size) + { + $this->size = $size; + } + + /** + * Get file of creating on bucket + * + * @return string + */ + public function getTimeCreated() + { + return $this->timeCreated; + } + + /** + * Set file of creating on bucket + * + * @return string + */ + public function setTimeCreated(string $timeCreated) + { + $this->timeCreated = $timeCreated; + } } \ No newline at end of file