Skip to content

Commit

Permalink
adding a method to abstractDuff in order to decide which file type is…
Browse files Browse the repository at this point in the history
… going to up uploaded
  • Loading branch information
Luizao committed Dec 17, 2021
1 parent d12e33c commit 2134e6c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 31 deletions.
60 changes: 34 additions & 26 deletions src/AbstractDuf.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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) {
Expand Down
7 changes: 2 additions & 5 deletions src/Duffer/GCSDuffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 2134e6c

Please sign in to comment.