Files
alrahma_sunday_school_api/app/Services/BroadcastEmail/BroadcastEmailImageService.php
T

60 lines
1.5 KiB
PHP

<?php
namespace App\Services\BroadcastEmail;
use Illuminate\Http\UploadedFile;
class BroadcastEmailImageService
{
private const MAX_SIZE_BYTES = 5242880;
private array $allowedTypes = [
'image/jpeg' => 'jpg',
'image/png' => 'png',
'image/gif' => 'gif',
'image/webp' => 'webp',
];
public function store(UploadedFile $file): array
{
if (! $file->isValid()) {
return [
'ok' => false,
'status' => 400,
'error' => 'Invalid upload',
];
}
$mime = strtolower((string) $file->getMimeType());
if (!isset($this->allowedTypes[$mime])) {
return [
'ok' => false,
'status' => 415,
'error' => 'Unsupported image type',
];
}
if ($file->getSize() > self::MAX_SIZE_BYTES) {
return [
'ok' => false,
'status' => 413,
'error' => 'Image too large (max 5MB)',
];
}
$targetDir = public_path('uploads/email');
if (!is_dir($targetDir)) {
@mkdir($targetDir, 0755, true);
}
$ext = $this->allowedTypes[$mime];
$newName = 'em_' . bin2hex(random_bytes(16)) . '.' . $ext;
$file->move($targetDir, $newName);
return [
'ok' => true,
'url' => asset('uploads/email/' . $newName),
];
}
}