Skip to content

Commit

Permalink
Creates component for store files in AWS S3
Browse files Browse the repository at this point in the history
  • Loading branch information
greeflas committed Feb 22, 2018
1 parent 63b33c7 commit 7f60b2f
Show file tree
Hide file tree
Showing 3 changed files with 193 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Tinify API Change Log
2.1.0 Under development
-----------------------
* Enh #4: Auto setting of API key in TinifyResize (greeflas)
* Enh: Created component for uploading files to AWS S3 storage (greeflas)

2.0.0 October 24, 2017
----------------------
Expand Down
59 changes: 49 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<img src="https://tinypng.com/images/social/website.jpg" height="150px">
</a>
<h1 align="center">Tinify API</h1>
<br>
</p>

Facade of Tinify API for Yii2 Framework. This extension allows you to resize and compress images without loss of quality.
Expand All @@ -18,23 +17,27 @@ For more information you can [read official](https://tinypng.com/developers/refe
Installation
------------

#### Install package
The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Run command
```bash
Either run

```
$ composer require vintage/yii2-tinify
```

or add
```json

```
"vintage/yii2-tinify": "~2.0"
```
to the require section of your `composer.json` file.

to the `require` section of your `composer.json`.

Usage
-----

### Component

1. Configure API token in app params with key `tinify-api-token` or in `UploadedFile` component
2. Use `\vintage\tinify\UploadedFile` instead of `\yii\web\UploadedFile`

Expand All @@ -49,8 +52,42 @@ $file->saveMetadata = UploadedFile::METADATA_LOCATION;
$file->saveMetadata = [UploadedFile::METADATA_LOCATION, UploadedFile::METADATA_CREATION];
```

### Upload files to AWS S3 storage

1. Configure AWS S3 service in `config/params-local.php`

```php
use vintage\tinify\UploadedFileS3;

return [
// ...
UploadedFileS3::PARAM_KEY_AWS_ACCESS_KEY_ID => '',
UploadedFileS3::PARAM_KEY_AWS_SECRET_ACCESS_KEY => '',
UploadedFileS3::PARAM_KEY_S3_REGION => '',
UploadedFileS3::PARAM_KEY_S3_BUCKET => '',
];
```

2. Use `\vintage\tinify\UploadedFileS3` insead of `\vintage\tinify\UploadedFile`
3. Provide region and bucket name in methods calls

```php
$file = UploadedFile::getInstance($model, 'image')->saveAs('image.jpg');
```

also you can override region and bucket

```php
$file = UploadedFile::getInstance($model, 'image')
->setRegion('us-west-1')
->setPath('images-bucket/uploads') // path must be provided without slash in the end
->saveAs('image.jpg');
```

### Resizing

You can resize uploaded file

```php
$file = \vintage\tinify\UploadedFile::getInstance($model, 'image');
$file->resize() // creates \vintage\tinify\components\TinifyResize object
Expand All @@ -62,23 +99,25 @@ $file->saveAs('@webroot/uploads');
```

or resize existing image
```php
\Tinify\setKey(\vintage\tinify\helpers\TinifyData::getApiToken());

```php
(new \vintage\tinify\components\TinifyResize('@webroot/uploads/image.jpg'))
->scale()
->width(600)
->process();
```

### CLI

1. Configure console controller in `console/config/main.php`

```php
'controllerMap' => [
// ...
'tinify' => \vintage\tinify\cli\TinifyController::class,
],
```

2. Run in console `./yii tinify/<command>`

| Command | Description |
Expand All @@ -92,13 +131,13 @@ Tests
-----
You can run tests with composer command

```bash
```
$ composer test
```

or using following command

```bash
```
$ codecept build && codecept run
```

Expand Down
143 changes: 143 additions & 0 deletions src/UploadedFileS3.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<?php

/*
* This file is part of the yii2-tinify package.
*
* (c) Vladimir Kuprienko <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace vintage\tinify;

use Yii;
use yii\base\InvalidConfigException;
use Tinify\AccountException;
use Tinify\Source;

/**
* Component for Tinify PHP SDK provides uploading files to AWS S3 storage.
*
* @author Vladimir Kuprienko <[email protected]>
* @since 2.1
*/
class UploadedFileS3 extends UploadedFile
{
const PARAM_KEY_AWS_ACCESS_KEY_ID = 'aws-access-key-id';
const PARAM_KEY_AWS_SECRET_ACCESS_KEY = 'aws-secret-access-key';
const PARAM_KEY_S3_REGION = 's3-region';
const PARAM_KEY_S3_BUCKET = 's3-bucket';

/**
* AWS region.
*
* @var string
*/
protected $region;
/**
* Path in bucket.
* Example: images-bucket/uploads
*
* @var string
*/
protected $path;
/**
* AWS access key id.
*
* @var string
*/
protected $accessKeyId;
/**
* AWS secret access key.
*
* @var string
*/
protected $secretAccessKey;

/**
* @inheritdoc
*/
public function init()
{
parent::init();

$params = Yii::$app->params;

if (
empty($params[self::PARAM_KEY_AWS_ACCESS_KEY_ID]) ||
empty($params[self::PARAM_KEY_AWS_SECRET_ACCESS_KEY])
) {
throw new InvalidConfigException('You must provide AWS credentials');
}

$this->accessKeyId = $params[self::PARAM_KEY_AWS_ACCESS_KEY_ID];
$this->secretAccessKey = $params[self::PARAM_KEY_AWS_SECRET_ACCESS_KEY];

if (isset($params[self::PARAM_KEY_S3_REGION])) {
$this->region = $params[self::PARAM_KEY_S3_REGION];
}

if (isset($params[self::PARAM_KEY_S3_BUCKET])) {
$this->path = $params[self::PARAM_KEY_S3_BUCKET];
}
}

/**
* Set AWS region.
*
* @param string $region
*
* @return self
*/
public function setRegion($region)
{
$this->region = $region;

return $this;
}

/**
* Set bucket name and path to save.
*
* @param string $path
*
* @return self
*/
public function setPath($path)
{
$this->path = $path;

return $this;
}

/**
* @inheritdoc
*/
protected function compressFile($tempFile, $resultFile)
{
$result = false;

try {
$source = Source::fromFile($tempFile);

if (!empty($this->saveMetadata)) {
$source = $source->preserve($this->saveMetadata);
}

$result = $source->store([
'service' => 's3',
'region' => $this->region,
'path' => $this->path . '/' . $resultFile,
'aws_access_key_id' => $this->accessKeyId,
'aws_secret_access_key' => $this->secretAccessKey,
]);
} catch (AccountException $ex) {
throw $ex;
} catch (\Exception $ex) {
Yii::$app->getErrorHandler()->logException($ex);
}

return (bool) $result;
}
}

0 comments on commit 7f60b2f

Please sign in to comment.