Skip to content

Commit

Permalink
Initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
kdambekalns committed Oct 21, 2024
1 parent b24204a commit ac4995a
Show file tree
Hide file tree
Showing 8 changed files with 235 additions and 0 deletions.
70 changes: 70 additions & 0 deletions Classes/Jobs/CreateImageVariantsJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
declare(strict_types=1);

namespace Flownative\Neos\UniqueFilenames\Jobs;

use Flowpack\JobQueue\Common\Job\JobInterface;
use Flowpack\JobQueue\Common\Queue\Message;
use Flowpack\JobQueue\Common\Queue\QueueInterface;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Log\Utility\LogEnvironment;
use Neos\Media\Domain\Repository\ImageRepository;
use Neos\Media\Domain\Service\AssetVariantGenerator;
use Psr\Log\LoggerInterface;

class CreateImageVariantsJob implements JobInterface
{
protected string $imageIdentifier;

/**
* @Flow\Inject
* @var ImageRepository
*/
protected $imageRepository;

/**
* @Flow\Inject
* @var AssetVariantGenerator
*/
protected $assetVariantGenerator;

/**
* @Flow\Inject
* @var LoggerInterface
*/
protected $logger;

public function __construct(string $imageIdentifier)
{
$this->imageIdentifier = $imageIdentifier;
}

public function execute(QueueInterface $queue, Message $message): bool
{
try {
$image = $this->imageRepository->findByIdentifier($this->imageIdentifier);
if ($image === null) {
$this->logger->notice(sprintf('%s Job skipped for image %s – image not found.', $this->getIdentifier(), $this->imageIdentifier), LogEnvironment::fromMethodName(__METHOD__));
return true;
}
$variants = $this->assetVariantGenerator->createVariants($image);
$this->imageRepository->update($image);
$this->logger->debug(sprintf('%s Job succeeded for image %s – %d variants created.', $this->getIdentifier(), $this->imageIdentifier, count($variants)), LogEnvironment::fromMethodName(__METHOD__));
} catch (\Exception $exception) {
$this->logger->error(sprintf('%s Job failed. Error message: %s', $this->getIdentifier(), $exception->getMessage()), LogEnvironment::fromMethodName(__METHOD__));
return false;
}

return true;
}

public function getIdentifier(): string
{
return 'CreateImageVariants';
}

public function getLabel(): string
{
return sprintf('CreateImageVariantsJob (image: "%s")', $this->imageIdentifier);
}
}
37 changes: 37 additions & 0 deletions Classes/Package.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
declare(strict_types=1);

namespace Flownative\Neos\UniqueFilenames;

use Flowpack\JobQueue\Common\Job\JobManager;
use Neos\Flow\Configuration\ConfigurationManager;
use Neos\Flow\Core\Bootstrap;
use Neos\Flow\Persistence\PersistenceManagerInterface;
use Neos\Media\Domain\Model\AssetInterface;
use Neos\Media\Domain\Model\Image;
use Neos\Media\Domain\Service\AssetService;
use Wysiwyg\Brabus\Website\Jobs\CreateImageVariantsJob;

class Package extends \Neos\Flow\Package\Package
{
public function boot(Bootstrap $bootstrap)
{
$dispatcher = $bootstrap->getSignalSlotDispatcher();

// handle asset variant creation in job queue
$dispatcher->connect(AssetService::class, 'assetCreated', function (AssetInterface $asset) use ($bootstrap) {
$configurationManager = $bootstrap->getObjectManager()->get(ConfigurationManager::class);
if ($configurationManager->getConfiguration(ConfigurationManager::CONFIGURATION_TYPE_SETTINGS, 'Neos.Media.autoCreateImageVariantPresets') === false) {
if ($asset instanceof Image) {
$persistenceManager = $bootstrap->getObjectManager()->get(PersistenceManagerInterface::class);
$job = new CreateImageVariantsJob(
$persistenceManager->getIdentifierByObject($asset)
);

$queueName = $configurationManager->getConfiguration(ConfigurationManager::CONFIGURATION_TYPE_SETTINGS, 'Flownative.Neos.AsyncImageVariants.jobQueue');
$bootstrap->getObjectManager()->get(JobManager::class)->queue($queueName, $job);
}
}
});
}
}
4 changes: 4 additions & 0 deletions Configuration/Settings.Media.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Neos:
Media:
# these are created asynchronously using a job queue
autoCreateImageVariantPresets: false
5 changes: 5 additions & 0 deletions Configuration/Settings.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Flownative:
Neos:
AsyncImageVariants:
# the queue to use for jobs
jobQueue: 'media-queue'
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2024 Karsten Dambekalns / Flownative GmbH

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
63 changes: 63 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
[![MIT license](http://img.shields.io/badge/license-MIT-brightgreen.svg)](http://opensource.org/licenses/MIT)
[![Packagist](https://img.shields.io/packagist/v/flownative/neos-asyncimagevariants.svg)](https://packagist.org/packages/flownative/neos-asyncimagevariants)
[![Packagist](https://img.shields.io/packagist/dm/flownative/neos-asyncimagevariants)](https://packagist.org/packages/flownative/neos-asyncimagevariants)
[![Maintenance level: Love](https://img.shields.io/badge/maintenance-%E2%99%A1%E2%99%A1-ff69b4.svg)](https://www.flownative.com/en/products/open-source.html)

# Flownative.Neos.AsyncImageVariants

## Description

This [Flow](https://flow.neos.io) package allows to asynchronously generate image variants for Neos.Media images.

It does this by switching off automatic variant creation (through settings) and wiring a slot to the `assetCreated`
signal emitted in the `AssetService`. That slot creates a job in the job queue that executes the asset variant
creation asynchronously.

## Installation

This is installed as a regular Flow package via Composer. For your existing project, simply include
`flownative/neos-asyncimagevariants` into the dependencies of your Flow or Neos distribution:

composer require flownative/neos-asyncimagevariants

## Configuration

The package itself has one configuration option for the job queue name to use, it defaults to `media-queue`.

```yaml
Flownative:
Neos:
AsyncImageVariants:
# the queue to use for jobs
jobQueue: 'media-queue'
```
That queue of course needs to be configured, e.g. like this:
```yaml
Flowpack:
JobQueue:
Common:
queues:
'media-queue':
className: 'Flowpack\JobQueue\Doctrine\Queue\DoctrineQueue'
executeIsolated: true
releaseOptions:
delay: 15
options:
backendOptions:
driver: '%env:DATABASE_DRIVER%'
host: '%env:DATABASE_HOST%'
port: '%env:DATABASE_PORT%'
dbname: '%env:DATABASE_NAME%'
user: '%env:DATABASE_USER%'
password: '%env:DATABASE_PASSWORD%'
```
Make sure to run `./flow job:work media-queue` continuously in the background.

## Troubleshooting

- If things don't work as expected, check the system log.
- Check if jobs are queued by using `./flow queue:list`
- Run `./flow job:work media-queue --verbose --limit 1` to debug job execution
11 changes: 11 additions & 0 deletions SECURITY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Security Policy

## Supported Versions

| Version | Supported |
| ------- | ------------------ |
| 1.x | :white_check_mark: |

## Reporting a Vulnerability

Please email us at [email protected] with details.
24 changes: 24 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "flownative/neos-asyncimagevariants",
"type": "neos-package",
"description": "A package that allows for asynchronous image variant creation in Neos.Media",
"license": "MIT",
"require": {
"php": "^8.2",
"neos/media": "^8.3",
"flowpack/jobqueue-common": "^3.3"
},
"suggest": {
"flowpack/jobqueue-doctrine": "To store jobs",
"flowpack/jobqueue-redis": "To store jobs",
"flowpack/jobqueue-beanstalkd": "To store jobs"
},
"autoload": {
"psr-4": {
"Flownative\\Neos\\AsyncImageVariants\\": "Classes"
}
},
"extras": {
"installer-name": "Flownative.Neos.AsyncImageVariants"
}
}

0 comments on commit ac4995a

Please sign in to comment.