Skip to content

Commit

Permalink
#34 Remove dframe core
Browse files Browse the repository at this point in the history
  • Loading branch information
dusta committed Jul 15, 2021
1 parent 593415c commit 3ffceb4
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 155 deletions.
127 changes: 50 additions & 77 deletions src/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@

namespace Dframe\FileStorage;

use Dframe\Config\Config;
use Dframe\FileStorage\Stylist\SimpleStylist;
use Dframe\Router\Response;
use Dframe\Router\Router;
use Exception;
use League\Flysystem\MountManager;

Expand Down Expand Up @@ -50,11 +47,6 @@ class Image
*/
protected $storage;

/**
* @var Router
*/
protected $router;

/**
* @var mixed|null
*/
Expand All @@ -70,6 +62,11 @@ class Image
*/
protected $originalImage;

/**
* @var string
*/
protected $adapter;

/**
* Image constructor.
*
Expand All @@ -78,18 +75,13 @@ class Image
*/
public function __construct($driver, $config)
{
if (is_null($config)) {
$ConfigFileStorage = Config::load('fileStorage');
$adapters = $ConfigFileStorage->get('adapters', []);
$cache = $ConfigFileStorage->get('cache', ['life' => 600]);
} else {
$adapters = $config['adapters'];
$cache = $config['cache'] ?? ['life' => 600];
}
$this->config = $config;
$adapters = $config['adapters'];
$cache = $config['cache'] ?? ['life' => 600];

$this->cache = $cache;
$this->manager = new MountManager($adapters);
$this->storage = $driver;
$this->driver = $driver;
}

/**
Expand All @@ -98,8 +90,9 @@ public function __construct($driver, $config)
*
* @return $this
*/
public function setImage($image, $default = false)
public function setImage($adapter, $image, $default = false)
{
$this->adapter = $adapter;
$this->originalImage = $image;
$this->defaultImage = $default;

Expand Down Expand Up @@ -137,23 +130,11 @@ public function size($size)
*/
public function display($adapter = 'local')
{
$get = $this->cache($adapter, $this->originalImage);
return (new Router())->makeUrl('filestorage/images/:params?params=' . $get['cache']);
return $this->cache($adapter, $this->originalImage);
}

/**
* @param $adapter
* @param $originalImage
* @param bool $default
*
* @return mixed
*/
public function cache($adapter, $originalImage, $default = false)
protected function createCachePath($originalImage, $output = [])
{
$output = [];
$output['stylist'] = $this->stylist;
$output['size'] = $this->size;

/**
* Get extension
*/
Expand All @@ -164,7 +145,6 @@ public function cache($adapter, $originalImage, $default = false)
if (isset($output['size']) and !empty($output['size'])) {
$stylist .= '-' . $this->size;
}

/**
* Create Static cache path based on $originalImage
*/
Expand All @@ -178,43 +158,56 @@ public function cache($adapter, $originalImage, $default = false)
if (!empty($basename)) {
$basename = $basename . '-';
}
$cache = $basename . $cachePath[0] . '-' . $cachePath[1] . '-' . $cachePath[2] . '-' . $cachePath[3] . '.' . $ext;
$cache = str_replace($basename, rtrim($originalImage, '.' . $ext), $cache);
$cache = $basename . '-' . $cachePath[0] . '-' . $cachePath[1] . '-' . $cachePath[2] . '-' . $cachePath[3] . '.' . $ext;

return str_replace($basename, rtrim($originalImage, '.' . $ext), $cache);
}

/**
* @param $adapter
* @param $originalImage
* @param bool $default
*
* @return mixed
*/
public function cache($adapter, $originalImage, $default = false)
{
$output = [];
$output['stylist'] = $this->stylist;
$output['size'] = $this->size;

$cache = $this->createCachePath($originalImage, $output);

$cacheAdapter = 'cache://' . $cache;
$sourceAdapter = $adapter . '://' . $originalImage;

$has = $this->manager->has($cacheAdapter);
if ($has == false or ($has == true and $this->manager->getTimestamp($cacheAdapter) < strtotime(
"-" . $this->cache['life'] . " seconds"
))) {
if ($has == true) { // zrobić update zamiast delete
if ($has == false
or ($has == true and $this->manager->getTimestamp($cacheAdapter) < strtotime(
"-" . $this->cache['life'] . " seconds"
))) {
/** @todo: Rewrite to update */
if ($has == true) {
$this->manager->delete($cacheAdapter);
}

if ($this->manager->has($sourceAdapter)) {
$mimetype = $this->manager->getMimetype($sourceAdapter);

$readStream = $this->manager->readStream($sourceAdapter);

if (!empty($output)) {
$getStylist = $this->getStylist($output['stylist']);
$readStream = $getStylist->stylize($readStream, null, $getStylist, $output);
}

if (!empty($this->storage)) {
if (!empty($this->driver)) {
$this->driver
->cache($adapter, $originalImage, $cache, $mimetype, $readStream);
}
$this->manager->putStream($cacheAdapter, $readStream);
} else {
return false;
if (!empty($this->driver)) {
$this->driver->cache($adapter, $originalImage, $cache, $mimetype, $readStream);
}

$this->manager->putStream($cacheAdapter, $readStream);
} elseif (!empty($this->defaultImage)) {
if (!empty($this->driver)) {
$get = $this->driver
->get($adapter, $originalImage, true);
$get = $this->driver->get($adapter, $originalImage, true);
if ($get['return'] == true) {
foreach ($get['cache'] as $key => $value) {
if ($this->manager->has('cache://' . $value['file_cache_path'])) {
Expand All @@ -234,6 +227,7 @@ public function cache($adapter, $originalImage, $default = false)
$this->cache = $cache;

return [
'default' => $default,
'cache' => $cache
];
}
Expand Down Expand Up @@ -262,13 +256,12 @@ protected function getStylist($stylist = 'orginal')
*
* @return array
*/
public function get($adapter = 'local', $data = false)
public function get($data = false)
{
$data = $this->cache($adapter, $this->originalImage);
$data = $this->cache($this->adapter, $this->originalImage);

if (!empty($this->driver) and $data === true) {
$get = $this->driver
->get($adapter, $this->originalImage, $data['cache']);
$get = $this->driver->get($this->adapter, $this->originalImage, $data['cache']);
if ($get['return'] === true) {
$data['data'] = $get['cache'];
}
Expand All @@ -277,30 +270,10 @@ public function get($adapter = 'local', $data = false)
return $data;
}

/**
* @param $file
* @param string $adapter
*
* @return mixed
*/
public function renderFile($file, $adapter = 'local')
public function getUrl()
{
$fileAdapter = $adapter . '://' . $file;
// Retrieve a read-stream
if (!$this->manager->has($fileAdapter)) {
$body = "<h1>404 Not Found</h1> \n\r" . "The page that you have requested could not be found.";

return Response::render($body)
->status(404);
}

$getMimetype = $this->manager->getMimetype($fileAdapter);
$stream = $this->manager->readStream($fileAdapter);
$contents = stream_get_contents($stream);
fclose($stream);

return Response::render($contents)
->headers(['Content-type' => $getMimetype]);
$cache = $this->cache($this->adapter, $this->originalImage);
return $this->config['publicUrls']['cache'] . $cache['cache'];
}

/**
Expand Down
Loading

0 comments on commit 3ffceb4

Please sign in to comment.