add Moderation.php (#14)
Co-authored-by: bastien <bastien.ollier1@gmail.com> Co-authored-by: clfreville2 <clement.freville2@etu.uca.fr> Reviewed-on: https://codefirst.iut.uca.fr/git/clement.freville2/herbarium/pulls/14 Reviewed-by: Clément FRÉVILLE <clement.freville2@etu.uca.fr>
This commit is contained in:
42
src/Service/SightEngineImageSafetyService.php
Normal file
42
src/Service/SightEngineImageSafetyService.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?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');
|
||||
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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user