Convert Moderation to a service+validator
continuous-integration/drone/push Build is failing
Details
continuous-integration/drone/push Build is failing
Details
parent
32e0774302
commit
b0f09a8217
@ -1,39 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Security\Moderation;
|
|
||||||
|
|
||||||
use CURLFile;
|
|
||||||
|
|
||||||
class Moderation
|
|
||||||
{
|
|
||||||
private String $api_key;
|
|
||||||
|
|
||||||
public function __construct(String $api_key)
|
|
||||||
{
|
|
||||||
$this->api_key = $api_key;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function valide($file_image)
|
|
||||||
{
|
|
||||||
$params_api = array(
|
|
||||||
'media' => new CurlFile($file_image),
|
|
||||||
'models' => 'nudity-2.1',
|
|
||||||
'api_user' => '26959338',
|
|
||||||
'api_secret' => $this->api_key,
|
|
||||||
);
|
|
||||||
|
|
||||||
$ch = curl_init('https://api.sightengine.com/1.0/check.json');
|
|
||||||
curl_setopt($ch, CURLOPT_POST, true);
|
|
||||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
||||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $params_api);
|
|
||||||
$response = curl_exec($ch);
|
|
||||||
curl_close($ch);
|
|
||||||
|
|
||||||
$output = json_decode($response, true);
|
|
||||||
|
|
||||||
$score_nudity = $output["nudity"];
|
|
||||||
return $score_nudity["sexual_activity"] < 0.8 &&
|
|
||||||
$score_nudity["sexual_display"] < 0.8 &&
|
|
||||||
$score_nudity["erotica"] < 0.8;
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,13 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Service;
|
||||||
|
|
||||||
|
use Symfony\Component\HttpFoundation\File\File;
|
||||||
|
|
||||||
|
class DummyImageSafetyService implements ImageSafetyServiceInterface
|
||||||
|
{
|
||||||
|
public function isValid(File $file): bool
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Service;
|
||||||
|
|
||||||
|
use Symfony\Component\HttpFoundation\File\File;
|
||||||
|
|
||||||
|
interface ImageSafetyServiceInterface
|
||||||
|
{
|
||||||
|
public function isValid(File $file): bool;
|
||||||
|
}
|
@ -0,0 +1,39 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Service;
|
||||||
|
|
||||||
|
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||||
|
use Symfony\Component\HttpFoundation\File\File;
|
||||||
|
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||||
|
|
||||||
|
readonly class SightEngineImageSafetyService implements ImageSafetyServiceInterface
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private HttpClientInterface $client,
|
||||||
|
#[Autowire(env: 'API_USER_SIGHT_ENGINE')] private string $apiUser,
|
||||||
|
#[Autowire(env: 'API_KEY_SIGHT_ENGINE')] private string $apiKey,
|
||||||
|
)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isValid(File $file): bool
|
||||||
|
{
|
||||||
|
$handle = fopen($file->getRealPath(), 'r');
|
||||||
|
$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;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Validator;
|
||||||
|
|
||||||
|
use Symfony\Component\Validator\Constraint;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Annotation
|
||||||
|
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
|
||||||
|
*/
|
||||||
|
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
|
||||||
|
class ImageSafety extends Constraint
|
||||||
|
{
|
||||||
|
public string $message = 'The uploaded image is not safe.';
|
||||||
|
}
|
@ -0,0 +1,27 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Validator;
|
||||||
|
|
||||||
|
use App\Service\ImageSafetyServiceInterface;
|
||||||
|
use Symfony\Component\Validator\Constraint;
|
||||||
|
use Symfony\Component\Validator\ConstraintValidator;
|
||||||
|
|
||||||
|
class ImageSafetyValidator extends ConstraintValidator
|
||||||
|
{
|
||||||
|
public function __construct(private readonly ImageSafetyServiceInterface $imageSafetyService)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public function validate($value, Constraint $constraint): void
|
||||||
|
{
|
||||||
|
/* @var ImageSafety $constraint */
|
||||||
|
|
||||||
|
if (null === $value || '' === $value) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$this->imageSafetyService->isValid($value)) {
|
||||||
|
$this->context->buildViolation($constraint->message)->addViolation();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue