Skip to content

Commit

Permalink
Added Flysystem
Browse files Browse the repository at this point in the history
Added Flysystem *
  • Loading branch information
ElisDN committed Sep 25, 2017
1 parent 860ca85 commit 364c736
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 2 deletions.
12 changes: 12 additions & 0 deletions common/bootstrap/SetUp.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Elasticsearch\Client;
use Elasticsearch\ClientBuilder;
use League\Flysystem\Adapter\Ftp;
use League\Flysystem\Filesystem;
use shop\cart\Cart;
use shop\cart\cost\calculator\DynamicCost;
use shop\cart\cost\calculator\SimpleCost;
Expand All @@ -12,6 +14,7 @@
use shop\dispatchers\DeferredEventDispatcher;
use shop\dispatchers\EventDispatcher;
use shop\dispatchers\SimpleEventDispatcher;
use shop\entities\behaviors\FlySystemImageUploadBehavior;
use shop\entities\Shop\Product\events\ProductAppearedInStock;
use shop\jobs\AsyncEventJobHandler;
use shop\listeners\Shop\Category\CategoryPersistenceListener;
Expand Down Expand Up @@ -40,6 +43,7 @@
use yii\mail\MailerInterface;
use yii\rbac\ManagerInterface;
use yii\queue\Queue;
use yiidreamteam\upload\ImageUploadBehavior;

class SetUp implements BootstrapInterface
{
Expand Down Expand Up @@ -125,5 +129,13 @@ public function bootstrap($app): void
$container->setSingleton(AsyncEventJobHandler::class, [], [
Instance::of(SimpleEventDispatcher::class)
]);

/*
$container->setSingleton(Filesystem::class, function () use ($app) {
return new Filesystem(new Ftp($app->params['ftp']));
});
$container->set(ImageUploadBehavior::class, FlySystemImageUploadBehavior::class);
*/
}
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"flow/jsonpath": "^0.3.4",
"drewm/mailchimp-api": "^2.4",
"yiisoft/yii2-redis": "^2.0",
"yiisoft/yii2-queue": "^2.0"
"yiisoft/yii2-queue": "^2.0",
"league/flysystem": "^1.0"
},
"require-dev": {
"yiisoft/yii2-debug": "~2.0.0",
Expand Down
85 changes: 84 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

78 changes: 78 additions & 0 deletions shop/entities/behaviors/FlySystemImageUploadBehavior.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace shop\entities\behaviors;

use League\Flysystem\Filesystem;
use PHPThumb\GD;
use yii\base\Exception;
use yii\helpers\FileHelper;
use yii\web\UploadedFile;
use yiidreamteam\upload\ImageUploadBehavior;

class FlySystemImageUploadBehavior extends ImageUploadBehavior
{
private $filesystem;

public function __construct(Filesystem $filesystem, $config = [])
{
parent::__construct($config);
$this->filesystem = $filesystem;
}

public function cleanFiles(): void
{
$this->filesystem->delete($this->resolvePath($this->filePath));
foreach (array_keys($this->thumbs) as $profile) {
$this->filesystem->delete($this->getThumbFilePath($this->attribute, $profile));
}
}

public function afterSave(): void
{
if ($this->file instanceof UploadedFile) {
$path = $this->getUploadedFilePath($this->attribute);
$this->filesystem->createDir(pathinfo($path, PATHINFO_DIRNAME));
$this->filesystem->put($path, file_get_contents($this->file->tempName));
$this->owner->trigger(static::EVENT_AFTER_FILE_SAVE);
}
}

public function createThumbs(): void
{
$path = $this->getUploadedFilePath($this->attribute);
foreach ($this->thumbs as $profile => $config) {
$thumbPath = static::getThumbFilePath($this->attribute, $profile);
if ($this->filesystem->has($path) && !$this->filesystem->has($thumbPath)) {

// setup image processor function
if (isset($config['processor']) && is_callable($config['processor'])) {
$processor = $config['processor'];
unset($config['processor']);
} else {
$processor = function (GD $thumb) use ($config) {
$thumb->adaptiveResize($config['width'], $config['height']);
};
}

$tmpPath = $this->getTempPath($thumbPath);
FileHelper::createDirectory(pathinfo($tmpPath, PATHINFO_DIRNAME), 0775, true);
file_put_contents($tmpPath, $this->filesystem->get($path));
$thumb = new GD($tmpPath, $config);
call_user_func($processor, $thumb, $this->attribute);
FileHelper::createDirectory(pathinfo($thumbPath, PATHINFO_DIRNAME), 0775, true);
$thumb->save($tmpPath);
$this->filesystem->createDir(pathinfo($thumbPath, PATHINFO_DIRNAME));
$this->filesystem->put($thumbPath, file_get_contents($tmpPath));
}
}
}

/**
* @param $path
* @return string
*/
private function getTempPath($path): string
{
return sys_get_temp_dir() . $path;
}
}

0 comments on commit 364c736

Please sign in to comment.