Skip to content

Commit

Permalink
feat: poll image create/delete events
Browse files Browse the repository at this point in the history
  • Loading branch information
imorland committed Mar 28, 2024
1 parent 1a548f0 commit 14de880
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 10 deletions.
14 changes: 13 additions & 1 deletion src/Api/Controllers/DeletePollImageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
namespace FoF\Polls\Api\Controllers;

use Flarum\Http\RequestUtil;
use FoF\Polls\Events\PollImageDeleting;
use FoF\Polls\Poll;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Filesystem\Cloud;
use Illuminate\Contracts\Filesystem\Factory;
use Illuminate\Support\Arr;
Expand All @@ -28,9 +30,15 @@ class DeletePollImageController implements RequestHandlerInterface
*/
protected $uploadDir;

public function __construct(Factory $filesystemFactory)
/**
* @var Dispatcher
*/
protected $events;

public function __construct(Factory $filesystemFactory, Dispatcher $events)
{
$this->uploadDir = $filesystemFactory->disk('fof-polls');
$this->events = $events;
}

public function handle(ServerRequestInterface $request): ResponseInterface
Expand All @@ -41,6 +49,10 @@ public function handle(ServerRequestInterface $request): ResponseInterface
/** @var Poll $poll */
$poll = Poll::find($pollId);

$this->events->dispatch(
new PollImageDeleting($poll->image, $actor)
);

$this->uploadDir->delete($poll->image);

$poll->image = null;
Expand Down
14 changes: 13 additions & 1 deletion src/Api/Controllers/DeletePollOptionImageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
namespace FoF\Polls\Api\Controllers;

use Flarum\Http\RequestUtil;
use FoF\Polls\Events\PollImageDeleting;
use FoF\Polls\PollOption;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Filesystem\Cloud;
use Illuminate\Contracts\Filesystem\Factory;
use Illuminate\Support\Arr;
Expand All @@ -28,9 +30,15 @@ class DeletePollOptionImageController implements RequestHandlerInterface
*/
protected $uploadDir;

public function __construct(Factory $filesystemFactory)
/**
* @var Dispatcher
*/
protected $events;

public function __construct(Factory $filesystemFactory, Dispatcher $events)
{
$this->uploadDir = $filesystemFactory->disk('fof-polls');
$this->events = $events;
}

public function handle(ServerRequestInterface $request): ResponseInterface
Expand All @@ -44,6 +52,10 @@ public function handle(ServerRequestInterface $request): ResponseInterface
// if the image_url is a fully qualified URL, we just set it to null
if (filter_var($option->image_url, FILTER_VALIDATE_URL)) {
} else {
$this->events->dispatch(
new PollImageDeleting($option->image, $actor)

Check failure on line 56 in src/Api/Controllers/DeletePollOptionImageController.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 7.3

Access to an undefined property FoF\Polls\PollOption::$image.

Check failure on line 56 in src/Api/Controllers/DeletePollOptionImageController.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 7.4

Access to an undefined property FoF\Polls\PollOption::$image.

Check failure on line 56 in src/Api/Controllers/DeletePollOptionImageController.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 8.0

Access to an undefined property FoF\Polls\PollOption::$image.

Check failure on line 56 in src/Api/Controllers/DeletePollOptionImageController.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 8.2

Access to an undefined property FoF\Polls\PollOption::$image.

Check failure on line 56 in src/Api/Controllers/DeletePollOptionImageController.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 8.3

Access to an undefined property FoF\Polls\PollOption::$image.
);

// otherwise we check and delete it from the filesystem
$this->uploadDir->delete($option->image_url);
}
Expand Down
8 changes: 4 additions & 4 deletions src/Api/Controllers/UploadPollImageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ public function handle(ServerRequestInterface $request): ResponseInterface

$file = Arr::get($request->getUploadedFiles(), $this->filenamePrefix);

$encodedImage = $this->makeImage($file);

$uploadName = $this->uploadName();

$encodedImage = $this->makeImage($file, $uploadName);

$this->uploadDir->put($uploadName, $encodedImage);

if ($pollId && $poll = Poll::find($pollId)) {
Expand All @@ -91,14 +91,14 @@ public function handle(ServerRequestInterface $request): ResponseInterface
return $this->jsonResponse($uploadName);
}

protected function makeImage(UploadedFileInterface $file): Image
protected function makeImage(UploadedFileInterface $file, string $uploadName): Image
{
$image = $this->imageManager->make($file->getStream()->getMetadata('uri'));

$height = $this->settings->get('fof-polls.image_height');
$width = $this->settings->get('fof-polls.image_width');

$this->events->dispatch(new PollImageWillBeResized($image, $height, $width));
$this->events->dispatch(new PollImageWillBeResized($image, $uploadName, $height, $width));

$encodedImage = $this->resizeImage($image, $height, $width);

Expand Down
6 changes: 3 additions & 3 deletions src/Api/Controllers/UploadPollOptionImageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ public function handle(ServerRequestInterface $request): ResponseInterface
}

$file = Arr::get($request->getUploadedFiles(), $this->filenamePrefix);

$encodedImage = $this->makeImage($file);


$uploadName = $uploadName = $this->uploadName();

$encodedImage = $this->makeImage($file, $uploadName);

$this->uploadDir->put($uploadName, $encodedImage);

if ($optionId && $option = PollOption::find($optionId)) {
Expand Down
24 changes: 24 additions & 0 deletions src/Events/PollImageDeleting.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace FoF\Polls\Events;

use Flarum\User\User;

class PollImageDeleting
{
/**
* @var string
*/
public $fileName;

/**
* @var User
*/
public $actor;

public function __construct(string $fileName, User $actor)
{
$this->fileName = $fileName;
$this->actor = $actor;
}
}
8 changes: 7 additions & 1 deletion src/Events/PollImageWillBeResized.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ class PollImageWillBeResized
*/
public $image;

/**
* @var string
*/
public $fileName;

/**
* @var int
*/
Expand All @@ -30,9 +35,10 @@ class PollImageWillBeResized
*/
public $width;

public function __construct(Image $image, int $height, int $width)
public function __construct(Image $image, string $fileName, int $height, int $width)
{
$this->image = $image;
$this->fileName = $fileName;
$this->height = $height;
$this->width = $width;
}
Expand Down

0 comments on commit 14de880

Please sign in to comment.