From ccb2b541ea546adf034b8409ae1243c634b7cc20 Mon Sep 17 00:00:00 2001 From: Bastien OLLIER Date: Thu, 13 Jun 2024 08:08:46 +0200 Subject: [PATCH] add Moderation.php (#14) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: bastien Co-authored-by: clfreville2 Reviewed-on: https://codefirst.iut.uca.fr/git/clement.freville2/herbarium/pulls/14 Reviewed-by: Clément FRÉVILLE --- .drone.yml | 4 ++ config/services.yaml | 6 +++ src/Entity/Post.php | 5 +++ src/Form/PostType.php | 4 +- src/Service/DummyImageSafetyService.php | 13 ++++++ src/Service/ImageSafetyServiceInterface.php | 10 +++++ src/Service/SightEngineImageSafetyService.php | 42 +++++++++++++++++++ src/Validator/ImageSafety.php | 15 +++++++ src/Validator/ImageSafetyValidator.php | 30 +++++++++++++ 9 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 src/Service/DummyImageSafetyService.php create mode 100644 src/Service/ImageSafetyServiceInterface.php create mode 100644 src/Service/SightEngineImageSafetyService.php create mode 100644 src/Validator/ImageSafety.php create mode 100644 src/Validator/ImageSafetyValidator.php diff --git a/.drone.yml b/.drone.yml index 945f522..2a41250 100644 --- a/.drone.yml +++ b/.drone.yml @@ -62,6 +62,10 @@ steps: CODEFIRST_CLIENTDRONE_ENV_SERVER_NAME: http://codefirst.iut.uca.fr CODEFIRST_CLIENTDRONE_ENV_CORS_ALLOW_ORIGIN: https://codefirst.iut.uca.fr CODEFIRST_CLIENTDRONE_ENV_TRUSTED_PROXIES: REMOTE_ADDR + CODEFIRST_CLIENTDRONE_ENV_API_USER_SIGHT_ENGINE: + from_secret: API_USER_SIGHT_ENGINE + CODEFIRST_CLIENTDRONE_ENV_API_KEY_SIGHT_ENGINE: + from_secret: API_KEY_SIGHT_ENGINE depends_on: - docker-image when: diff --git a/config/services.yaml b/config/services.yaml index 223b4e5..f07d469 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -26,5 +26,11 @@ services: bind: $processor: '@api_platform.doctrine.orm.state.persist_processor' + App\Service\ImageSafetyServiceInterface: '@App\Service\DummyImageSafetyService' + # add more service definitions when explicit configuration is needed # please note that last definitions always *replace* previous ones + +when@prod: + services: + App\Service\ImageSafetyServiceInterface: '@App\Service\SightEngineImageSafetyService' diff --git a/src/Entity/Post.php b/src/Entity/Post.php index 1ec9f69..fa0217a 100644 --- a/src/Entity/Post.php +++ b/src/Entity/Post.php @@ -8,6 +8,7 @@ use ApiPlatform\Metadata\ApiProperty; use ApiPlatform\Metadata\ApiResource; use ApiPlatform\Metadata\GetCollection; use App\Repository\PostRepository; +use App\Validator\ImageSafety; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\DBAL\Types\Types; @@ -64,6 +65,7 @@ class Post #[Vich\UploadableField(mapping: 'posts', fileNameProperty: 'image')] #[Assert\Image] + #[ImageSafety] private ?File $imageFile = null; #[ORM\Column(type: Types::TEXT)] @@ -184,6 +186,9 @@ class Post public function setImageFile(?File $imageFile): static { $this->imageFile = $imageFile; + if ($imageFile !== null) { + $this->updatedAt = new \DateTimeImmutable(); + } return $this; } diff --git a/src/Form/PostType.php b/src/Form/PostType.php index d384eb3..e32a849 100644 --- a/src/Form/PostType.php +++ b/src/Form/PostType.php @@ -21,7 +21,9 @@ class PostType extends AbstractType ->add('latitude') ->add('longitude') ->add('altitude') - ->add('imageFile', FileType::class) + ->add('imageFile', FileType::class, [ + 'required' => false, + ]) ->add('commentary') ->add('species', EntityType::class, [ 'class' => Species::class, diff --git a/src/Service/DummyImageSafetyService.php b/src/Service/DummyImageSafetyService.php new file mode 100644 index 0000000..56f7f25 --- /dev/null +++ b/src/Service/DummyImageSafetyService.php @@ -0,0 +1,13 @@ +getRealPath(), 'r'); + if ($handle === false) { + return false; + } + $response = $this->client->request('POST', 'https://api.sightengine.com/1.0/check.json', [ + 'body' => [ + 'media' => $handle, + 'models' => 'nudity-2.1', + 'api_user' => $this->apiUser, + 'api_secret' => $this->apiKey, + ], + ]); + fclose($handle); + + $output = $response->toArray(); + $scoreNudity = $output['nudity']; + + return $scoreNudity['sexual_activity'] < 0.8 && + $scoreNudity['sexual_display'] < 0.8 && + $scoreNudity['erotica'] < 0.8; + } +} diff --git a/src/Validator/ImageSafety.php b/src/Validator/ImageSafety.php new file mode 100644 index 0000000..0815de0 --- /dev/null +++ b/src/Validator/ImageSafety.php @@ -0,0 +1,15 @@ +imageSafetyService->isValid($value)) { + $this->context->buildViolation($constraint->message)->addViolation(); + } + } +}