Skip to content

Commit

Permalink
Merge pull request #203 from TypiCMS/master
Browse files Browse the repository at this point in the history
Bug fixed and Laravel < 9 and Lumen compatibility removed
  • Loading branch information
weotch authored May 13, 2022
2 parents 2fec6dc + 822af4a commit 67febc1
Show file tree
Hide file tree
Showing 10 changed files with 172 additions and 364 deletions.
186 changes: 69 additions & 117 deletions README.md

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion src/Bkwld/Croppa/Commands/Purge.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Bkwld\Croppa\Commands;

// Deps
use Bkwld\Croppa\Storage;
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
Expand Down
36 changes: 15 additions & 21 deletions src/Bkwld/Croppa/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Bkwld\Croppa;

// Deps
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
Expand Down Expand Up @@ -48,58 +47,53 @@ public function __construct(URL $url, Storage $storage, Request $request, $confi
/**
* Handles a Croppa style route.
*
* @param string $request The `Request::path()`
*
* @throws Exception
*
* @return Symfony\Component\HttpFoundation\StreamedResponse
*/
public function handle($request)
public function handle(string $requestPath)
{
// Validate the signing token
if (($token = $this->url->signingToken($request))
&& $token != $this->request->input('token')
) {
$token = $this->url->signingToken($requestPath);
if ($token !== $this->request->input('token')) {
throw new NotFoundHttpException('Token mismatch');
}

// Create the image file
$crop_path = $this->render($request);
$cropPath = $this->render($requestPath);

// Redirect to remote crops ...
if ($this->storage->cropsAreRemote()) {
return new RedirectResponse($this->url->pathToUrl($crop_path), 301);
return new RedirectResponse($this->url->pathToUrl($cropPath), 301);
// ... or echo the image data to the browser
}
$absolute_path = $this->storage->getLocalCropsDirPath().'/'.$crop_path;
$absolutePath = $this->storage->getLocalCropsDirPath().'/'.$cropPath;

return new BinaryFileResponse($absolute_path, 200, [
'Content-Type' => $this->getContentType($absolute_path),
return new BinaryFileResponse($absolutePath, 200, [
'Content-Type' => $this->getContentType($absolutePath),
]);
}

/**
* Render image directly.
*
* @param string $request_path The `Request::path()`
*
* @return string The path, relative to the storage disk, to the crop
*/
public function render($request_path)
public function render(string $requestPath)
{
// Get crop path relative to it's dir
$crop_path = $this->url->relativePath($request_path);
$cropPath = $this->url->relativePath($requestPath);

// If the crops_dir is a remote disk and if the crop has already been
// created. If it has, just return that path.
if ($this->storage->cropsAreRemote()
&& $this->storage->cropExists($crop_path)) {
return $crop_path;
&& $this->storage->cropExists($cropPath)) {
return $cropPath;
}

// Parse the path. In the case there is an error (the pattern on the route
// SHOULD have caught all errors with the pattern) just return
if (!$params = $this->url->parse($request_path)) {
if (!$params = $this->url->parse($requestPath)) {
return;
}
list($path, $width, $height, $options) = $params;
Expand All @@ -122,12 +116,12 @@ public function render($request_path)

// Process the image and write its data to disk
$this->storage->writeCrop(
$crop_path,
$cropPath,
$image->process($width, $height, $options)->get()
);

// Return the paht to the crop, relative to the storage disk
return $crop_path;
return $cropPath;
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/Bkwld/Croppa/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Bkwld\Croppa;

// Dependencies
use PhpThumbFactory;

/**
Expand Down
95 changes: 7 additions & 88 deletions src/Bkwld/Croppa/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,11 @@

class ServiceProvider extends \Illuminate\Support\ServiceProvider
{
/**
* Get the major Laravel version number.
*
* @return int
*/
public function version()
{
$app = $this->app;
if (defined(get_class($app).'::VERSION')) {
return (int) ($app::VERSION);
}

if (is_callable([$app, 'version'])) {
preg_match('/(\((\d+\.\d+\.\d+)\))/', $app->version(), $v);
if (isset($v[2])) {
return -(int) ($v[2]);
}
}

return null;
}

/**
* Register the service provider.
*/
public function register()
{
// Version specific registering
if (abs($this->version()) >= 5) {
$this->registerLaravel5Lumen();
}

// Bind the Croppa URL generator and parser
$this->app->singleton('Bkwld\Croppa\URL', function ($app) {
return new URL($this->getConfig());
Expand Down Expand Up @@ -70,83 +43,29 @@ public function register()
$this->commands('Bkwld\Croppa\Commands\Purge');
}

/**
* Register specific logic for Laravel/Lumen 5. Merges package config with user config.
*/
public function registerLaravel5Lumen()
{
$this->mergeConfigFrom(__DIR__.'/../../config/config.php', 'croppa');
}

/**
* Bootstrap the application events.
*/
public function boot()
{
// Version specific booting
if ($this->version() == 4) {
$this->bootLaravel4();
} elseif ($this->version() >= 5) {
$this->bootLaravel();
} elseif ($this->version() <= -5) {
$this->bootLumen();
} else {
throw new Exception('Unsupported Laravel version');
}
$this->publishes([__DIR__.'/../../config/config.php' => config_path('croppa.php')], 'croppa');

// Listen for Cropa style URLs, these are how Croppa gets triggered
if ($this->version() > 0) { // Laravel
$this->app['router']
->get('{path}', 'Bkwld\Croppa\Handler@handle')
->where('path', $this->app['Bkwld\Croppa\URL']->routePattern());
} else { // Lumen
$this->app->get('{path:'.$this->app['Bkwld\Croppa\URL']->routePattern().'}', [
'uses' => 'Bkwld\Croppa\Handler@handle',
]);
}
$this->app['router']
->get('{path}', 'Bkwld\Croppa\Handler@handle')
->where('path', $this->app['Bkwld\Croppa\URL']->routePattern());
}

/**
* Boot specific logic for Laravel 4. Tells Laravel about the package for auto
* namespacing of config files.
*/
public function bootLaravel4()
{
$this->package('bkwld/croppa');
}

/**
* Boot specific logic for Laravel 5. Registers the config file for publishing
* to app directory.
*/
public function bootLaravel()
{
$this->publishes([
__DIR__.'/../../config/config.php' => config_path('croppa.php'),
], 'croppa');
}

/**
* Boot specific logic for Lumen. Load custom croppa config file.
*/
public function bootLumen()
{
$this->app->configure('croppa');
}

/**
* Get the configuration, which is keyed differently in L5 vs l4.
* Get the configuration.
*
* @return array
*/
public function getConfig()
{
$key = abs($this->version()) >= 5 ? 'croppa' : 'croppa::config';

$config = $this->app->make('config')->get($key);
$config = $this->app->make('config')->get('croppa');

// Use Laravel's encryption key if instructed to
if (isset($config['signing_key']) && $config['signing_key'] == 'app.key') {
if (isset($config['signing_key']) && $config['signing_key'] === 'app.key') {
$config['signing_key'] = $this->app->make('config')->get('app.key');
}

Expand Down
Loading

0 comments on commit 67febc1

Please sign in to comment.