Skip to content

Commit

Permalink
Merge pull request #10 from LuizinhoF/feature/changing-upload-behaviour
Browse files Browse the repository at this point in the history
changing upload behaviour
  • Loading branch information
jeansouzak authored Dec 17, 2021
2 parents c2d2f56 + 9c8b8c0 commit fed02ec
Show file tree
Hide file tree
Showing 5 changed files with 230 additions and 9 deletions.
19 changes: 16 additions & 3 deletions src/AbstractDuf.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -55,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 @@ -76,20 +85,25 @@ public function prepare(array $resources)
}
$this->fileResources[] = $resource;
}, ARRAY_FILTER_USE_BOTH);
$this->setFileType();

return $this;
}

protected function setFileType(){
$this->fileType = File::class;
}

/**
* Download prepared files
* Download a file
*
* @return void
*/
public function download(DownloadOptions $options = null)
{
/** @var Resource $fileResource */
foreach ($this->fileResources as $fileResource) {
$file = new File();
$file = new $this->fileType();
try {
$response = $fileResource->download($options);
if (!$response) {
Expand All @@ -115,7 +129,6 @@ public function download(DownloadOptions $options = null)
return $this;
}


public function upload()
{
$this->filesToUpload = array_filter($this->downloadedFiles, function ($downloadedFile) {
Expand Down
19 changes: 15 additions & 4 deletions src/Duffer/GCSDuffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -39,6 +41,9 @@ public function addBucket($bucketName)
return $this;
}

protected function setFileType() {
$this->fileType = GCSFile::class;
}

public function upload()
{
Expand All @@ -65,11 +70,17 @@ 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->setId($bucketObj->info()['id']);
$fileToUpload->setSelfLink($bucketObj->info()['selfLink']);
$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) {
$fileToUpload->setErrorMessage($e->getMessage());
$fileToUpload->setStatus(FILE::ERROR);
$fileToUpload->setStatus(File::ERROR);
throw $e;
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/File.php → src/Files/File.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php
declare(strict_types = 1);

namespace JeanSouzaK\Duf;
namespace JeanSouzaK\Duf\Files;

class File
use JeanSouzaK\Duf\Files\Fileable;

class File implements Fileable
{
const DOWNLOADED = 1;
const ERROR = 0;
Expand Down
19 changes: 19 additions & 0 deletions src/Files/Fileable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
declare(strict_types = 1);

namespace JeanSouzaK\Duf\Files;

interface Fileable
{
public function getName();

public function getStatus();

public function getResultPath();

public function setResultPath(string $resultPath);

public function hasError();

public function setHasError(bool $hasError);
}
176 changes: 176 additions & 0 deletions src/Files/GCSFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
<?php
declare(strict_types = 1);

namespace JeanSouzaK\Duf\Files;

use JeanSouzaK\Duf\Files\File;
use JeanSouzaK\Duf\Files\Fileable;

class GCSFile extends File implements Fileable{

public function __construct(){
parent::__construct();
}

/**
* File ID on GCS
*
* @var string
*/
private $id;

/**
* Link for file info
*
* @var string
*/
private $selfLink;

/**
* Bucket name
*
* @var string
*/
private $bucket;

/**
* File content-type
*
* @var string
*/
private $contentType;

/**
* File size
*
* @var string
*/
private $size;

/**
* File ID
*
* @var string
*/
private $timeCreated;

/**
* Get file id
*
* @return string
*/
public function getId()
{
return $this->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;
}

/**
* Set files bucket name
*
* @return string
*/
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;
}
}

0 comments on commit fed02ec

Please sign in to comment.